[Dive4elements-commits] [PATCH 5 of 8] Refactor GaugePanel and GaugeInfo to extract a base class
Wald Commits
scm-commit at wald.intevation.org
Fri Oct 26 12:27:41 CEST 2012
# HG changeset patch
# User Björn Ricks <bjoern.ricks at intevation.de>
# Date 1351246794 -7200
# Node ID f75968f0ce802b140ae22020222a1e156088726b
# Parent 8f9f80db46f3aa418168caae8ae398026c7c5490
Refactor GaugePanel and GaugeInfo to extract a base class
Extract a base class from GaugePanel and GaugeInfo to reuse code for displaying
the measurement station information.
diff -r 8f9f80db46f3 -r f75968f0ce80 flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugePanel.java
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugePanel.java Fri Oct 26 12:17:30 2012 +0200
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugePanel.java Fri Oct 26 12:19:54 2012 +0200
@@ -1,100 +1,30 @@
package de.intevation.flys.client.client.ui;
import com.google.gwt.core.client.GWT;
-import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
-import com.google.gwt.user.client.ui.Anchor;
-import com.google.gwt.user.client.ui.HorizontalPanel;
-import com.google.gwt.user.client.ui.Label;
-
-import com.smartgwt.client.types.Overflow;
-import com.smartgwt.client.widgets.Canvas;
-import com.smartgwt.client.widgets.WidgetCanvas;
-import com.smartgwt.client.widgets.events.ResizedEvent;
-import com.smartgwt.client.widgets.events.ResizedHandler;
-import com.smartgwt.client.widgets.layout.SectionStackSection;
-import com.smartgwt.client.widgets.layout.VLayout;
-
import de.intevation.flys.client.client.FLYS;
-import de.intevation.flys.client.client.FLYSConstants;
-import de.intevation.flys.client.client.services.RiverInfoService;
-import de.intevation.flys.client.client.services.RiverInfoServiceAsync;
-import de.intevation.flys.client.shared.model.DataList;
import de.intevation.flys.client.shared.model.RiverInfo;
/**
* The GaugePanel is intended to be used within a SectionStackSection
- * It extends the VLayout by two methods to show and hide the
- * section stack section.
*
* @author <a href="mailto:bjoern.ricks at intevation.de">Björn Ricks</a>
*/
-public class GaugePanel extends VLayout implements ResizedHandler {
-
- /** SectionStackSection where this GaugePanel belongs in*/
- private SectionStackSection section;
-
- /** Name of the river */
- private String river;
-
- /** The message class that provides i18n strings.*/
- protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
-
- protected RiverInfoServiceAsync riverInfoService =
- GWT.create(RiverInfoService.class);
-
- protected GaugeTree gaugetree;
- protected Canvas gaugetreecanvas;
-
- protected RiverInfoPanel riverinfopanel;
-
- public final static String SECTION_ID = "GaugePanelSection";
- private final static String GAUGE_TREE_CANVAS_ID =
- "GaugeTreeCanvas";
+public class GaugePanel extends InfoPanel {
/**
- * Creates a new VLayout with a SectionStackSection
- * The GaugePanel's SectionStackSection is hidden by default.
+ * GaugePanel loads the GaugeInfo from the RiverInfoService and
+ * displays them in a tree underneath a RiverInfoPanel
*
* @param flys The FLYS object
- * @param section The section stack section to place the VLayout in.
*/
public GaugePanel(FLYS flys) {
- SectionStackSection section = new SectionStackSection();
- section.setExpanded(false);
- section.setTitle(MSG.gaugePanelTitle());
- section.setName(SECTION_ID);
- section.setID(SECTION_ID);
-
- gaugetree = new GaugeTree(flys);
- gaugetreecanvas = new WidgetCanvas(gaugetree);
- gaugetreecanvas.setID(GAUGE_TREE_CANVAS_ID);
-
- setOverflow(Overflow.HIDDEN);
- setStyleName("gaugepanel");
-
- section.setHidden(true);
- section.setItems(this);
- this.section = section;
-
- addResizedHandler(this);
+ super(new GaugeTree(flys));
}
- /**
- * Sets and loads the river data if river is not the current set river
- */
- public void setRiver(String river) {
- if (!river.equals(this.river)) {
- this.river = river;
- this.refresh();
- }
- }
-
- /**
- * Sets the data and closes not corresponding folds in the gauge tree
- */
- public void setData(DataList[] data) {
- gaugetree.setData(data);
+ @Override
+ public String getSectionTitle() {
+ return MSG.gaugePanelTitle();
}
/**
@@ -112,96 +42,10 @@
@Override
public void onSuccess(RiverInfo riverinfo) {
GWT.log("Loaded river info");
- renderGaugeOverviewInfo(riverinfo);
+ render(riverinfo);
expand();
}
});
}
- public void renderGaugeOverviewInfo(RiverInfo riverinfo) {
- gaugetree.setGauges(riverinfo);
-
- if (riverinfopanel == null) {
- removeAllMembers();
-
- riverinfopanel = new RiverInfoPanel(riverinfo);
-
- addMember(riverinfopanel);
- addMember(gaugetreecanvas);
- }
- else {
- riverinfopanel.setRiverInfo(riverinfo);
- }
- }
-
- @Override
- public void onResized(ResizedEvent event) {
- /* this height calculation is only an approximation and doesn't reflect
- * the real height of the the gaugetree. */
- int height = getInnerContentHeight() -
- RiverInfoPanel.getStaticHeight();
- int width = getInnerContentWidth();
-
- if (height < 0) {
- height = 0;
- }
-
- GWT.log("GaugePanel - onResize " + height);
-
- gaugetree.setHeight("" + height + "px");
- gaugetree.setWidth("" + width + "px");
- }
-
-
- /**
- * Hide the section stack section.
- */
- @Override
- public void hide() {
- GWT.log("GaugePanel - hide");
- this.section.setHidden(true);
- }
-
- /**
- * Show the section stack section.
- */
- @Override
- public void show() {
- GWT.log("GaugePanel - show");
- this.section.setHidden(false);
- }
-
- @Override
- public void addMember(Canvas component) {
- super.addMember(component);
- expand();
- }
-
- @Override
- public void removeMembers(Canvas[] components) {
- super.removeMembers(components);
- contract();
- }
-
- public SectionStackSection getSection() {
- return this.section;
- }
-
- private void removeAllMembers() {
- removeMembers(getMembers());
- }
-
- /**
- * Expands the gauge section
- */
- public void expand() {
- section.setExpanded(true);
- }
-
- /**
- * Contracts/shrinks the expanded gauge section
- */
- public void contract() {
- section.setExpanded(false);
- }
}
diff -r 8f9f80db46f3 -r f75968f0ce80 flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugeTree.java
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugeTree.java Fri Oct 26 12:17:30 2012 +0200
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/GaugeTree.java Fri Oct 26 12:19:54 2012 +0200
@@ -8,14 +8,12 @@
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
-import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.smartgwt.client.widgets.layout.HLayout;
import de.intevation.flys.client.client.FLYS;
-import de.intevation.flys.client.client.FLYSConstants;
import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.DataList;
@@ -26,15 +24,10 @@
import java.util.Iterator;
import java.util.List;
-
-public class GaugeTree extends ScrollPanel {
-
- private FLYS flys;
- private Tree tree;
- private DataList[] data;
-
- /** The message class that provides i18n strings.*/
- protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
+/**
+ * @author <a href="mailto:bjoern.ricks at intevation.de">Björn Ricks</a>
+ */
+public class GaugeTree extends InfoTree {
public GaugeTree(FLYS flys) {
this.flys = flys;
@@ -46,7 +39,8 @@
* Resets the items of the tree.
* If the list of gauges is empty or null the tree will be empty.
*/
- public void setGauges(RiverInfo riverinfo) {
+ @Override
+ public void setRiverInfo(RiverInfo riverinfo) {
tree.clear();
List<GaugeInfo> gauges = riverinfo.getGauges();
@@ -90,114 +84,7 @@
tree.addItem(gaugeitem);
}
- public void openAll() {
- GWT.log("GaugeTree - openAll");
- for (Iterator<TreeItem> it = tree.treeItemIterator(); it.hasNext();) {
- TreeItem item = it.next();
- item.setState(true);
- }
- }
-
- public void setData(DataList[] data) {
- this.data = data;
- if (tree.getItemCount() > 0) {
- open();
- }
- }
-
- public void open() {
- ArrayList<Double> locations = new ArrayList<Double>();
-
- if (data != null && data.length > 0) {
- for (int i = 0; i < data.length; i++) {
- DataList dl = data[i];
- String state = dl.getState();
- GWT.log("GaugeTree - open " + state);
- if (state.equals("state.winfo.location_distance")) {
- Double ldfrom = null;
- Double ldto = null;
-
- for (int j = dl.size()-1; j >= 0; --j) {
- Data d = dl.get(j);
- String label = d.getLabel();
- GWT.log("GaugeTree - setData - label " + label + " " + d.getStringValue());
- if (label.equals("ld_from")) {
- ldfrom = getDoubleValue(d);
- }
- else if (label.equals("ld_to")) {
- ldto = getDoubleValue(d);
- }
- else if (label.equals("ld_locations")) {
- getLocationsFromData(locations, d);
- openOnLocations(locations);
- return;
- }
- }
- if (ldfrom != null) {
- openOnDistance(ldfrom, ldto);
- return;
- }
- }
- else if(state.equals("state.winfo.distance_only") ||
- state.equals("state.winfo.distance")) {
- Double ldfrom = null;
- Double ldto = null;
-
- for (int j = dl.size()-1; j >= 0; --j) {
- Data d = dl.get(j);
- String label = d.getLabel();
- GWT.log("GaugeTree - setData - label " + label + " " + d.getStringValue());
- if (label.equals("ld_from")) {
- ldfrom = getDoubleValue(d);
- }
- else if (label.equals("ld_to")) {
- ldto = getDoubleValue(d);
- }
- }
-
- if (ldfrom != null) {
- openOnDistance(ldfrom, ldto);
- return;
- }
- }
- else if (state.equals("state.winfo.location")) {
- getLocations("ld_locations", locations, dl);
- openOnLocations(locations);
- return;
- }
- else if (state.equals("state.winfo.reference.curve.input.start")) {
- getLocations("reference_startpoint", locations, dl);
- }
- else if (state.equals("state.winfo.reference.curve.input.end")) {
- getLocations("reference_endpoint", locations, dl);
- }
- else if (state.equals("state.winfo.historicalq.reference_gauge")) {
- for (int j = dl.size()-1; j >= 0; --j) {
- Data d = dl.get(j);
- String label = d.getLabel();
- if (label.equals("reference_gauge")) {
- String tmp = d.getStringValue();
- if (tmp != null) {
- Long gaugereference = Long.valueOf(tmp);
- if (gaugereference != null) {
- openOnReference(gaugereference);
- return;
- }
- }
- }
- }
- }
- }
- }
- if (!locations.isEmpty()) {
- openOnLocations(locations);
- }
- else {
- openAll();
- }
- }
-
- private void getLocations(String labelname, List<Double> locations, DataList dl) {
+ void getLocations(String labelname, List<Double> locations, DataList dl) {
for (int j = dl.size()-1; j >= 0; --j) {
Data d = dl.get(j);
String label = d.getLabel();
@@ -207,7 +94,7 @@
}
}
- private void getLocationsFromData(List<Double> locations, Data data) {
+ void getLocationsFromData(List<Double> locations, Data data) {
DataItem[] items = data.getItems();
for (int k = 0; k < items.length; k++) {
String tmp = items[k].getStringValue();
@@ -233,14 +120,6 @@
}
}
- private Double getDoubleValue(Data d) {
- String tmp = d.getStringValue();
- if (tmp != null) {
- return Double.valueOf(tmp);
- }
- return null;
- }
-
public void openOnReference(Long number) {
GWT.log("GaugeTree - openOnReference " + number);
for (Iterator<TreeItem> it = tree.treeItemIterator(); it.hasNext();) {
@@ -476,4 +355,95 @@
}
}
+ public void open() {
+ ArrayList<Double> locations = new ArrayList<Double>();
+
+ if (data != null && data.length > 0) {
+ for (int i = 0; i < data.length; i++) {
+ DataList dl = data[i];
+ String state = dl.getState();
+ GWT.log("GaugeTree - open " + state);
+ if (state.equals("state.winfo.location_distance")) {
+ Double ldfrom = null;
+ Double ldto = null;
+
+ for (int j = dl.size()-1; j >= 0; --j) {
+ Data d = dl.get(j);
+ String label = d.getLabel();
+ GWT.log("GaugeTree - setData - label " + label + " " + d.getStringValue());
+ if (label.equals("ld_from")) {
+ ldfrom = getDoubleValue(d);
+ }
+ else if (label.equals("ld_to")) {
+ ldto = getDoubleValue(d);
+ }
+ else if (label.equals("ld_locations")) {
+ getLocationsFromData(locations, d);
+ openOnLocations(locations);
+ return;
+ }
+ }
+ if (ldfrom != null) {
+ openOnDistance(ldfrom, ldto);
+ return;
+ }
+ }
+ else if(state.equals("state.winfo.distance_only") ||
+ state.equals("state.winfo.distance")) {
+ Double ldfrom = null;
+ Double ldto = null;
+
+ for (int j = dl.size()-1; j >= 0; --j) {
+ Data d = dl.get(j);
+ String label = d.getLabel();
+ GWT.log("GaugeTree - setData - label " + label + " " + d.getStringValue());
+ if (label.equals("ld_from")) {
+ ldfrom = getDoubleValue(d);
+ }
+ else if (label.equals("ld_to")) {
+ ldto = getDoubleValue(d);
+ }
+ }
+
+ if (ldfrom != null) {
+ openOnDistance(ldfrom, ldto);
+ return;
+ }
+ }
+ else if (state.equals("state.winfo.location")) {
+ getLocations("ld_locations", locations, dl);
+ openOnLocations(locations);
+ return;
+ }
+ else if (state.equals("state.winfo.reference.curve.input.start")) {
+ getLocations("reference_startpoint", locations, dl);
+ }
+ else if (state.equals("state.winfo.reference.curve.input.end")) {
+ getLocations("reference_endpoint", locations, dl);
+ }
+ else if (state.equals("state.winfo.historicalq.reference_gauge")) {
+ for (int j = dl.size()-1; j >= 0; --j) {
+ Data d = dl.get(j);
+ String label = d.getLabel();
+ if (label.equals("reference_gauge")) {
+ String tmp = d.getStringValue();
+ if (tmp != null) {
+ Long gaugereference = Long.valueOf(tmp);
+ if (gaugereference != null) {
+ openOnReference(gaugereference);
+ return;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ if (!locations.isEmpty()) {
+ openOnLocations(locations);
+ }
+ else {
+ openAll();
+ }
+ }
}
diff -r 8f9f80db46f3 -r f75968f0ce80 flys-client/src/main/java/de/intevation/flys/client/client/ui/InfoPanel.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/InfoPanel.java Fri Oct 26 12:19:54 2012 +0200
@@ -0,0 +1,170 @@
+package de.intevation.flys.client.client.ui;
+
+import com.google.gwt.core.client.GWT;
+
+import com.smartgwt.client.types.Overflow;
+import com.smartgwt.client.widgets.Canvas;
+import com.smartgwt.client.widgets.events.ResizedEvent;
+import com.smartgwt.client.widgets.events.ResizedHandler;
+import com.smartgwt.client.widgets.layout.SectionStackSection;
+import com.smartgwt.client.widgets.layout.VLayout;
+import com.smartgwt.client.widgets.WidgetCanvas;
+
+import de.intevation.flys.client.client.FLYSConstants;
+import de.intevation.flys.client.client.services.RiverInfoService;
+import de.intevation.flys.client.client.services.RiverInfoServiceAsync;
+import de.intevation.flys.client.shared.model.DataList;
+import de.intevation.flys.client.shared.model.RiverInfo;
+
+/**
+ * @author <a href="mailto:bjoern.ricks at intevation.de">Björn Ricks</a>
+ */
+public abstract class InfoPanel extends VLayout implements ResizedHandler {
+
+ /** SectionStackSection where this InfoPanel belongs in*/
+ protected SectionStackSection section;
+
+ /** Name of the river */
+ protected String river;
+
+ /** The message class that provides i18n strings.*/
+ protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
+
+ protected RiverInfoServiceAsync riverInfoService = GWT.create(RiverInfoService.class);
+
+ /** Panel to show the info about the river */
+ protected RiverInfoPanel riverinfopanel;
+ protected InfoTree tree;
+
+ /** Wrapper arround the GWT Tree (InfoTree) object */
+ protected Canvas treecanvas;
+
+ protected final static String SECTION_ID = "InfoPanelSection";
+
+ public InfoPanel(InfoTree tree) {
+ SectionStackSection section = new SectionStackSection();
+ section.setExpanded(false);
+ section.setTitle(getSectionTitle());
+ section.setName(SECTION_ID);
+ section.setID(SECTION_ID);
+
+ treecanvas = new WidgetCanvas(tree);
+
+ setOverflow(Overflow.HIDDEN);
+ setStyleName("infopanel");
+
+ section.setHidden(true);
+ section.setItems(this);
+ this.section = section;
+ this.tree = tree;
+
+ addResizedHandler(this);
+ }
+
+ /**
+ * Sets and loads the river data if river is not the current set river
+ */
+ public void setRiver(String river) {
+ if (!river.equals(this.river)) {
+ this.river = river;
+ this.refresh();
+ }
+ }
+
+ /**
+ * Sets the data and closes not corresponding folds in the gauge tree
+ */
+ public void setData(DataList[] data) {
+ tree.setData(data);
+ }
+
+ protected void render(RiverInfo riverinfo) {
+ tree.setRiverInfo(riverinfo);
+
+ if (riverinfopanel == null) {
+ removeAllMembers();
+
+ riverinfopanel = new RiverInfoPanel(riverinfo);
+
+ addMember(riverinfopanel);
+ addMember(treecanvas);
+ }
+ else {
+ riverinfopanel.setRiverInfo(riverinfo);
+ }
+ }
+
+ @Override
+ public void onResized(ResizedEvent event) {
+ /* this height calculation is only an approximation and doesn't reflect
+ * the real height of the the gaugetree. */
+ int height = getInnerContentHeight() -
+ RiverInfoPanel.getStaticHeight();
+ int width = getInnerContentWidth();
+
+ if (height < 0) {
+ height = 0;
+ }
+
+ GWT.log("InfoPanel - onResize " + height);
+
+ tree.setHeight("" + height + "px");
+ tree.setWidth("" + width + "px");
+ }
+
+ /**
+ * Hide the section stack section.
+ */
+ @Override
+ public void hide() {
+ GWT.log("InfoPanel - hide");
+ this.section.setHidden(true);
+ }
+
+ /**
+ * Show the section stack section.
+ */
+ @Override
+ public void show() {
+ GWT.log("InfoPanel - show");
+ this.section.setHidden(false);
+ }
+
+ @Override
+ public void addMember(Canvas component) {
+ super.addMember(component);
+ expand();
+ }
+
+ @Override
+ public void removeMembers(Canvas[] components) {
+ super.removeMembers(components);
+ contract();
+ }
+
+ public SectionStackSection getSection() {
+ return this.section;
+ }
+
+ protected void removeAllMembers() {
+ removeMembers(getMembers());
+ }
+
+ /**
+ * Expands the gauge section
+ */
+ public void expand() {
+ section.setExpanded(true);
+ }
+
+ /**
+ * Contracts/shrinks the expanded gauge section
+ */
+ public void contract() {
+ section.setExpanded(false);
+ }
+
+ protected abstract void refresh();
+
+ protected abstract String getSectionTitle();
+}
diff -r 8f9f80db46f3 -r f75968f0ce80 flys-client/src/main/java/de/intevation/flys/client/client/ui/InfoTree.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/InfoTree.java Fri Oct 26 12:19:54 2012 +0200
@@ -0,0 +1,55 @@
+package de.intevation.flys.client.client.ui;
+
+import java.util.Iterator;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.user.client.ui.ScrollPanel;
+import com.google.gwt.user.client.ui.Tree;
+import com.google.gwt.user.client.ui.TreeItem;
+
+import de.intevation.flys.client.client.FLYS;
+import de.intevation.flys.client.client.FLYSConstants;
+import de.intevation.flys.client.shared.model.Data;
+import de.intevation.flys.client.shared.model.DataList;
+import de.intevation.flys.client.shared.model.RiverInfo;
+
+/**
+ * @author <a href="mailto:bjoern.ricks at intevation.de">Björn Ricks</a>
+ */
+public abstract class InfoTree extends ScrollPanel {
+
+ protected FLYS flys;
+ protected Tree tree;
+ protected DataList[] data;
+
+ /** The message class that provides i18n strings.*/
+ protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
+
+ public void openAll() {
+ for (Iterator<TreeItem> it = tree.treeItemIterator(); it.hasNext();) {
+ TreeItem item = it.next();
+ item.setState(true);
+ }
+ }
+
+ public void setData(DataList[] data) {
+ this.data = data;
+ if (tree.getItemCount() > 0) {
+ open();
+ }
+ }
+
+ protected Double getDoubleValue(Data d) {
+ String tmp = d.getStringValue();
+ if (tmp != null) {
+ return Double.valueOf(tmp);
+ }
+ return null;
+ }
+
+ public abstract void open() ;
+
+ public abstract void setRiverInfo(RiverInfo riverinfo);
+
+
+}
\ No newline at end of file
More information about the Dive4elements-commits
mailing list