322 lines
9.2 KiB
Java
322 lines
9.2 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.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.Alert;
|
||
|
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.AlertType;
|
||
|
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 WorkspaceExplorerSaveDialog 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 WorkspaceExplorerSaveDialog INSTANCE = this;
|
||
|
private boolean isSelect = false;
|
||
|
private String captionTxt;
|
||
|
private ModalFooter footer = new ModalFooter();
|
||
|
private Alert alertConfirm;
|
||
|
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 WorkspaceExplorerSaveDialog(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 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 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 WorkspaceExplorerSaveDialog(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);
|
||
|
}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);
|
||
|
add(footer);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets the alert confirm.
|
||
|
*
|
||
|
* @param html the html
|
||
|
* @param bool the bool
|
||
|
*/
|
||
|
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;
|
||
|
INSTANCE.hide();
|
||
|
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 (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);
|
||
|
}
|
||
|
}
|