comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/MousePositionPanel.java @ 538:75df57220104

Adapted the MousePositionPanel to use the Transform2D object for computing the chart coordinates from image coordinates. flys-client/trunk@2036 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 01 Jun 2011 09:03:18 +0000
parents e08777967bad
children fea93eebd2fa
comparison
equal deleted inserted replaced
537:d2c37ba78feb 538:75df57220104
1 package de.intevation.flys.client.client.ui.chart; 1 package de.intevation.flys.client.client.ui.chart;
2 2
3 import com.smartgwt.client.core.Rectangle; 3 import com.google.gwt.i18n.client.NumberFormat;
4
4 import com.smartgwt.client.types.Alignment; 5 import com.smartgwt.client.types.Alignment;
5 import com.smartgwt.client.widgets.Canvas; 6 import com.smartgwt.client.widgets.Canvas;
6 import com.smartgwt.client.widgets.Label; 7 import com.smartgwt.client.widgets.Label;
7 import com.smartgwt.client.widgets.layout.HLayout; 8 import com.smartgwt.client.widgets.layout.HLayout;
8 import com.smartgwt.client.widgets.events.MouseMoveEvent; 9 import com.smartgwt.client.widgets.events.MouseMoveEvent;
9 import com.smartgwt.client.widgets.events.MouseMoveHandler; 10 import com.smartgwt.client.widgets.events.MouseMoveHandler;
10 import com.smartgwt.client.widgets.events.ResizedEvent; 11
11 import com.smartgwt.client.widgets.events.ResizedHandler; 12 import de.intevation.flys.client.shared.Transform2D;
12 13
13 14
14 /** 15 /**
15 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
16 */ 17 */
17 public class MousePositionPanel 18 public class MousePositionPanel extends HLayout implements MouseMoveHandler {
18 extends HLayout 19
19 implements MouseMoveHandler, ResizedHandler 20 protected ChartOutputTab chartTab;
20 { 21
21 protected Canvas window; 22 protected NumberFormat nf;
22 protected Rectangle viewport;
23 23
24 protected Label x; 24 protected Label x;
25 protected Label y; 25 protected Label y;
26 26
27 27
28 public MousePositionPanel(Canvas window, Rectangle viewport) { 28 public MousePositionPanel(ChartOutputTab chartTab) {
29 super(); 29 super();
30 30
31 this.window = window; 31 this.chartTab = chartTab;
32 this.viewport = viewport;
33 32
34 x = new Label(); 33 x = new Label();
35 y = new Label(); 34 y = new Label();
35
36 nf = NumberFormat.getDecimalFormat();
37
38 chartTab.getChartPanel().addMouseMoveHandler(this);
36 39
37 initLayout(); 40 initLayout();
38 } 41 }
39 42
40 43
59 addMember(y); 62 addMember(y);
60 } 63 }
61 64
62 65
63 /** 66 /**
64 * Computes the x|y position (in chart coordinates) and updates the position 67 * /Updates the X value displayed in the <i>x</i> label.
65 * in the UI.
66 * 68 *
67 * @param event The MouseMoveEvent. 69 * @param x the new x value.
68 */ 70 */
69 public void onMouseMove(MouseMoveEvent event) { 71 public void setX(double x) {
70 int posX = event.getX(); 72 this.x.setContents(nf.format(x));
71 int posY = event.getY();
72
73 if (!validX(posX) || !validY(posY)) {
74 return;
75 }
76
77 x.setContents(String.valueOf(getXPosition(posX)));
78 y.setContents(String.valueOf(getYPosition(posY)));
79 }
80
81
82 public void onResized(ResizedEvent event) {
83 } 73 }
84 74
85 75
86 /** 76 /**
87 * Computes the left coordinate of the chart. 77 * /Updates the Y value displayed in the <i>y</i> label.
88 * 78 *
89 * @return the left coordinate of the chart. 79 * @param y the new y value.
90 */ 80 */
91 protected int computeMinX() { 81 public void setY(double y) {
92 return window.getPageLeft() + viewport.getLeft(); 82 this.y.setContents(nf.format(y));
93 } 83 }
94 84
95 85
96 /** 86 /**
97 * Computes the right coordinate of the chart. 87 * Listens to mouse move events to refresh the xy position.
98 * 88 *
99 * @return the right coordinate of the chart. 89 * @param event The move event.
100 */ 90 */
101 protected int computeMaxX() { 91 public void onMouseMove(MouseMoveEvent event) {
102 return window.getPageLeft() + window.getWidth() - viewport.getWidth(); 92 updateMousePosition(event.getX(), event.getY());
103 } 93 }
104 94
105 95
106 /** 96 /**
107 * Computes the upper Y coordinate of the chart. 97 * This method takes pixel coordinates, transforms those values into chart
98 * coordinates using the Transform2D class and updates the mouse position.
108 * 99 *
109 * @return the lower Y coordinate of the chart. 100 * @param x The x part of the pixel.
101 * @param y The y part of the pixel.
110 */ 102 */
111 protected int computeMinY() { 103 public void updateMousePosition(double x, double y) {
112 return window.getPageTop() + viewport.getTop(); 104 Transform2D transformer = chartTab.getTransformer();
113 }
114 105
106 if (transformer == null) {
107 return;
108 }
115 109
116 /** 110 Canvas chart = chartTab.getChartPanel();
117 * Computes the lower Y coordinate of the chart. 111 int xOffset = chart.getPageLeft();
118 * 112 int yOffset = chart.getPageTop();
119 * @return the lower Y coordinate of the chart.
120 */
121 protected int computeMaxY() {
122 return window.getPageBottom() - viewport.getHeight();
123 }
124 113
114 x = x - xOffset;
115 y = y - yOffset;
125 116
126 /** 117 double[] xy = transformer.transform(x,y);
127 * Determines if the x value is in the valid range of the chart.
128 *
129 * @param x A pixel.
130 *
131 * @return true if the x position is valid, otherwise false.
132 */
133 protected boolean validX(int x) {
134 return (x > computeMinX() && x < computeMaxX());
135 }
136 118
137 119 setX(xy[0]);
138 /** 120 setY(xy[1]);
139 * Determines if the y value is in the valid range of the chart.
140 *
141 * @param y A pixel.
142 *
143 * @return true if the y position is valid, otherwise false.
144 */
145 protected boolean validY(int y) {
146 return (y > computeMinY() && y < computeMaxY());
147 }
148
149
150 /**
151 * Computes the X coordinate in a chart based on the current mouse position.
152 *
153 * @param pixel A pixel on the screen.
154 *
155 * @return the computed X value.
156 */
157 protected double getXPosition(int pixel) {
158 // TODO Compute the x relative to the chart range
159
160 return pixel - computeMinX();
161 }
162
163
164 /**
165 * Computes the Y coordinate in a chart based on the current mouse position.
166 *
167 * @param pixel A pixel on the screen.
168 *
169 * @return the computed Y value.
170 */
171 protected double getYPosition(int pixel) {
172 // TODO Compute the y relative to the chart range
173
174 return pixel - computeMinY();
175 } 121 }
176 } 122 }
177 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 123 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org