comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java @ 2477:936e3e6cd9b9

Issue 502. Create label to show coordinates for each y axis in the chart. flys-client/trunk@4220 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Thu, 12 Apr 2012 09:49:41 +0000
parents ef745bc6bed9
children 8d27d2d33d70
comparison
equal deleted inserted replaced
2476:e8c4f8f2220e 2477:936e3e6cd9b9
1 package de.intevation.flys.client.client.ui.chart; 1 package de.intevation.flys.client.client.ui.chart;
2
3 import java.util.ArrayList;
4
5 import com.google.gwt.core.client.GWT;
2 6
3 import com.google.gwt.i18n.client.NumberFormat; 7 import com.google.gwt.i18n.client.NumberFormat;
4 8
5 import com.smartgwt.client.widgets.Canvas; 9 import com.smartgwt.client.widgets.Canvas;
6 import com.smartgwt.client.widgets.Label; 10 import com.smartgwt.client.widgets.Label;
18 22
19 protected ChartOutputTab chartTab; 23 protected ChartOutputTab chartTab;
20 24
21 protected NumberFormat nf; 25 protected NumberFormat nf;
22 26
23 protected Label x; 27 protected HLayout xLayout;
24 protected Label y; 28 protected ArrayList<HLayout> yLayouts;
25
26 29
27 public MousePositionPanel(ChartOutputTab chartTab) { 30 public MousePositionPanel(ChartOutputTab chartTab) {
28 super(); 31 super();
29 32
30 this.chartTab = chartTab; 33 this.chartTab = chartTab;
31
32 x = new Label();
33 y = new Label();
34 34
35 nf = NumberFormat.getDecimalFormat(); 35 nf = NumberFormat.getDecimalFormat();
36 36
37 chartTab.getChartPanel().addMouseMoveHandler(this); 37 chartTab.getChartPanel().addMouseMoveHandler(this);
38 38
45 * fixed width of 195px plus a margin of 5px. 45 * fixed width of 195px plus a margin of 5px.
46 */ 46 */
47 protected void initLayout() { 47 protected void initLayout() {
48 setMembersMargin(5); 48 setMembersMargin(5);
49 49
50 Label xDesc = new Label("Position: X = "); 50 xLayout = null;
51 Label yDesc = new Label("Y = "); 51 yLayouts = new ArrayList<HLayout>();
52
53 HLayout xLayout = new HLayout();
54 xLayout.setWidth(125);
55 xLayout.addMember(xDesc);
56 xLayout.addMember(x);
57
58 HLayout yLayout = new HLayout();
59 yLayout.setWidth(70);
60 yLayout.addMember(yDesc);
61 yLayout.addMember(y);
62
63 xDesc.setWidth(70);
64 x.setWidth(55);
65 yDesc.setWidth(20);
66 y.setWidth(50);
67
68 addMember(xLayout);
69 addMember(yLayout);
70 }
71
72
73 /**
74 * /Updates the X value displayed in the <i>x</i> label.
75 *
76 * @param x the new x value.
77 */
78 public void setX(String x) {
79 this.x.setContents(x);
80 }
81
82
83 /**
84 * /Updates the Y value displayed in the <i>y</i> label.
85 *
86 * @param y the new y value.
87 */
88 public void setY(String y) {
89 this.y.setContents(y);
90 } 52 }
91 53
92 54
93 /** 55 /**
94 * Listens to mouse move events to refresh the xy position. 56 * Listens to mouse move events to refresh the xy position.
106 * 68 *
107 * @param x The x part of the pixel. 69 * @param x The x part of the pixel.
108 * @param y The y part of the pixel. 70 * @param y The y part of the pixel.
109 */ 71 */
110 public void updateMousePosition(double x, double y) { 72 public void updateMousePosition(double x, double y) {
111 Transform2D transformer = chartTab.getTransformer(); 73 int transformerCount = chartTab.getTransformerCount();
112
113 if (transformer == null) {
114 return;
115 }
116 74
117 Canvas chart = chartTab.getChartPanel(); 75 Canvas chart = chartTab.getChartPanel();
118 int xOffset = chart.getPageLeft(); 76 int xOffset = chart.getPageLeft();
119 int yOffset = chart.getPageTop(); 77 int yOffset = chart.getPageTop();
120 78
121 x = x - xOffset; 79 x = x - xOffset;
122 y = y - yOffset; 80 y = y - yOffset;
123 81
124 double[] xy = transformer.transform(x,y); 82 // Create Layout for x coordinates.
125 String[] xyStr = transformer.format(new Number[] { 83 if (xLayout == null){
126 new Double(xy[0]), new Double(xy[1]) }); 84 Label xDesc = new Label("Position: X = ");
85 Label xLabel = new Label();
86 xLayout = new HLayout();
87 xLayout.setWidth(125);
88 xLayout.addMember(xDesc);
89 xLayout.addMember(xLabel);
90 xDesc.setWidth(70);
91 xLabel.setWidth(55);
92 addMember(xLayout);
93 }
127 94
128 setX(xyStr[0]); 95 ArrayList<String> yCoordinates = new ArrayList<String>();
129 setY(xyStr[1]); 96 String xCoordinate = "";
97 for (int i = 0; i < transformerCount; i++) {
98 HLayout yLayout = null;
99 // If no layout exists for this y axis, create one.
100 // else use the existing one.
101 if (yLayouts.size() <= i) {
102 Label yDesc = new Label("Y" + (i+1) + " = ");
103 Label yLabel = new Label();
104 yLayout = new HLayout();
105 yLayout.setWidth(80);
106 yLayout.addMember(yDesc);
107 yLayout.addMember(yLabel);
108 yDesc.setWidth(30);
109 yLabel.setWidth(50);
110 addMember(yLayout);
111 yLayouts.add(i, yLayout);
112 }
113 else {
114 yLayout = yLayouts.get(i);
115 }
116
117 Transform2D transformer = chartTab.getTransformer(i);
118
119 if (transformer == null) {
120 return;
121 }
122
123 // Get the label for the coordinates.
124 Canvas xLabel = xLayout.getMember(1);
125 Canvas yLabel = yLayout.getMember(1);
126
127 double[] xy = transformer.transform(x,y);
128 String[] xyStr = transformer.format(new Number[] {
129 new Double(xy[0]), new Double(xy[1]) });
130 // Set the coordinates.
131 xLabel.setContents(xyStr[0]);
132 yLabel.setContents(xyStr[1]);
133 }
134
135 // Remove y coordinates.
136 if (yLayouts.size() > transformerCount) {
137 for (int i = yLayouts.size() - 1; i >= transformerCount; i--) {
138 removeMember(yLayouts.get(i));
139 yLayouts.remove(i);
140 }
141 }
130 } 142 }
131 } 143 }
132 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 144 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org