diff --git a/src/main/java/org/gcube/portlets/widgets/wsexplorer/client/select/WorkspaceExplorerSelectPanel.java b/src/main/java/org/gcube/portlets/widgets/wsexplorer/client/select/WorkspaceExplorerSelectPanel.java new file mode 100644 index 0000000..1685ff8 --- /dev/null +++ b/src/main/java/org/gcube/portlets/widgets/wsexplorer/client/select/WorkspaceExplorerSelectPanel.java @@ -0,0 +1,300 @@ +/** + * + */ +package org.gcube.portlets.widgets.wsexplorer.client.select; + +import java.util.ArrayList; +import java.util.List; + +import org.gcube.portlets.widgets.wsexplorer.client.WorkspaceExplorerConstants; +import org.gcube.portlets.widgets.wsexplorer.client.WorkspaceExplorerController; +import org.gcube.portlets.widgets.wsexplorer.client.event.LoadRootEvent; +import org.gcube.portlets.widgets.wsexplorer.client.notification.WorkspaceExplorerSelectNotification.HasWorskpaceExplorerSelectNotificationListener; +import org.gcube.portlets.widgets.wsexplorer.client.notification.WorkspaceExplorerSelectNotification.WorskpaceExplorerSelectNotificationListener; +import org.gcube.portlets.widgets.wsexplorer.shared.Item; +import org.gcube.portlets.widgets.wsexplorer.shared.ItemType; + +import com.github.gwtbootstrap.client.ui.Alert; +import com.github.gwtbootstrap.client.ui.Button; +import com.github.gwtbootstrap.client.ui.ModalFooter; +import com.github.gwtbootstrap.client.ui.constants.AlertType; +import com.github.gwtbootstrap.client.ui.constants.ButtonType; +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.user.client.ui.HTML; +import com.google.gwt.user.client.ui.HorizontalPanel; +import com.google.gwt.user.client.ui.ScrollPanel; +import com.google.gwt.user.client.ui.VerticalPanel; + + + +/** + * The Class WorkspaceExplorerSelectPanel. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jul 28, 2015 + */ +public class WorkspaceExplorerSelectPanel extends ScrollPanel implements HasWorskpaceExplorerSelectNotificationListener{ + + private Button selectButton; + private WorkspaceExplorerSelectPanel INSTANCE = this; + private boolean isSelect = false; + private String captionTxt; + private ModalFooter footer = new ModalFooter(); + private Alert alertConfirm; + private WorkspaceExplorerController controller = new WorkspaceExplorerController(); + private List listeners = new ArrayList(); + private VerticalPanel mainVP = new VerticalPanel(); + private HorizontalPanel footerHP = new HorizontalPanel(); + + /** + * Instantiates a new workspace explorer select panel. + * + * @param captionTxt the caption txt + */ + public WorkspaceExplorerSelectPanel(String captionTxt) { + WorkspaceExplorerController.eventBus.fireEvent(new LoadRootEvent()); + initDialog(captionTxt); + } + + + /** + * Instantiates a new workspace explorer select panel. + * + * @param captionTxt the caption txt + * @param showOnlyFolders the show only folders + */ + public WorkspaceExplorerSelectPanel(String captionTxt, boolean showOnlyFolders) { + + if(showOnlyFolders){ + ItemType[] itemsType = new ItemType[1]; + itemsType[0] = ItemType.FOLDER; + setSelectableTypes(itemsType); + setShowableTypes(itemsType); + } + + WorkspaceExplorerController.eventBus.fireEvent(new LoadRootEvent()); + initDialog(captionTxt); + } + + + /** + * Instantiates a new workspace explorer select panel. + * + * @param captionTxt the caption txt + * @param selectableTypes the selectable types + * @param showableTypes the showable types + */ + public WorkspaceExplorerSelectPanel(String captionTxt, List selectableTypes, List showableTypes) { + if(selectableTypes!=null){ + ItemType[] itemsType = new ItemType[selectableTypes.size()]; + itemsType = selectableTypes.toArray(itemsType); + setSelectableTypes(selectableTypes.toArray(itemsType)); + } + + if(showableTypes!=null){ + ItemType[] itemsType = new ItemType[showableTypes.size()]; + itemsType = showableTypes.toArray(itemsType); + setShowableTypes(showableTypes.toArray(itemsType)); + } + + WorkspaceExplorerController.eventBus.fireEvent(new LoadRootEvent()); + initDialog(captionTxt); + } + + + + /** + * Inits the dialog. + * + * @param captionTxt the caption txt + */ + private void initDialog(String captionTxt) { + this.captionTxt = (captionTxt==null || captionTxt.isEmpty())?WorkspaceExplorerConstants.WORKSPACE_EXPLORER_CAPTION:captionTxt; + setTitle(this.captionTxt); + selectButton = new Button(WorkspaceExplorerConstants.SELECT); + selectButton.setType(ButtonType.PRIMARY); + setWidth(WorkspaceExplorerConstants.WIDHT_DIALOG+"px"); + + add(controller.getWorkspaceExplorerPanel()); + selectButton.addClickHandler(new ClickHandler() { + + @Override + public void onClick(ClickEvent event) { + isSelect = false; + Item item = controller.getWsExplorer().getItemSelected(); + if(item!=null){ + isSelect = true; + notifySelectedItem(item); + }else if(!controller.getWsExplorer().itemIsSelectable()){ //IGNORING ITEMS NOT SELECTABLE, USING BREADCRUMBS + Item itemB = controller.getBreadcrumbs().getLastParent(); + if(itemB!=null){ + setAlertConfirm("Selecting \""+itemB.getName()+"\", confirm?", true, itemB); + } + } + } + }); + + setAlertConfirm("", false, null); + footer.add(selectButton); + mainVP.add(controller.getWorkspaceExplorerPanel()); + mainVP.add(footer); + + add(mainVP); + } + + + /** + * Sets the alert confirm. + * + * @param html the html + * @param show the show + * @param item the item + */ + private void setAlertConfirm(String html, boolean show, final Item item){ + try{ + footer.remove(alertConfirm); + }catch(Exception e){ + //silent + } + + alertConfirm = new Alert(); + alertConfirm.setText(html); + alertConfirm.setVisible(show); + alertConfirm.setClose(true); + alertConfirm.setType(AlertType.INFO); + + Button yes = new Button("Yes"); + yes.setType(ButtonType.LINK); + yes.addClickHandler(new ClickHandler() { + + @Override + public void onClick(ClickEvent event) { + if(item!=null){ + isSelect = true; + notifySelectedItem(item); + } + } + }); + + Button no = new Button("No"); + no.setType(ButtonType.LINK); + no.addClickHandler(new ClickHandler() { + + @Override + public void onClick(ClickEvent event) { + alertConfirm.close(); + } + }); + alertConfirm.add(yes); + alertConfirm.add(no); + footer.insert(alertConfirm,0); + } + /** + * Notify selected item. + * + * @param selected the selected + */ + private void notifySelectedItem(Item selected){ + + for (WorskpaceExplorerSelectNotificationListener worskpaceExplorerNotificationListener : listeners) { + worskpaceExplorerNotificationListener.onSelectedItem(selected); + } + } + + /** + * Notify aborted. + */ + private void notifyAborted(){ + + for (WorskpaceExplorerSelectNotificationListener worskpaceExplorerNotificationListener : listeners) { + worskpaceExplorerNotificationListener.onAborted(); + } + } + + /** + * Notify failed. + * + * @param t the t + */ + @SuppressWarnings("unused") + private void notifyFailed(Throwable t){ + + for (WorskpaceExplorerSelectNotificationListener worskpaceExplorerNotificationListener : listeners) { + worskpaceExplorerNotificationListener.onFailed(t); + } + } + + /** + * Gets the caption txt. + * + * @return the captionTxt + */ + public String getCaptionTxt() { + return captionTxt; + } + + /** + * Checks if is valid hide. + * + * @return the isValidHide + */ + public boolean isValidHide() { + return isSelect; + } + + /* (non-Javadoc) + * @see org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerNotification.HasWorskpaceExplorerNotificationListener#addWorkspaceExplorerNotificationListener(org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerNotification.WorskpaceExplorerNotificationListener) + */ + @Override + public void addWorkspaceExplorerSelectNotificationListener(WorskpaceExplorerSelectNotificationListener handler) { + if(handler!=null) + listeners.add(handler); + } + + /* (non-Javadoc) + * @see org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerNotification.HasWorskpaceExplorerNotificationListener#removeWorkspaceExplorerNotificationListener(org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerNotification.WorskpaceExplorerNotificationListener) + */ + @Override + public void removeWorkspaceExplorerSelectNotificationListener(WorskpaceExplorerSelectNotificationListener handler) { + if(handler!=null){ + if(listeners.contains(handler)) + listeners.remove(handler); + } + } + + /** + * Set which items are selectable. + * @param selectableTypes the selectableTypes to set + */ + private void setSelectableTypes(ItemType ... selectableTypes) { + controller.setSelectableTypes(selectableTypes); + } + + + /** + * Gets the selectable types. + * + * @return the selectable types + */ + public List getSelectableTypes() { + return controller.getSelectableTypes(); + } + + /** + * Return the showable items. + * @return the showableTypes + */ + public List getShowableTypes() { + return controller.getShowableTypes(); + } + + /** + * Set the showable items. The folders items are show as default. + * @param showableTypes the showableTypes to set + */ + private void setShowableTypes(ItemType ... showableTypes) { + controller.setShowableTypes(showableTypes); + } +}