2016-06-09 18:58:19 +02:00
|
|
|
package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-07-14 12:14:33 +02:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.CloseCreationFormEvent;
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.CloseCreationFormEventHandler;
|
2016-07-13 16:11:15 +02:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui.utils.GcubeDialogExtended;
|
2016-06-09 18:58:19 +02:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetadataFieldWrapper;
|
|
|
|
|
|
|
|
import com.github.gwtbootstrap.client.ui.CheckBox;
|
2016-07-05 10:29:30 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.ControlLabel;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Controls;
|
2016-07-13 16:11:15 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.Icon;
|
2016-06-09 18:58:19 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.ListBox;
|
2016-07-13 16:11:15 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.Popover;
|
2016-06-09 18:58:19 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.TextBox;
|
|
|
|
import com.google.gwt.core.client.GWT;
|
|
|
|
import com.google.gwt.dom.client.Element;
|
|
|
|
import com.google.gwt.dom.client.SpanElement;
|
2016-07-13 16:11:15 +02:00
|
|
|
import com.google.gwt.dom.client.Style.Cursor;
|
2016-06-09 18:58:19 +02:00
|
|
|
import com.google.gwt.dom.client.Style.Display;
|
2016-07-13 16:11:15 +02:00
|
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
|
import com.google.gwt.event.logical.shared.ResizeEvent;
|
|
|
|
import com.google.gwt.event.logical.shared.ResizeHandler;
|
2016-07-14 12:14:33 +02:00
|
|
|
import com.google.gwt.event.shared.HandlerManager;
|
2016-06-09 18:58:19 +02:00
|
|
|
import com.google.gwt.uibinder.client.UiBinder;
|
|
|
|
import com.google.gwt.uibinder.client.UiField;
|
2016-07-13 16:11:15 +02:00
|
|
|
import com.google.gwt.uibinder.client.UiHandler;
|
|
|
|
import com.google.gwt.user.client.Window;
|
2016-06-09 18:58:19 +02:00
|
|
|
import com.google.gwt.user.client.ui.Composite;
|
2016-07-04 11:50:49 +02:00
|
|
|
import com.google.gwt.user.client.ui.FlowPanel;
|
2016-07-13 16:11:15 +02:00
|
|
|
import com.google.gwt.user.client.ui.FocusPanel;
|
|
|
|
import com.google.gwt.user.client.ui.HTML;
|
2016-06-09 18:58:19 +02:00
|
|
|
import com.google.gwt.user.client.ui.SimplePanel;
|
|
|
|
import com.google.gwt.user.client.ui.Widget;
|
|
|
|
|
|
|
|
public class MetaDataFieldSkeleton extends Composite{
|
|
|
|
|
|
|
|
private static MetaDataFieldSkeletonUiBinder uiBinder = GWT
|
|
|
|
.create(MetaDataFieldSkeletonUiBinder.class);
|
|
|
|
|
|
|
|
interface MetaDataFieldSkeletonUiBinder extends
|
|
|
|
UiBinder<Widget, MetaDataFieldSkeleton> {
|
|
|
|
}
|
|
|
|
|
|
|
|
@UiField Element mandatorySymbol;
|
|
|
|
@UiField SpanElement name;
|
|
|
|
@UiField SimplePanel elementPanel;
|
2016-07-04 11:50:49 +02:00
|
|
|
@UiField FlowPanel noteFieldContainer;
|
2016-07-13 16:11:15 +02:00
|
|
|
@UiField Popover noteFieldPopover;
|
2016-07-05 10:29:30 +02:00
|
|
|
@UiField ControlLabel controlLabel;
|
|
|
|
@UiField Controls controls;
|
2016-07-13 16:11:15 +02:00
|
|
|
@UiField Icon infoIcon;
|
|
|
|
@UiField FocusPanel focusPanelIconContainer;
|
2016-06-09 18:58:19 +02:00
|
|
|
|
|
|
|
// the element that holds the value (it could be a checkbox, textbox or listbox)
|
|
|
|
private Widget holder;
|
|
|
|
|
2016-07-04 11:50:49 +02:00
|
|
|
// the field this object represents
|
2016-06-09 18:58:19 +02:00
|
|
|
private MetadataFieldWrapper field;
|
|
|
|
|
2016-07-13 16:11:15 +02:00
|
|
|
// the dialog box for this metadata
|
|
|
|
private GcubeDialogExtended dialog;
|
2016-07-14 12:14:33 +02:00
|
|
|
|
|
|
|
// save event bus referene
|
|
|
|
private HandlerManager eventBus;
|
2016-07-13 16:11:15 +02:00
|
|
|
|
2016-07-14 12:14:33 +02:00
|
|
|
public MetaDataFieldSkeleton(MetadataFieldWrapper field, HandlerManager eventBus) {
|
2016-06-09 18:58:19 +02:00
|
|
|
initWidget(uiBinder.createAndBindUi(this));
|
|
|
|
|
|
|
|
// prepare information
|
|
|
|
this.field = field;
|
2016-07-14 12:14:33 +02:00
|
|
|
|
|
|
|
// event bus
|
|
|
|
this.eventBus = eventBus;
|
|
|
|
|
|
|
|
// bind
|
|
|
|
bind();
|
2016-07-13 16:11:15 +02:00
|
|
|
|
2016-07-05 10:29:30 +02:00
|
|
|
// add custom css properties
|
|
|
|
controls.addStyleName("form-controls-custom");
|
|
|
|
controlLabel.addStyleName("form-control-label-custom");
|
2016-06-09 18:58:19 +02:00
|
|
|
|
|
|
|
// save the name
|
|
|
|
name.setInnerText(field.getFieldName() + ":");
|
|
|
|
|
|
|
|
// check if it is mandatory
|
|
|
|
if(!field.getMandatory())
|
|
|
|
mandatorySymbol.getStyle().setDisplay(Display.NONE);
|
|
|
|
|
|
|
|
if(field.getIsBoolean()){
|
|
|
|
|
|
|
|
// its a checkbox
|
|
|
|
holder = new CheckBox();
|
|
|
|
|
|
|
|
if(field.getDefaulValue() != null)
|
|
|
|
((CheckBox)holder).setValue(Boolean.valueOf(field.getDefaulValue()));
|
|
|
|
|
|
|
|
// add to the elementPanel
|
|
|
|
elementPanel.add(holder);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// it could be a listbox or a textbox according to the vocabulary fields
|
|
|
|
if(field.getVocabulary() == null || field.getVocabulary().isEmpty()){
|
|
|
|
|
|
|
|
// textbox
|
|
|
|
holder = new TextBox();
|
|
|
|
|
|
|
|
if(field.getDefaulValue() != null)
|
|
|
|
((TextBox)holder).setText(field.getDefaulValue());
|
|
|
|
|
|
|
|
// add to the elementPanel
|
|
|
|
elementPanel.add(holder);
|
|
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// listbox
|
|
|
|
holder = new ListBox();
|
|
|
|
|
2016-07-13 18:13:50 +02:00
|
|
|
// if it is not mandatory, add a disabled option
|
|
|
|
if(!field.getMandatory()){
|
|
|
|
((ListBox)holder).addItem("Select " + field.getFieldName());
|
|
|
|
((ListBox)holder).setSelectedValue("Select " + field.getFieldName());
|
|
|
|
((ListBox)holder).getElement().getElementsByTagName("option").getItem(0).setAttribute("disabled", "disabled");
|
|
|
|
}
|
|
|
|
|
2016-06-09 18:58:19 +02:00
|
|
|
// get vocabulary fields
|
|
|
|
List<String> vocabulary = field.getVocabulary();
|
|
|
|
|
|
|
|
for (String term : vocabulary) {
|
|
|
|
|
|
|
|
((ListBox)holder).addItem(term);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// set default value
|
2016-07-13 18:13:50 +02:00
|
|
|
if(field.getDefaulValue() != null)
|
|
|
|
((ListBox)holder).setSelectedValue(field.getDefaulValue());
|
2016-09-05 18:19:18 +02:00
|
|
|
|
|
|
|
// check if multiple selection is allowed TODO
|
|
|
|
boolean isMultipleSelection = false; //field.isMultipleSelection();
|
|
|
|
((ListBox)holder).setMultipleSelect(isMultipleSelection);
|
2016-06-09 18:58:19 +02:00
|
|
|
|
|
|
|
// add to the elementPanel
|
|
|
|
elementPanel.add(holder);
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 16:11:15 +02:00
|
|
|
|
2016-07-04 11:50:49 +02:00
|
|
|
// set holder width
|
2016-07-13 16:11:15 +02:00
|
|
|
if(holder.getClass().equals(ListBox.class))
|
|
|
|
holder.setWidth("96%");
|
|
|
|
else
|
|
|
|
holder.setWidth("95%");
|
2016-06-09 18:58:19 +02:00
|
|
|
|
2016-07-13 16:11:15 +02:00
|
|
|
// set the notes, if any, and the popover
|
2016-06-09 18:58:19 +02:00
|
|
|
if(field.getNote() != null && !field.getNote().isEmpty()){
|
2016-07-13 16:11:15 +02:00
|
|
|
noteFieldPopover.setText(new HTML("<p style='color:initial'>" + field.getNote() +"</p>").getHTML());
|
|
|
|
noteFieldPopover.setHeading(new HTML("<b>" + field.getFieldName() +"</b>").getHTML());
|
|
|
|
infoIcon.getElement().getStyle().setCursor(Cursor.HELP);
|
|
|
|
noteFieldPopover.setHtml(true);
|
2016-07-04 11:50:49 +02:00
|
|
|
noteFieldContainer.setVisible(true);
|
2016-06-09 18:58:19 +02:00
|
|
|
}else{
|
2016-07-04 11:50:49 +02:00
|
|
|
noteFieldContainer.setVisible(false);
|
2016-06-09 18:58:19 +02:00
|
|
|
}
|
2016-07-13 16:11:15 +02:00
|
|
|
|
|
|
|
// add a resize handler to center the dialog box if it's not null
|
|
|
|
Window.addResizeHandler(new ResizeHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResize(ResizeEvent event) {
|
|
|
|
|
|
|
|
if(dialog != null)
|
|
|
|
dialog.center();
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-14 12:14:33 +02:00
|
|
|
/**
|
|
|
|
* Bind on events
|
|
|
|
*/
|
|
|
|
private void bind() {
|
|
|
|
|
|
|
|
// on close form
|
|
|
|
eventBus.addHandler(CloseCreationFormEvent.TYPE, new CloseCreationFormEventHandler() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClose(CloseCreationFormEvent event) {
|
|
|
|
|
|
|
|
if(dialog != null)
|
|
|
|
dialog.hide();
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-13 16:11:15 +02:00
|
|
|
@UiHandler("focusPanelIconContainer")
|
|
|
|
void onInfoIconClick(ClickEvent c){
|
|
|
|
|
|
|
|
if(dialog == null){
|
|
|
|
|
|
|
|
// create the dialog box
|
|
|
|
dialog = new GcubeDialogExtended(field.getFieldName(), field.getNote());
|
|
|
|
|
|
|
|
// set as non modal
|
|
|
|
dialog.setModal(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// else just show and center
|
|
|
|
dialog.center();
|
|
|
|
dialog.show();
|
2016-06-09 18:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this field has a valid values
|
2016-07-14 16:47:17 +02:00
|
|
|
* @return a string with the occurred error on error, null otherwise
|
2016-06-09 18:58:19 +02:00
|
|
|
*/
|
2016-07-14 16:47:17 +02:00
|
|
|
public String isFieldValueValid(){
|
2016-06-09 18:58:19 +02:00
|
|
|
|
2016-07-13 18:13:50 +02:00
|
|
|
if(field.getMandatory()){
|
|
|
|
if(holder.getClass().equals(TextBox.class)){
|
2016-06-09 18:58:19 +02:00
|
|
|
|
2016-07-13 18:13:50 +02:00
|
|
|
if(!getFieldCurrentValue().isEmpty())
|
|
|
|
if(field.getValidator() == null || field.getValidator().isEmpty())
|
2016-07-14 16:47:17 +02:00
|
|
|
return null;
|
|
|
|
else return checkValidator(holder, field) ? null : " the inserted value has a wrong format";
|
2016-07-13 18:13:50 +02:00
|
|
|
else
|
2016-07-14 16:47:17 +02:00
|
|
|
return " a mandatory attribute cannot be empty";
|
2016-06-09 18:58:19 +02:00
|
|
|
|
2016-07-13 18:13:50 +02:00
|
|
|
}else
|
2016-07-14 16:47:17 +02:00
|
|
|
return null;
|
2016-07-13 18:13:50 +02:00
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
if(holder.getClass().equals(TextBox.class) && getFieldCurrentValue().isEmpty())
|
2016-07-14 16:47:17 +02:00
|
|
|
return null;
|
2016-07-13 18:13:50 +02:00
|
|
|
|
2016-07-14 16:47:17 +02:00
|
|
|
return checkValidator(holder, field) ? null : " please select a different value for this field";
|
2016-07-13 16:11:15 +02:00
|
|
|
|
2016-06-09 18:58:19 +02:00
|
|
|
}
|
2016-07-13 18:13:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkValidator(Widget holder, MetadataFieldWrapper field){
|
2016-06-09 18:58:19 +02:00
|
|
|
|
2016-07-13 18:13:50 +02:00
|
|
|
String validator = field.getValidator();
|
|
|
|
|
|
|
|
if(validator == null || validator.isEmpty())
|
|
|
|
return true;
|
|
|
|
else if(holder.getClass().equals(TextBox.class)){
|
|
|
|
return getFieldCurrentValue().matches(field.getValidator().trim());
|
|
|
|
}else
|
|
|
|
return true;
|
2016-06-09 18:58:19 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-05 18:19:18 +02:00
|
|
|
* Returns the current value of the field (in case of multiselection Listbox returns
|
|
|
|
* the values separated by column)
|
2016-06-09 18:58:19 +02:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getFieldCurrentValue(){
|
|
|
|
|
2016-09-05 18:19:18 +02:00
|
|
|
String value = "";
|
2016-06-09 18:58:19 +02:00
|
|
|
|
|
|
|
// we validate only listbox and textbox
|
2016-07-13 18:13:50 +02:00
|
|
|
if(holder.getClass().equals(ListBox.class)){
|
2016-09-05 18:19:18 +02:00
|
|
|
|
|
|
|
boolean first = true;
|
|
|
|
|
|
|
|
for(int i = 0; i < ((ListBox)holder).getItemCount(); i++){
|
|
|
|
if(((ListBox)holder).isItemSelected(i)){
|
|
|
|
value += first ? ((ListBox)holder).getItemText(i) : ", " + ((ListBox)holder).getItemText(i);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:35:46 +02:00
|
|
|
// if it was not mandatory but there was no choice, returning empty string
|
2016-07-13 18:13:50 +02:00
|
|
|
if(!field.getMandatory())
|
|
|
|
if(value.equals("Select " + field.getFieldName()))
|
2016-07-13 18:35:46 +02:00
|
|
|
return "";
|
2016-07-13 18:13:50 +02:00
|
|
|
}
|
2016-06-09 18:58:19 +02:00
|
|
|
else if(holder.getClass().equals(TextBox.class))
|
|
|
|
value = ((TextBox)holder).getText();
|
|
|
|
else
|
|
|
|
value = ((CheckBox)holder).getValue().toString();
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-05 18:19:18 +02:00
|
|
|
* Returns the current name of the field
|
2016-06-09 18:58:19 +02:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getFieldName(){
|
|
|
|
|
|
|
|
return field.getFieldName();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Freeze this widget (after on create)
|
|
|
|
*/
|
|
|
|
public void freeze() {
|
|
|
|
|
|
|
|
if(holder.getClass().equals(ListBox.class))
|
|
|
|
((ListBox)holder).setEnabled(false);
|
|
|
|
else if(holder.getClass().equals(TextBox.class))
|
|
|
|
((TextBox)holder).setEnabled(false);
|
|
|
|
else
|
|
|
|
((CheckBox)holder).setEnabled(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|