package org.gcube.portlets.widgets.wsexplorer.client.save; 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.WorkspaceExplorerSaveNotification.HasWorskpaceExplorerSaveNotificationListener; import org.gcube.portlets.widgets.wsexplorer.client.notification.WorkspaceExplorerSaveNotification.WorskpaceExplorerSaveNotificationListener; import org.gcube.portlets.widgets.wsexplorer.shared.Item; import org.gcube.portlets.widgets.wsexplorer.shared.ItemType; import com.github.gwtbootstrap.client.ui.Button; import com.github.gwtbootstrap.client.ui.Modal; import com.github.gwtbootstrap.client.ui.ModalFooter; import com.github.gwtbootstrap.client.ui.constants.ButtonType; import com.github.gwtbootstrap.client.ui.event.HideEvent; import com.github.gwtbootstrap.client.ui.event.HideHandler; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; /** * The Class WorkspaceExplorerSaveDialog. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * Jul 7, 2015 */ public class WorkspaceExplorerSaveDialog extends Modal implements HasWorskpaceExplorerSaveNotificationListener { private Button saveButton; private WorkspaceExplorerSaveDialog INSTANCE = this; private boolean isSelect = false; private String captionTxt; private ModalFooter footer = new ModalFooter(); private WorkspaceExplorerController controller = new WorkspaceExplorerController(); private List listeners = new ArrayList(); /** * Instantiates a new workspace explorer save dialog. * * @param captionTxt the caption txt */ public WorkspaceExplorerSaveDialog(String captionTxt) { WorkspaceExplorerController.eventBus.fireEvent(new LoadRootEvent()); initDialog(captionTxt); } /** * Instantiates a new workspace explorer save dialog. * * @param captionTxt the caption txt * @param showOnlyFolders the show only folders */ public WorkspaceExplorerSaveDialog(String captionTxt, boolean showOnlyFolders) { if (showOnlyFolders) { ItemType[] itemsType = new ItemType[2]; itemsType[0] = ItemType.ROOT; itemsType[1] = ItemType.FOLDER; setSelectableTypes(itemsType); setShowableTypes(itemsType); } WorkspaceExplorerController.eventBus.fireEvent(new LoadRootEvent()); initDialog(captionTxt); } /** * Instantiates a new workspace explorer save dialog. * * @param captionTxt the caption txt * @param selectableTypes the selectable types * @param showableTypes the showable types */ public WorkspaceExplorerSaveDialog(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; setAnimation(false); setCloseVisible(true); setTitle(this.captionTxt); saveButton = new Button(WorkspaceExplorerConstants.SAVE); saveButton.setType(ButtonType.PRIMARY); setWidth(WorkspaceExplorerConstants.WIDHT_DIALOG); setMaxHeigth(WorkspaceExplorerConstants.MAX_HEIGHT_DIALOG); hide(false); addHideHandler(new HideHandler() { @Override public void onHide(HideEvent hideEvent) { if (!isSelect) notifyAborted(); } }); add(controller.getWorkspaceExplorerPanel()); saveButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { isSelect = false; Item item = controller.getWsExplorer().getItemSelected(); if (item != null) { isSelect = true; INSTANCE.hide(); notifyParentSelected(item); } else if (!controller.getWsExplorer().itemIsSelectable()) { // IGNORING ITEMS NOT SELECTABLE, USING BREADCRUMBS Item itemB = controller.getBreadcrumbs().getLastParent(); if (itemB != null) { } } } }); footer.add(saveButton); add(footer); } /** * Notify parent selected. * * @param selected the selected */ private void notifyParentSelected(Item selected) { for (WorskpaceExplorerSaveNotificationListener worskpaceExplorerNotificationListener : listeners) { worskpaceExplorerNotificationListener.onParentSelected(selected); } } /** * Notify aborted. */ private void notifyAborted() { for (WorskpaceExplorerSaveNotificationListener worskpaceExplorerNotificationListener : listeners) { worskpaceExplorerNotificationListener.onAborted(); } } /** * Notify failed. * * @param t * the t */ @SuppressWarnings("unused") private void notifyFailed(Throwable t) { for (WorskpaceExplorerSaveNotificationListener 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 com.github.gwtbootstrap.client.ui.Modal#show() */ @Override public void show() { super.show(); isSelect = false; } /** * 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); } /* (non-Javadoc) * @see org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerSaveNotification.HasWorskpaceExplorerSaveNotificationListener#addWorkspaceExplorerSaveNotificationListener(org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerSaveNotification.WorskpaceExplorerSaveNotificationListener) */ @Override public void addWorkspaceExplorerSaveNotificationListener(WorskpaceExplorerSaveNotificationListener handler) { if(handler!=null) listeners.add(handler); } /* (non-Javadoc) * @see org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerSaveNotification.HasWorskpaceExplorerSaveNotificationListener#removeWorkspaceExplorerSaveNotificationListener(org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerSaveNotification.WorskpaceExplorerSaveNotificationListener) */ @Override public void removeWorkspaceExplorerSaveNotificationListener(WorskpaceExplorerSaveNotificationListener handler) { if(handler!=null){ if(listeners.contains(handler)) listeners.remove(handler); } } }