ingo@42: package de.intevation.flys.client.client.ui;
ingo@42:
ingo@42: import java.util.LinkedHashMap;
ingo@53: import java.util.List;
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@53: import com.smartgwt.client.widgets.layout.HLayout;
ingo@42: import com.smartgwt.client.widgets.layout.VLayout;
ingo@42:
ingo@42: import de.intevation.flys.client.shared.model.Data;
ingo@47: import de.intevation.flys.client.shared.model.DataItem;
ingo@51: import de.intevation.flys.client.shared.model.DataList;
ingo@47: import de.intevation.flys.client.shared.model.DefaultData;
ingo@47: import de.intevation.flys.client.shared.model.DefaultDataItem;
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@51: public Canvas create(DataList 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@53: public Canvas createOld(DataList dataList) {
ingo@53: List items = dataList.getAll();
ingo@53:
ingo@53: Data dMode = getData(items, "wq_mode");
ingo@53: Data dFrom = getData(items, "wq_from");
ingo@53: Data dTo = getData(items, "wq_to");
ingo@53: Data dStep = getData(items, "wq_step");
ingo@53:
ingo@53: DataItem[] mode = dMode.getItems();
ingo@53: DataItem[] from = dFrom.getItems();
ingo@53: DataItem[] to = dTo.getItems();
ingo@53: DataItem[] step = dStep.getItems();
ingo@53:
ingo@53: HLayout layout = new HLayout();
ingo@53: Label label = new Label(dataList.getLabel());
ingo@53:
ingo@53: label.setWidth("50%");
ingo@53:
ingo@53: String text = mode[0].getStringValue().equals(FIELD_WQ_W)
ingo@53: ? createWString(from[0], to[0], step[0])
ingo@53: : createQString(from[0], to[0], step[0]);
ingo@53:
ingo@53: layout.addMember(label);
ingo@53: layout.addMember(new Label(text));
ingo@53:
ingo@53: return layout;
ingo@53: }
ingo@53:
ingo@53:
ingo@53: protected String createWString(DataItem from, DataItem to, DataItem step) {
ingo@53: StringBuilder sb = new StringBuilder();
ingo@53: sb.append(from.getLabel());
ingo@53: sb.append(" " + MSG.unitWFrom() + " ");
ingo@53: sb.append(to.getLabel());
ingo@53: sb.append(" " + MSG.unitWTo() + " ");
ingo@53: sb.append(step.getLabel());
ingo@53: sb.append(" " + MSG.unitWStep());
ingo@53:
ingo@53: return sb.toString();
ingo@53: }
ingo@53:
ingo@53:
ingo@53: protected String createQString(DataItem from, DataItem to, DataItem step) {
ingo@53: StringBuilder sb = new StringBuilder();
ingo@53: sb.append(from.getLabel());
ingo@53: sb.append(" " + MSG.unitQFrom() + " ");
ingo@53: sb.append(to.getLabel());
ingo@53: sb.append(" " + MSG.unitQTo() + " ");
ingo@53: sb.append(step.getLabel());
ingo@53: sb.append(" " + MSG.unitQStep());
ingo@53:
ingo@53: return sb.toString();
ingo@53: }
ingo@53:
ingo@53:
ingo@53: /**
ingo@53: * This method greps the Data with name name from the list and
ingo@53: * returns it.
ingo@53: *
ingo@53: * @param items A list of Data.
ingo@53: * @param name The name of the Data that we are searching for.
ingo@53: *
ingo@53: * @return the Data with the name name.
ingo@53: */
ingo@53: protected Data getData(List data, String name) {
ingo@53: for (Data d: data) {
ingo@53: if (name.equals(d.getLabel())) {
ingo@53: return d;
ingo@53: }
ingo@53: }
ingo@53:
ingo@53: return null;
ingo@53: }
ingo@53:
ingo@53:
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@51: protected Canvas createWidget(DataList data) {
ingo@42: VLayout layout = new VLayout();
ingo@42: container = new VLayout();
ingo@42: Canvas modeForm = createModePanel();
ingo@42:
ingo@48: // the initial panel is the Single-W panel.
ingo@48: double[] values = getSingleW();
ingo@48: container.addMember(new DoubleArrayPanel(
ingo@48: MSG.unitWSingle(), values, this));
ingo@48:
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@47: return new Data[] {
ingo@47: getDataMode(),
ingo@47: getDataFrom(),
ingo@47: getDataTo(),
ingo@47: getDataStep() };
ingo@47: }
ingo@42:
ingo@47:
ingo@47: /**
ingo@47: * Returns the Data object for the 'mode' attribute.
ingo@47: *
ingo@47: * @return the Data object for the 'mode' attribute.
ingo@47: */
ingo@47: protected Data getDataMode() {
ingo@47: String wqMode = modes.getValueAsString(FIELD_WQ);
ingo@47: DataItem item = new DefaultDataItem("wq_mode", "wq_mode", wqMode);
ingo@47: return new DefaultData(
ingo@51: "wq_mode", null, null, new DataItem[] { item });
ingo@47: }
ingo@47:
ingo@47:
ingo@47: /**
ingo@47: * Returns the Data object for the 'from' attribute.
ingo@47: *
ingo@47: * @return the Data object for the 'from' attribute.
ingo@47: */
ingo@47: protected Data getDataFrom() {
ingo@47: String value = Double.valueOf(getFinalFrom()).toString();
ingo@47: DataItem item = new DefaultDataItem("wq_from", "wq_from", value);
ingo@47: return new DefaultData(
ingo@51: "wq_from", null, null, new DataItem[] { item });
ingo@47: }
ingo@47:
ingo@47:
ingo@47: /**
ingo@47: * Returns the Data object for the 'to' attribute.
ingo@47: *
ingo@47: * @return the Data object for the 'to' attribute.
ingo@47: */
ingo@47: protected Data getDataTo() {
ingo@47: String value = Double.valueOf(getFinalTo()).toString();
ingo@47: DataItem item = new DefaultDataItem("wq_to", "wq_to", value);
ingo@47: return new DefaultData(
ingo@51: "wq_to", null, null, new DataItem[] { item });
ingo@47: }
ingo@47:
ingo@47:
ingo@47: /**
ingo@47: * Returns the Data object for the 'step' attribute.
ingo@47: *
ingo@47: * @return the Data object for the 'step' attribute.
ingo@47: */
ingo@47: protected Data getDataStep() {
ingo@47: String value = Double.valueOf(getFinalStep()).toString();
ingo@47: DataItem item = new DefaultDataItem("wq_step","wq_step", value);
ingo@47: return new DefaultData(
ingo@51: "wq_step", null, null, new DataItem[] { item });
ingo@47: }
ingo@47:
ingo@47:
ingo@47: /**
ingo@47: * Returns the value of 'from' depending on the selected input mode.
ingo@47: *
ingo@47: * @return the value of 'from' depending on the selected input mode.
ingo@47: */
ingo@47: protected double getFinalFrom() {
ingo@47: boolean wMode = isWMode();
ingo@47: boolean rangeMode = isRangeMode();
ingo@47:
ingo@47: if (rangeMode) {
ingo@47: return wMode ? getFromW() : getFromQ();
ingo@47:
ingo@47: }
ingo@47: else {
ingo@47: double[] values = wMode ? getSingleW() : getSingleQ();
ingo@47: double value = Double.MAX_VALUE;
ingo@47:
ingo@47: for (double v: values) {
ingo@47: value = value < v ? value : v;
ingo@47: }
ingo@47:
ingo@47: return value;
ingo@47: }
ingo@47: }
ingo@47:
ingo@47:
ingo@47: /**
ingo@47: * Returns the value of 'to' depending on the selected input mode.
ingo@47: *
ingo@47: * @return the value of 'to' depending on the selected input mode.
ingo@47: */
ingo@47: protected double getFinalTo() {
ingo@47: boolean wMode = isWMode();
ingo@47: boolean rangeMode = isRangeMode();
ingo@47:
ingo@47: if (rangeMode) {
ingo@47: return wMode ? getToW() : getToQ();
ingo@47:
ingo@47: }
ingo@47: else {
ingo@47: double[] values = wMode ? getSingleW() : getSingleQ();
ingo@47: double value = Double.MIN_VALUE;
ingo@47:
ingo@47: for (double v: values) {
ingo@47: value = value > v ? value : v;
ingo@47: }
ingo@47:
ingo@47: return value;
ingo@47: }
ingo@47: }
ingo@47:
ingo@47:
ingo@47: /**
ingo@47: * Returns the value of 'step' depending on the selected input mode.
ingo@47: *
ingo@47: * @return the value of 'step' depending on the selected input mode.
ingo@47: */
ingo@47: protected double getFinalStep() {
ingo@47: boolean wMode = isWMode();
ingo@47: boolean rangeMode = isRangeMode();
ingo@47:
ingo@47: if (rangeMode) {
ingo@47: // we have no field to enter the 'step' attribute in the
ingo@47: // range mode
ingo@47: return 0.0;
ingo@47: }
ingo@47: else {
ingo@47: if (wMode) {
ingo@47: return getStepW();
ingo@47: }
ingo@47: else {
ingo@47: // we have no field to enter the 'step' attribute in the
ingo@47: // range mode
ingo@47: return getStepQ();
ingo@47: }
ingo@47: }
ingo@47: }
ingo@47:
ingo@47:
ingo@47: /**
ingo@47: * Determines the range/single mode.
ingo@47: *
ingo@47: * @return true if the range mode is activated.
ingo@47: */
ingo@47: public boolean isRangeMode() {
ingo@47: String rMode = modes.getValueAsString(FIELD_MODE);
ingo@47:
ingo@47: return rMode.equals(FIELD_MODE_RANGE) ? true : false;
ingo@47: }
ingo@47:
ingo@47:
ingo@47: /**
ingo@47: * Determines the w/q mode.
ingo@47: *
ingo@47: * @return true, if the W mode is activated.
ingo@47: */
ingo@47: public boolean isWMode() {
ingo@47: String wq = modes.getValueAsString(FIELD_WQ);
ingo@47:
ingo@47: return wq.equals(FIELD_WQ_W) ? true : false;
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 :