ckan-metadata-publisher-widget/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/client/ui/TwinColumnSelection/TwinColumnSelectionMainPane...

337 lines
11 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.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<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 final static HTML aboutHeader = new HTML("<b>Resource Manager</b>");
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<Widget, TwinColumnSelectionMainPanel> {
}
public TwinColumnSelectionMainPanel(List<ResourceElementBean> 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<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){
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<ResourceElementBean>(cell, ResourceElementBean.KEY_PROVIDER);
cellListRight.setKeyboardPagingPolicy(KeyboardPagingPolicy.INCREASE_RANGE);
// 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) {
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<ResourceElementBean> 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<ResourceElementBean> 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<ResourceElementBean> setSelected){
if(setSelected == null || setSelected.isEmpty())
return;
Iterator<ResourceElementBean> iterator = setSelected.iterator();
while (iterator.hasNext()) {
ResourceElementBean resourceElementBean = (ResourceElementBean) iterator
.next();
resourceElementBean.setMovedToRight(true);
boolean contains = dataProviderRight.getList().contains(resourceElementBean);
if(!contains)
dataProviderRight.getList().add(resourceElementBean);
}
// refresh providers
dataProviderLeft.flush();
dataProviderRight.flush();
dataProviderLeft.refresh();
dataProviderRight.refresh();
}
/**
* Move to left elements
* @param setSelected the elements to move
*/
private void moveToLeft(Set<ResourceElementBean> setSelected){
if(setSelected == null || setSelected.isEmpty())
return;
Iterator<ResourceElementBean> iterator = setSelected.iterator();
while (iterator.hasNext()) {
ResourceElementBean resourceElementBean = (ResourceElementBean) iterator.next();
int index = dataProviderLeft.getList().indexOf(resourceElementBean);
if(index != -1){
resourceElementBean.setMovedToRight(false);
dataProviderLeft.getList().set(index, resourceElementBean); // replace
}
}
// refresh providers
dataProviderRight.flush();
dataProviderRight.refresh();
}
}