workspace-explorer/src/main/java/org/gcube/portlets/widgets/wsexplorer/client/load/WorkspaceExplorerLoadDialog...

266 lines
7.8 KiB
Java

/**
*
*/
package org.gcube.portlets.widgets.wsexplorer.client.load;
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.WorskpaceExplorerNotification.HasWorskpaceExplorerNotificationListener;
import org.gcube.portlets.widgets.wsexplorer.client.notification.WorskpaceExplorerNotification.WorskpaceExplorerNotificationListener;
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 WorkspaceExplorerLoadDialog.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Jun 30, 2015
*/
public class WorkspaceExplorerLoadDialog extends Modal implements HasWorskpaceExplorerNotificationListener{
public static final String SELECT = "Select";
public static final String MAX_HEIGHT_DIALOG = "500px";
public static final int WIDHT_DIALOG = 720;
private Button selectButton;
private WorkspaceExplorerLoadDialog INSTANCE = this;
private boolean isSelect = false;
private String captionTxt;
private ModalFooter footer = new ModalFooter();
private WorkspaceExplorerController controller = new WorkspaceExplorerController();
private List<WorskpaceExplorerNotificationListener> listeners = new ArrayList<WorskpaceExplorerNotificationListener>();
/**
* Instantiates a new workspace explorer load dialog.
*
* @param captionTxt the caption txt
* You can implement {@link WorskpaceExplorerNotificationListener} to receive events
*/
public WorkspaceExplorerLoadDialog(String captionTxt) {
WorkspaceExplorerController.eventBus.fireEvent(new LoadRootEvent());
initDialog(captionTxt);
}
/**
* Instantiates a new workspace explorer load dialog.
*
* @param captionTxt the caption txt
* @param showOnlyFolders the show only folders
*/
public WorkspaceExplorerLoadDialog(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 load dialog.
*
* @param captionTxt captionTxt sets the text inside the caption, if null sets "Workspace Explorer"
* @param selectableTypes the selectable types
* @param showableTypes the showable types
*
* You can implement {@link WorskpaceExplorerNotificationListener} to receive events
*/
public WorkspaceExplorerLoadDialog(String captionTxt, List<ItemType> selectableTypes, List<ItemType> 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);
selectButton = new Button(SELECT);
selectButton.setType(ButtonType.PRIMARY);
setWidth(WIDHT_DIALOG);
setMaxHeigth(MAX_HEIGHT_DIALOG);
hide(false);
addHideHandler(new HideHandler() {
@Override
public void onHide(HideEvent hideEvent) {
if(!isSelect)
notifyAborted();
}
});
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;
INSTANCE.hide();
notifySelectedItem(item);
}
}
});
footer.add(selectButton);
add(footer);
}
/**
* Notify selected item.
*
* @param selected the selected
*/
private void notifySelectedItem(Item selected){
for (WorskpaceExplorerNotificationListener worskpaceExplorerNotificationListener : listeners) {
worskpaceExplorerNotificationListener.onSelectedItem(selected);
}
}
/**
* Notify aborted.
*/
private void notifyAborted(){
for (WorskpaceExplorerNotificationListener worskpaceExplorerNotificationListener : listeners) {
worskpaceExplorerNotificationListener.onAborted();
}
}
/**
* Notify failed.
*
* @param t the t
*/
@SuppressWarnings("unused")
private void notifyFailed(Throwable t){
for (WorskpaceExplorerNotificationListener 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;
}
/* (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 addWorkspaceExplorerNotificationListener(WorskpaceExplorerNotificationListener 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 removeWorkspaceExplorerNotificationListener(WorskpaceExplorerNotificationListener 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<ItemType> getSelectableTypes() {
return controller.getSelectableTypes();
}
/**
* Return the showable items.
* @return the showableTypes
*/
public List<ItemType> 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);
}
}