forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathContextMenusOnShapes.java
More file actions
275 lines (227 loc) · 10.6 KB
/
ContextMenusOnShapes.java
File metadata and controls
275 lines (227 loc) · 10.6 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
/*
* 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.WorldWind;
import gov.nasa.worldwind.avlist.*;
import gov.nasa.worldwind.event.*;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.RenderableLayer;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwindx.applications.worldwindow.util.Util;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
/**
* Illustrates how to attach context (popup) menus to shapes. The example creates several <code>{@link
* PointPlacemark}s</code> and assigns each of them a context-menu definition. When the user presses the right mouse
* button while the cursor is on a placemark, the placemark's context menu is shown and the user may select an item in
* it.
*
* @author tag
* @version $Id: ContextMenusOnShapes.java 2109 2014-06-30 16:52:38Z tgaskins $
*/
public class ContextMenusOnShapes extends ApplicationTemplate {
/**
* The Controller listens for selection events and either highlights a selected item or shows its context menu.
*/
protected static class ContextMenuController implements SelectListener {
protected PointPlacemark lastPickedPlacemark = null;
@Override
public void selected(SelectEvent event) {
try {
if (event.getEventAction().equals(SelectEvent.ROLLOVER)) {
highlight(event, event.getTopObject());
} else if (event.getEventAction().equals(SelectEvent.RIGHT_PRESS)) // Could do RIGHT_CLICK instead
{
showContextMenu(event);
}
} catch (Exception e) {
Util.getLogger().warning(e.getMessage() != null ? e.getMessage() : e.toString());
}
}
protected void highlight(SelectEvent event, Object o) {
if (this.lastPickedPlacemark == o) {
return; // same thing selected
}
// Turn off highlight if on.
if (this.lastPickedPlacemark != null) {
this.lastPickedPlacemark.setHighlighted(false);
this.lastPickedPlacemark = null;
}
// Turn on highlight if object selected.
if (o != null && o instanceof PointPlacemark) {
this.lastPickedPlacemark = (PointPlacemark) o;
this.lastPickedPlacemark.setHighlighted(true);
}
}
protected void showContextMenu(SelectEvent event) {
if (!(event.getTopObject() instanceof PointPlacemark)) {
return;
}
// See if the top picked object has context-menu info defined. Show the menu if it does.
Object o = event.getTopObject();
if (o instanceof AVList) // Uses an AVList in order to be applicable to all shapes.
{
AVList params = (AVList) o;
ContextMenuInfo menuInfo = (ContextMenuInfo) params.getValue(ContextMenu.CONTEXT_MENU_INFO);
if (menuInfo == null) {
return;
}
if (!(event.getSource() instanceof Component)) {
return;
}
ContextMenu menu = new ContextMenu((Component) event.getSource(), menuInfo);
menu.show(event.getAwtMousePt());
}
}
}
/**
* The ContextMenu class implements the context menu.
*/
protected static class ContextMenu {
public static final String CONTEXT_MENU_INFO = "ContextMenuInfo";
protected ContextMenuInfo ctxMenuInfo;
protected Component sourceComponent;
protected JMenuItem menuTitleItem;
protected ArrayList<JMenuItem> menuItems = new ArrayList<>();
public ContextMenu(Component sourceComponent, ContextMenuInfo contextMenuInfo) {
this.sourceComponent = sourceComponent;
this.ctxMenuInfo = contextMenuInfo;
this.makeMenuTitle();
this.makeMenuItems();
}
protected void makeMenuTitle() {
this.menuTitleItem = new JMenuItem(this.ctxMenuInfo.menuTitle);
}
protected void makeMenuItems() {
for (ContextMenuItemInfo itemInfo : this.ctxMenuInfo.menuItems) {
this.menuItems.add(new JMenuItem(new ContextMenuItemAction(itemInfo)));
}
}
public void show(final Point screenPt) {
JPopupMenu popup = new JPopupMenu();
popup.add(this.menuTitleItem);
popup.addSeparator();
for (JMenuItem subMenu : this.menuItems) {
popup.add(subMenu);
}
popup.show(sourceComponent, (int)screenPt.getX(), (int)screenPt.getY());
}
}
/**
* The ContextMenuInfo class specifies the contents of the context menu.
*/
protected static class ContextMenuInfo {
protected String menuTitle;
protected ContextMenuItemInfo[] menuItems;
public ContextMenuInfo(String title, ContextMenuItemInfo[] menuItems) {
this.menuTitle = title;
this.menuItems = menuItems;
}
}
/**
* The ContextMenuItemInfo class specifies the contents of one entry in the context menu.
*/
protected static class ContextMenuItemInfo {
protected String displayString;
public ContextMenuItemInfo(String displayString) {
this.displayString = displayString;
}
}
/**
* The ContextMenuItemAction responds to user selection of a context menu item.
*/
public static class ContextMenuItemAction extends AbstractAction {
protected ContextMenuItemInfo itemInfo;
public ContextMenuItemAction(ContextMenuItemInfo itemInfo) {
super(itemInfo.displayString);
this.itemInfo = itemInfo;
}
@Override
public void actionPerformed(ActionEvent event) {
System.out.println(this.itemInfo.displayString); // Replace with application's menu-item response.
}
}
// The code below makes and displays some placemarks. The context menu info for each placemark is also specified.
public static class AppFrame extends ApplicationTemplate.AppFrame {
public AppFrame() {
RenderableLayer layer = new RenderableLayer();
// Create and set an attribute bundle.
PointPlacemarkAttributes attrs = new PointPlacemarkAttributes();
attrs.setAntiAliasHint(WorldWind.ANTIALIAS_FASTEST);
attrs.setLineMaterial(Material.WHITE);
attrs.setLineWidth(2d);
attrs.setImageAddress("images/pushpins/push-pin-yellow.png");
attrs.setScale(0.6);
attrs.setImageOffset(new Offset(19d, 11d, AVKey.PIXELS, AVKey.PIXELS));
PointPlacemarkAttributes highlightAttrs = new PointPlacemarkAttributes(attrs);
highlightAttrs.setScale(0.7);
ContextMenuItemInfo[] itemActionNames = new ContextMenuItemInfo[]{
new ContextMenuItemInfo("Do This"),
new ContextMenuItemInfo("Do That"),
new ContextMenuItemInfo("Do the Other Thing"),};
PointPlacemark pp = new PointPlacemark(Position.fromDegrees(28, -102, 1e4));
pp.setAttributes(attrs);
pp.setHighlightAttributes(highlightAttrs);
pp.setAltitudeMode(WorldWind.CLAMP_TO_GROUND);
pp.setValue(ContextMenu.CONTEXT_MENU_INFO, new ContextMenuInfo("Placemark A", itemActionNames));
layer.addRenderable(pp);
pp = new PointPlacemark(Position.fromDegrees(29, -104, 2e4));
pp.setAttributes(attrs);
pp.setHighlightAttributes(highlightAttrs);
pp.setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);
pp.setValue(ContextMenu.CONTEXT_MENU_INFO, new ContextMenuInfo("Placemark B", itemActionNames));
layer.addRenderable(pp);
pp = new PointPlacemark(Position.fromDegrees(30, -104.5, 2e4));
pp.setAttributes(attrs);
pp.setHighlightAttributes(highlightAttrs);
pp.setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);
pp.setValue(ContextMenu.CONTEXT_MENU_INFO, new ContextMenuInfo("Placemark C", itemActionNames));
layer.addRenderable(pp);
pp = new PointPlacemark(Position.fromDegrees(28, -104.5, 2e4));
pp.setAttributes(attrs);
pp.setHighlightAttributes(highlightAttrs);
pp.setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);
pp.setValue(ContextMenu.CONTEXT_MENU_INFO, new ContextMenuInfo("Placemark D", itemActionNames));
layer.addRenderable(pp);
// Create a placemark that uses all default values.
pp = new PointPlacemark(Position.fromDegrees(30, -103.5, 2e3));
pp.setValue(ContextMenu.CONTEXT_MENU_INFO, new ContextMenuInfo("Placemark E", itemActionNames));
layer.addRenderable(pp);
// Add the layer to the model.
insertBeforeCompass(getWwd(), layer);
// Set up the context menu
ContextMenuController contextMenuController = new ContextMenuController();
getWwd().addSelectListener(contextMenuController);
}
}
public static void main(String[] args) {
ApplicationTemplate.start("WorldWind Context Menus on Shapes", AppFrame.class);
}
}