package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui.TwinColumnSelection; import java.util.Iterator; import java.util.List; import java.util.Set; import com.github.gwtbootstrap.client.ui.Button; import com.github.gwtbootstrap.client.ui.Popover; import com.github.gwtbootstrap.client.ui.constants.Placement; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style.BorderStyle; import com.google.gwt.dom.client.Style.Float; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.cellview.client.CellList; import com.google.gwt.user.cellview.client.HasKeyboardPagingPolicy.KeyboardPagingPolicy; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; import com.google.gwt.view.client.ListDataProvider; import com.google.gwt.view.client.MultiSelectionModel; import com.google.gwt.view.client.SelectionChangeEvent; /** * The twin column panels for selection of the files to attach to the catalague product. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ public class TwinColumnSelectionMainPanel extends Composite{ @UiField VerticalPanel leftContainer; @UiField VerticalPanel rightContainer; @UiField VerticalPanel buttonsPanel; @UiField Button allToRightButton; @UiField Button toRightButton; @UiField Button toLeftButton; @UiField Button allToLeftButton; @UiField Button goRootButton; @UiField Popover popoverResourceSelection; @UiField Button resourceInfoButton; private static final String PANEL_BORDER_COLOR = "#08c"; private static final String PANEL_HEIGHT = "400px"; private ShowMorePagerPanel showMorePanelLeft = new ShowMorePagerPanel(); private ShowMorePagerPanel showMorePanelRight = new ShowMorePagerPanel(); private CellList cellListLeft; private CellList cellListRight; private ListDataProvider dataProviderLeft = new ListDataProvider(); private ListDataProvider dataProviderRight = new ListDataProvider(); private MultiSelectionModel selectionModelRight; private MultiSelectionModel selectionModelLeft; private final List initialElements; private final static HTML aboutHeader = new HTML("Resource Manager"); private final static HTML aboutBody = new HTML("Move the files you want to attach to the product on the right panel below." + " Please consider that any complex hierarchy structure you may have will be flatten."); private static TwinColumnSelectionMainPanelUiBinder uiBinder = GWT .create(TwinColumnSelectionMainPanelUiBinder.class); interface TwinColumnSelectionMainPanelUiBinder extends UiBinder { } public TwinColumnSelectionMainPanel(List elements) { initWidget(uiBinder.createAndBindUi(this)); this.initialElements = elements; this.buttonsPanel.getElement().getStyle().setMarginTop(50, Unit.PCT); goRootButton.getElement().getStyle().setPaddingLeft(0, Unit.PX); allToRightButton.getElement().getStyle().setMarginBottom(4, Unit.PX); toRightButton.getElement().getStyle().setMarginBottom(4, Unit.PX); toLeftButton.getElement().getStyle().setMarginBottom(4, Unit.PX); allToLeftButton.getElement().getStyle().setMarginBottom(4, Unit.PX); buttonsPanel.getElement().setAttribute("align", "center"); popoverResourceSelection.setPlacement(Placement.LEFT); popoverResourceSelection.setHeading(aboutHeader.getHTML()); popoverResourceSelection.setText(aboutBody.getHTML()); resourceInfoButton.getElement().getStyle().setFloat(Float.RIGHT); prepareHandlers(); initLeftSidePanel(elements); initRightSidePanel(); } /** * Initialize the left side panel */ private void initLeftSidePanel(List elements) { // initialize the left side list ResourceCellLeft cell = new ResourceCellLeft(); // Set a key provider that provides a unique key for each object. cellListLeft = new CellList(cell, ResourceElementBean.KEY_PROVIDER); cellListLeft.setPageSize(elements.size()); cellListLeft.setKeyboardPagingPolicy(KeyboardPagingPolicy.INCREASE_RANGE); // Add a selection model so we can select cells. selectionModelLeft = new MultiSelectionModel(ResourceElementBean.KEY_PROVIDER); cellListLeft.setSelectionModel(selectionModelLeft); // perform an action on selection selectionModelLeft.addSelectionChangeHandler(new SelectionChangeEvent.Handler() { public void onSelectionChange(SelectionChangeEvent event) { Iterator selectedObjectsIterator = selectionModelLeft.getSelectedSet().iterator(); while (selectedObjectsIterator.hasNext()) { ResourceElementBean selectedBean = (ResourceElementBean) selectedObjectsIterator.next(); if(selectedBean.isFolder()){ if(selectionModelLeft.getSelectedSet().size() == 1){ dataProviderLeft.setList(selectedBean.getChildren()); dataProviderLeft.refresh(); dataProviderLeft.flush(); } selectionModelLeft.setSelected(selectedBean, false); } } // enable the buttons that allows to move the objects to the right enableMoveToRightButtons(selectionModelLeft.getSelectedSet()); } }); // set the list into the provider dataProviderLeft.setList(elements); // set the cell list into the provider dataProviderLeft.addDataDisplay(cellListLeft); // manage showMorePanelLeft showMorePanelLeft.setDisplay(cellListLeft); showMorePanelLeft.setHeight(PANEL_HEIGHT); showMorePanelLeft.getElement().getStyle().setBorderStyle(BorderStyle.SOLID); showMorePanelLeft.getElement().getStyle().setBorderColor(PANEL_BORDER_COLOR); // add the list to the leftContainerPanel leftContainer.add(showMorePanelLeft); } /** * Initialize the left side panel */ private void initRightSidePanel() { // initialize the left side list ResourceCellRight cell = new ResourceCellRight(); // Set a key provider that provides a unique key for each object. cellListRight = new CellList(cell, ResourceElementBean.KEY_PROVIDER); cellListRight.setKeyboardPagingPolicy(KeyboardPagingPolicy.INCREASE_RANGE); // Add a selection model so we can select cells. selectionModelRight = new MultiSelectionModel(ResourceElementBean.KEY_PROVIDER); cellListRight.setSelectionModel(selectionModelRight); // perform an action on selection selectionModelRight.addSelectionChangeHandler(new SelectionChangeEvent.Handler() { public void onSelectionChange(SelectionChangeEvent event) { enableMoveToLeftButtons(selectionModelRight.getSelectedSet()); } }); // set the cell list into the provider dataProviderRight.addDataDisplay(cellListRight); // manage showMorePanelRight showMorePanelRight.setDisplay(cellListRight); showMorePanelRight.setHeight(PANEL_HEIGHT); showMorePanelRight.getElement().getStyle().setBorderStyle(BorderStyle.SOLID); showMorePanelRight.getElement().getStyle().setBorderColor(PANEL_BORDER_COLOR); // add the list to the leftContainerPanel rightContainer.add(showMorePanelRight); } /** * Enable/disable the buttons to move objects from left to right properly. * @param setselectedItemsLeft */ private void enableMoveToRightButtons(Set setselectedItemsLeft){ if(setselectedItemsLeft == null || setselectedItemsLeft.isEmpty()){ allToRightButton.setEnabled(false); toRightButton.setEnabled(false); } else if(setselectedItemsLeft.size() > 1){ allToRightButton.setEnabled(true); toRightButton.setEnabled(false); } else{ allToRightButton.setEnabled(false); toRightButton.setEnabled(true); } } /** * Enable/disable the buttons to move objects from right to left properly. * @param setselectedItemsRight */ private void enableMoveToLeftButtons(Set setselectedItemsRight){ if(setselectedItemsRight == null || setselectedItemsRight.isEmpty()){ allToLeftButton.setEnabled(false); allToLeftButton.setEnabled(false); } else if(setselectedItemsRight.size() > 1){ allToLeftButton.setEnabled(true); toLeftButton.setEnabled(false); } else{ allToLeftButton.setEnabled(false); toLeftButton.setEnabled(true); } } /** * Prepare the buttons' handlers */ private void prepareHandlers() { goRootButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { dataProviderLeft.setList(initialElements); dataProviderLeft.flush(); } }); allToRightButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { moveToRight(selectionModelLeft.getSelectedSet()); } }); toRightButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { moveToRight(selectionModelLeft.getSelectedSet()); } }); allToLeftButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { moveToLeft(selectionModelRight.getSelectedSet()); } }); toLeftButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { moveToLeft(selectionModelRight.getSelectedSet()); } }); } /** * Move to right elements * @param set the elements to move */ private void moveToRight(Set setSelected){ if(setSelected == null || setSelected.isEmpty()) return; Iterator iterator = setSelected.iterator(); while (iterator.hasNext()) { ResourceElementBean resourceElementBean = (ResourceElementBean) iterator .next(); resourceElementBean.setMovedToRight(true); int indexRight = dataProviderRight.getList().indexOf(resourceElementBean); if(indexRight >= 0) dataProviderRight.getList().set(indexRight, resourceElementBean); else dataProviderRight.getList().add(resourceElementBean); int indexLeft = dataProviderLeft.getList().indexOf(resourceElementBean); if(indexLeft >= 0) dataProviderLeft.getList().set(indexLeft, resourceElementBean); } } /** * Move to left elements * @param setSelected the elements to move */ private void moveToLeft(Set setSelected){ if(setSelected == null || setSelected.isEmpty()) return; Iterator iterator = setSelected.iterator(); while (iterator.hasNext()) { ResourceElementBean resourceElementBean = (ResourceElementBean) iterator.next(); resourceElementBean.setMovedToRight(false); int indexLeft = dataProviderLeft.getList().indexOf(resourceElementBean); if(indexLeft != -1) dataProviderLeft.getList().set(indexLeft, resourceElementBean); // replace else dataProviderLeft.getList().add(resourceElementBean); int indexRight = dataProviderRight.getList().indexOf(resourceElementBean); if(indexRight >= 0) dataProviderLeft.getList().set(indexLeft, resourceElementBean); } } }