# HG changeset patch # User Felix Wolfsteller # Date 1328190132 0 # Node ID 4b773cfd11b5a9add529b81b702d223d584ff83e # Parent c9ae3c8504d5e1ea6e58e98b639d482379091346 Refactoring to ease code-reuse. flys-client/trunk@3882 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r c9ae3c8504d5 -r 4b773cfd11b5 flys-client/ChangeLog --- a/flys-client/ChangeLog Thu Feb 02 13:37:26 2012 +0000 +++ b/flys-client/ChangeLog Thu Feb 02 13:42:12 2012 +0000 @@ -1,3 +1,9 @@ +2012-02-02 Felix Wolfsteller + + * src/main/java/de/intevation/flys/client/client/ui/SingleLocationPanel.java, + src/main/java/de/intevation/flys/client/client/ui/LocationPicker.java: + Amateurly extracted Input-Helper-related code to ease code re-use. + 2012-02-02 Felix Wolfsteller * src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java: diff -r c9ae3c8504d5 -r 4b773cfd11b5 flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationPicker.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationPicker.java Thu Feb 02 13:42:12 2012 +0000 @@ -0,0 +1,253 @@ +package de.intevation.flys.client.client.ui; + +import java.util.LinkedHashMap; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.i18n.client.NumberFormat; + +import com.smartgwt.client.data.Criteria; +import com.smartgwt.client.data.Criterion; +import com.smartgwt.client.data.AdvancedCriteria; +import com.smartgwt.client.types.OperatorId; + +import com.smartgwt.client.widgets.form.DynamicForm; +import com.smartgwt.client.widgets.form.fields.events.ChangedHandler; +import com.smartgwt.client.widgets.form.fields.events.ChangedEvent; + +import com.smartgwt.client.widgets.form.fields.SelectItem; +import com.smartgwt.client.widgets.form.fields.StaticTextItem; +import com.smartgwt.client.widgets.layout.HLayout; +import com.smartgwt.client.widgets.grid.ListGrid; +import com.smartgwt.client.widgets.grid.ListGridField; +import com.smartgwt.client.widgets.grid.ListGridRecord; +import com.smartgwt.client.widgets.grid.CellFormatter; +import com.smartgwt.client.widgets.grid.events.RecordClickHandler; + +import com.smartgwt.client.types.ListGridFieldType; +import com.smartgwt.client.types.Alignment; + +import de.intevation.flys.client.client.FLYSConstants; +import de.intevation.flys.client.client.event.FilterHandler; +import de.intevation.flys.client.client.event.StringFilterEvent; +import de.intevation.flys.client.client.event.RangeFilterEvent; + +/** + * Bundle widgets and handler for a lacation input helper. + * + * Note that the construction is weird and driven by issues that arose due to + * reasons not understood. + */ +public class LocationPicker +implements FilterHandler +{ + /** The message class that provides i18n strings.*/ + protected FLYSConstants MSG = GWT.create(FLYSConstants.class); + + /** The locations table. */ + protected ListGrid locationTable; + + protected HLayout filterLayout; + + DynamicForm resultCountForm; + + RecordClickHandler handler; + + /** Text to show number of matched items when filtered. */ + protected StaticTextItem filterResultCount; + + + public LocationPicker(RecordClickHandler handler) { + locationTable = new ListGrid(); + locationTable.setShowHeaderContextMenu(false); + this.handler = handler; + } + + public void prepareFilter() { + + filterResultCount = new StaticTextItem(MSG.resultCount()); + filterResultCount.setTitleAlign(Alignment.LEFT); + filterResultCount.setTitleStyle("color: #000"); + + final TableFilter filter = new TableFilter(); + filter.setHeight("30px"); + filter.addFilterHandler(this); + + final RangeTableFilter filterRange = new RangeTableFilter(); + filterRange.setHeight("30px"); + filterRange.addFilterHandler(this); + filterRange.setVisible(false); + + SelectItem filterCriteria = new SelectItem(); + filterCriteria.setShowTitle(false); + filterCriteria.setWidth(100); + filterCriteria.addChangedHandler(new ChangedHandler() { + public void onChanged(ChangedEvent e) { + if(e.getValue().toString().equals("range")) { + filterRange.setVisible(true); + filter.setVisible(false); + filter.clear(); + filterResultCount.setValue(""); + } + else { + filterRange.setVisible(false); + filterRange.clear(); + filter.setVisible(true); + filterResultCount.setValue(""); + } + } + }); + + LinkedHashMap filterMap = + new LinkedHashMap(); + filterMap.put("description", MSG.description()); + filterMap.put("range", MSG.range()); + filterCriteria.setValueMap(filterMap); + filterCriteria.setValue("description"); + + DynamicForm form = new DynamicForm(); + form.setFields(filterCriteria); + + resultCountForm = new DynamicForm(); + resultCountForm.setFields(filterResultCount); + + filterLayout = new HLayout(); + filterLayout.addMember(form); + filterLayout.addMember(filter); + filterLayout.addMember(filterRange); + } + + + /** Access the main widget, a table in which locations can be chosen. */ + public ListGrid getLocationTable() { + return locationTable; + } + + + /** Access the 'form' that shows the filter result count. */ + public DynamicForm getResultCountForm() { + return resultCountForm; + } + + + /** Access the layout containing filter stuff. */ + public HLayout getFilterLayout() { + return filterLayout; + } + + + /** + * This method creates a table that contains the location values. + */ + protected void createLocationTable(/*RecordClickHandler handler*/) { + GWT.log("Create Location Table in LocationPicker"); + + String baseUrl = GWT.getHostPageBaseURL(); + + locationTable.setWidth100(); + locationTable.setShowRecordComponents(true); + locationTable.setShowRecordComponentsByCell(true); + locationTable.setHeight100(); + locationTable.setEmptyMessage(MSG.empty_filter()); + locationTable.setCanReorderFields(false); + + ListGridField addLocation = new ListGridField ("", ""); + addLocation.setType (ListGridFieldType.ICON); + addLocation.setWidth (20); + addLocation.addRecordClickHandler (handler); + addLocation.setCellIcon (baseUrl + MSG.markerGreen()); + GWT.log ("i18n: ..... " + MSG.description()); + + ListGridField ldescr = new ListGridField("description", + MSG.description()); + ldescr.setType(ListGridFieldType.TEXT); + ldescr.setWidth("*"); + ListGridField lside = new ListGridField("riverside", + MSG.riverside()); + lside.setType(ListGridFieldType.TEXT); + lside.setWidth("10%"); + + ListGridField loc = new ListGridField("from", MSG.location()); + loc.setCellFormatter(new CellFormatter() { + public String format( + Object value, + ListGridRecord record, + int rowNum, int colNum) { + if (value == null) return null; + try { + NumberFormat nf; + double v = Double.parseDouble((String)value); + nf = NumberFormat.getFormat("###0.00##"); + return nf.format(v); + } + catch (Exception e) { + return value.toString(); + } + } + } + ); + loc.setType(ListGridFieldType.FLOAT); + + loc.setWidth("10%"); + + ListGridField bottom = + new ListGridField("bottom", MSG.bottom_edge()); + bottom.setType(ListGridFieldType.TEXT); + bottom.setWidth("10%"); + + ListGridField top = + new ListGridField("top", MSG.top_edge()); + top.setType(ListGridFieldType.TEXT); + top.setWidth("10%"); + + + locationTable.setFields( + addLocation, ldescr, loc, lside, bottom, top); + } + + + @Override + public void onFilterCriteriaChanged(StringFilterEvent event) { + String search = event.getFilter(); + + if (search != null && search.length() > 0) { + Criteria c = new Criteria("description", search); + locationTable.filterData(c); + filterResultCount.setValue(locationTable.getRecords().length); + } + else { + // TODO Remove filter + } + } + + + @Override + public void onFilterCriteriaChanged(RangeFilterEvent event) { + Float from = event.getFrom() - 0.001f; + Float to = event.getTo() + 0.001f; + + Criterion combinedFilter = null; + if (from.equals(Float.NaN) && to.equals(Float.NaN)) { + locationTable.clearCriteria(); + filterResultCount.setValue(""); + return; + } + else if (from.equals(Float.NaN)) { + combinedFilter = + new Criterion("from", OperatorId.LESS_OR_EQUAL, to); + } + else if (to.equals(Float.NaN)) { + combinedFilter = + new Criterion("from", OperatorId.GREATER_OR_EQUAL, from); + } + else { + combinedFilter = + new AdvancedCriteria(OperatorId.AND, new Criterion[] { + new Criterion("from", OperatorId.GREATER_OR_EQUAL, from), + new Criterion("from", OperatorId.LESS_OR_EQUAL, to) + }); + } + locationTable.filterData(combinedFilter); + filterResultCount.setValue(locationTable.getRecords().length); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c9ae3c8504d5 -r 4b773cfd11b5 flys-client/src/main/java/de/intevation/flys/client/client/ui/SingleLocationPanel.java --- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/SingleLocationPanel.java Thu Feb 02 13:37:26 2012 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/SingleLocationPanel.java Thu Feb 02 13:42:12 2012 +0000 @@ -2,7 +2,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.LinkedHashMap; import com.google.gwt.core.client.GWT; import com.google.gwt.i18n.client.NumberFormat; @@ -10,60 +9,41 @@ import com.smartgwt.client.util.SC; import com.smartgwt.client.widgets.Canvas; import com.smartgwt.client.widgets.Label; -import com.smartgwt.client.widgets.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.events.BlurHandler; import com.smartgwt.client.widgets.form.fields.events.BlurEvent; import com.smartgwt.client.widgets.form.fields.FormItem; -import com.smartgwt.client.widgets.form.fields.SelectItem; -import com.smartgwt.client.widgets.form.fields.StaticTextItem; -import com.smartgwt.client.widgets.form.fields.events.ChangedHandler; -import com.smartgwt.client.widgets.form.fields.events.ChangedEvent; import com.smartgwt.client.widgets.layout.HLayout; import com.smartgwt.client.widgets.layout.VLayout; -import com.smartgwt.client.widgets.grid.ListGrid; -import com.smartgwt.client.widgets.grid.ListGridField; -import com.smartgwt.client.widgets.grid.ListGridRecord; import com.smartgwt.client.widgets.grid.events.RecordClickHandler; import com.smartgwt.client.widgets.grid.events.RecordClickEvent; -import com.smartgwt.client.widgets.grid.CellFormatter; - -import com.smartgwt.client.data.Criteria; -import com.smartgwt.client.data.Criterion; -import com.smartgwt.client.data.AdvancedCriteria; -import com.smartgwt.client.types.OperatorId; -import com.smartgwt.client.types.Alignment; import com.smartgwt.client.data.Record; -import com.smartgwt.client.types.ListGridFieldType; +import de.intevation.flys.client.shared.model.ArtifactDescription; import de.intevation.flys.client.shared.model.Data; import de.intevation.flys.client.shared.model.DataItem; import de.intevation.flys.client.shared.model.DataList; import de.intevation.flys.client.shared.model.DefaultData; import de.intevation.flys.client.shared.model.DefaultDataItem; import de.intevation.flys.client.shared.model.DistanceInfoObject; -import de.intevation.flys.client.shared.model.ArtifactDescription; +import de.intevation.flys.client.shared.model.RangeData; import de.intevation.flys.client.client.services.DistanceInfoService; import de.intevation.flys.client.client.services.DistanceInfoServiceAsync; -import de.intevation.flys.client.client.FLYSConstants; import de.intevation.flys.client.client.Config; import de.intevation.flys.client.client.ui.range.DistanceInfoDataSource; -import de.intevation.flys.client.client.event.FilterHandler; -import de.intevation.flys.client.client.event.StringFilterEvent; -import de.intevation.flys.client.client.event.RangeFilterEvent; /** - * This UIProvider creates a widget to enter locations. + * This UIProvider creates a widget to enter a single location (km). * * @author Raimund Renkert */ public class SingleLocationPanel extends AbstractUIProvider -implements FilterHandler +implements RecordClickHandler { /** The DistanceInfoService used to retrieve locations about rivers. */ protected DistanceInfoServiceAsync distanceInfoService = @@ -84,25 +64,21 @@ /** The input panel for locations. */ protected DoubleArrayPanel locationPanel; - /** The locations table. */ - protected ListGrid locationTable; - /** The table data. */ protected DistanceInfoObject[] tableData; - /** Text to show number of matched items when filtered. */ - protected StaticTextItem filterResultCount; - /** Name of the data item that keeps locations. */ protected String DATA_ITEM_NAME = "ld_locations"; + /** The input helper (usually right side, table to click on, values are + * then entered in the texfield. */ + protected LocationPicker picker; /** * Creates a new LocationDistancePanel instance. */ public SingleLocationPanel() { - locationTable = new ListGrid(); - locationTable.setShowHeaderContextMenu(false); + picker = new LocationPicker(this); } @@ -121,12 +97,13 @@ layout.setMembersMargin(10); Label label = new Label(MSG.location ()); + //picker = new LocationPicker(this); Canvas widget = createWidget(data); Canvas submit = getNextButton(); initDefaults(data); - createLocationTable(); + picker.createLocationTable(); widget.setHeight(50); label.setHeight(25); @@ -139,86 +116,6 @@ } - /** - * This method creates a table that contains the location values. - */ - protected void createLocationTable() { - GWT.log("---------- I WAS HERE ---------"); - - String baseUrl = GWT.getHostPageBaseURL(); - - locationTable.setWidth100(); - locationTable.setShowRecordComponents(true); - locationTable.setShowRecordComponentsByCell(true); - locationTable.setHeight100(); - locationTable.setEmptyMessage(MSG.empty_filter()); - locationTable.setCanReorderFields(false); - - ListGridField addLocation = new ListGridField ("", ""); - addLocation.setType (ListGridFieldType.ICON); - addLocation.setWidth (20); - addLocation.addRecordClickHandler (new RecordClickHandler () { - public void onRecordClick (RecordClickEvent e) { - Record record = e.getRecord(); - double[] selected = new double[1]; - try { - selected[0] = - Double.parseDouble(record.getAttribute("from")); - } - catch(NumberFormatException nfe) { - // Is there anything else to do here? - } - setLocationValues(selected); - } - }); - addLocation.setCellIcon (baseUrl + MSG.markerGreen()); - - ListGridField ldescr = new ListGridField("description", - MSG.description()); - ldescr.setType(ListGridFieldType.TEXT); - ldescr.setWidth("*"); - ListGridField lside = new ListGridField("riverside", - MSG.riverside()); - lside.setType(ListGridFieldType.TEXT); - lside.setWidth("10%"); - - ListGridField loc = new ListGridField("from", MSG.location()); - loc.setCellFormatter(new CellFormatter() { - public String format( - Object value, - ListGridRecord record, - int rowNum, int colNum) { - if (value == null) return null; - try { - NumberFormat nf; - double v = Double.parseDouble((String)value); - nf = NumberFormat.getFormat("###0.00##"); - return nf.format(v); - } - catch (Exception e) { - return value.toString(); - } - } - } - ); - loc.setType(ListGridFieldType.FLOAT); - - loc.setWidth("10%"); - - ListGridField bottom = - new ListGridField("bottom", MSG.bottom_edge()); - bottom.setType(ListGridFieldType.TEXT); - bottom.setWidth("10%"); - - ListGridField top = - new ListGridField("top", MSG.top_edge()); - top.setType(ListGridFieldType.TEXT); - top.setWidth("10%"); - - locationTable.setFields( - addLocation, ldescr, loc, lside, bottom, top); - } - /** * This method creates a Canvas element showing the old Data objects in the @@ -257,22 +154,40 @@ protected void initDefaults(DataList list) { Data data = list.get(0); - if (data == null) { - return; + /* + // Compatibility with MinMax- DataItems: + RangeData rangeData = null; + + for (int i = 0, n = list.size(); i < n; i++) { + Data tmp = list.get(i); + + if (tmp instanceof RangeData) { + rangeData = (RangeData) tmp; + } } - DataItem[] items = data.getItems(); - DataItem iMin = getDataItem(items, "min"); - DataItem iMax = getDataItem(items, "max"); + if (rangeData != null) { + min = Double.parseDouble(rangeData.getDefaultLower().toString()); + max = Double.parseDouble(rangeData.getDefaultUpper().toString()); + // catch ..? + } + */ - try { - min = Double.parseDouble(iMin.getStringValue()); - max = Double.parseDouble(iMax.getStringValue()); - } - catch (NumberFormatException nfe) { - SC.warn(MSG.error_read_minmax_values()); - min = -Double.MAX_VALUE; - max = Double.MAX_VALUE; + if (false) {} + else { + DataItem[] items = data.getItems(); + DataItem iMin = getDataItem(items, "min"); + DataItem iMax = getDataItem(items, "max"); + + try { + min = Double.parseDouble(iMin.getStringValue()); + max = Double.parseDouble(iMax.getStringValue()); + } + catch (NumberFormatException nfe) { + SC.warn(MSG.error_read_minmax_values()); + min = -Double.MAX_VALUE; + max = Double.MAX_VALUE; + } } DataItem def = data.getDefault(); @@ -319,7 +234,7 @@ getLocationValues(), new BlurHandler(){public void onBlur(BlurEvent be) {}}); - locationTable.setAutoFetchData(true); + picker.getLocationTable().setAutoFetchData(true); container.addMember(locationPanel); @@ -327,112 +242,17 @@ container.setMembersMargin(30); - filterResultCount = new StaticTextItem(MSG.resultCount()); - filterResultCount.setTitleAlign(Alignment.LEFT); - filterResultCount.setTitleStyle("color: #000"); - - final TableFilter filter = new TableFilter(); - filter.setHeight("30px"); - filter.addFilterHandler(this); - - final RangeTableFilter filterRange = new RangeTableFilter(); - filterRange.setHeight("30px"); - filterRange.addFilterHandler(this); - filterRange.setVisible(false); + picker.prepareFilter(); - SelectItem filterCriteria = new SelectItem(); - filterCriteria.setShowTitle(false); - filterCriteria.setWidth(100); - filterCriteria.addChangedHandler(new ChangedHandler() { - public void onChanged(ChangedEvent e) { - if(e.getValue().toString().equals("range")) { - filterRange.setVisible(true); - filter.setVisible(false); - filter.clear(); - filterResultCount.setValue(""); - } - else { - filterRange.setVisible(false); - filterRange.clear(); - filter.setVisible(true); - filterResultCount.setValue(""); - } - } - }); - - LinkedHashMap filterMap = - new LinkedHashMap(); - filterMap.put("description", MSG.description()); - filterMap.put("range", MSG.range()); - filterCriteria.setValueMap(filterMap); - filterCriteria.setValue("description"); - - DynamicForm form = new DynamicForm(); - form.setFields(filterCriteria); - - DynamicForm form2 = new DynamicForm(); - form2.setFields(filterResultCount); - - HLayout filterLayout = new HLayout(); - filterLayout.addMember(form); - filterLayout.addMember(filter); - filterLayout.addMember(filterRange); - - helperContainer.addMember(locationTable); - helperContainer.addMember(filterLayout); - helperContainer.addMember(form2); + helperContainer.addMember(picker.getLocationTable()); + helperContainer.addMember(picker.getFilterLayout()); + helperContainer.addMember(picker.getResultCountForm()); createInputPanel(); return layout; } @Override - public void onFilterCriteriaChanged(StringFilterEvent event) { - String search = event.getFilter(); - - if (search != null && search.length() > 0) { - Criteria c = new Criteria("description", search); - locationTable.filterData(c); - filterResultCount.setValue(locationTable.getRecords().length); - } - else { - // TODO Remove filter - } - } - - - @Override - public void onFilterCriteriaChanged(RangeFilterEvent event) { - Float from = event.getFrom() - 0.001f; - Float to = event.getTo() + 0.001f; - - Criterion combinedFilter = null; - if (from.equals(Float.NaN) && to.equals(Float.NaN)) { - locationTable.clearCriteria(); - filterResultCount.setValue(""); - return; - } - else if (from.equals(Float.NaN)) { - combinedFilter = - new Criterion("from", OperatorId.LESS_OR_EQUAL, to); - } - else if (to.equals(Float.NaN)) { - combinedFilter = - new Criterion("from", OperatorId.GREATER_OR_EQUAL, from); - } - else { - combinedFilter = - new AdvancedCriteria(OperatorId.AND, new Criterion[] { - new Criterion("from", OperatorId.GREATER_OR_EQUAL, from), - new Criterion("from", OperatorId.LESS_OR_EQUAL, to) - }); - } - locationTable.filterData(combinedFilter); - filterResultCount.setValue(locationTable.getRecords().length); - } - - - @Override public List validate() { List errors = new ArrayList(); NumberFormat nf = NumberFormat.getDecimalFormat(); @@ -568,8 +388,8 @@ } } - locationTable.setDataSource(new DistanceInfoDataSource( - url, river, "locations")); + picker.getLocationTable().setDataSource(new DistanceInfoDataSource( + url, /*river*/ "Saar" , "locations")); } @@ -584,5 +404,24 @@ this.values = values; locationPanel.setValues(values); } + + + /** + * Callback when an item from the input helper was clicked. + * Set the respective km-value in the location value field. + * @param e event passed. + */ + public void onRecordClick (RecordClickEvent e) { + Record record = e.getRecord(); + double[] selected = new double[1]; + try { + selected[0] = + Double.parseDouble(record.getAttribute("from")); + } + catch(NumberFormatException nfe) { + // Is there anything else to do here? + } + setLocationValues(selected); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :