raimund@2906: package de.intevation.flys.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:
raimund@2906: import de.intevation.flys.client.client.Config;
raimund@2906:
sascha@2911: import de.intevation.flys.client.client.ui.CollectionView;
sascha@2911:
sascha@2911: import de.intevation.flys.client.shared.model.Artifact;
sascha@2911: import de.intevation.flys.client.shared.model.Collection;
sascha@2911: import de.intevation.flys.client.shared.model.FixAnalysisArtifact;
sascha@2911: import de.intevation.flys.client.shared.model.OutputMode;
sascha@2911:
sascha@2911: import java.util.Date;
raimund@2906:
raimund@2906: /**
raimund@2906: * Tab representing and showing one Chart-output.
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: FixAnalysisArtifact fix = (FixAnalysisArtifact) art;
raimund@2906:
raimund@2906: String s = fix.getArtifactDescription().getDataValueAsString("step");
raimund@2906: try {
sascha@3368: double ds = Double.parseDouble(s);
raimund@2906: collectionView.setSteps(ds);
raimund@2906: }
raimund@2906: catch(NumberFormatException nfe) {
raimund@2906: collectionView.setSteps(100d);
raimund@2906: }
raimund@2906: collectionView.setMinKm(fix.getFilter().getFromKm());
raimund@2906: collectionView.setMaxKm(fix.getFilter().getToKm());
raimund@2906:
raimund@3362: NumberFormat nf = NumberFormat.getDecimalFormat();
raimund@2906: if (collectionView.getCurrentKm() == -1d) {
raimund@3362: try {
raimund@3362: double d = Double.valueOf(fix.getFilter().getFromKm());
raimund@3362: currentkm.setValue(nf.format(d));
raimund@3362: } catch (NumberFormatException e) {
raimund@3362: currentkm.setValue(fix.getFilter().getFromKm());
raimund@3362: }
raimund@2906: collectionView.setCurrentKm(fix.getFilter().getFromKm());
raimund@2906: }
raimund@2906: else {
raimund@3362: try {
raimund@3362: double d = Double.valueOf(fix.getFilter().getFromKm());
raimund@3362: currentkm.setValue(nf.format(d));
raimund@3362: } catch (NumberFormatException e) {
raimund@3362: currentkm.setValue(fix.getFilter().getFromKm());
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: NumberFormat nf = NumberFormat.getDecimalFormat();
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: NumberFormat nf = NumberFormat.getDecimalFormat();
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: NumberFormat nf = NumberFormat.getDecimalFormat();
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@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:
raimund@2906: protected void updateChartUp() {
raimund@2906: double currentKm = collectionView.getCurrentKm();
raimund@2906: if (currentKm < collectionView.getMaxKm()) {
raimund@2906: double newVal = currentKm * 100;
raimund@2906: newVal += (collectionView.getSteps() / 10);
raimund@2906: collectionView.setCurrentKm((double)Math.round(newVal) / 100);
raimund@2906: updateChartPanel();
raimund@2906: updateChartInfo();
raimund@2906: }
raimund@2906: }
raimund@2906: protected void updateChartDown() {
raimund@2906: double currentKm = collectionView.getCurrentKm();
raimund@2906: if (currentKm > collectionView.getMinKm()) {
raimund@2906: double newVal = currentKm * 100;
raimund@2906: newVal -= (collectionView.getSteps() / 10);
raimund@2906: collectionView.setCurrentKm((double)Math.round(newVal) / 100);
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:
raimund@2906: 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: }
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@2935:
raimund@2906: }