ckan-metadata-publisher-widget/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/client/ui/MetaDataFieldSkeleton.java

202 lines
4.8 KiB
Java
Raw Normal View History

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;
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;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
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;
@UiField FlowPanel noteFieldContainer;
@UiField InlineLabel noteField;
// the element that holds the value (it could be a checkbox, textbox or listbox)
private Widget holder;
// the field this object represents
private MetadataFieldWrapper field;
public MetaDataFieldSkeleton(MetadataFieldWrapper field) {
initWidget(uiBinder.createAndBindUi(this));
// prepare information
this.field = field;
// 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);
}
}
// set holder width
holder.setWidth("90%");
// set the notes, if any
if(field.getNote() != null && !field.getNote().isEmpty()){
noteField.setText(field.getNote());
noteFieldContainer.setVisible(true);
}else{
noteFieldContainer.setVisible(false);
}
}
/**
* Check if this field has a valid values
* @return
*/
public boolean isFieldValueValid() {
String validator = field.getValidator();
// if validator is not present and it is not a textbox
if((validator == null || validator.isEmpty()) && !(holder.getClass().equals(TextBox.class) && field.getMandatory()))
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();
// if there is not a validator...
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
return true; // it's a checkbox (the value doesn't matter)
return value.matches(validator);
}
/**
* 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);
}
}