bjoern@3715: package de.intevation.flys.client.client.ui; bjoern@3715: ingo@3719: import java.util.ArrayList; ingo@3719: import java.util.Iterator; bjoern@3715: import java.util.List; bjoern@3715: bjoern@3715: import com.google.gwt.core.client.GWT; ingo@3719: import com.google.gwt.i18n.client.NumberFormat; bjoern@3715: import com.google.gwt.user.client.rpc.AsyncCallback; ingo@3719: import com.google.gwt.user.client.ui.DecoratorPanel; ingo@3719: import com.google.gwt.user.client.ui.Grid; ingo@3719: import com.google.gwt.user.client.ui.HorizontalPanel; ingo@3719: import com.google.gwt.user.client.ui.Label; ingo@3719: import com.google.gwt.user.client.ui.ScrollPanel; bjoern@3715: import com.google.gwt.user.client.ui.Tree; bjoern@3715: import com.google.gwt.user.client.ui.TreeItem; bjoern@3715: ingo@3719: import com.smartgwt.client.types.Alignment; bjoern@3715: import com.smartgwt.client.types.Overflow; ingo@3719: import com.smartgwt.client.widgets.events.ResizedEvent; ingo@3719: import com.smartgwt.client.widgets.events.ResizedHandler; ingo@3719: import com.smartgwt.client.widgets.layout.HLayout; bjoern@3715: import com.smartgwt.client.widgets.layout.SectionStackSection; bjoern@3715: import com.smartgwt.client.widgets.layout.VLayout; bjoern@3715: bjoern@3715: import de.intevation.flys.client.client.FLYSConstants; bjoern@3715: import de.intevation.flys.client.client.services.GaugeOverviewInfoService; bjoern@3715: import de.intevation.flys.client.client.services.GaugeOverviewInfoServiceAsync; bjoern@3715: import de.intevation.flys.client.shared.model.GaugeInfo; bjoern@3715: import de.intevation.flys.client.shared.model.RiverInfo; bjoern@3715: bjoern@3715: /** bjoern@3715: * The GaugePanel is intendet to be used within a SectionStackSection bjoern@3715: * It extends the VLayout by two methods to show and hide the bjoern@3715: * section stack section. bjoern@3715: */ ingo@3719: public class GaugePanel extends VLayout implements ResizedHandler { bjoern@3715: bjoern@3715: /** SectionStackSection where this GaugePanel belongs in*/ bjoern@3715: private SectionStackSection sectionStack; bjoern@3715: bjoern@3715: /** Name of the river */ bjoern@3715: private String river; bjoern@3715: bjoern@3715: /** The message class that provides i18n strings.*/ bjoern@3715: protected FLYSConstants MSG = GWT.create(FLYSConstants.class); bjoern@3715: bjoern@3715: protected GaugeOverviewInfoServiceAsync gaugeOverviewInfoService = bjoern@3715: GWT.create(GaugeOverviewInfoService.class); bjoern@3715: ingo@3719: protected GaugeTree gaugetree = new GaugeTree(); ingo@3719: ingo@3719: protected RiverInfoPanel riverinfopanel; ingo@3719: bjoern@3715: /** bjoern@3715: * Creates a new VLayout with a SectionStackSection bjoern@3715: * The GaugePanel's SectionStackSection is hidden by default. bjoern@3715: * @param sectionStack The section stack section to place the VLayout in. bjoern@3715: */ bjoern@3715: public GaugePanel(SectionStackSection sectionStack) { ingo@3719: setOverflow(Overflow.HIDDEN); ingo@3719: sectionStack.setHidden(true); bjoern@3715: sectionStack.setItems(this); bjoern@3715: this.sectionStack = sectionStack; ingo@3719: setStyleName("gaugepanel"); ingo@3719: addResizedHandler(this); bjoern@3715: } bjoern@3715: ingo@3719: /** ingo@3719: * Sets and loads the river data if river is not the current set river ingo@3719: */ bjoern@3715: public void setRiver(String river) { ingo@3719: if (!river.equals(this.river)) { ingo@3719: this.river = river; ingo@3719: this.refresh(); ingo@3719: } bjoern@3715: } bjoern@3715: bjoern@3715: /** bjoern@3715: * Loads the river info and renders it afterwards bjoern@3715: */ bjoern@3715: public void refresh() { bjoern@3715: gaugeOverviewInfoService.getRiverInfo(this.river, new AsyncCallback() { bjoern@3715: public void onFailure(Throwable e) { bjoern@3715: GWT.log("Could not load the river info." + e); bjoern@3715: } bjoern@3715: bjoern@3715: public void onSuccess(RiverInfo riverinfo) { bjoern@3715: GWT.log("Loaded river info"); bjoern@3715: renderGaugeOverviewInfo(riverinfo); ingo@3719: gaugetree.openAll(); bjoern@3715: } bjoern@3715: }); bjoern@3715: } bjoern@3715: bjoern@3715: public void renderGaugeOverviewInfo(RiverInfo riverinfo) { ingo@3719: removeMembers(getMembers()); bjoern@3715: ingo@3719: riverinfopanel = new RiverInfoPanel(riverinfo); ingo@3719: addMember(riverinfopanel); ingo@3719: addMember(gaugetree); bjoern@3715: ingo@3719: gaugetree.setGauges(riverinfo); ingo@3719: } bjoern@3715: ingo@3719: @Override ingo@3719: public void onResized(ResizedEvent event) { ingo@3719: /* this height calculation is only an approximation and doesn't reflect ingo@3719: * the real height of the the gaugetree. */ ingo@3719: int height = getInnerContentHeight() - ingo@3719: (RiverInfoPanel.HEIGHT + ingo@3719: (2 * RiverInfoPanel.BORDER_WIDTH) + ingo@3719: (2 * RiverInfoPanel.PADDING) + ingo@3719: (2 * RiverInfoPanel.MARGIN)); ingo@3719: ingo@3719: if (height < 0) { ingo@3719: height = 0; bjoern@3715: } ingo@3719: ingo@3719: gaugetree.setHeight("" + height + "px"); bjoern@3715: } bjoern@3715: ingo@3719: bjoern@3715: /** bjoern@3715: * Hide the section stack section. bjoern@3715: */ bjoern@3715: public void hide() { bjoern@3715: GWT.log("GaugePanel - hide"); bjoern@3715: this.sectionStack.setHidden(true); bjoern@3715: } bjoern@3715: bjoern@3715: /** bjoern@3715: * Show the section stack section. bjoern@3715: */ bjoern@3715: public void show() { bjoern@3715: GWT.log("GaugePanel - show"); bjoern@3715: this.sectionStack.setHidden(false); bjoern@3715: } bjoern@3715: ingo@3719: class GaugeTree extends ScrollPanel { ingo@3719: ingo@3719: private Tree tree; ingo@3719: ingo@3719: public GaugeTree() { ingo@3719: tree = new Tree(); ingo@3719: setWidget(tree); ingo@3719: } ingo@3719: ingo@3719: /** ingo@3719: * Resets the items of the tree. ingo@3719: * If the list of gauges is empty or null the tree will be empty. ingo@3719: */ ingo@3719: public void setGauges(RiverInfo riverinfo) { ingo@3719: tree.clear(); ingo@3719: ingo@3719: List gauges = riverinfo.getGauges(); ingo@3719: ingo@3719: if (gauges != null && !gauges.isEmpty()) { ingo@3719: ingo@3719: ArrayList emptygauges = new ArrayList(); ingo@3719: ingo@3719: if (!riverinfo.isKmUp()) { ingo@3719: for (GaugeInfo gauge : gauges) { ingo@3719: addGauge(gauge, emptygauges); ingo@3719: } ingo@3719: } ingo@3719: else { ingo@3719: for (int i = gauges.size(); i >= 0; i--) { ingo@3719: GaugeInfo gauge = gauges.get(i); ingo@3719: addGauge(gauge, emptygauges); ingo@3719: } ingo@3719: } ingo@3719: ingo@3719: // put empty gauges to the end ingo@3719: for (GaugeInfo gauge : emptygauges) { ingo@3719: addGauge(gauge); ingo@3719: } ingo@3719: } ingo@3719: } ingo@3719: ingo@3719: private void addGauge(GaugeInfo gauge, List empty) { ingo@3719: if (gauge.getKmStart() != null && gauge.getKmEnd() != null) { ingo@3719: addGauge(gauge); ingo@3719: } ingo@3719: else { ingo@3719: empty.add(gauge); ingo@3719: } ingo@3719: } ingo@3719: ingo@3719: private void addGauge(GaugeInfo gauge) { ingo@3719: TreeItem gaugeitem = new GaugeInfoItem(gauge); ingo@3719: tree.addItem(gaugeitem); ingo@3719: } ingo@3719: ingo@3719: public void openAll() { ingo@3719: for (Iterator it = tree.treeItemIterator(); it.hasNext();) { ingo@3719: TreeItem item = it.next(); ingo@3719: item.setState(true); ingo@3719: } ingo@3719: } ingo@3719: } ingo@3719: ingo@3719: class RiverInfoPanel extends HorizontalPanel { ingo@3719: ingo@3719: public final static int HEIGHT = 30; ingo@3719: public final static int BORDER_WIDTH = 3; ingo@3719: public final static int PADDING = 8; ingo@3719: public final static int MARGIN = 10; bjoern@3715: bjoern@3715: public RiverInfoPanel(RiverInfo riverinfo) { ingo@3719: setStyleName("riverinfopanel"); ingo@3719: setHeight("" + HEIGHT + "px"); ingo@3719: setVerticalAlignment(ALIGN_MIDDLE); bjoern@3715: ingo@3719: NumberFormat nf = NumberFormat.getDecimalFormat(); ingo@3719: ingo@3719: addLabel(riverinfo.getName(), false); bjoern@3715: bjoern@3715: String kmtext = ""; bjoern@3715: Double start = riverinfo.getKmStart(); bjoern@3715: Double end = riverinfo.getKmEnd(); bjoern@3715: bjoern@3715: if (!riverinfo.isKmUp()) { bjoern@3715: Double tmp = end; bjoern@3715: end = start; bjoern@3715: start = tmp; bjoern@3715: } bjoern@3715: if (end != null) { ingo@3719: kmtext += nf.format(end); bjoern@3715: kmtext += " - "; bjoern@3715: } bjoern@3715: if (start != null) { ingo@3719: kmtext += nf.format(start); bjoern@3715: } bjoern@3715: kmtext += " km"; bjoern@3715: ingo@3719: addLabel(kmtext, false); bjoern@3715: bjoern@3715: String qtext = ""; bjoern@3715: Double qmin = riverinfo.getMinQ(); bjoern@3715: Double qmax = riverinfo.getMaxQ(); bjoern@3715: if (qmin != null) { ingo@3719: qtext += nf.format(qmin); ingo@3719: qtext += " " + MSG.gauge_q_unit(); bjoern@3715: qtext += " - "; bjoern@3715: } bjoern@3715: if (qmax != null) { ingo@3719: qtext += nf.format(qmax); ingo@3719: qtext += " " + MSG.gauge_q_unit(); bjoern@3715: } bjoern@3715: ingo@3719: addLabel(qtext, false); ingo@3719: } ingo@3719: ingo@3719: private void addLabel(String text, boolean wordwrap) { ingo@3719: Label label = new Label(text, wordwrap); ingo@3719: add(label); ingo@3719: setCellHeight(label, "" + HEIGHT + "px"); bjoern@3715: } bjoern@3715: } bjoern@3715: bjoern@3715: class GaugeInfoItem extends TreeItem { bjoern@3715: public GaugeInfoItem(GaugeInfo gauge) { bjoern@3715: GaugeInfoHead gaugeinfohead = new GaugeInfoHead(gauge); bjoern@3715: GaugeInfoPanel gaugeinfopanel = new GaugeInfoPanel(gauge); bjoern@3715: setWidget(gaugeinfohead); bjoern@3715: addItem(gaugeinfopanel); bjoern@3715: } bjoern@3715: } bjoern@3715: bjoern@3715: class GaugeInfoHead extends HLayout { bjoern@3715: bjoern@3715: public GaugeInfoHead(GaugeInfo gauge) { bjoern@3715: setStyleName("gaugeinfohead"); bjoern@3715: setAutoHeight(); bjoern@3715: setAutoWidth(); bjoern@3715: ingo@3719: NumberFormat nf = NumberFormat.getDecimalFormat(); ingo@3719: ingo@3719: Label label = new Label(gauge.getName(), true); bjoern@3715: addMember(label); bjoern@3715: ingo@3719: Double start; ingo@3719: Double end; ingo@3719: ingo@3719: if (!gauge.isKmUp()) { ingo@3719: start = gauge.getKmStart(); ingo@3719: end = gauge.getKmEnd(); ingo@3719: } ingo@3719: else { ingo@3719: start = gauge.getKmEnd(); ingo@3719: end = gauge.getKmStart(); ingo@3719: } ingo@3719: bjoern@3715: String kmtext = ""; bjoern@3715: if (start != null) { ingo@3719: kmtext += nf.format(start); bjoern@3715: kmtext += " - "; bjoern@3715: } bjoern@3715: if (end != null) { ingo@3719: kmtext += nf.format(end); bjoern@3715: } ingo@3719: if (start != null || end != null) { ingo@3719: kmtext += " km"; ingo@3719: } bjoern@3715: bjoern@3715: label = new Label(kmtext); bjoern@3715: bjoern@3715: addMember(label); ingo@3719: ingo@3719: Double station = gauge.getStation(); ingo@3719: if (station != null) { ingo@3719: String stext = nf.format(station); ingo@3719: stext += " km"; ingo@3719: label = new Label(stext); ingo@3719: addMember(label); ingo@3719: } bjoern@3715: } bjoern@3715: } bjoern@3715: bjoern@3715: class GaugeInfoPanel extends DecoratorPanel { ingo@3719: bjoern@3715: public GaugeInfoPanel(GaugeInfo gauge) { bjoern@3715: setStyleName("gaugeinfopanel"); bjoern@3715: Grid grid = new Grid(4, 2); bjoern@3715: ingo@3719: NumberFormat nf = NumberFormat.getDecimalFormat(); ingo@3719: ingo@3719: Double minw = gauge.getMinW(); ingo@3719: Double maxw = gauge.getMaxW(); ingo@3719: if (minw != null && maxw != null) { ingo@3719: grid.setText(0, 0, MSG.wq_value_q()); ingo@3719: grid.setText(0, 1, "" + nf.format(minw) + ingo@3719: " - " + nf.format(maxw)); ingo@3719: } ingo@3719: ingo@3719: Double minq = gauge.getMinQ(); ingo@3719: Double maxq = gauge.getMaxQ(); ingo@3719: if (minq != null && maxq != null) { ingo@3719: grid.setText(1, 0, MSG.wq_value_w()); ingo@3719: grid.setText(1, 1, "" + nf.format(minq) + ingo@3719: " - " + nf.format(maxq)); ingo@3719: } ingo@3719: ingo@3719: Double aeo = gauge.getAeo(); ingo@3719: if (aeo != null) { ingo@3719: grid.setText(2, 0, "AEO [kmĀ²]"); ingo@3719: grid.setText(2, 1, "" + nf.format(aeo)); ingo@3719: } ingo@3719: ingo@3719: Double datum = gauge.getDatum(); ingo@3719: if (datum != null) { ingo@3719: grid.setText(3, 0, MSG.gauge_zero() + " [" + ingo@3719: gauge.getWstUnit() + "]"); ingo@3719: grid.setText(3, 1, "" + nf.format(datum)); ingo@3719: } bjoern@3715: bjoern@3715: setWidget(grid); bjoern@3715: } bjoern@3715: } bjoern@3715: }