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

260 lines
6.7 KiB
Java
Raw Normal View History

package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui;
import java.util.List;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui.utils.GcubeDialogExtended;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetadataFieldWrapper;
import com.github.gwtbootstrap.client.ui.CheckBox;
import com.github.gwtbootstrap.client.ui.ControlLabel;
import com.github.gwtbootstrap.client.ui.Controls;
import com.github.gwtbootstrap.client.ui.Icon;
import com.github.gwtbootstrap.client.ui.ListBox;
import com.github.gwtbootstrap.client.ui.Popover;
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.Cursor;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
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.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
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 Popover noteFieldPopover;
@UiField ControlLabel controlLabel;
@UiField Controls controls;
@UiField Icon infoIcon;
@UiField FocusPanel focusPanelIconContainer;
// 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;
// the dialog box for this metadata
private GcubeDialogExtended dialog;
public MetaDataFieldSkeleton(MetadataFieldWrapper field) {
initWidget(uiBinder.createAndBindUi(this));
// prepare information
this.field = field;
// add custom css properties
controls.addStyleName("form-controls-custom");
controlLabel.addStyleName("form-control-label-custom");
// 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
if(holder.getClass().equals(ListBox.class))
holder.setWidth("96%");
else
holder.setWidth("95%");
// set the notes, if any, and the popover
if(field.getNote() != null && !field.getNote().isEmpty()){
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);
noteFieldContainer.setVisible(true);
}else{
noteFieldContainer.setVisible(false);
}
// 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();
}
});
}
@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();
}
/**
* Check if this field has a valid values
* @return
*/
public boolean isFieldValueValid() {
String validator = field.getValidator();
GWT.log("Validator is " + validator);
// 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);
}
}