ingo@41: package de.intevation.flys.client.client.ui; ingo@41: ingo@520: import java.util.ArrayList; ingo@43: import java.util.LinkedHashMap; ingo@53: import java.util.List; ingo@41: ingo@41: import com.google.gwt.core.client.GWT; ingo@562: import com.google.gwt.i18n.client.NumberFormat; raimund@235: import com.google.gwt.user.client.rpc.AsyncCallback; ingo@41: ingo@41: import com.smartgwt.client.widgets.Canvas; ingo@41: import com.smartgwt.client.widgets.Label; ingo@41: import com.smartgwt.client.widgets.form.DynamicForm; ingo@41: import com.smartgwt.client.widgets.form.fields.events.BlurHandler; ingo@41: import com.smartgwt.client.widgets.form.fields.events.BlurEvent; ingo@41: import com.smartgwt.client.widgets.form.fields.events.ChangeHandler; ingo@41: import com.smartgwt.client.widgets.form.fields.events.ChangeEvent; ingo@41: import com.smartgwt.client.widgets.form.fields.FormItem; ingo@41: import com.smartgwt.client.widgets.form.fields.RadioGroupItem; ingo@41: import com.smartgwt.client.widgets.layout.HLayout; ingo@41: import com.smartgwt.client.widgets.layout.VLayout; raimund@235: import com.smartgwt.client.widgets.grid.ListGrid; raimund@235: import com.smartgwt.client.widgets.grid.ListGridField; raimund@235: import com.smartgwt.client.widgets.grid.ListGridRecord; raimund@235: import com.smartgwt.client.widgets.grid.events.RecordClickHandler; raimund@235: import com.smartgwt.client.widgets.grid.events.RecordClickEvent; raimund@235: import com.smartgwt.client.widgets.grid.events.CellClickHandler; raimund@235: import com.smartgwt.client.widgets.grid.events.CellClickEvent; raimund@235: raimund@235: import com.smartgwt.client.widgets.tab.TabSet; raimund@235: import com.smartgwt.client.widgets.tab.Tab; raimund@235: import com.smartgwt.client.data.Record; raimund@235: raimund@235: import com.smartgwt.client.types.ListGridFieldType; ingo@41: ingo@41: import de.intevation.flys.client.shared.model.Data; ingo@46: import de.intevation.flys.client.shared.model.DataItem; ingo@51: import de.intevation.flys.client.shared.model.DataList; ingo@46: import de.intevation.flys.client.shared.model.DefaultData; ingo@46: import de.intevation.flys.client.shared.model.DefaultDataItem; raimund@235: import de.intevation.flys.client.shared.model.DistanceInfoObject; raimund@235: import de.intevation.flys.client.shared.model.DistanceInfoRecord; raimund@235: import de.intevation.flys.client.shared.model.ArtifactDescription; raimund@235: raimund@235: import de.intevation.flys.client.client.services.DistanceInfoService; raimund@235: import de.intevation.flys.client.client.services.DistanceInfoServiceAsync; ingo@211: import de.intevation.flys.client.client.FLYSConstants; raimund@235: import de.intevation.flys.client.client.FLYSImages; raimund@235: import de.intevation.flys.client.client.Config; ingo@41: ingo@41: ingo@41: /** ingo@41: * This UIProvider creates a widget to enter locations or a distance. ingo@41: * ingo@41: * @author Ingo Weinzierl ingo@41: */ ingo@41: public class LocationDistancePanel ingo@41: extends AbstractUIProvider ingo@41: implements ChangeHandler, BlurHandler ingo@41: { ingo@41: /** The message class that provides i18n strings.*/ ingo@211: protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); ingo@41: raimund@235: /** The interface that provides the image resources. */ raimund@235: private FLYSImages IMAGES = GWT.create(FLYSImages.class); raimund@235: raimund@235: /** The DistanceInfoService used to retrieve locations about rivers.*/ raimund@235: protected DistanceInfoServiceAsync distanceInfoService = raimund@235: GWT.create(DistanceInfoService.class); ingo@41: ingo@516: public static final String FIELD_MODE = "mode"; ingo@43: ingo@41: /** The constant name of the input field to enter the start of a distance.*/ ingo@41: public static final String FIELD_FROM = "from"; ingo@41: ingo@41: /** The constant name of the input field to enter the end of a distance.*/ ingo@41: public static final String FIELD_TO = "to"; ingo@41: ingo@516: /** The constant name of the input field to enter locations.*/ ingo@516: public static final String FIELD_VALUE_LOCATION = "location"; ingo@516: ingo@516: /** The constant name of the input field to enter distance.*/ ingo@516: public static final String FIELD_VALUE_DISTANCE = "distance"; ingo@516: ingo@41: /** The constant name of the input field to enter the step width of a ingo@41: * distance.*/ ingo@41: public static final String FIELD_WIDTH = "width"; ingo@41: ingo@53: public static final int WIDTH = 250; ingo@53: ingo@41: ingo@41: /** The radio group for input mode selection.*/ ingo@43: protected DynamicForm mode; ingo@41: ingo@41: /** A container that will contain the location or the distance panel.*/ ingo@41: protected HLayout container; ingo@41: ingo@55: /** The min value for a distance.*/ ingo@55: protected double min; ingo@55: ingo@55: /** The max value for a distance.*/ ingo@55: protected double max; ingo@55: ingo@43: /** The 'from' value entered in the distance mode.*/ ingo@43: protected double from; ingo@43: ingo@43: /** The 'to' value entered in the distance mode.*/ ingo@43: protected double to; ingo@43: ingo@43: /** The 'step' value entered in the distance mode.*/ ingo@43: protected double step; ingo@43: ingo@43: /** The values entered in the location mode.*/ ingo@43: protected double[] values; ingo@43: raimund@235: /** The input panel for locations */ raimund@235: protected DoubleArrayPanel locationPanel; raimund@235: raimund@235: /** The input panel for distances */ raimund@235: protected DoubleRangePanel distancePanel; raimund@235: raimund@235: /** The tab set containing the location and distance table*/ raimund@235: protected TabSet inputTables; raimund@235: raimund@235: /** The distance table. */ raimund@235: protected ListGrid distanceTable; raimund@235: raimund@235: /** The locations table */ raimund@235: protected ListGrid locationsTable; raimund@235: raimund@235: /** The locations table for distance input. */ raimund@235: protected ListGrid locationDistanceTable; ingo@41: raimund@237: /** The table data. */ raimund@237: protected DistanceInfoObject[] tableData; raimund@237: ingo@41: /** ingo@41: * Creates a new LocationDistancePanel instance. ingo@41: */ ingo@41: public LocationDistancePanel() { raimund@235: distanceTable = new ListGrid(); raimund@235: locationsTable = new ListGrid(); raimund@235: locationDistanceTable = new ListGrid(); raimund@284: locationDistanceTable.setShowHeaderContextMenu(false); raimund@284: distanceTable.setShowHeaderContextMenu(false); raimund@284: locationsTable.setShowHeaderContextMenu(false); ingo@41: } ingo@41: ingo@41: ingo@41: /** ingo@41: * This method creates a widget that contains a label, a panel with ingo@41: * checkboxes to switch the input mode between location and distance input, ingo@41: * and a the mode specific panel. ingo@41: * ingo@41: * @param data The data that might be inserted. ingo@41: * ingo@41: * @return a panel. ingo@41: */ ingo@51: public Canvas create(DataList data) { ingo@41: VLayout layout = new VLayout(); ingo@83: layout.setMembersMargin(10); ingo@41: ingo@57: Label label = new Label(MESSAGES.location_distance_state()); ingo@57: Canvas widget = createWidget(data); ingo@57: Canvas submit = getNextButton(); raimund@235: createDistanceInputPanel(); raimund@235: ingo@516: initDefaults(data); ingo@516: raimund@235: createDistanceTable(); raimund@235: createLocationTable(); raimund@237: createLocationTableDistance (); ingo@41: ingo@83: widget.setHeight(50); ingo@41: label.setHeight(25); ingo@41: ingo@41: layout.addMember(label); ingo@45: layout.addMember(widget); ingo@45: layout.addMember(submit); ingo@45: ingo@45: return layout; ingo@45: } ingo@45: ingo@45: raimund@235: /** raimund@235: * This method creates a table that contains the distance values. raimund@235: */ raimund@235: protected void createDistanceTable() { raimund@235: distanceTable.setWidth100(); raimund@235: distanceTable.setShowRecordComponents(true); raimund@235: distanceTable.setShowRecordComponentsByCell(true); raimund@235: distanceTable.setHeight100(); raimund@263: distanceTable.setEmptyMessage(MESSAGES.empty_table()); raimund@235: raimund@235: ListGridField addDistance = new ListGridField ("", ""); raimund@235: addDistance.setType (ListGridFieldType.ICON); sascha@568: addDistance.setWidth (20); raimund@235: addDistance.addRecordClickHandler (new RecordClickHandler () { raimund@235: public void onRecordClick (RecordClickEvent e) { raimund@235: if (!isLocationMode ()) { raimund@235: Record r = e.getRecord(); raimund@235: double min = r.getAttributeAsDouble("from"); raimund@235: double max = r.getAttributeAsDouble("to"); raimund@235: setDistanceValues(min, max); raimund@235: } raimund@235: else { raimund@235: double[] selected; raimund@261: Record r = e.getRecord(); raimund@261: double min = r.getAttributeAsDouble("from"); raimund@261: double max = r.getAttributeAsDouble("to"); raimund@235: if (getLocationValues() != null) { raimund@235: double[] val = getLocationValues(); raimund@235: selected = new double[val.length + 2]; raimund@235: for(int i = 0; i < val.length; i++){ raimund@235: selected[i] = val[i]; raimund@235: } raimund@235: selected[val.length] = min; raimund@235: selected[val.length + 1] = max; raimund@235: } raimund@235: else { raimund@235: selected = new double[2]; raimund@235: selected[0] = min; raimund@235: selected[1] = max; raimund@235: } raimund@235: setLocationValues(selected); raimund@235: } raimund@235: } raimund@235: }); raimund@235: addDistance.setCellIcon (IMAGES.markerGreen ().getURL ()); raimund@235: raimund@235: ListGridField ddescr = new ListGridField("description", raimund@235: MESSAGES.description()); raimund@235: ddescr.setType(ListGridFieldType.TEXT); raimund@235: ddescr.setWidth("*"); raimund@235: ListGridField from = new ListGridField("from", MESSAGES.from()); raimund@235: from.setType(ListGridFieldType.TEXT); sascha@568: from.setWidth(40); raimund@235: ListGridField to = new ListGridField("to", MESSAGES.to()); raimund@235: to.setType(ListGridFieldType.TEXT); sascha@568: to.setWidth(40); raimund@235: ListGridField dside = new ListGridField("riverside", raimund@235: MESSAGES.riverside()); raimund@235: dside.setType(ListGridFieldType.TEXT); sascha@568: dside.setWidth(40); raimund@235: sascha@568: ListGridField bottom = sascha@568: new ListGridField("bottom", MESSAGES.bottom_edge()); sascha@568: bottom.setType(ListGridFieldType.TEXT); sascha@568: bottom.setWidth(30); sascha@568: sascha@568: ListGridField top = sascha@568: new ListGridField("top", MESSAGES.top_edge()); sascha@568: top.setType(ListGridFieldType.TEXT); sascha@568: top.setWidth(30); sascha@568: sascha@568: distanceTable.setFields( sascha@568: addDistance, ddescr, from, to, dside, bottom, top); raimund@235: } raimund@235: raimund@235: raimund@235: /** raimund@235: * This method creates a table that contains the location values. raimund@235: */ raimund@235: protected void createLocationTable() { raimund@235: locationsTable.setWidth100(); raimund@235: locationsTable.setShowRecordComponents(true); raimund@235: locationsTable.setShowRecordComponentsByCell(true); raimund@235: locationsTable.setHeight100(); raimund@263: locationsTable.setEmptyMessage(MESSAGES.empty_table()); raimund@235: raimund@235: ListGridField addLocation = new ListGridField ("", ""); raimund@235: addLocation.setType (ListGridFieldType.ICON); sascha@568: addLocation.setWidth (20); sascha@568: raimund@235: addLocation.addRecordClickHandler (new RecordClickHandler () { raimund@235: public void onRecordClick (RecordClickEvent e) { raimund@235: ListGridRecord[] records = locationsTable.getSelection(); raimund@235: double[] selected; raimund@235: if (getLocationValues() != null) { raimund@235: double[] val = getLocationValues(); raimund@235: selected = new double[val.length + 1]; raimund@235: for(int i = 0; i < val.length; i++){ raimund@235: selected[i] = val[i]; raimund@235: } raimund@235: selected[val.length] = raimund@235: records[0].getAttributeAsDouble("from"); raimund@235: } raimund@235: else { raimund@235: selected = new double[1]; raimund@235: selected[0] = records[0].getAttributeAsDouble("from"); raimund@235: } raimund@235: setLocationValues(selected); raimund@235: } raimund@235: }); raimund@235: addLocation.setCellIcon (IMAGES.markerGreen ().getURL ()); raimund@235: raimund@235: ListGridField ldescr = new ListGridField("description", raimund@235: MESSAGES.description()); raimund@235: ldescr.setType(ListGridFieldType.TEXT); raimund@235: ldescr.setWidth("*"); raimund@235: ListGridField lside = new ListGridField("riverside", raimund@235: MESSAGES.riverside()); raimund@235: lside.setType(ListGridFieldType.TEXT); sascha@568: lside.setWidth(40); ingo@570: ListGridField loc = new ListGridField("from", MESSAGES.locations()); raimund@235: loc.setType(ListGridFieldType.TEXT); sascha@568: loc.setWidth(40); sascha@568: sascha@568: ListGridField bottom = sascha@568: new ListGridField("bottom", MESSAGES.bottom_edge()); sascha@568: bottom.setType(ListGridFieldType.TEXT); sascha@568: bottom.setWidth(30); sascha@568: sascha@568: ListGridField top = sascha@568: new ListGridField("top", MESSAGES.top_edge()); sascha@568: top.setType(ListGridFieldType.TEXT); sascha@568: top.setWidth(30); sascha@568: sascha@568: locationsTable.setFields(addLocation, ldescr, loc, lside, bottom, top); raimund@235: } raimund@235: raimund@235: raimund@235: /** raimund@235: * This method creates a table that contains the location values. raimund@235: */ raimund@235: protected void createLocationTableDistance (){ raimund@237: locationDistanceTable = null; raimund@237: locationDistanceTable = new ListGrid (); raimund@235: locationDistanceTable.setWidth100(); raimund@235: locationDistanceTable.setShowRecordComponents(true); raimund@235: locationDistanceTable.setShowRecordComponentsByCell(true); raimund@235: locationDistanceTable.setHeight100(); raimund@263: locationDistanceTable.setEmptyMessage(MESSAGES.empty_table()); raimund@235: raimund@235: ListGridField addfrom = new ListGridField ("", ""); raimund@235: addfrom.setType (ListGridFieldType.ICON); sascha@568: addfrom.setWidth (20); raimund@235: addfrom.setCellIcon (IMAGES.markerGreen ().getURL ()); raimund@235: raimund@235: ListGridField addto2 = new ListGridField ("", ""); raimund@235: addto2.setType (ListGridFieldType.ICON); sascha@568: addto2.setWidth (20); raimund@235: addto2.setCellIcon (IMAGES.markerRed ().getURL ()); raimund@235: raimund@235: raimund@235: ListGridField ldescr = new ListGridField("description", raimund@235: MESSAGES.description()); raimund@235: ldescr.setType(ListGridFieldType.TEXT); raimund@235: ldescr.setWidth("*"); raimund@235: ListGridField lside = new ListGridField("riverside", raimund@235: MESSAGES.riverside()); raimund@235: lside.setType(ListGridFieldType.TEXT); sascha@568: lside.setWidth(40); ingo@570: ListGridField loc = new ListGridField("from", MESSAGES.locations()); raimund@235: loc.setType(ListGridFieldType.TEXT); sascha@568: loc.setWidth(40); raimund@235: locationDistanceTable.addCellClickHandler (new CellClickHandler () { raimund@235: public void onCellClick (CellClickEvent e) { raimund@235: if (e.getColNum() == 0) { raimund@235: Record r = e.getRecord (); raimund@235: double fromvalue = r.getAttributeAsDouble ("from"); raimund@235: double tovalue = getTo (); raimund@235: setDistanceValues (fromvalue, tovalue); raimund@235: } raimund@235: else if (e.getColNum() == 1) { raimund@235: Record r = e.getRecord (); raimund@235: double fromvalue = getFrom (); raimund@235: double tovalue = r.getAttributeAsDouble ("to"); raimund@235: setDistanceValues (fromvalue, tovalue); raimund@235: } raimund@235: } raimund@235: }); sascha@568: ListGridField bottom = sascha@568: new ListGridField("bottom", MESSAGES.bottom_edge()); sascha@568: bottom.setType(ListGridFieldType.TEXT); sascha@568: bottom.setWidth(30); sascha@568: sascha@568: ListGridField top = sascha@568: new ListGridField("top", MESSAGES.top_edge()); sascha@568: top.setType(ListGridFieldType.TEXT); sascha@568: top.setWidth(30); sascha@568: sascha@568: locationDistanceTable.setFields( sascha@568: addfrom, addto2, ldescr, loc, lside, bottom, top); raimund@235: } raimund@235: raimund@235: ingo@53: public Canvas createOld(DataList dataList) { ingo@53: List items = dataList.getAll(); ingo@53: ingo@516: Data dMode = getData(items, "ld_mode"); ingo@516: DataItem[] dItems = dMode.getItems(); ingo@516: ingo@516: boolean rangeMode = true; ingo@516: if (dItems != null && dItems[0] != null) { ingo@516: rangeMode = FIELD_VALUE_DISTANCE.equals(dItems[0].getStringValue()); ingo@516: } ingo@516: ingo@516: HLayout layout = new HLayout(); ingo@516: layout.setWidth("400px"); ingo@516: ingo@516: Label label = new Label(dataList.getLabel()); ingo@516: label.setWidth("200px"); ingo@516: ingo@516: Canvas back = getBackButton(dataList.getState()); ingo@516: ingo@516: layout.addMember(label); ingo@516: ingo@516: if (rangeMode) { ingo@516: layout.addMember(getOldRangeSelection(dataList)); ingo@516: } ingo@516: else { ingo@516: layout.addMember(getOldLocationSelection(dataList)); ingo@516: } ingo@516: ingo@516: layout.addMember(back); ingo@516: ingo@516: return layout; ingo@516: } ingo@516: ingo@516: ingo@516: /** ingo@516: * Creates a label for the selected range. ingo@516: * ingo@516: * @param dataList The DataList containing all values for this state. ingo@516: * ingo@516: * @return A label displaying the selected values. ingo@516: */ ingo@516: protected Label getOldRangeSelection(DataList dataList) { ingo@516: List items = dataList.getAll(); ingo@516: ingo@53: Data dFrom = getData(items, "ld_from"); ingo@53: Data dTo = getData(items, "ld_to"); ingo@53: Data dStep = getData(items, "ld_step"); ingo@53: ingo@53: DataItem[] from = dFrom.getItems(); ingo@53: DataItem[] to = dTo.getItems(); ingo@53: DataItem[] step = dStep.getItems(); ingo@53: ingo@53: StringBuilder sb = new StringBuilder(); ingo@53: sb.append(from[0].getLabel()); ingo@53: sb.append(" " + MESSAGES.unitFrom() + " "); ingo@53: sb.append(to[0].getLabel()); ingo@53: sb.append(" " + MESSAGES.unitTo() + " "); ingo@53: sb.append(step[0].getLabel()); ingo@53: sb.append(" " + MESSAGES.unitWidth()); ingo@53: raimund@91: Label selected = new Label(sb.toString()); raimund@91: selected.setWidth("130px"); raimund@91: ingo@516: return selected; ingo@516: } ingo@53: ingo@516: ingo@516: /** ingo@516: * Creates a label for the selected locations. ingo@516: * ingo@516: * @param dataList The DataList containing all values for this state. ingo@516: * ingo@516: * @return A label displaying the selected values. ingo@516: */ ingo@516: protected Label getOldLocationSelection(DataList dataList) { ingo@516: List items = dataList.getAll(); ingo@516: ingo@516: Data dLocations = getData(items, "ld_locations"); ingo@516: DataItem[] lItems = dLocations.getItems(); ingo@516: ingo@516: String value = lItems[0].getStringValue(); ingo@516: ingo@516: Label selected = new Label(value); ingo@516: selected.setWidth(130); ingo@516: ingo@516: return selected; ingo@53: } ingo@53: ingo@53: ingo@53: /** ingo@55: * This method reads the default values defined in the DataItems of the Data ingo@55: * objects in list. ingo@55: * ingo@55: * @param list The DataList container that stores the Data objects. ingo@55: */ ingo@55: protected void initDefaults(DataList list) { ingo@516: Data m = getData(list.getAll(), "ld_mode"); ingo@516: Data l = getData(list.getAll(), "ld_locations"); ingo@55: Data f = getData(list.getAll(), "ld_from"); ingo@55: Data t = getData(list.getAll(), "ld_to"); ingo@55: Data s = getData(list.getAll(), "ld_step"); ingo@55: ingo@55: DataItem[] fItems = f.getItems(); ingo@55: DataItem[] tItems = t.getItems(); ingo@55: DataItem[] sItems = s.getItems(); ingo@55: ingo@55: min = Double.valueOf(fItems[0].getStringValue()); ingo@55: max = Double.valueOf(tItems[0].getStringValue()); ingo@55: step = Double.valueOf(sItems[0].getStringValue()); ingo@55: ingo@516: DataItem mDef = m.getDefault(); ingo@516: DataItem lDef = l.getDefault(); ingo@516: DataItem fDef = f.getDefault(); ingo@516: DataItem tDef = t.getDefault(); ingo@516: DataItem sDef = s.getDefault(); ingo@55: ingo@517: String mDefValue = mDef != null ? mDef.getStringValue() : null; ingo@517: String theMode = mDefValue != null && mDefValue.length() > 0 ingo@516: ? mDef.getStringValue() ingo@588: : FIELD_VALUE_DISTANCE; ingo@516: ingo@516: mode.setValue(FIELD_MODE, theMode); ingo@516: ingo@517: String fDefValue = fDef != null ? fDef.getStringValue() : null; ingo@517: setFrom(fDefValue != null && fDefValue.length() > 0 ingo@516: ? Double.valueOf(fDef.getStringValue()) ingo@516: : min); ingo@516: ingo@517: String tDefValue = tDef != null ? tDef.getStringValue() : null; ingo@517: setTo(tDefValue != null && tDefValue.length() > 0 ingo@516: ? Double.valueOf(tDef.getStringValue()) ingo@516: : max); ingo@516: ingo@517: String sDefValue = sDef != null ? sDef.getStringValue() : null; ingo@517: setStep(sDefValue != null && sDefValue.length() > 0 ingo@516: ? Double.valueOf(sDef.getStringValue()) ingo@516: : step); ingo@516: ingo@516: if (lDef != null) { ingo@517: String lDefValue = lDef != null ? lDef.getStringValue() : null; ingo@517: ingo@517: if (lDefValue != null && lDefValue.length() > 0) { ingo@517: setLocationValues(lDef.getStringValue()); ingo@517: } ingo@53: } ingo@53: ingo@516: if (theMode.equals(FIELD_VALUE_DISTANCE)) { ingo@516: enableDistancePanel(); ingo@588: inputTables.selectTab(1); ingo@516: } ingo@53: } ingo@53: ingo@53: ingo@51: protected Canvas createWidget(DataList data) { ingo@45: VLayout layout = new VLayout(); ingo@45: container = new HLayout(); ingo@45: Canvas checkboxPanel = createRadioButtonPanel(); ingo@45: ingo@45: // the initial view will display the location input mode raimund@235: locationPanel = new DoubleArrayPanel( ingo@45: MESSAGES.unitLocation(), ingo@45: getLocationValues(), ingo@45: this); ingo@45: container.addMember(locationPanel); ingo@45: ingo@41: layout.addMember(checkboxPanel); ingo@41: layout.addMember(container); ingo@41: raimund@235: container.setMembersMargin(30); raimund@235: raimund@235: inputTables = new TabSet(); ingo@570: Tab locations = new Tab(MESSAGES.locations()); raimund@235: Tab distances = new Tab(MESSAGES.distance()); raimund@235: raimund@272: inputTables.setWidth100(); raimund@272: inputTables.setHeight100(); raimund@235: raimund@235: locations.setPane(locationsTable); raimund@235: distances.setPane(distanceTable); raimund@235: raimund@235: inputTables.addTab(locations); raimund@235: inputTables.addTab(distances); raimund@235: raimund@272: helperContainer.addChild(inputTables); ingo@41: ingo@41: return layout; ingo@41: } ingo@41: ingo@41: ingo@562: @Override ingo@562: public List validate() { ingo@562: if (isLocationMode()) { ingo@562: return validateLocations(); ingo@562: } ingo@562: else { ingo@562: return validateRange(); ingo@562: } ingo@562: } ingo@562: ingo@562: ingo@562: protected List validateLocations() { ingo@562: List errors = new ArrayList(); ingo@562: NumberFormat nf = NumberFormat.getDecimalFormat(); ingo@562: ingo@562: try { ingo@562: saveLocationValues(locationPanel); ingo@562: } ingo@562: catch (Exception e) { ingo@562: errors.add(MESSAGES.wrongFormat()); ingo@562: } ingo@562: ingo@562: double[] values = getLocationValues(); ingo@562: double[] good = new double[values.length]; ingo@562: int idx = 0; ingo@562: ingo@562: for (double value: values) { ingo@562: if (value < min || value > max) { ingo@562: String tmp = MESSAGES.error_validate_range(); ingo@562: tmp = tmp.replace("$1", nf.format(value)); ingo@562: tmp = tmp.replace("$2", nf.format(min)); ingo@562: tmp = tmp.replace("$3", nf.format(max)); ingo@562: errors.add(tmp); ingo@562: } ingo@562: else { ingo@562: good[idx++] = value; ingo@562: } ingo@562: } ingo@562: ingo@562: double[] justGood = new double[idx]; ingo@562: for (int i = 0; i < justGood.length; i++) { ingo@562: justGood[i] = good[i]; ingo@562: } ingo@562: ingo@562: if (!errors.isEmpty()) { ingo@562: locationPanel.setValues(justGood); ingo@562: } ingo@562: ingo@562: return errors; ingo@562: } ingo@562: ingo@562: ingo@562: protected List validateRange() { ingo@562: List errors = new ArrayList(); ingo@562: NumberFormat nf = NumberFormat.getDecimalFormat(); ingo@562: ingo@562: try { ingo@562: saveDistanceValues(distancePanel); ingo@562: } ingo@562: catch (Exception e) { ingo@562: errors.add(MESSAGES.wrongFormat()); ingo@562: } ingo@562: ingo@562: double from = getFrom(); ingo@562: double to = getTo(); ingo@562: double step = getStep(); ingo@562: ingo@562: if (from < min || from > max) { ingo@562: String tmp = MESSAGES.error_validate_lower_range(); ingo@562: tmp = tmp.replace("$1", nf.format(from)); ingo@562: tmp = tmp.replace("$2", nf.format(min)); ingo@562: errors.add(tmp); ingo@562: from = min; ingo@562: } ingo@562: ingo@562: if (to < min || to > max) { ingo@562: String tmp = MESSAGES.error_validate_upper_range(); ingo@562: tmp = tmp.replace("$1", nf.format(to)); ingo@562: tmp = tmp.replace("$2", nf.format(max)); ingo@562: errors.add(tmp); ingo@562: to = max; ingo@562: } ingo@562: ingo@562: if (!errors.isEmpty()) { ingo@562: distancePanel.setValues(from, to, step); ingo@562: } ingo@562: ingo@562: return errors; ingo@562: } ingo@562: ingo@562: ingo@41: /** ingo@41: * This method returns the selected data. ingo@41: * ingo@41: * @return the selected/inserted data. ingo@41: */ ingo@41: public Data[] getData() { ingo@59: // XXX If we have entered a value and click right afterwards on the ingo@59: // 'next' button, the BlurEvent is not fired, and the values are not ingo@59: // saved. So, we gonna save those values explicitly. ingo@59: if (isLocationMode()) { ingo@59: Canvas member = container.getMember(0); ingo@59: if (member instanceof DoubleArrayPanel) { ingo@59: DoubleArrayPanel form = (DoubleArrayPanel) member; ingo@59: saveLocationValues(form); ingo@59: } ingo@59: } ingo@59: else { ingo@59: Canvas member = container.getMember(0); ingo@59: if (member instanceof DoubleRangePanel) { ingo@59: DoubleRangePanel form = (DoubleRangePanel) member; ingo@59: saveDistanceValues(form); ingo@59: } ingo@59: } ingo@59: ingo@520: Data dMode = getDataMode(); ingo@520: Data dLocations = getDataLocations(); ingo@520: Data dFrom = getDataFrom(); ingo@520: Data dTo = getDataTo(); ingo@520: Data dStep = getDataStep(); ingo@520: ingo@520: List data = new ArrayList(); ingo@520: ingo@520: if (dMode != null) { ingo@520: data.add(dMode); ingo@520: } ingo@520: ingo@520: if (dLocations != null) { ingo@520: data.add(dLocations); ingo@520: } ingo@520: ingo@520: if (dFrom != null) { ingo@520: data.add(dFrom); ingo@520: } ingo@520: ingo@520: if (dTo != null) { ingo@520: data.add(dTo); ingo@520: } ingo@520: ingo@520: if (dStep != null) { ingo@520: data.add(dStep); ingo@520: } ingo@520: ingo@520: return (Data[]) data.toArray(new Data[data.size()]); ingo@516: } ingo@516: ingo@516: ingo@516: /** ingo@516: * Returns the Data object for the 'mode' attribute. ingo@516: * ingo@516: * @return the Data object for the 'mode' attribute. ingo@516: */ ingo@516: protected Data getDataMode() { ingo@516: String value = mode.getValueAsString(FIELD_MODE); ingo@516: DataItem item = new DefaultDataItem("ld_mode", "ld_mode", value); ingo@516: return new DefaultData("ld_mode", null, null, new DataItem[] { item }); ingo@516: } ingo@516: ingo@516: ingo@516: protected Data getDataLocations() { ingo@516: double[] locations = getLocationValues(); ingo@516: boolean first = true; ingo@516: ingo@520: if (locations == null) { ingo@520: return null; ingo@520: } ingo@520: ingo@516: StringBuilder sb = new StringBuilder(); ingo@516: ingo@516: for (double l: locations) { ingo@516: if (!first) { ingo@516: sb.append(" "); ingo@516: } ingo@516: ingo@516: sb.append(l); ingo@516: ingo@516: first = false; ingo@516: } ingo@516: ingo@516: DataItem item = new DefaultDataItem( ingo@516: "ld_locations", ingo@516: "ld_locations", ingo@516: sb.toString()); ingo@516: ingo@516: return new DefaultData( ingo@516: "ld_locations", ingo@516: null, ingo@516: null, ingo@516: new DataItem[] { item }); ingo@46: } ingo@41: ingo@46: ingo@46: /** ingo@46: * Returns the Data object for the 'from' attribute. ingo@46: * ingo@46: * @return the Data object for the 'from' attribute. ingo@46: */ ingo@46: protected Data getDataFrom() { ingo@516: String value = Double.valueOf(getFrom()).toString(); ingo@46: DataItem item = new DefaultDataItem("ld_from", "ld_from", value); ingo@46: return new DefaultData( ingo@51: "ld_from", null, null, new DataItem[] { item }); ingo@46: } ingo@46: ingo@46: ingo@46: /** ingo@46: * Returns the Data object for the 'to' attribute. ingo@46: * ingo@46: * @return the Data object for the 'to' attribute. ingo@46: */ ingo@46: protected Data getDataTo() { ingo@516: String value = Double.valueOf(getTo()).toString(); ingo@46: DataItem item = new DefaultDataItem("ld_to", "ld_to", value); ingo@46: return new DefaultData( ingo@51: "ld_to", null, null, new DataItem[] { item }); ingo@46: } ingo@46: ingo@46: ingo@46: /** ingo@46: * Returns the Data object for the 'step' attribute. ingo@46: * ingo@46: * @return the Data object for the 'step' attribute. ingo@46: */ ingo@46: protected Data getDataStep() { ingo@516: String value = Double.valueOf(getStep()).toString(); ingo@46: DataItem item = new DefaultDataItem("ld_step","ld_step", value); ingo@46: return new DefaultData( ingo@51: "ld_step", null, null, new DataItem[] { item }); ingo@46: } ingo@46: ingo@46: ingo@46: /** ingo@46: * Determines the current input mode. ingo@46: * ingo@46: * @return true, if 'location' is the current input mode, otherwise false. ingo@46: */ ingo@46: public boolean isLocationMode() { ingo@516: String inputMode = mode.getValueAsString(FIELD_MODE); ingo@46: ingo@516: return inputMode.equals(FIELD_VALUE_LOCATION) ? true : false; ingo@516: } ingo@516: ingo@516: ingo@516: /** ingo@516: * Activates the location panel. ingo@516: */ ingo@516: protected void enableLocationPanel() { ingo@516: locationPanel = new DoubleArrayPanel( ingo@516: MESSAGES.unitLocation(), ingo@516: getLocationValues(), ingo@516: this); ingo@516: ingo@516: container.removeMembers(container.getMembers()); ingo@516: container.addMember(locationPanel); ingo@516: } ingo@516: ingo@516: ingo@516: /** ingo@516: * Activates the distance panel. ingo@516: */ ingo@516: protected void enableDistancePanel() { ingo@516: distancePanel = new DoubleRangePanel( ingo@516: MESSAGES.unitFrom(), MESSAGES.unitTo(), MESSAGES.unitWidth(), ingo@516: getFrom(), getTo(), getStep(), ingo@516: 250, ingo@516: this); ingo@516: ingo@516: container.removeMembers(container.getMembers()); ingo@516: container.addMember(distancePanel); ingo@41: } ingo@41: ingo@41: ingo@41: /** ingo@41: * This method switches the input mode between location and distance input. ingo@41: * ingo@41: * @param event The click event fired by a RadioButtonGroupItem. ingo@41: */ ingo@41: public void onChange(ChangeEvent event) { ingo@41: String value = (String) event.getValue(); ingo@41: ingo@41: if (value == null) { ingo@41: return; ingo@41: } ingo@516: if (value.equals(FIELD_VALUE_LOCATION)) { ingo@516: enableLocationPanel(); ingo@43: raimund@237: while (inputTables.getNumTabs() > 0) { raimund@237: inputTables.removeTab(0); raimund@237: } ingo@570: Tab t1 = new Tab (MESSAGES.locations()); raimund@235: createLocationTable(); raimund@235: t1.setPane(locationsTable); raimund@235: inputTables.addTab(t1); raimund@235: createDistanceTable(); raimund@235: Tab t2 = new Tab (MESSAGES.distance()); raimund@235: t2.setPane(distanceTable); raimund@235: inputTables.addTab(t2); raimund@237: updateDistanceInfo(tableData); raimund@237: raimund@272: helperContainer.addChild(inputTables); raimund@235: inputTables.selectTab(0); ingo@41: } ingo@43: else { ingo@516: enableDistancePanel(); ingo@43: raimund@237: while (inputTables.getNumTabs () > 0) { raimund@237: inputTables.removeTab(0); raimund@237: } ingo@570: Tab t1 = new Tab(MESSAGES.locations()); raimund@235: createLocationTableDistance (); raimund@235: t1.setPane(locationDistanceTable); raimund@235: inputTables.addTab(t1); raimund@235: createDistanceTable (); raimund@235: Tab t2 = new Tab(MESSAGES.distance ()); raimund@235: t2.setPane(distanceTable); raimund@235: inputTables.addTab(t2); raimund@244: if (tableData != null) { raimund@244: updateDistanceInfo(tableData); raimund@244: } raimund@237: raimund@272: helperContainer.addChild(inputTables); raimund@235: inputTables.selectTab(1); ingo@41: } ingo@41: } ingo@41: ingo@41: ingo@41: /** ingo@41: * This method is used to validate the inserted data in the form fields. ingo@41: * ingo@41: * @param event The BlurEvent that gives information about the FormItem that ingo@41: * has been modified and its value. ingo@41: */ ingo@41: public void onBlur(BlurEvent event) { ingo@41: FormItem item = event.getItem(); ingo@41: String field = item.getFieldName(); ingo@41: ingo@41: if (field == null) { ingo@41: return; ingo@41: } ingo@41: ingo@43: if (field.equals(DoubleArrayPanel.FIELD_NAME)) { ingo@43: DoubleArrayPanel p = (DoubleArrayPanel) event.getForm(); ingo@41: ingo@59: saveLocationValue(p, item); ingo@41: } ingo@43: else { ingo@43: DoubleRangePanel p = (DoubleRangePanel) event.getForm(); ingo@41: ingo@59: saveDistanceValue(p, item); ingo@59: } ingo@59: } ingo@59: ingo@59: raimund@235: ingo@59: /** ingo@59: * Validates and stores all values entered in the location mode. ingo@59: * ingo@59: * @param p The DoubleArrayPanel. ingo@59: */ ingo@59: protected void saveLocationValues(DoubleArrayPanel p) { ingo@59: FormItem[] formItems = p.getFields(); ingo@59: ingo@59: for (FormItem item: formItems) { ingo@59: if (item.getFieldName().equals(DoubleArrayPanel.FIELD_NAME)) { ingo@59: saveLocationValue(p, item); ingo@43: } ingo@41: } ingo@41: } ingo@41: ingo@41: ingo@41: /** ingo@59: * Validates and stores all values entered in the distance mode. ingo@59: * ingo@59: * @param p The DoubleRangePanel. ingo@59: */ ingo@59: protected void saveDistanceValues(DoubleRangePanel p) { ingo@59: FormItem[] formItems = p.getFields(); ingo@59: ingo@59: for (FormItem item: formItems) { ingo@59: saveDistanceValue(p, item); ingo@59: } ingo@59: } ingo@59: ingo@59: ingo@59: /** ingo@59: * Validates and stores a value entered in the location mode. ingo@59: * ingo@59: * @param p The DoubleArrayPanel. ingo@59: * @param item The item that needs to be validated. ingo@59: */ ingo@59: protected void saveLocationValue(DoubleArrayPanel p, FormItem item) { ingo@59: if (p.validateForm(item)) { ingo@59: setLocationValues(p.getInputValues(item)); ingo@59: } ingo@59: } ingo@59: ingo@59: ingo@59: /** ingo@59: * Validates and stores value entered in the distance mode. ingo@59: * ingo@59: * @param p The DoubleRangePanel. ingo@59: * @param item The item that needs to be validated. ingo@59: */ ingo@59: protected void saveDistanceValue(DoubleRangePanel p, FormItem item) { ingo@59: if (p.validateForm(item)) { ingo@59: setFrom(p.getFrom()); ingo@59: setTo(p.getTo()); ingo@59: setStep(p.getStep()); ingo@59: } ingo@59: } ingo@59: ingo@59: ingo@59: /** ingo@41: * This method creates the panel that contains the checkboxes to switch ingo@41: * between the input mode 'location' and 'distance'. ingo@41: * ingo@41: * @return the checkbox panel. ingo@41: */ ingo@41: protected Canvas createRadioButtonPanel() { ingo@43: mode = new DynamicForm(); ingo@41: ingo@516: RadioGroupItem radio = new RadioGroupItem(FIELD_MODE); ingo@41: radio.setShowTitle(false); ingo@41: radio.setVertical(false); ingo@41: ingo@43: LinkedHashMap values = new LinkedHashMap(); ingo@516: values.put(FIELD_VALUE_LOCATION, MESSAGES.location()); ingo@516: values.put(FIELD_VALUE_DISTANCE, MESSAGES.distance()); ingo@43: ingo@45: LinkedHashMap initial = new LinkedHashMap(); ingo@588: initial.put(FIELD_MODE, FIELD_VALUE_DISTANCE); ingo@45: ingo@43: radio.setValueMap(values); ingo@41: radio.addChangeHandler(this); ingo@41: ingo@43: mode.setFields(radio); ingo@45: mode.setValues(initial); ingo@41: ingo@43: return mode; ingo@41: } ingo@41: ingo@41: raimund@235: protected void createDistanceInputPanel() { raimund@235: Config config = Config.getInstance(); raimund@235: String url = config.getServerUrl(); raimund@235: String locale = config.getLocale (); raimund@235: String river = ""; raimund@235: raimund@235: ArtifactDescription adescr = artifact.getArtifactDescription(); raimund@235: DataList[] data = adescr.getOldData(); raimund@235: raimund@235: if (data != null && data.length > 0) { raimund@235: for (int i = 0; i < data.length; i++) { raimund@235: DataList dl = data[i]; raimund@235: if (dl.getState().equals("state.winfo.river")) { raimund@235: for (int j = 0; j < dl.size(); j++) { raimund@235: Data d = dl.get(j); raimund@235: DataItem[] di = d.getItems(); raimund@235: if (di != null && di.length == 1) { raimund@235: river = d.getItems()[0].getStringValue(); raimund@235: } raimund@235: } raimund@235: } raimund@235: } raimund@235: } raimund@235: raimund@235: distanceInfoService.getDistanceInfo(url, locale, river, raimund@235: new AsyncCallback() { raimund@235: public void onFailure(Throwable caught) { raimund@235: GWT.log("Could not recieve distance informations."); raimund@235: GWT.log(caught.getMessage()); raimund@235: } raimund@235: raimund@235: public void onSuccess(DistanceInfoObject[] di) { raimund@235: int num = di != null ? di.length :0; raimund@237: GWT.log("Recieved " + num + " distance informations."); raimund@235: raimund@235: if (num == 0) { raimund@235: return; raimund@235: } raimund@237: tableData = di; raimund@235: updateDistanceInfo(di); raimund@235: } raimund@235: } raimund@235: ); raimund@235: } raimund@235: raimund@235: raimund@235: protected void updateDistanceInfo(DistanceInfoObject[] di) { raimund@235: int i = 0; raimund@235: for (DistanceInfoObject dio: di) { raimund@235: if (dio.getTo() != null) { raimund@235: distanceTable.addData(new DistanceInfoRecord(dio)); raimund@235: } raimund@235: else { raimund@235: locationsTable.addData(new DistanceInfoRecord(dio)); raimund@235: locationDistanceTable.addData(new DistanceInfoRecord(dio)); raimund@235: } raimund@235: } raimund@235: return; raimund@235: } raimund@235: raimund@235: ingo@43: protected double getFrom() { ingo@43: return from; ingo@41: } ingo@41: ingo@41: ingo@43: protected void setFrom(double from) { ingo@43: this.from = from; ingo@43: } ingo@41: ingo@41: ingo@43: protected double getTo() { ingo@43: return to; ingo@43: } ingo@41: ingo@41: ingo@43: protected void setTo(double to) { ingo@43: this.to = to; ingo@43: } ingo@41: ingo@43: ingo@43: protected double getStep() { ingo@43: return step; ingo@43: } ingo@43: ingo@43: ingo@43: protected void setStep(double step) { ingo@43: this.step = step; ingo@43: } ingo@43: ingo@43: ingo@43: protected double[] getLocationValues() { ingo@43: return values; ingo@43: } ingo@43: ingo@43: ingo@43: protected void setLocationValues(double[] values) { ingo@43: this.values = values; raimund@235: locationPanel.setValues(values); raimund@235: } raimund@235: raimund@235: ingo@516: protected void setLocationValues(String values) { ingo@516: String[] vs = values.split(" "); ingo@516: ingo@516: if (vs == null) { ingo@516: return; ingo@516: } ingo@516: ingo@516: double[] ds = new double[vs.length]; ingo@516: int idx = 0; ingo@516: ingo@516: for (String s: vs) { ingo@516: try { ingo@516: ds[idx++] = Double.valueOf(s); ingo@516: } ingo@516: catch (NumberFormatException nfe) { ingo@516: // do nothing ingo@516: } ingo@516: } ingo@516: ingo@516: setLocationValues(ds); ingo@516: } ingo@516: ingo@516: raimund@235: protected void setDistanceValues (double from, double to) { raimund@235: setFrom(from); raimund@235: setTo(to); raimund@235: distancePanel.setValues(from, to, getStep()); ingo@41: } ingo@41: } ingo@41: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :