328 lines
9.7 KiB
Java
328 lines
9.7 KiB
Java
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.google.gwt.core.client.GWT;
|
|
import com.google.gwt.dom.client.Style.BorderStyle;
|
|
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.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;
|
|
|
|
private final String PANEL_BORDER_COLOR = "#08c";
|
|
private final String PANEL_HEIGHT = "400px";
|
|
private ShowMorePagerPanel showMorePanelLeft = new ShowMorePagerPanel();
|
|
private ShowMorePagerPanel showMorePanelRight = new ShowMorePagerPanel();
|
|
private CellList<ResourceElementBean> cellListLeft;
|
|
private CellList<ResourceElementBean> cellListRight;
|
|
private ListDataProvider<ResourceElementBean> dataProviderLeft = new ListDataProvider<ResourceElementBean>();
|
|
private ListDataProvider<ResourceElementBean> dataProviderRight = new ListDataProvider<ResourceElementBean>();
|
|
private MultiSelectionModel<ResourceElementBean> selectionModelRight;
|
|
private MultiSelectionModel<ResourceElementBean> selectionModelLeft;
|
|
private final List<ResourceElementBean> initialElements;
|
|
|
|
private static TwinColumnSelectionMainPanelUiBinder uiBinder = GWT
|
|
.create(TwinColumnSelectionMainPanelUiBinder.class);
|
|
|
|
interface TwinColumnSelectionMainPanelUiBinder extends
|
|
UiBinder<Widget, TwinColumnSelectionMainPanel> {
|
|
}
|
|
|
|
public TwinColumnSelectionMainPanel(List<ResourceElementBean> elements) {
|
|
initWidget(uiBinder.createAndBindUi(this));
|
|
|
|
this.initialElements = elements;
|
|
this.buttonsPanel.getElement().getStyle().setMarginTop(50, Unit.PCT);
|
|
|
|
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");
|
|
|
|
prepareHandlers();
|
|
initLeftSidePanel(elements);
|
|
initRightSidePanel(elements);
|
|
|
|
}
|
|
|
|
/**
|
|
* Initialize the left side panel
|
|
*/
|
|
private void initLeftSidePanel(List<ResourceElementBean> 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<ResourceElementBean>(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>(ResourceElementBean.KEY_PROVIDER);
|
|
cellListLeft.setSelectionModel(selectionModelLeft);
|
|
|
|
// perform an action on selection
|
|
selectionModelLeft.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
|
|
public void onSelectionChange(SelectionChangeEvent event) {
|
|
Iterator<ResourceElementBean> selectedObjectsIterator = selectionModelLeft.getSelectedSet().iterator();
|
|
|
|
while (selectedObjectsIterator.hasNext()) {
|
|
ResourceElementBean selectedBean = (ResourceElementBean) selectedObjectsIterator.next();
|
|
if(selectedBean.isFolder()){
|
|
if(selectionModelLeft.getSelectedSet().size() == 1){
|
|
GWT.log("Selected folder");
|
|
dataProviderLeft.setList(selectedBean.getChildren());
|
|
dataProviderLeft.flush();
|
|
dataProviderLeft.refresh();
|
|
}
|
|
selectionModelLeft.setSelected(selectedBean, false); // unselect the folder
|
|
}
|
|
}
|
|
|
|
// 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(List<ResourceElementBean> elements) {
|
|
|
|
GWT.log("Size is " + elements.size());
|
|
|
|
// initialize the left side list
|
|
ResourceCellRight cell = new ResourceCellRight();
|
|
|
|
// Set a key provider that provides a unique key for each object.
|
|
cellListRight = new CellList<ResourceElementBean>(cell, ResourceElementBean.KEY_PROVIDER);
|
|
cellListRight.setKeyboardPagingPolicy(KeyboardPagingPolicy.INCREASE_RANGE);
|
|
cellListRight.setPageSize(elements.size());
|
|
|
|
// Add a selection model so we can select cells.
|
|
selectionModelRight = new MultiSelectionModel<ResourceElementBean>(ResourceElementBean.KEY_PROVIDER);
|
|
cellListRight.setSelectionModel(selectionModelRight);
|
|
|
|
// perform an action on selection
|
|
selectionModelRight.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
|
|
public void onSelectionChange(SelectionChangeEvent event) {
|
|
// enable the buttons that allows to move the objects to the left
|
|
enableMoveToLeftButtons(selectionModelRight.getSelectedSet());
|
|
}
|
|
});
|
|
|
|
|
|
// set the list into the provider
|
|
dataProviderRight.setList(elements);
|
|
|
|
// 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<ResourceElementBean> setselectedItemsLeft){
|
|
|
|
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<ResourceElementBean> setselectedItemsRight){
|
|
|
|
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
|
|
* @param toMoveRight
|
|
*/
|
|
private void moveToRight(Set<ResourceElementBean> set){
|
|
|
|
if(set == null)
|
|
return;
|
|
|
|
Iterator<ResourceElementBean> iterator = set.iterator();
|
|
while (iterator.hasNext()) {
|
|
ResourceElementBean resourceElementBean = (ResourceElementBean) iterator
|
|
.next();
|
|
resourceElementBean.setMovedToRight(true);
|
|
GWT.log("To move right " + resourceElementBean);
|
|
}
|
|
|
|
// refresh providers
|
|
dataProviderLeft.flush();
|
|
dataProviderLeft.refresh();
|
|
dataProviderRight.flush();
|
|
dataProviderRight.refresh();
|
|
|
|
}
|
|
|
|
/**
|
|
* Move to right
|
|
* @param toMoveLeft
|
|
*/
|
|
private void moveToLeft(Set<ResourceElementBean> set){
|
|
|
|
if(set == null)
|
|
return;
|
|
|
|
Iterator<ResourceElementBean> iterator = set.iterator();
|
|
while (iterator.hasNext()) {
|
|
ResourceElementBean resourceElementBean = (ResourceElementBean) iterator
|
|
.next();
|
|
resourceElementBean.setMovedToRight(false);
|
|
GWT.log("To move left " + resourceElementBean);
|
|
}
|
|
|
|
// refresh providers
|
|
dataProviderLeft.flush();
|
|
dataProviderLeft.refresh();
|
|
dataProviderRight.flush();
|
|
dataProviderRight.refresh();
|
|
|
|
}
|
|
|
|
}
|