ingo@42: package de.intevation.flys.client.client.ui; ingo@42: ingo@42: import java.util.LinkedHashMap; ingo@42: ingo@42: import com.google.gwt.core.client.GWT; ingo@42: ingo@42: import com.smartgwt.client.widgets.Canvas; ingo@42: import com.smartgwt.client.widgets.IButton; ingo@42: import com.smartgwt.client.widgets.Label; ingo@42: import com.smartgwt.client.widgets.form.DynamicForm; ingo@42: import com.smartgwt.client.widgets.form.fields.FormItem; ingo@42: import com.smartgwt.client.widgets.form.fields.RadioGroupItem; ingo@42: import com.smartgwt.client.widgets.form.fields.events.BlurHandler; ingo@42: import com.smartgwt.client.widgets.form.fields.events.BlurEvent; ingo@42: import com.smartgwt.client.widgets.form.fields.events.ChangeHandler; ingo@42: import com.smartgwt.client.widgets.form.fields.events.ChangeEvent; ingo@42: import com.smartgwt.client.widgets.layout.VLayout; ingo@42: ingo@42: import de.intevation.flys.client.shared.model.Data; ingo@42: import de.intevation.flys.client.client.FLYSMessages; ingo@42: ingo@42: ingo@42: /** ingo@42: * This UIProvider creates a widget to enter W or Q data. ingo@42: * ingo@42: * @author Ingo Weinzierl ingo@42: */ ingo@42: public class WQInputPanel ingo@42: extends AbstractUIProvider ingo@42: implements ChangeHandler, BlurHandler ingo@42: { ingo@42: /** The message class that provides i18n strings.*/ ingo@42: protected FLYSMessages MSG = GWT.create(FLYSMessages.class); ingo@42: ingo@42: ingo@42: /** The constant field name for choosing w or q mode.*/ ingo@42: public static final String FIELD_WQ = "wq"; ingo@42: ingo@42: /** The constant field value for W input mode.*/ ingo@42: public static final String FIELD_WQ_W = "W"; ingo@42: ingo@42: /** The constant field value for Q input mode.*/ ingo@42: public static final String FIELD_WQ_Q = "Q"; ingo@42: ingo@42: /** The constant field name for choosing single values or range.*/ ingo@42: public static final String FIELD_MODE = "mode"; ingo@42: ingo@42: /** The constant field value for single input mode.*/ ingo@42: public static final String FIELD_MODE_SINGLE = "single"; ingo@42: ingo@42: /** The constant field value for range input mode.*/ ingo@42: public static final String FIELD_MODE_RANGE = "range"; ingo@42: ingo@42: /** The constant value that determines the width of the left panel.*/ ingo@42: public static final int WIDTH_LEFT = 200; ingo@42: ingo@42: ingo@42: /** The container that manages the w and q panels.*/ ingo@42: protected VLayout container; ingo@42: ingo@42: /** The RadioGroupItem that determines the w/q input mode.*/ ingo@42: protected DynamicForm modes; ingo@42: ingo@42: /** The 'from' value entered in the range W mode.*/ ingo@42: protected double fromW; ingo@42: ingo@42: /** The 'to' value entered in the range W mode.*/ ingo@42: protected double toW; ingo@42: ingo@42: /** The 'step' value entered in the range W mode.*/ ingo@42: protected double stepW; ingo@42: ingo@42: /** The values entered in the single W mode.*/ ingo@42: protected double[] valuesW; ingo@42: ingo@42: /** The 'from' value entered in the range Q mode.*/ ingo@42: protected double fromQ; ingo@42: ingo@42: /** The 'to' value entered in the range Q mode.*/ ingo@42: protected double toQ; ingo@42: ingo@42: /** The 'step' value entered in the range Q mode.*/ ingo@42: protected double stepQ; ingo@42: ingo@42: /** The values entered in the single Q mode.*/ ingo@42: protected double[] valuesQ; ingo@42: ingo@42: ingo@42: /** ingo@42: * Creates a new WQInputPanel instance. ingo@42: */ ingo@42: public WQInputPanel() { ingo@42: } ingo@42: ingo@42: ingo@42: /** ingo@42: * This method calls createWidget and puts a 'next' button to the bottom. ingo@42: * ingo@42: * @param data The data that is displayed. ingo@42: * ingo@42: * @return the widget. ingo@42: */ ingo@42: public Canvas create(Data data) { ingo@42: Canvas widget = createWidget(data); ingo@45: IButton submit = new IButton(MSG.next(), this); ingo@42: Label label = new Label(MSG.wqTitle()); ingo@42: ingo@42: label.setHeight(25); ingo@42: ingo@42: VLayout layout = new VLayout(); ingo@42: layout.addMember(label); ingo@42: layout.addMember(widget); ingo@42: layout.addMember(submit); ingo@42: ingo@42: return layout; ingo@42: } ingo@42: ingo@42: ingo@42: /** ingo@42: * This method creates the whole widget. There is a panel on the left, that ingo@42: * allows the user to enter values manually by keyboard. On the right, there ingo@42: * is a table that allows the user to enter values by mouse click. ingo@42: * ingo@42: * @param data The data that is displayed in the table on the right. ingo@42: * ingo@42: * @return the widget. ingo@42: */ ingo@42: protected Canvas createWidget(Data data) { ingo@42: VLayout layout = new VLayout(); ingo@42: container = new VLayout(); ingo@42: Canvas modeForm = createModePanel(); ingo@42: ingo@42: layout.addMember(modeForm); ingo@42: layout.addMember(container); ingo@42: ingo@42: // TODO add a table on the right to select Q values ingo@42: ingo@42: return layout; ingo@42: } ingo@42: ingo@42: ingo@42: /** ingo@42: * This method creates the mode panel. It contains two radio button panels ingo@42: * that allows the user to switch the input mode between w/q and ingo@42: * single/range input. ingo@42: * ingo@42: * @return a panel. ingo@42: */ ingo@42: protected Canvas createModePanel() { ingo@42: RadioGroupItem wq = new RadioGroupItem(FIELD_WQ); ingo@42: wq.setShowTitle(false); ingo@42: wq.setVertical(false); ingo@42: wq.setWidth(WIDTH_LEFT); ingo@42: ingo@42: RadioGroupItem mode = new RadioGroupItem(FIELD_MODE); ingo@42: mode.setShowTitle(false); ingo@42: mode.setVertical(false); ingo@42: mode.setWidth(WIDTH_LEFT); ingo@42: ingo@42: LinkedHashMap wqValues = new LinkedHashMap(); ingo@42: wqValues.put(FIELD_WQ_W, MSG.wqW()); ingo@42: wqValues.put(FIELD_WQ_Q, MSG.wqQ()); ingo@42: ingo@42: LinkedHashMap modeValues = new LinkedHashMap(); ingo@42: modeValues.put(FIELD_MODE_SINGLE, MSG.wqSingle()); ingo@42: modeValues.put(FIELD_MODE_RANGE, MSG.wqRange()); ingo@42: ingo@42: wq.setValueMap(wqValues); ingo@42: mode.setValueMap(modeValues); ingo@42: ingo@42: wq.addChangeHandler(this); ingo@42: mode.addChangeHandler(this); ingo@42: ingo@42: modes = new DynamicForm(); ingo@42: modes.setFields(wq, mode); ingo@42: modes.setWidth(WIDTH_LEFT); ingo@42: modes.setNumCols(1); ingo@42: ingo@42: LinkedHashMap initial = new LinkedHashMap(); ingo@42: initial.put(FIELD_WQ, FIELD_WQ_W); ingo@42: initial.put(FIELD_MODE, FIELD_MODE_SINGLE); ingo@42: modes.setValues(initial); ingo@42: ingo@42: return modes; ingo@42: } ingo@42: ingo@42: ingo@42: /** ingo@42: * This method returns the selected data. ingo@42: * ingo@42: * @return the selected/inserted data. ingo@42: */ ingo@42: public Data[] getData() { ingo@42: // TODO Implement me ingo@42: ingo@42: return null; ingo@42: } ingo@42: ingo@42: ingo@42: /** ingo@42: * This method changes the lower panel with the input fields depending on ingo@42: * the combination of the two radio button panels. ingo@42: * ingo@42: * @param event The ChangeEvent. ingo@42: */ ingo@42: public void onChange(ChangeEvent event) { ingo@42: DynamicForm form = event.getForm(); ingo@42: FormItem item = event.getItem(); ingo@42: ingo@42: String wqMode = null; ingo@42: String inputMode = null; ingo@42: ingo@42: if (item.getFieldName().equals(FIELD_MODE)) { ingo@42: wqMode = form.getValueAsString(FIELD_WQ); ingo@42: inputMode = (String) event.getValue(); ingo@42: } ingo@42: else { ingo@42: wqMode = (String) event.getValue(); ingo@42: inputMode = form.getValueAsString(FIELD_MODE); ingo@42: } ingo@42: ingo@42: container.removeMembers(container.getMembers()); ingo@42: ingo@42: Canvas newPanel = null; ingo@42: ingo@42: if (wqMode.equals(FIELD_WQ_W)) { ingo@42: if (inputMode.equals(FIELD_MODE_SINGLE)) { ingo@42: // Single W mode ingo@42: double[] values = getSingleW(); ingo@42: ingo@42: newPanel = new DoubleArrayPanel( ingo@42: MSG.unitWSingle(), values, this); ingo@42: } ingo@42: else { ingo@42: // Range W mode ingo@42: double from = getFromW(); ingo@42: double to = getToW(); ingo@42: double step = getStepW(); ingo@42: ingo@42: newPanel = new DoubleRangePanel( ingo@42: MSG.unitWFrom(), MSG.unitWTo(), MSG.unitWStep(), ingo@42: from, to, step, ingo@42: 250, ingo@42: this); ingo@42: } ingo@42: } ingo@42: else { ingo@42: if (inputMode.equals(FIELD_MODE_SINGLE)) { ingo@42: // Single Q mode ingo@42: double[] values = getSingleQ(); ingo@42: ingo@42: newPanel = new DoubleArrayPanel( ingo@42: MSG.unitQSingle(), values, this); ingo@42: } ingo@42: else { ingo@42: // Range Q mode ingo@42: double from = getFromQ(); ingo@42: double to = getToQ(); ingo@42: double step = getStepQ(); ingo@42: ingo@42: newPanel = new DoubleRangePanel( ingo@42: MSG.unitQFrom(), MSG.unitQTo(), MSG.unitQStep(), ingo@42: from, to, step, ingo@42: 250, ingo@42: this); ingo@42: } ingo@42: } ingo@42: ingo@42: container.addMember(newPanel); ingo@42: } ingo@42: ingo@42: ingo@42: /** ingo@42: * This method is called if the value of one of the input fields might have ingo@42: * changed. The entered values are validated and stored. ingo@42: * ingo@42: * @param event The BlurEvent. ingo@42: */ ingo@42: public void onBlur(BlurEvent event) { ingo@42: DynamicForm form = event.getForm(); ingo@42: FormItem item = event.getItem(); ingo@42: ingo@42: String wqMode = (String) modes.getValue(FIELD_WQ); ingo@42: String inputMode = (String) modes.getValue(FIELD_MODE); ingo@42: ingo@42: if (wqMode.equals(FIELD_WQ_W)) { ingo@42: if (inputMode.equals(FIELD_MODE_SINGLE)) { ingo@42: DoubleArrayPanel p = (DoubleArrayPanel) form; ingo@42: ingo@42: if (p.validateForm(item)) { ingo@42: setSingleW(p.getInputValues(item)); ingo@42: } ingo@42: } ingo@42: else { ingo@42: DoubleRangePanel p = (DoubleRangePanel) form; ingo@42: ingo@42: if (p.validateForm(item)) { ingo@42: setFromW(p.getFrom()); ingo@42: setToW(p.getTo()); ingo@42: setStepW(p.getStep()); ingo@42: } ingo@42: } ingo@42: } ingo@42: else { ingo@42: if (inputMode.equals(FIELD_MODE_SINGLE)) { ingo@42: DoubleArrayPanel p = (DoubleArrayPanel) form; ingo@42: ingo@42: if (p.validateForm(item)) { ingo@42: setSingleQ(p.getInputValues(item)); ingo@42: } ingo@42: } ingo@42: else { ingo@42: DoubleRangePanel p = (DoubleRangePanel) form; ingo@42: ingo@42: if (p.validateForm(item)) { ingo@42: setFromQ(p.getFrom()); ingo@42: setToQ(p.getTo()); ingo@42: setStepQ(p.getStep()); ingo@42: } ingo@42: } ingo@42: } ingo@42: } ingo@42: ingo@42: ingo@42: protected double[] getSingleQ() { ingo@42: return valuesQ; ingo@42: } ingo@42: ingo@42: ingo@42: protected void setSingleQ(double[] values) { ingo@42: valuesQ = values; ingo@42: } ingo@42: ingo@42: ingo@42: protected double getFromQ() { ingo@42: return fromQ; ingo@42: } ingo@42: ingo@42: ingo@42: protected void setFromQ(double fromQ) { ingo@42: this.fromQ = fromQ; ingo@42: } ingo@42: ingo@42: ingo@42: protected double getToQ() { ingo@42: return toQ; ingo@42: } ingo@42: ingo@42: ingo@42: protected void setToQ(double toQ) { ingo@42: this.toQ = toQ; ingo@42: } ingo@42: ingo@42: ingo@42: protected double getStepQ() { ingo@42: return stepQ; ingo@42: } ingo@42: ingo@42: ingo@42: protected void setStepQ(double stepQ) { ingo@42: this.stepQ = stepQ; ingo@42: } ingo@42: ingo@42: ingo@42: protected double[] getSingleW() { ingo@42: return valuesW; ingo@42: } ingo@42: ingo@42: ingo@42: protected void setSingleW(double[] values) { ingo@42: valuesW = values; ingo@42: } ingo@42: ingo@42: ingo@42: protected double getFromW() { ingo@42: return fromW; ingo@42: } ingo@42: ingo@42: ingo@42: protected void setFromW(double fromW) { ingo@42: this.fromW = fromW; ingo@42: } ingo@42: ingo@42: ingo@42: protected double getToW() { ingo@42: return toW; ingo@42: } ingo@42: ingo@42: ingo@42: protected void setToW(double toW) { ingo@42: this.toW = toW; ingo@42: } ingo@42: ingo@42: ingo@42: protected double getStepW() { ingo@42: return stepW; ingo@42: } ingo@42: ingo@42: ingo@42: protected void setStepW(double stepW) { ingo@42: this.stepW = stepW; ingo@42: } ingo@42: } ingo@42: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :