ws-task-executor-widget/src/main/java/org/gcube/portlets/widgets/wstaskexecutor/client/view/binder/CreateTaskConfigurationView...

312 lines
8.1 KiB
Java

package org.gcube.portlets.widgets.wstaskexecutor.client.view.binder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.common.workspacetaskexecutor.shared.TaskParameter;
import org.gcube.common.workspacetaskexecutor.shared.TaskParameterType;
import org.gcube.portlets.widgets.wstaskexecutor.client.WsTaskExecutorWidget;
import org.gcube.portlets.widgets.wstaskexecutor.client.event.DeleteCustomFieldEvent;
import org.gcube.portlets.widgets.wstaskexecutor.client.event.DeleteCustomFieldEventHandler;
import org.gcube.portlets.widgets.wstaskexecutor.client.view.FormatUtil;
import org.gcube.portlets.widgets.wstaskexecutor.shared.GcubeScope;
import org.gcube.portlets.widgets.wstaskexecutor.shared.WsFolder;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.ControlGroup;
import com.github.gwtbootstrap.client.ui.Controls;
import com.github.gwtbootstrap.client.ui.Fieldset;
import com.github.gwtbootstrap.client.ui.ListBox;
import com.github.gwtbootstrap.client.ui.Pager;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.github.gwtbootstrap.client.ui.constants.ControlGroupType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
/**
* The Class CreateTaskConfigurationView.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* May 4, 2018
*/
public abstract class CreateTaskConfigurationView extends Composite {
/** The ui binder. */
private static CreateTaskConfigurationViewUiBinder uiBinder =
GWT.create(CreateTaskConfigurationViewUiBinder.class);
/**
* The Interface CreateTaskConfigurationViewUiBinder.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* May 4, 2018
*/
interface CreateTaskConfigurationViewUiBinder
extends UiBinder<Widget, CreateTaskConfigurationView> {
}
/** The pager. */
@UiField
Pager pager;
@UiField
ListBox field_select_scope;
@UiField
ControlGroup cg_input_task_id;
@UiField
ControlGroup cg_select_vre;
@UiField
ControlGroup cg_parameters_control;
@UiField
Fieldset form_unit_fields;
@UiField
TextBox field_task_id;
@UiField
Controls task_parameters_control;
@UiField
Button addCustomFieldButton;
@UiField
HTMLPanel html_panel_field;
/** The map VR es. */
private Map<String, GcubeScope> mapScopes = new HashMap<String, GcubeScope>();
private String currentScope;
public final static HandlerManager eventBus = new HandlerManager(null);
// added custom field entries (by the user)
private List<CustomFieldEntry> customFieldEntriesList = new ArrayList<CustomFieldEntry>();
private WsFolder folder;
/**
* Submit handler.
*/
public abstract void submitHandler();
/**
* Sets the error.
*
* @param visible the visible
* @param error the error
*/
public abstract void setError(boolean visible, String error);
/**
* Sets the confirm.
*
* @param visible the visible
* @param msg the msg
*/
public abstract void setConfirm(boolean visible, String msg);
/**
* Because this class has a default constructor, it can
* be used as a binder template. In other words, it can be used in other
* *.ui.xml files as follows:
* <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
* xmlns:g="urn:import:**user's package**">
* <g:**UserClassName**>Hello!</g:**UserClassName>
* </ui:UiBinder>
* Note that depending on the widget that is used, it may be necessary to
* implement HasHTML instead of HasText.
*
* @param folder the folder
*/
public CreateTaskConfigurationView(WsFolder folder) {
this.folder = folder;
initWidget(uiBinder.createAndBindUi(this));
bindEvents();
pager.getLeft().setVisible(false);
pager.getRight().addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
setError(false, "");
boolean isValid = validateSubmit();
if(isValid)
submitHandler();
}
});
WsTaskExecutorWidget.wsTaskService.getListOfScopesForLoggedUser(new AsyncCallback<List<GcubeScope>>() {
@Override
public void onSuccess(List<GcubeScope> result) {
for (GcubeScope gcubeScope : result) {
String toValue = FormatUtil.toScopeValue(gcubeScope);
mapScopes.put(gcubeScope.getScopeName(), gcubeScope);
field_select_scope.addItem(toValue, gcubeScope.getScopeName());
}
if(result.size()>0){
field_select_scope.setSelectedValue(result.get(0).getScopeName());
//field_select_vre.setSelectedIndex(0);
//field_select_vre.fireEvent(DomEvent.fireNativeEvent(nativeEvent, handlerSource););
DomEvent.fireNativeEvent(Document.get().createChangeEvent(), field_select_scope);
}
}
@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
});
CustomFieldEntry customFieldEntry;
if(folder.getPublicLink()!=null){
customFieldEntry = new CustomFieldEntry(eventBus, "publicLink", folder.getPublicLink(), "FILE");
customFieldEntriesList.add(customFieldEntry);
cg_parameters_control.add(customFieldEntry);
}else
addCustomFieldEvent(null);
}
/**
* Adds the custom field event.
*
* @param e the e
*/
@UiHandler("addCustomFieldButton")
void addCustomFieldEvent(ClickEvent e){
CustomFieldEntry toAdd = new CustomFieldEntry(eventBus, null, null, null);
customFieldEntriesList.add(toAdd);
cg_parameters_control.add(toAdd);
}
/**
* Bind events.
*/
private void bindEvents() {
// when a custom field is removed, remove it from the list
eventBus.addHandler(DeleteCustomFieldEvent.TYPE, new DeleteCustomFieldEventHandler() {
@Override
public void onRemoveEntry(DeleteCustomFieldEvent event) {
customFieldEntriesList.remove(event.getRemovedEntry());
cg_parameters_control.remove(event.getRemovedEntry());
}
});
}
/**
* Validate submit.
*
* @return true, if successful
*/
protected boolean validateSubmit() {
cg_input_task_id.setType(ControlGroupType.NONE);
//cg_parameters_control.setType(ControlGroupType.NONE);
//cg_remote_path.setType(ControlGroupType.NONE);
if(field_select_scope.getSelectedItemText()==null){
cg_select_vre.setType(ControlGroupType.ERROR);
setError(true, "You must select a Scope!");
return false;
}
if(field_task_id.getValue() == null || field_task_id.getValue().isEmpty()){
cg_input_task_id.setType(ControlGroupType.ERROR);
setError(true, "You must type an Algorithm Identifier!");
return false;
}
for (CustomFieldEntry cFE : customFieldEntriesList) {
cFE.getControlGroup().setType(ControlGroupType.NONE);
if(cFE.getKey()==null || cFE.getKey().isEmpty()){
cFE.getControlGroup().setType(ControlGroupType.ERROR);
//cg_parameters_control.setType(ControlGroupType.ERROR);
setError(true, "You must type a valid key parameter!");
return false;
}
}
return true;
}
/**
* Gets the selected scope.
*
* @return the selected scope
*/
public GcubeScope getSelectedScope(){
//String item = field_select_scope.getSelectedItemText();
String text = field_select_scope.getSelectedValue();
return mapScopes.get(text);
}
/**
* Gets the task id.
*
* @return the task id
*/
public String getTaskId(){
return field_task_id.getValue();
}
/**
* Gets the parameters.
*
* @return the parameters
*/
public List<TaskParameter> getParameters(){
List<TaskParameter> listParameters = new ArrayList<TaskParameter>();
for (CustomFieldEntry cFE : customFieldEntriesList) {
TaskParameter tp = new TaskParameter(cFE.getKey(), cFE.getValue(), null, new TaskParameterType(cFE.getType()));
listParameters.add(tp);
}
return listParameters;
}
}