Java tutorial
/* * The DecidR Development Team licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package de.decidr.ui.view.uibuilder; import java.util.HashMap; import java.util.Map; import com.vaadin.data.validator.AbstractValidator; import com.vaadin.data.validator.IntegerValidator; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.CheckBox; import com.vaadin.ui.Component; import com.vaadin.ui.Field; import com.vaadin.ui.Form; import com.vaadin.ui.Label; import com.vaadin.ui.TextField; import de.decidr.model.schema.decidrtypes.ContentType; import de.decidr.model.schema.decidrtypes.TBoolean; import de.decidr.model.schema.decidrtypes.TInformation; import de.decidr.model.schema.decidrtypes.TTaskItem; import de.decidr.model.schema.dwdl.DWDLSimpleVariableType; import de.decidr.model.schema.dwdl.TVariable; import de.decidr.ui.controller.UploadAction; import de.decidr.ui.data.FloatValidator; import de.decidr.ui.view.ConfigVariableForm; import de.decidr.ui.view.DateField; import de.decidr.ui.view.UploadComponent; import de.decidr.ui.view.windows.WorkItemWindow; /** * Private interface used by {@link FormBuilder}.<br> * Each DWDL variable type is mapped to a class building UI controls for that * specific type. * * XXX styleguide - perhaps we should name the builder classes XXXBuilder? ~dh * * @author wf */ interface FieldBuilder { /** * Create an editor component for the given taskItem.<br> * * @param label that the control should edit. * @param value that the control should initially display.<br>Should correspond * to the according value in taskItem, but is given for convenience. * @return Implementations must return an instance of either {@link Field} * or {@link AbstractComponent}. */ public Object createControl(String label, String value); } class StringFieldBuilder implements FieldBuilder { @Override public Field createControl(String label, String value) { TextField field = new TextField(label); field.setImmediate(true); field.setValue(value); return field; } } class BooleanFieldBuilder implements FieldBuilder { @Override public Field createControl(String label, String value) { CheckBox field = new CheckBox(label); field.setValue(value.equals(TBoolean.YES.value())); return field; } } class DateFieldBuilder implements FieldBuilder { @Override public Field createControl(String label, String value) { DateField field = new DateField(label); field.setValue(value); return field; } } class FloatFieldBuilder implements FieldBuilder { @Override public Field createControl(String label, String value) { TextField field = new TextField(label); field.addValidator(new FloatValidator("Please enter a valid float")); field.setImmediate(true); field.setValue(value); return field; } } class IntegerFieldBuilder implements FieldBuilder { @Override public Field createControl(String label, String value) { TextField field = new TextField(label); field.addValidator(new IntegerValidator("Please enter an Integer!")); field.setImmediate(true); field.setValue(value); return field; } } class TimeFieldBuilder implements FieldBuilder { @Override public Field createControl(String label, String value) { TextField field = new TextField(label); field.addValidator(new AbstractValidator("Please enter time in the format hh:mm:ss.") { private static final long serialVersionUID = -2794593423408118150L; @Override public boolean isValid(Object value) { String[] vals = ((String) value).split(":"); if (vals == null || vals.length != 3) { return false; } try { int hour = Integer.parseInt(vals[0], 10); int minute = Integer.parseInt(vals[1], 10); int second = Integer.parseInt(vals[2], 10); return (hour >= 0 && hour < 24 && minute >= 0 && minute <= 60 && second >= 0); } catch (NumberFormatException e) { return false; } } }); field.setImmediate(true); field.setValue(value); return field; } } class FileUploadComponentBuilder implements FieldBuilder { @Override public Component createControl(String label, String value) { String fileId = value; Long id; if (fileId.equals("")) { id = null; } else { id = Long.valueOf(fileId); } UploadComponent upload = new UploadComponent(id, new UploadAction()); return upload; } } /** * Used by {@link WorkItemWindow} and {@link ConfigVariableForm} to build * form controls from WorkItems or Variables.<br> * Actual instantiation is passed down to a private class, based on the * WorkItem's type. * * @author Wolfgang Fellger */ public class FormBuilder { private Map<DWDLSimpleVariableType, FieldBuilder> fieldBuilders; public FormBuilder() { fieldBuilders = new HashMap<DWDLSimpleVariableType, FieldBuilder>(); fieldBuilders.put(DWDLSimpleVariableType.STRING, new StringFieldBuilder()); fieldBuilders.put(DWDLSimpleVariableType.BOOLEAN, new BooleanFieldBuilder()); fieldBuilders.put(DWDLSimpleVariableType.DATE, new DateFieldBuilder()); fieldBuilders.put(DWDLSimpleVariableType.FLOAT, new FloatFieldBuilder()); fieldBuilders.put(DWDLSimpleVariableType.INTEGER, new IntegerFieldBuilder()); fieldBuilders.put(DWDLSimpleVariableType.TIME, new TimeFieldBuilder()); fieldBuilders.put(DWDLSimpleVariableType.ANY_URI, new FileUploadComponentBuilder()); } public void addControl(Form form, DWDLSimpleVariableType type, String name, String label, String value, String hint) { FieldBuilder fieldBuilder = fieldBuilders.get(type); if (fieldBuilder != null) { Object control = fieldBuilder.createControl(label, value); if (control instanceof Field) { ((Field) control).setDescription(hint); form.addField(name, (Field) control); } else { ((AbstractComponent) control).setDescription(hint); ((AbstractComponent) control).setData(name); form.getLayout().addComponent((Component) control); } } else { // TODO Better error handling form.addField("error", new TextField("Unknown field type: " + type)); } } /** * Create and add a Vaadin control to edit the given {@link TTaskItem}.<br> * * @param form to manipulate (most not be <code>null</code>).<br> * In particular, this method may call either addField, or * addComponent of the form's corresponding Layout. * @param taskItem that the new control should edit. * (must not be <code>null</code>. */ public void addControl(Form form, TTaskItem taskItem) { String value = taskItem.getValue() == null ? "" : taskItem.getValue().toString(); DWDLSimpleVariableType type = DWDLSimpleVariableType.valueOf(taskItem.getType().toString()); addControl(form, type, taskItem.getName(), taskItem.getLabel(), value, taskItem.getHint()); } public void addControl(Form form, TVariable variable) { DWDLSimpleVariableType type = DWDLSimpleVariableType.fromValue(variable.getType()); String value = ""; if (variable.getInitialValue() != null && variable.getInitialValue().getContent() != null && variable.getInitialValue().getContent().size() > 0) { value = variable.getInitialValue().getContent().get(0).toString(); } addControl(form, type, variable.getName(), variable.getLabel(), value, variable.getLabel()); } /** * Adds a pretty label for a read-only information item to the given form. * * @param form * form to manipulate (must not be <code>null</code>) * @param information * read-only information item to turn into a control (must not be * <code>null</code>) */ public void addControl(Form form, TInformation information) { if (form == null) { throw new IllegalArgumentException("Form is null"); } if (information == null) { throw new IllegalArgumentException("Information is null"); } Label label = new Label(); /* * An information item may contain plain text or XHTML, we change the * content mode of our label accordingly. */ label.setContentMode(information.getContent().getType().equals(ContentType.XHTML) ? Label.CONTENT_XHTML : Label.CONTENT_TEXT); label.setValue(information.getContent().getValue()); /* * TODO there's a high chance that this label will look like crap when * slapped into the default layout. Remove this marker if it looks * pretty ~dh */ form.getLayout().addComponent(label); } }