workspace-explorer/src/main/java/org/gcube/portlets/widgets/wsexplorer/client/save/WorkspaceExplorerSaveDialog...

361 lines
10 KiB
Java

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.ClickItemEvent;
import org.gcube.portlets.widgets.wsexplorer.client.event.ClickItemEventHandler;
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.base.TextBox;
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.github.gwtbootstrap.client.ui.event.ShownEvent;
import com.github.gwtbootstrap.client.ui.event.ShownHandler;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
/**
* 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 boolean isSave = false;
private WorkspaceExplorerSaveDialog INSTANCE = this;
private String captionTxt;
private ModalFooter footer = new ModalFooter();
private WorkspaceExplorerController controller = new WorkspaceExplorerController();
private List<WorskpaceExplorerSaveNotificationListener> listeners = new ArrayList<WorskpaceExplorerSaveNotificationListener>();
private TextBox fileNameTextBox = new TextBox();
/**
* Instantiates a new workspace explorer save dialog.
*
* @param captionTxt the caption txt
* @param fileName the file name
*/
public WorkspaceExplorerSaveDialog(String captionTxt, String fileName) {
WorkspaceExplorerController.eventBus.fireEvent(new LoadRootEvent());
initDialog(captionTxt, fileName);
}
/**
* Instantiates a new workspace explorer save dialog.
*
* @param captionTxt the caption txt
* @param fileName the file name
* @param showOnlyFolders the show only folders
*/
public WorkspaceExplorerSaveDialog(String captionTxt, String fileName, 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, fileName);
}
/**
* Instantiates a new workspace explorer save dialog.
*
* @param captionTxt the caption txt
* @param fileName the file name
* @param showableTypes the showable types
*/
public WorkspaceExplorerSaveDialog(String captionTxt, String fileName, List<ItemType> showableTypes) {
if (showableTypes != null) {
ItemType[] itemsType = new ItemType[showableTypes.size()];
itemsType = showableTypes.toArray(itemsType);
setShowableTypes(showableTypes.toArray(itemsType));
}
WorkspaceExplorerController.eventBus.fireEvent(new LoadRootEvent());
initDialog(captionTxt, fileName);
}
/**
* Inits the dialog.
*
* @param captionTxt the caption txt
* @param fileName the file name
*/
private void initDialog(String captionTxt, String fileName) {
this.captionTxt = (captionTxt == null || captionTxt.isEmpty()) ? WorkspaceExplorerConstants.WORKSPACE_EXPLORER_CAPTION: captionTxt;
setAnimation(false);
setCloseVisible(true);
hide(false);
setTitle(this.captionTxt);
saveButton = new Button(WorkspaceExplorerConstants.SAVE);
saveButton.setType(ButtonType.PRIMARY);
setWidth(WorkspaceExplorerConstants.WIDHT_DIALOG);
setMaxHeigth(WorkspaceExplorerConstants.MAX_HEIGHT_DIALOG);
addHideHandler(new HideHandler() {
@Override
public void onHide(HideEvent hideEvent) {
if (!isSave)
notifyAborted();
}
});
add(controller.getWorkspaceExplorerPanel());
saveButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
isSave = false;
Item item = controller.getWsExplorer().getItemSelected();
if (item != null && item.isFolder()) {
//GO INTO FOLDER
WorkspaceExplorerController.eventBus.fireEvent(new org.gcube.portlets.widgets.wsexplorer.client.event.LoadFolderEvent(item));
} else {
//VALIDATING FILE NAME
String fileName = getFileName();
if(fileName==null || fileName.isEmpty()){
Window.alert("You must insert a valid file name!! It cannot be empty!!");
fileNameTextBox.setFocus(true);
return;
}
Item itemB = controller.getBreadcrumbs().getLastParent();
if (itemB != null) {
if(itemB.getPath().equals(WorkspaceExplorerConstants.WORKSPACE_MY_SPECIAL_FOLDERS_PATH)){
Window.alert("Destination folder "+WorkspaceExplorerConstants.WORKSPACE_MY_SPECIAL_FOLDERS_PATH+" is not valid!");
return;
}
INSTANCE.hide();
notifySaving(itemB, getFileName());
isSave = true;
}else
Window.alert("Parent item is null!!");
}
}
});
fileNameTextBox.addStyleName("fileNameTextBox");
fileNameTextBox.setAlignment(TextAlignment.LEFT);
setFileName(fileName);
footer.add(fileNameTextBox);
footer.add(saveButton);
add(footer);
addHandlers();
}
/**
*
*/
private void addHandlers() {
WorkspaceExplorerController.eventBus.addHandler(ClickItemEvent.TYPE, new ClickItemEventHandler() {
@Override
public void onClick(final ClickItemEvent clickItemEvent) {
Item item = clickItemEvent.getItem();
if(item!=null && !item.isFolder())
setFileName("New_"+item.getName());
}
});
addShownHandler(new ShownHandler() {
@Override
public void onShown(ShownEvent shownEvent) {
// fileNameTextBox.setCursorPos(fileNameTextBox.getText().length());
fileNameTextBox.selectAll();
fileNameTextBox.setFocus(true);
}
});
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand () {
public void execute () {
fileNameTextBox.selectAll();
fileNameTextBox.setFocus(true);
}
});
}
/**
* Sets the file name.
*
* @param txt the txt
* @param select the select
*/
private void setFileName(final String txt){
if(txt==null)
return;
fileNameTextBox.setValue(txt);
}
/**
* Gets the file name.
*
* @return the file name
*/
private String getFileName(){
return fileNameTextBox.getValue();
}
/**
* Notify parent selected.
*
* @param selected the selected
*/
private void notifySaving(Item parent, String fileName) {
for (WorskpaceExplorerSaveNotificationListener worskpaceExplorerNotificationListener : listeners) {
worskpaceExplorerNotificationListener.onSaving(parent, fileName);
}
}
/**
* 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 isSave;
}
/*
* (non-Javadoc)
*
* @see com.github.gwtbootstrap.client.ui.Modal#show()
*/
@Override
public void show() {
super.show();
isSave = 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<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);
}
/* (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);
}
}
}