431 lines
12 KiB
Java
431 lines
12 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.dataminermanager.client.parametersfield;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
import org.apache.commons.digester.SetRootRule;
|
|
import org.gcube.portlets.user.dataminermanager.client.DataMinerManager;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.parameters.Parameter;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.parameters.TabularListParameter;
|
|
import org.gcube.portlets.user.dataminermanager.client.bean.parameters.TabularParameter;
|
|
import org.gcube.portlets.user.dataminermanager.client.rpc.DataMinerPortletServiceAsync;
|
|
import org.gcube.portlets.user.dataminermanager.client.util.UtilsGXT3;
|
|
import org.gcube.portlets.user.dataminermanager.shared.data.TableItemSimple;
|
|
import org.gcube.portlets.user.dataminermanager.shared.exception.ExpiredSessionServiceException;
|
|
import org.gcube.portlets.widgets.wsexplorer.client.notification.WorkspaceExplorerSelectNotification.WorskpaceExplorerSelectNotificationListener;
|
|
import org.gcube.portlets.widgets.wsexplorer.client.select.WorkspaceExplorerSelectDialog;
|
|
import org.gcube.portlets.widgets.wsexplorer.shared.Item;
|
|
import org.gcube.portlets.widgets.wsexplorer.shared.ItemType;
|
|
|
|
import com.allen_sauer.gwt.log.client.Log;
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
import com.sencha.gxt.core.client.dom.XDOM;
|
|
import com.sencha.gxt.core.client.util.Margins;
|
|
import com.sencha.gxt.widget.core.client.button.TextButton;
|
|
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
|
|
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
|
|
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer;
|
|
import com.sencha.gxt.widget.core.client.container.HtmlLayoutContainer;
|
|
import com.sencha.gxt.widget.core.client.container.MarginData;
|
|
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
|
|
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
|
|
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
|
|
import com.sencha.gxt.widget.core.client.event.SelectEvent;
|
|
import com.sencha.gxt.widget.core.client.form.TextField;
|
|
|
|
/**
|
|
*
|
|
* @author Giancarlo Panichi email: <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class TabularListFld extends AbstractFld {
|
|
|
|
private SimpleContainer fieldContainer;
|
|
private HBoxLayoutContainer horiz;
|
|
private SimpleContainer listContainer;
|
|
private List<TabItem> items;
|
|
private VerticalLayoutContainer vp;
|
|
private TabularListParameter tabularListParameter;
|
|
private WorkspaceExplorerSelectDialog wselectDialog;
|
|
|
|
/**
|
|
* @param parameter
|
|
*/
|
|
public TabularListFld(Parameter parameter) {
|
|
super(parameter);
|
|
Log.debug("TabularListField");
|
|
|
|
tabularListParameter = (TabularListParameter) parameter;
|
|
|
|
vp = new VerticalLayoutContainer();
|
|
items = new ArrayList<>();
|
|
|
|
listContainer = new SimpleContainer();
|
|
listContainer.add(vp, new MarginData(new Margins(0)));
|
|
|
|
/*
|
|
* List<String> templates = tabularListParameter.getTemplates(); String
|
|
* list = ""; boolean firstTemplate = true; for (String template :
|
|
* templates) { list += (firstTemplate ? "" : ", ") +
|
|
* Format.ellipse(template,50); firstTemplate = false; }
|
|
* HtmlLayoutContainer templatesList = new
|
|
* HtmlLayoutContainer("<p>Suitable Data Set Templates: <br>" +
|
|
* list+"</p>");
|
|
* templatesList.addStyleName("workflow-parameters-description");
|
|
*/
|
|
|
|
fieldContainer = new SimpleContainer();
|
|
// fieldContainer.setResize(true);
|
|
horiz = new HBoxLayoutContainer();
|
|
horiz.setPack(BoxLayoutPack.START);
|
|
horiz.setEnableOverflow(false);
|
|
|
|
HtmlLayoutContainer descr;
|
|
|
|
if (tabularListParameter.getDescription() == null) {
|
|
descr = new HtmlLayoutContainer("<p style='margin-left:5px;'></p>");
|
|
descr.addStyleName("workflow-fieldDescription");
|
|
} else {
|
|
/* listContainer.setToolTip(listParameter.getDescription()); */
|
|
descr = new HtmlLayoutContainer("<p style='margin-left:5px;'>"
|
|
+ tabularListParameter.getDescription() + "</p>");
|
|
descr.addStyleName("workflow-fieldDescription");
|
|
}
|
|
|
|
horiz.add(listContainer, new BoxLayoutData(new Margins(0)));
|
|
horiz.add(descr, new BoxLayoutData(new Margins(0)));
|
|
|
|
fieldContainer.add(horiz, new MarginData(new Margins(0)));
|
|
addField(null);
|
|
|
|
|
|
fieldContainer.forceLayout();
|
|
|
|
}
|
|
|
|
private void addField(TabItem upperItem) {
|
|
try {
|
|
|
|
TabularParameter tabPar = new TabularParameter(
|
|
tabularListParameter.getName(),
|
|
tabularListParameter.getDescription(), null,
|
|
tabularListParameter.getTemplates());
|
|
|
|
if (upperItem == null) {
|
|
TabItem item = new TabItem(tabPar, true);
|
|
items.add(item);
|
|
vp.add(item, new VerticalLayoutData(1, -1, new Margins(0)));
|
|
} else {
|
|
// search the position of the upper item
|
|
int pos = items.indexOf(upperItem);
|
|
if (pos > -1) {
|
|
upperItem.showCancelButton();
|
|
TabItem item = new TabItem(tabPar, false);
|
|
items.add(pos + 1, item);
|
|
vp.insert(item, pos + 1, new VerticalLayoutData(1, -1,
|
|
new Margins(0)));
|
|
} else {
|
|
TabItem item = new TabItem(tabPar, true);
|
|
items.add(item);
|
|
vp.add(item, new VerticalLayoutData(1, -1, new Margins(0)));
|
|
}
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
Log.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param item
|
|
*/
|
|
private void removeField(TabItem item) {
|
|
items.remove(item);
|
|
|
|
vp.remove(item);
|
|
|
|
if (items.size() == 1) {
|
|
items.get(0).hideCancelButton();
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
@Override
|
|
public String getValue() {
|
|
String separator = tabularListParameter.getSeparator();
|
|
String value = "";
|
|
boolean first = true;
|
|
for (TabItem item : items) {
|
|
String itemValue = item.getValue();
|
|
if (itemValue != null && !itemValue.contentEquals("")) {
|
|
value += (first ? "" : separator) + itemValue;
|
|
first = false;
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
@Override
|
|
public Widget getWidget() {
|
|
return fieldContainer;
|
|
}
|
|
|
|
@Override
|
|
public boolean isValid() {
|
|
boolean valid = false;
|
|
for (TabItem item : items)
|
|
if (item.isValid()) {
|
|
valid = true;
|
|
break;
|
|
}
|
|
return valid;
|
|
}
|
|
|
|
private class TabItem extends HBoxLayoutContainer {
|
|
|
|
private TextButton selectButton, selectButton2;
|
|
private TableItemSimple selectedTableItem = null;
|
|
private TextButton addBtn;
|
|
private TextButton removeBtn;
|
|
private TextField tableDescription;
|
|
|
|
/**
|
|
* @param objPar
|
|
*/
|
|
public TabItem(TabularParameter tabularParameter, boolean first) {
|
|
super();
|
|
create(tabularParameter, first);
|
|
initDialog();
|
|
|
|
}
|
|
|
|
private void create(TabularParameter tabularParameter, boolean first) {
|
|
|
|
tableDescription = new TextField();
|
|
tableDescription.setReadOnly(true);
|
|
//tableDescription.setVisible(false);
|
|
|
|
selectButton = new TextButton("Select Data Set");
|
|
selectButton.addSelectHandler(new SelectEvent.SelectHandler() {
|
|
|
|
@Override
|
|
public void onSelect(SelectEvent event) {
|
|
wselectDialog.show();
|
|
}
|
|
});
|
|
|
|
selectButton.setIcon(DataMinerManager.resources.folderExplore());
|
|
selectButton.setToolTip("Select Data Set");
|
|
|
|
selectButton2 = new TextButton("");
|
|
selectButton2.addSelectHandler(new SelectEvent.SelectHandler() {
|
|
|
|
@Override
|
|
public void onSelect(SelectEvent event) {
|
|
wselectDialog.show();
|
|
}
|
|
});
|
|
|
|
selectButton2.setIcon(DataMinerManager.resources.folderExplore());
|
|
selectButton2.setToolTip("Select Another Data Set");
|
|
selectButton2.setVisible(false);
|
|
|
|
|
|
addBtn = new TextButton("");
|
|
|
|
addBtn.setIcon(DataMinerManager.resources.add());
|
|
|
|
addBtn.addSelectHandler(new SelectEvent.SelectHandler() {
|
|
|
|
@Override
|
|
public void onSelect(SelectEvent event) {
|
|
addField(TabItem.this);
|
|
fieldContainer.forceLayout();
|
|
|
|
}
|
|
});
|
|
|
|
removeBtn = new TextButton("");
|
|
|
|
removeBtn.setIcon(DataMinerManager.resources.cancel());
|
|
|
|
removeBtn.addSelectHandler(new SelectEvent.SelectHandler() {
|
|
|
|
@Override
|
|
public void onSelect(SelectEvent event) {
|
|
selectedTableItem = null;
|
|
removeField(TabItem.this);
|
|
fieldContainer.forceLayout();
|
|
|
|
}
|
|
});
|
|
|
|
removeBtn.setVisible(!first);
|
|
|
|
setPack(BoxLayoutPack.START);
|
|
setEnableOverflow(false);
|
|
|
|
add(tableDescription, new BoxLayoutData(new Margins(0)));
|
|
add(selectButton, new BoxLayoutData(new Margins(0)));
|
|
add(selectButton2, new BoxLayoutData(new Margins(0)));
|
|
add(addBtn, new BoxLayoutData(new Margins(0)));
|
|
add(removeBtn, new BoxLayoutData(new Margins(0)));
|
|
|
|
|
|
}
|
|
|
|
private void initDialog() {
|
|
|
|
List<ItemType> selectableTypes = new ArrayList<ItemType>();
|
|
selectableTypes.add(ItemType.EXTERNAL_FILE);
|
|
List<ItemType> showableTypes = new ArrayList<ItemType>();
|
|
showableTypes.addAll(Arrays.asList(ItemType.values()));
|
|
|
|
/*
|
|
* "application/zip", "application/x-zip",
|
|
* "application/x-zip-compressed", "application/octet-stream",
|
|
* "application/x-compress", "application/x-compressed",
|
|
* "multipart/x-zip"
|
|
*/
|
|
// List<String> allowedMimeTypes =
|
|
// Arrays.asList("text/csv","text/plain","text/plain; charset=ISO-8859-1");
|
|
|
|
/**
|
|
* "zip"
|
|
*/
|
|
|
|
/*
|
|
* List<String> allowedFileExtensions = Arrays.asList("csv");
|
|
*
|
|
* FilterCriteria filterCriteria = new
|
|
* FilterCriteria(allowedMimeTypes, allowedFileExtensions, new
|
|
* HashMap<String, String>());
|
|
*/
|
|
wselectDialog = new WorkspaceExplorerSelectDialog("Select CSV",
|
|
false);
|
|
// filterCriteria, selectableTypes);
|
|
|
|
WorskpaceExplorerSelectNotificationListener handler = new WorskpaceExplorerSelectNotificationListener() {
|
|
|
|
@Override
|
|
public void onSelectedItem(Item item) {
|
|
|
|
if (item.isFolder() || item.isRoot()) {
|
|
UtilsGXT3.info("Attention", "Select a valid csv!");
|
|
|
|
} else {
|
|
TabItem.this.retrieveTableInformation(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onFailed(Throwable throwable) {
|
|
Log.error("Error in create project: "
|
|
+ throwable.getLocalizedMessage());
|
|
UtilsGXT3.alert("Error", throwable.getLocalizedMessage());
|
|
throwable.printStackTrace();
|
|
}
|
|
|
|
@Override
|
|
public void onAborted() {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onNotValidSelection() {
|
|
UtilsGXT3.info("Attention", "Select a valid csv!");
|
|
}
|
|
};
|
|
|
|
wselectDialog
|
|
.addWorkspaceExplorerSelectNotificationListener(handler);
|
|
wselectDialog.setZIndex(XDOM.getTopZIndex());
|
|
|
|
}
|
|
|
|
private void retrieveTableInformation(Item item) {
|
|
DataMinerPortletServiceAsync.INSTANCE.retrieveTableInformation(
|
|
item, new AsyncCallback<TableItemSimple>() {
|
|
|
|
@Override
|
|
public void onFailure(Throwable caught) {
|
|
Log.error("Error in retrieveTableInformation "
|
|
+ caught.getMessage());
|
|
if (caught instanceof ExpiredSessionServiceException) {
|
|
UtilsGXT3.alert("Error", "Expired Session");
|
|
|
|
} else {
|
|
UtilsGXT3.alert("Error",
|
|
"Error retrieving table information: "
|
|
+ caught.getLocalizedMessage());
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(TableItemSimple result) {
|
|
Log.debug("Retrieved: " + result);
|
|
selectedTableItem = result;
|
|
showFieldWithSelection();
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
private void showFieldWithSelection() {
|
|
try {
|
|
String tableName = selectedTableItem.getName();
|
|
|
|
if (tableName == null || tableName.isEmpty()) {
|
|
tableName = "NoName";
|
|
}
|
|
|
|
tableDescription.setValue(tableName);
|
|
selectButton.setVisible(false);
|
|
selectButton2.setVisible(true);
|
|
|
|
} catch (Throwable e) {
|
|
Log.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void showCancelButton() {
|
|
removeBtn.setVisible(true);
|
|
}
|
|
|
|
public void hideCancelButton() {
|
|
removeBtn.setVisible(false);
|
|
}
|
|
|
|
public String getValue() {
|
|
return (selectedTableItem == null) ? null : selectedTableItem
|
|
.getId();
|
|
}
|
|
|
|
public boolean isValid() {
|
|
return (selectedTableItem != null);
|
|
}
|
|
|
|
}
|
|
|
|
}
|