teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5861: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5861: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.client.ui.chart; raimund@2906: raimund@2935: import java.util.Map; raimund@2935: import java.util.HashMap; raimund@2935: sascha@2911: import com.google.gwt.core.client.GWT; raimund@3362: import com.google.gwt.i18n.client.NumberFormat; raimund@2906: sascha@2911: import com.smartgwt.client.types.Alignment; sascha@2911: sascha@2911: import com.smartgwt.client.widgets.Button; sascha@2911: import com.smartgwt.client.widgets.Canvas; sascha@2911: sascha@2911: import com.smartgwt.client.widgets.events.ClickEvent; sascha@2911: import com.smartgwt.client.widgets.events.ClickHandler; sascha@2911: sascha@2911: import com.smartgwt.client.widgets.form.DynamicForm; sascha@2911: sascha@2911: import com.smartgwt.client.widgets.form.fields.TextItem; sascha@2911: raimund@2918: import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent; raimund@2918: import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler; sascha@2911: raimund@2906: import com.smartgwt.client.widgets.layout.HLayout; raimund@2906: import com.smartgwt.client.widgets.layout.VLayout; raimund@2906: sascha@2911: import com.smartgwt.client.widgets.tab.events.TabSelectedEvent; raimund@2906: import com.smartgwt.client.widgets.tab.events.TabSelectedHandler; raimund@2906: teichmann@5835: import org.dive4elements.river.client.client.Config; sascha@2911: teichmann@5835: import org.dive4elements.river.client.client.ui.CollectionView; teichmann@5835: teichmann@5835: import org.dive4elements.river.client.shared.model.Artifact; teichmann@5835: import org.dive4elements.river.client.shared.model.Collection; teichmann@5835: import org.dive4elements.river.client.shared.model.FixAnalysisArtifact; teichmann@5835: import org.dive4elements.river.client.shared.model.FixFilter; teichmann@5835: import org.dive4elements.river.client.shared.model.OutputMode; sascha@2911: sascha@2911: import java.util.Date; raimund@2906: felix@4310: raimund@2906: /** felix@4320: * Tab representing and showing one Chart-output with a "navi" thing. raimund@2906: * raimund@2906: * @author Ingo Weinzierl raimund@2906: */ raimund@2906: public class NaviChartOutputTab raimund@2906: extends ChartOutputTab raimund@2906: implements TabSelectedHandler raimund@2906: { raimund@2906: protected TextItem currentkm; raimund@2906: raimund@2906: public NaviChartOutputTab( raimund@2906: String title, raimund@2906: Collection collection, raimund@2906: OutputMode mode, raimund@2906: CollectionView collectionView raimund@2906: ){ raimund@2906: super(title, collection, mode, collectionView); raimund@2906: right.removeChild(chart); raimund@2906: right.addChild(createNaviChart()); raimund@2906: collectionView.registerTabHandler(this); raimund@2906: } raimund@2906: raimund@2906: raimund@2906: protected Canvas createNaviChart() { raimund@2906: final Artifact art = collectionView.getArtifact(); raimund@2906: VLayout root = new VLayout(); raimund@2906: root.setWidth100(); raimund@2906: root.setHeight100(); raimund@2906: raimund@2906: HLayout layout = new HLayout(); raimund@2906: layout.setAlign(Alignment.CENTER); raimund@2906: raimund@2906: DynamicForm form = new DynamicForm(); raimund@2906: Button lower = new Button("<<"); raimund@2906: lower.setWidth(30); raimund@2906: Button upper = new Button(">>"); raimund@2906: upper.setWidth(30); raimund@2906: currentkm = new TextItem(); raimund@2906: currentkm.setWidth(60); raimund@2906: currentkm.setShowTitle(false); raimund@2906: raimund@2906: form.setFields(currentkm); raimund@2906: form.setWidth(60); raimund@2906: felix@4320: double fromKm; felix@4320: double toKm; felix@4320: felix@4320: if (art instanceof FixAnalysisArtifact) { felix@4320: FixAnalysisArtifact fix = (FixAnalysisArtifact) art; felix@4320: FixFilter fixFilter = fix.getFilter(); rrenkert@4866: String s = fix.getArtifactDescription().getDataValueAsString("ld_step"); felix@4320: try { felix@4320: double ds = Double.parseDouble(s); felix@4320: collectionView.setSteps(ds); felix@4320: } felix@4320: catch(NumberFormatException nfe) { felix@4320: collectionView.setSteps(100d); felix@4320: } felix@4320: fromKm = fixFilter.getFromKm(); felix@4320: toKm = fixFilter.getToKm(); raimund@2906: } felix@4320: else { felix@4320: // Probably WINFOArtifact kind of artifact. felix@4322: String ld_step = felix@4322: art.getArtifactDescription().getDataValueAsString("ld_step"); felix@4322: try { felix@4322: collectionView.setSteps(Double.valueOf(ld_step)); felix@4322: } felix@4322: catch (Exception e) { felix@4322: GWT.log("No ld_steps data or not parsable."); felix@4322: return root; felix@4322: } felix@4322: felix@4322: double[] kmRange = art.getArtifactDescription().getKMRange(); felix@4322: if (kmRange == null || kmRange.length == 2) { felix@4322: fromKm = kmRange[0]; felix@4322: toKm = kmRange[1]; felix@4322: } felix@4322: else { felix@4322: GWT.log("No KM range in description found."); felix@4322: return root; felix@4322: } raimund@2906: } felix@4320: felix@4320: collectionView.setMinKm(fromKm); felix@4320: collectionView.setMaxKm(toKm); raimund@2906: felix@4319: final NumberFormat nf = NumberFormat.getDecimalFormat(); raimund@2906: if (collectionView.getCurrentKm() == -1d) { raimund@3362: try { felix@4320: double d = Double.valueOf(fromKm); raimund@3362: currentkm.setValue(nf.format(d)); raimund@3362: } catch (NumberFormatException e) { felix@4320: currentkm.setValue(fromKm); raimund@3362: } felix@4320: collectionView.setCurrentKm(fromKm); raimund@2906: } raimund@2906: else { raimund@3362: try { felix@4320: double d = Double.valueOf(fromKm); raimund@3362: currentkm.setValue(nf.format(d)); raimund@3362: } catch (NumberFormatException e) { felix@4320: currentkm.setValue(fromKm); raimund@3362: } raimund@2906: currentkm.setValue(collectionView.getCurrentKm()); raimund@2906: } raimund@2906: raimund@2906: lower.addClickHandler(new ClickHandler() { raimund@2906: public void onClick(ClickEvent ce) { raimund@2936: tbarPanel.deselectControls(); raimund@2906: updateChartDown(); raimund@3362: try { raimund@3362: double d = Double.valueOf(collectionView.getCurrentKm()); raimund@3362: currentkm.setValue(nf.format(d)); raimund@3362: } catch (NumberFormatException e) { raimund@3362: currentkm.setValue(collectionView.getCurrentKm()); raimund@3362: } raimund@2906: } raimund@2906: }); raimund@2906: raimund@2906: upper.addClickHandler(new ClickHandler() { raimund@2906: public void onClick(ClickEvent ce) { raimund@2936: tbarPanel.deselectControls(); raimund@2906: updateChartUp(); raimund@3362: try { raimund@3362: double d = Double.valueOf(collectionView.getCurrentKm()); raimund@3362: currentkm.setValue(nf.format(d)); raimund@3362: } catch (NumberFormatException e) { raimund@3362: currentkm.setValue(collectionView.getCurrentKm()); raimund@3362: } raimund@2906: } raimund@2906: }); raimund@2906: raimund@2918: currentkm.addKeyPressHandler(new KeyPressHandler() { raimund@2918: public void onKeyPress(KeyPressEvent kpe) { raimund@2918: if (!kpe.getKeyName().equals("Enter")) { raimund@2918: return; raimund@2918: } raimund@3362: if(kpe.getItem().getValue() != null) { raimund@2936: tbarPanel.deselectControls(); raimund@2906: try { raimund@2918: String s = kpe.getItem().getValue().toString(); raimund@3362: double d; raimund@3362: try { raimund@3362: d = nf.parse(s); raimund@3362: currentkm.setValue(nf.format(d)); raimund@3362: } catch (NumberFormatException e) { raimund@3362: d = -1d; raimund@3362: } raimund@2918: if (d <= collectionView.getMaxKm() && raimund@2918: d >= collectionView.getMinKm()) { raimund@2906: collectionView.setCurrentKm(d); raimund@3499: tbarPanel.updateLinks(); raimund@2906: if (right != null) { raimund@2906: updateChartPanel(); raimund@2906: updateChartInfo(); raimund@2906: } raimund@2906: } raimund@2906: } raimund@2906: catch(NumberFormatException nfe) { raimund@2906: // Do nothing. raimund@2906: } raimund@2906: } raimund@2906: } raimund@2906: }); raimund@2906: layout.addMember(lower); raimund@2906: layout.addMember(form); raimund@2906: layout.addMember(upper); raimund@2906: raimund@2906: root.addMember(chart); raimund@2906: root.addMember(layout); raimund@2906: return root; raimund@2906: } raimund@2906: raimund@2906: felix@4321: /** Callback when km-up-button is clicked. felix@4321: * Increases collectionViews KM and refreshes view. */ raimund@2906: protected void updateChartUp() { raimund@2906: double currentKm = collectionView.getCurrentKm(); raimund@2906: if (currentKm < collectionView.getMaxKm()) { felix@4321: // Why this math? raimund@2906: double newVal = currentKm * 100; raimund@2906: newVal += (collectionView.getSteps() / 10); raimund@2906: collectionView.setCurrentKm((double)Math.round(newVal) / 100); raimund@3499: tbarPanel.updateLinks(); raimund@2906: updateChartPanel(); raimund@2906: updateChartInfo(); raimund@2906: } raimund@2906: } felix@4321: felix@4321: /** Callback when km-down-button is clicked. felix@4321: * Decreases collectionViews KM and refreshes view. */ raimund@2906: protected void updateChartDown() { raimund@2906: double currentKm = collectionView.getCurrentKm(); raimund@2906: if (currentKm > collectionView.getMinKm()) { felix@4321: // Why this math? raimund@2906: double newVal = currentKm * 100; raimund@2906: newVal -= (collectionView.getSteps() / 10); raimund@2906: collectionView.setCurrentKm((double)Math.round(newVal) / 100); raimund@3499: tbarPanel.updateLinks(); raimund@2906: updateChartPanel(); raimund@2906: updateChartInfo(); raimund@2906: } raimund@2906: raimund@2906: } raimund@2906: raimund@2936: /** raimund@2936: * Returns the existing chart panel. raimund@2936: * raimund@2936: * @return the existing chart panel. raimund@2936: */ raimund@2936: @Override raimund@2936: public Canvas getChartPanel() { raimund@2936: return chart; raimund@2936: } raimund@2936: raimund@2906: /** raimund@2906: * Builds the URL that points to the chart image. raimund@2906: * raimund@2906: * @param width The width of the requested chart. raimund@2906: * @param height The height of the requested chart. raimund@2906: * @param xr Optional x range (used for zooming). raimund@2906: * @param yr Optional y range (used for zooming). raimund@2906: * raimund@2906: * @return the URL to the chart image. raimund@2906: */ raimund@2906: @Override raimund@2906: protected String getImgUrl(int width, int height) { raimund@2906: Config config = Config.getInstance(); raimund@2906: raimund@2906: String imgUrl = GWT.getModuleBaseURL(); raimund@2906: imgUrl += "chart"; raimund@2906: imgUrl += "?uuid=" + collection.identifier(); raimund@2906: imgUrl += "&type=" + mode.getName(); raimund@2906: imgUrl += "&locale=" + config.getLocale(); raimund@2906: imgUrl += "×tamp=" + new Date().getTime(); raimund@2906: imgUrl += "&width=" + Integer.toString(width); raimund@2906: imgUrl += "&height=" + Integer.toString(height - 40); raimund@2906: raimund@2906: Number[] zoom = getZoomValues(); raimund@2906: raimund@2906: if (zoom != null) { raimund@2906: if (zoom[0].intValue() != 0 || zoom[1].intValue() != 1) { raimund@2906: // a zoom range of 0-1 means displaying the whole range. In such raimund@2906: // case we don't need to zoom. raimund@2906: imgUrl += "&minx=" + zoom[0]; raimund@2906: imgUrl += "&maxx=" + zoom[1]; raimund@2906: } raimund@2906: raimund@2906: if (zoom[2].intValue() != 0 || zoom[3].intValue() != 1) { raimund@2906: // a zoom range of 0-1 means displaying the whole range. In such raimund@2906: // case we don't need to zoom. raimund@2906: imgUrl += "&miny=" + zoom[2]; raimund@2906: imgUrl += "&maxy=" + zoom[3]; raimund@2906: } raimund@2906: } raimund@2906: felix@4322: if (collectionView.getArtifact() instanceof FixAnalysisArtifact) { raimund@2906: if (collectionView.getCurrentKm() == -1) { raimund@2906: FixAnalysisArtifact fix = raimund@2906: (FixAnalysisArtifact) collectionView.getArtifact(); raimund@2906: collectionView.setCurrentKm(fix.getFilter().getFromKm()); raimund@2906: } felix@4322: } felix@4322: else if (collectionView.getCurrentKm() == -1) { felix@4322: collectionView.setCurrentKm(collectionView.getArtifact().getArtifactDescription().getKMRange()[0]); felix@4322: } felix@4322: if (collectionView.getCurrentKm() != -1) { raimund@2906: imgUrl += "¤tKm=" + collectionView.getCurrentKm(); raimund@2906: } raimund@2906: raimund@2906: return imgUrl; raimund@2906: } raimund@2906: raimund@2906: public void onTabSelected(TabSelectedEvent tse) { raimund@2906: if (this.equals(tse.getTab())) { raimund@2906: updateChartPanel(); raimund@2906: updateChartInfo(); raimund@2906: currentkm.setValue(collectionView.getCurrentKm()); raimund@2906: } raimund@2906: } raimund@2906: raimund@2935: @Override raimund@2935: public Map getChartAttributes() { raimund@2935: Map attr = new HashMap(); raimund@2935: raimund@2935: attr = super.getChartAttributes(); raimund@2935: attr.put("km", String.valueOf(collectionView.getCurrentKm())); raimund@2935: raimund@2935: return attr; raimund@2935: } raimund@2906: } felix@4310: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :