comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/map/MeasureControl.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/client/ui/map/MeasureControl.java@a1ff911e8365
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.client.ui.map;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.i18n.client.NumberFormat;
5
6 import com.smartgwt.client.types.SelectionType;
7 import com.smartgwt.client.widgets.ImgButton;
8 import com.smartgwt.client.widgets.Label;
9 import com.smartgwt.client.widgets.events.ClickEvent;
10 import com.smartgwt.client.widgets.events.ClickHandler;
11 import com.smartgwt.client.widgets.layout.HLayout;
12
13 import org.gwtopenmaps.openlayers.client.control.Measure;
14 import org.gwtopenmaps.openlayers.client.event.MeasureEvent;
15 import org.gwtopenmaps.openlayers.client.event.MeasureListener;
16 import org.gwtopenmaps.openlayers.client.event.MeasurePartialListener;
17 import org.gwtopenmaps.openlayers.client.handler.PathHandler;
18 import org.gwtopenmaps.openlayers.client.handler.PolygonHandler;
19
20 import de.intevation.flys.client.client.FLYSConstants;
21 import de.intevation.flys.client.client.utils.EnableDisableCmd;
22
23
24 public class MeasureControl extends HLayout {
25
26 public static final String NUMBER_FORMAT_PATTERN = "#.##";
27
28 public static final String AREA_UNIT = "ha";
29 public static final int AREA_FACTOR_M = 10000;
30 public static final int AREA_FACTOR_KM = 100;
31
32 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
33
34 protected FloodMap floodMap;
35
36 protected Measure measureLine;
37 protected Measure measurePolygon;
38
39 protected ImgButton measureLineButton;
40 protected ImgButton measurePolyButton;
41 protected Label label;
42
43 protected NumberFormat formatter;
44
45
46 public MeasureControl(FloodMap floodMap, EnableDisableCmd cmd) {
47 this.floodMap = floodMap;
48
49 measureLineButton = createMeasureLineControl(cmd);
50 measurePolyButton = createMeasurePolyControl(cmd);
51
52 formatter = NumberFormat.getFormat(NUMBER_FORMAT_PATTERN);
53
54 label = new Label();
55
56 initLayout();
57 }
58
59
60 protected void initLayout() {
61 setWidth(100);
62 setMembersMargin(2);
63
64 label.setWidth(75);
65
66 addMember(measureLineButton);
67 addMember(measurePolyButton);
68 addMember(label);
69 }
70
71
72 protected ImgButton createMeasureLineControl(final EnableDisableCmd cmd) {
73 measureLine = new Measure(new PathHandler());
74 measureLine.setPersist(true);
75 measureLine.addMeasureListener(new MeasureListener() {
76 public void onMeasure(MeasureEvent e) {
77 updateMeasure(e.getMeasure(), e.getUnits());
78 }
79 });
80 measureLine.addMeasurePartialListener(new MeasurePartialListener() {
81 public void onMeasurePartial(MeasureEvent e) {
82 updateMeasure(e.getMeasure(), e.getUnits());
83 }
84 });
85
86 floodMap.getMap().addControl(measureLine);
87
88 final ImgButton btn = new ImgButton();
89 String baseUrl = GWT.getHostPageBaseURL();
90 btn.setSrc(baseUrl + MSG.measureLine());
91 btn.setActionType(SelectionType.CHECKBOX);
92 btn.setSize(20);
93 btn.setShowRollOver(false);
94 btn.setShowRollOverIcon(false);
95 btn.setSelected(false);
96 btn.setTooltip(MSG.measureDistance());
97 btn.addClickHandler(new ClickHandler() {
98 public void onClick(ClickEvent e) {
99 if (btn.isSelected()) {
100 cmd.enable();
101 activateMeasurePolygon(false);
102 activateMeasureLine(true);
103 }
104 else {
105 cmd.disable();
106 activateMeasureLine(false);
107 }
108 }
109 });
110
111 return btn;
112 }
113
114
115 protected ImgButton createMeasurePolyControl(final EnableDisableCmd cmd) {
116 measurePolygon = new Measure(new PolygonHandler());
117 measurePolygon.setPersist(true);
118 measurePolygon.addMeasureListener(new MeasureListener() {
119 public void onMeasure(MeasureEvent e) {
120 updateMeasureArea(e.getMeasure(), e.getUnits());
121 }
122 });
123 measurePolygon.addMeasurePartialListener(new MeasurePartialListener() {
124 public void onMeasurePartial(MeasureEvent e) {
125 updateMeasureArea(e.getMeasure(), e.getUnits());
126 }
127 });
128
129 floodMap.getMap().addControl(measurePolygon);
130
131 final ImgButton btn = new ImgButton();
132 String baseUrl = GWT.getHostPageBaseURL();
133 btn.setSrc(baseUrl + MSG.measurePolygon());
134 btn.setActionType(SelectionType.CHECKBOX);
135 btn.setSize(20);
136 btn.setShowRollOver(false);
137 btn.setShowRollOverIcon(false);
138 btn.setSelected(false);
139 btn.setTooltip(MSG.measureArea());
140 btn.addClickHandler(new ClickHandler() {
141 public void onClick(ClickEvent e) {
142 if (btn.isSelected()) {
143 cmd.enable();
144 activateMeasureLine(false);
145 activateMeasurePolygon(true);
146 }
147 else {
148 cmd.disable();
149 activateMeasurePolygon(false);
150 }
151 }
152 });
153
154 return btn;
155 }
156
157
158 protected void clearMeasure() {
159 label.setContents("");
160 }
161
162
163 protected void updateMeasure(float value, String unit) {
164 label.setContents(formatter.format(value) + " " + unit);
165 }
166
167
168 protected void updateMeasureArea(float value, String unit) {
169 float ha = value;
170 String ha_unit = unit;
171
172 if (unit.equals("m")) {
173 ha = (float) value / AREA_FACTOR_M;
174 ha_unit = AREA_UNIT;
175 }
176 else if (unit.equals("km")) {
177 ha = (float) value * AREA_FACTOR_KM;
178 ha_unit = AREA_UNIT;
179 }
180
181 label.setContents(formatter.format(ha) + " " + ha_unit);
182 }
183
184
185 public void activate(boolean activate) {
186 if (!activate) {
187 clearMeasure();
188 activateMeasureLine(activate);
189 activateMeasurePolygon(activate);
190 }
191 }
192
193
194 protected void activateMeasureLine(boolean activate) {
195 if (activate) {
196 clearMeasure();
197 measureLineButton.select();
198 measureLine.activate();
199 }
200 else {
201 measureLineButton.deselect();
202 measureLine.deactivate();
203 }
204 }
205
206
207 protected void activateMeasurePolygon(boolean activate) {
208 if (activate) {
209 clearMeasure();
210 measurePolyButton.select();
211 measurePolygon.activate();
212 }
213 else {
214 measurePolyButton.deselect();
215 measurePolygon.deactivate();
216 }
217 }
218 }
219 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org