forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLineBuilder.java
More file actions
378 lines (337 loc) · 14.7 KB
/
LineBuilder.java
File metadata and controls
378 lines (337 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright 2006-2009, 2017, 2020 United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASA World Wind Java (WWJ) platform is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* NASA World Wind Java (WWJ) also contains the following 3rd party Open Source
* software:
*
* Jackson Parser – Licensed under Apache 2.0
* GDAL – Licensed under MIT
* JOGL – Licensed under Berkeley Software Distribution (BSD)
* Gluegen – Licensed under Berkeley Software Distribution (BSD)
*
* A complete listing of 3rd Party software notices and licenses included in
* NASA World Wind Java (WWJ) can be found in the WorldWindJava-v2.2 3rd-party
* notices and licenses PDF found in code directory.
*/
package gov.nasa.worldwindx.examples;
import gov.nasa.worldwind.*;
import gov.nasa.worldwind.event.*;
import gov.nasa.worldwind.avlist.*;
import gov.nasa.worldwind.geom.*;
import gov.nasa.worldwind.layers.*;
import gov.nasa.worldwind.render.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
/**
* A utility class to interactively build a path. When armed, the class monitors mouse events and adds new positions to
* a path as the user identifies them. The interaction sequence for creating a line is as follows: <ul> <li> Arm the
* line builder by calling its {@link #setArmed(boolean)} method with an argument of true. </li> <li> Place the cursor
* at the first desired path position. Press and release mouse button one. </li> <li> Press button one near the next
* desired position, drag the mouse to the exact position, then release the button. The proposed line segment will echo
* while the mouse is dragged. Continue selecting new positions this way until the path contains all desired positions.
* </li> <li> Disarm the <code>LineBuilder</code> object by calling its {@link #setArmed(boolean)} method with an
* argument of false. </li> </ul>
* <p>
* While the line builder is armed, pressing and immediately releasing mouse button one while also pressing the control
* key (Ctl) removes the last position from the path. </p>
* <p>
* Mouse events the line builder acts on while armed are marked as consumed. These events are mouse pressed, released,
* clicked and dragged. These events are not acted on while the line builder is not armed. The builder can be
* continuously armed and rearmed to allow intervening maneuvering of the globe while building a path. A user can add
* positions, pause entry, maneuver the view, then continue entering positions. </p>
* <p>
* Arming and disarming the line builder does not change the contents or attributes of the line builder's layer. </p>
* <p>
* The path and a layer containing it may be specified when a <code>LineBuilder</code> is constructed. </p>
* <p>
* This class contains a <code>main</code> method implementing an example program illustrating use of
* <code>LineBuilder</code>. </p>
*
* @author tag
* @version $Id: LineBuilder.java 1171 2013-02-11 21:45:02Z dcollins $
*/
public class LineBuilder extends AVListImpl {
private final WorldWindow wwd;
private boolean armed = false;
private ArrayList<Position> positions = new ArrayList<>();
private final RenderableLayer layer;
private final Path line;
private boolean active = false;
/**
* Construct a new line builder using the specified path and layer and drawing events from the specified world
* window. Either or both the path and the layer may be null, in which case the necessary object is created.
*
* @param wwd the WorldWindow to draw events from.
* @param lineLayer the layer holding the path. May be null, in which case a new layer is created.
* @param path the path object to build. May be null, in which case a new path is created.
*/
public LineBuilder(final WorldWindow wwd, RenderableLayer lineLayer, Path path) {
this.wwd = wwd;
if (path != null) {
line = path;
} else {
this.line = new Path();
this.line.setSurfacePath(true);
}
this.layer = lineLayer != null ? lineLayer : new RenderableLayer();
this.layer.addRenderable(this.line);
this.wwd.getModel().getLayers().add(this.layer);
this.wwd.getInputHandler().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
if (armed && mouseEvent.getButton() == MouseEvent.BUTTON1) {
if (armed && (mouseEvent.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
if (!mouseEvent.isControlDown()) {
active = true;
addPosition();
}
}
mouseEvent.consume();
}
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
if (armed && mouseEvent.getButton() == MouseEvent.BUTTON1) {
if (positions.size() == 1) {
removePosition();
}
active = false;
mouseEvent.consume();
}
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
if (armed && mouseEvent.getButton() == MouseEvent.BUTTON1) {
if (mouseEvent.isControlDown()) {
removePosition();
}
mouseEvent.consume();
}
}
});
this.wwd.getInputHandler().addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent mouseEvent) {
if (armed && (mouseEvent.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
// Don't update the path here because the wwd current cursor position will not
// have been updated to reflect the current mouse position. Wait to update in the
// position listener, but consume the event so the view doesn't respond to it.
if (active) {
mouseEvent.consume();
}
}
}
});
this.wwd.addPositionListener((PositionEvent event) -> {
if (!active) {
return;
}
if (positions.size() == 1) {
addPosition();
} else {
replacePosition();
}
});
}
/**
* Returns the layer holding the path being created.
*
* @return the layer containing the path.
*/
public RenderableLayer getLayer() {
return this.layer;
}
/**
* Returns the layer currently used to display the path.
*
* @return the layer holding the path.
*/
public Path getLine() {
return this.line;
}
/**
* Removes all positions from the path.
*/
public void clear() {
while (this.positions.size() > 0) {
this.removePosition();
}
}
/**
* Identifies whether the line builder is armed.
*
* @return true if armed, false if not armed.
*/
public boolean isArmed() {
return this.armed;
}
/**
* Arms and disarms the line builder. When armed, the line builder monitors user input and builds the path in
* response to the actions mentioned in the overview above. When disarmed, the line builder ignores all user input.
*
* @param armed true to arm the line builder, false to disarm it.
*/
public void setArmed(boolean armed) {
this.armed = armed;
}
private void addPosition() {
Position curPos = this.wwd.getCurrentPosition();
if (curPos == null) {
return;
}
this.positions.add(curPos);
this.line.setPositions(this.positions);
this.firePropertyChange("LineBuilder.AddPosition", null, curPos);
this.wwd.redraw();
}
private void replacePosition() {
Position curPos = this.wwd.getCurrentPosition();
if (curPos == null) {
return;
}
int index = this.positions.size() - 1;
if (index < 0) {
index = 0;
}
Position currentLastPosition = this.positions.get(index);
this.positions.set(index, curPos);
this.line.setPositions(this.positions);
this.firePropertyChange("LineBuilder.ReplacePosition", currentLastPosition, curPos);
this.wwd.redraw();
}
private void removePosition() {
if (this.positions.isEmpty()) {
return;
}
Position currentLastPosition = this.positions.get(this.positions.size() - 1);
this.positions.remove(this.positions.size() - 1);
this.line.setPositions(this.positions);
this.firePropertyChange("LineBuilder.RemovePosition", currentLastPosition, null);
this.wwd.redraw();
}
// ===================== Control Panel ======================= //
// The following code is an example program illustrating LineBuilder usage. It is not required by the
// LineBuilder class, itself.
private static class LinePanel extends JPanel {
private final WorldWindow wwd;
private final LineBuilder lineBuilder;
private JButton newButton;
private JButton pauseButton;
private JButton endButton;
private JLabel[] pointLabels;
public LinePanel(WorldWindow wwd, LineBuilder lineBuilder) {
super(new BorderLayout());
this.wwd = wwd;
this.lineBuilder = lineBuilder;
this.makePanel(new Dimension(200, 400));
lineBuilder.addPropertyChangeListener((PropertyChangeEvent propertyChangeEvent) -> {
fillPointsPanel();
});
}
private void makePanel(Dimension size) {
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 5));
newButton = new JButton("New");
newButton.addActionListener((ActionEvent actionEvent) -> {
lineBuilder.clear();
lineBuilder.setArmed(true);
pauseButton.setText("Pause");
pauseButton.setEnabled(true);
endButton.setEnabled(true);
newButton.setEnabled(false);
((Component) wwd).setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
});
buttonPanel.add(newButton);
newButton.setEnabled(true);
pauseButton = new JButton("Pause");
pauseButton.addActionListener((ActionEvent actionEvent) -> {
lineBuilder.setArmed(!lineBuilder.isArmed());
pauseButton.setText(!lineBuilder.isArmed() ? "Resume" : "Pause");
((Component) wwd).setCursor(Cursor.getDefaultCursor());
});
buttonPanel.add(pauseButton);
pauseButton.setEnabled(false);
endButton = new JButton("End");
endButton.addActionListener((ActionEvent actionEvent) -> {
lineBuilder.setArmed(false);
newButton.setEnabled(true);
pauseButton.setEnabled(false);
pauseButton.setText("Pause");
endButton.setEnabled(false);
((Component) wwd).setCursor(Cursor.getDefaultCursor());
});
buttonPanel.add(endButton);
endButton.setEnabled(false);
JPanel pointPanel = new JPanel(new GridLayout(0, 1, 0, 10));
pointPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.pointLabels = new JLabel[20];
for (int i = 0; i < this.pointLabels.length; i++) {
this.pointLabels[i] = new JLabel("");
pointPanel.add(this.pointLabels[i]);
}
// Put the point panel in a container to prevent scroll panel from stretching the vertical spacing.
JPanel dummyPanel = new JPanel(new BorderLayout());
dummyPanel.add(pointPanel, BorderLayout.NORTH);
// Put the point panel in a scroll bar.
JScrollPane scrollPane = new JScrollPane(dummyPanel);
scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
if (size != null) {
scrollPane.setPreferredSize(size);
}
// Add the buttons, scroll bar and inner panel to a titled panel that will resize with the main window.
JPanel outerPanel = new JPanel(new BorderLayout());
outerPanel.setBorder(
new CompoundBorder(BorderFactory.createEmptyBorder(9, 9, 9, 9), new TitledBorder("Line")));
outerPanel.setToolTipText("Line control and info");
outerPanel.add(buttonPanel, BorderLayout.NORTH);
outerPanel.add(scrollPane, BorderLayout.CENTER);
this.add(outerPanel, BorderLayout.CENTER);
}
private void fillPointsPanel() {
int i = 0;
for (Position pos : lineBuilder.getLine().getPositions()) {
if (i == this.pointLabels.length) {
break;
}
String las = String.format("Lat %7.4f\u00B0", pos.getLatitude().getDegrees());
String los = String.format("Lon %7.4f\u00B0", pos.getLongitude().getDegrees());
pointLabels[i++].setText(las + " " + los);
}
for (; i < this.pointLabels.length; i++) {
pointLabels[i++].setText("");
}
}
}
/**
* Marked as deprecated to keep it out of the javadoc.
*
* @deprecated
*/
@Deprecated
public static class AppFrame extends ApplicationTemplate.AppFrame {
public AppFrame() {
super(true, false, false);
LineBuilder lineBuilder = new LineBuilder(this.getWwd(), null, null);
this.getContentPane().add(new LinePanel(this.getWwd(), lineBuilder), BorderLayout.WEST);
}
}
public static void main(String[] args) {
//noinspection deprecation
ApplicationTemplate.start("WorldWind Line Builder", LineBuilder.AppFrame.class);
}
}