2016-06-09 18:58:19 +02:00
|
|
|
package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
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-06-09 18:58:19 +02:00
|
|
|
import com.github.gwtbootstrap.client.ui.ListBox;
|
|
|
|
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;
|
|
|
|
import com.google.gwt.dom.client.Style.Display;
|
|
|
|
import com.google.gwt.uibinder.client.UiBinder;
|
|
|
|
import com.google.gwt.uibinder.client.UiField;
|
|
|
|
import com.google.gwt.user.client.ui.Composite;
|
2016-07-04 11:50:49 +02:00
|
|
|
import com.google.gwt.user.client.ui.FlowPanel;
|
|
|
|
import com.google.gwt.user.client.ui.InlineLabel;
|
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;
|
|
|
|
@UiField InlineLabel noteField;
|
2016-07-05 10:29:30 +02:00
|
|
|
@UiField ControlLabel controlLabel;
|
|
|
|
@UiField Controls controls;
|
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;
|
|
|
|
|
|
|
|
public MetaDataFieldSkeleton(MetadataFieldWrapper field) {
|
|
|
|
initWidget(uiBinder.createAndBindUi(this));
|
|
|
|
|
|
|
|
// prepare information
|
|
|
|
this.field = field;
|
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();
|
|
|
|
|
|
|
|
// get vocabulary fields
|
|
|
|
List<String> vocabulary = field.getVocabulary();
|
|
|
|
|
|
|
|
for (String term : vocabulary) {
|
|
|
|
|
|
|
|
((ListBox)holder).addItem(term);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// set default value
|
|
|
|
((ListBox)holder).setSelectedValue(field.getDefaulValue());
|
|
|
|
|
|
|
|
// add to the elementPanel
|
|
|
|
elementPanel.add(holder);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-07-04 11:50:49 +02:00
|
|
|
|
|
|
|
// set holder width
|
|
|
|
holder.setWidth("90%");
|
2016-06-09 18:58:19 +02:00
|
|
|
|
|
|
|
// set the notes, if any
|
|
|
|
if(field.getNote() != null && !field.getNote().isEmpty()){
|
2016-07-04 11:50:49 +02:00
|
|
|
noteField.setText(field.getNote());
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this field has a valid values
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean isFieldValueValid() {
|
2016-06-10 12:10:18 +02:00
|
|
|
|
2016-06-09 18:58:19 +02:00
|
|
|
String validator = field.getValidator();
|
|
|
|
|
|
|
|
// if validator is not present and it is not a textbox
|
2016-06-10 12:10:18 +02:00
|
|
|
if((validator == null || validator.isEmpty()) && !(holder.getClass().equals(TextBox.class) && field.getMandatory()))
|
2016-06-09 18:58:19 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// current value
|
|
|
|
String value;
|
|
|
|
|
|
|
|
// we validate only listbox and textbox
|
|
|
|
if(holder.getClass().equals(ListBox.class))
|
|
|
|
value = ((ListBox)holder).getSelectedItemText();
|
|
|
|
else if(holder.getClass().equals(TextBox.class)){
|
|
|
|
value = ((TextBox)holder).getText();
|
|
|
|
|
2016-06-10 12:10:18 +02:00
|
|
|
// if there is not a validator...
|
2016-06-09 18:58:19 +02:00
|
|
|
if(validator == null){
|
|
|
|
if(value.isEmpty() && field.getMandatory())
|
|
|
|
return false;
|
|
|
|
else if(value.isEmpty() && !field.getMandatory())
|
|
|
|
return true;
|
|
|
|
else if(!value.isEmpty())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-06-10 12:10:18 +02:00
|
|
|
return true; // it's a checkbox (the value doesn't matter)
|
2016-06-09 18:58:19 +02:00
|
|
|
|
2016-06-10 12:10:18 +02:00
|
|
|
return value.matches(validator);
|
2016-06-09 18:58:19 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current value of the field
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getFieldCurrentValue(){
|
|
|
|
|
|
|
|
String value;
|
|
|
|
|
|
|
|
// we validate only listbox and textbox
|
|
|
|
if(holder.getClass().equals(ListBox.class))
|
|
|
|
value = ((ListBox)holder).getSelectedItemText();
|
|
|
|
else if(holder.getClass().equals(TextBox.class))
|
|
|
|
value = ((TextBox)holder).getText();
|
|
|
|
else
|
|
|
|
value = ((CheckBox)holder).getValue().toString();
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current value of the field
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|