1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 |
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.GraphicsEnvironment;
|
---|
9 | import java.awt.MenuComponent;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Iterator;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Set;
|
---|
16 | import java.util.TreeSet;
|
---|
17 |
|
---|
18 | import javax.swing.Action;
|
---|
19 | import javax.swing.JComponent;
|
---|
20 | import javax.swing.JMenu;
|
---|
21 | import javax.swing.JMenuItem;
|
---|
22 | import javax.swing.JPopupMenu;
|
---|
23 | import javax.swing.MenuElement;
|
---|
24 | import javax.swing.event.MenuEvent;
|
---|
25 | import javax.swing.event.MenuListener;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.actions.AddImageryLayerAction;
|
---|
29 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
30 | import org.openstreetmap.josm.actions.MapRectifierWMSmenuAction;
|
---|
31 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
32 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
33 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
34 | import org.openstreetmap.josm.data.imagery.Shape;
|
---|
35 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
36 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
37 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
38 | import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
|
---|
39 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Imagery menu, holding entries for imagery preferences, offset actions and dynamic imagery entries
|
---|
43 | * depending on current maview coordinates.
|
---|
44 | * @since 3737
|
---|
45 | */
|
---|
46 | public class ImageryMenu extends JMenu implements LayerChangeListener {
|
---|
47 |
|
---|
48 | private Action offsetAction = new JosmAction(
|
---|
49 | tr("Imagery offset"), "mapmode/adjustimg", tr("Adjust imagery offset"), null, false, false) {
|
---|
50 | {
|
---|
51 | putValue("toolbar", "imagery-offset");
|
---|
52 | Main.toolbar.register(this);
|
---|
53 | }
|
---|
54 | @Override
|
---|
55 | public void actionPerformed(ActionEvent e) {
|
---|
56 | Collection<ImageryLayer> layers = Main.map.mapView.getLayersOfType(ImageryLayer.class);
|
---|
57 | if (layers.isEmpty()) {
|
---|
58 | setEnabled(false);
|
---|
59 | return;
|
---|
60 | }
|
---|
61 | Component source = null;
|
---|
62 | if (e.getSource() instanceof Component) {
|
---|
63 | source = (Component)e.getSource();
|
---|
64 | }
|
---|
65 | JPopupMenu popup = new JPopupMenu();
|
---|
66 | if (layers.size() == 1) {
|
---|
67 | JComponent c = layers.iterator().next().getOffsetMenuItem(popup);
|
---|
68 | if (c instanceof JMenuItem) {
|
---|
69 | ((JMenuItem) c).getAction().actionPerformed(e);
|
---|
70 | } else {
|
---|
71 | if (source == null) return;
|
---|
72 | popup.show(source, source.getWidth()/2, source.getHeight()/2);
|
---|
73 | }
|
---|
74 | return;
|
---|
75 | }
|
---|
76 | if (source == null) return;
|
---|
77 | for (ImageryLayer layer : layers) {
|
---|
78 | JMenuItem layerMenu = layer.getOffsetMenuItem();
|
---|
79 | layerMenu.setText(layer.getName());
|
---|
80 | layerMenu.setIcon(layer.getIcon());
|
---|
81 | popup.add(layerMenu);
|
---|
82 | }
|
---|
83 | popup.show(source, source.getWidth()/2, source.getHeight()/2);
|
---|
84 | }
|
---|
85 | };
|
---|
86 |
|
---|
87 | private final JMenuItem singleOffset = new JMenuItem(offsetAction);
|
---|
88 | private JMenuItem offsetMenuItem = singleOffset;
|
---|
89 | private final MapRectifierWMSmenuAction rectaction = new MapRectifierWMSmenuAction();
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Constructs a new {@code ImageryMenu}.
|
---|
93 | * @param subMenu submenu in that contains plugin-managed additional imagery layers
|
---|
94 | */
|
---|
95 | public ImageryMenu(JMenu subMenu) {
|
---|
96 | super(tr("Imagery"));
|
---|
97 | setupMenuScroller();
|
---|
98 | MapView.addLayerChangeListener(this);
|
---|
99 | // build dynamically
|
---|
100 | addMenuListener(new MenuListener() {
|
---|
101 | @Override
|
---|
102 | public void menuSelected(MenuEvent e) {
|
---|
103 | refreshImageryMenu();
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void menuDeselected(MenuEvent e) {
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public void menuCanceled(MenuEvent e) {
|
---|
112 | }
|
---|
113 | });
|
---|
114 | MainMenu.add(subMenu, rectaction);
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void setupMenuScroller() {
|
---|
118 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
119 | MenuScroller.setScrollerFor(this, 150, 2);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Refresh imagery menu.
|
---|
125 | *
|
---|
126 | * Outside this class only called in {@link ImageryPreference#initialize()}.
|
---|
127 | * (In order to have actions ready for the toolbar, see #8446.)
|
---|
128 | */
|
---|
129 | public void refreshImageryMenu() {
|
---|
130 | removeDynamicItems();
|
---|
131 |
|
---|
132 | addDynamic(offsetMenuItem);
|
---|
133 | addDynamicSeparator();
|
---|
134 |
|
---|
135 | // for each configured ImageryInfo, add a menu entry.
|
---|
136 | for (final ImageryInfo u : ImageryLayerInfo.instance.getLayers()) {
|
---|
137 | addDynamic(new AddImageryLayerAction(u));
|
---|
138 | }
|
---|
139 |
|
---|
140 | // list all imagery entries where the current map location
|
---|
141 | // is within the imagery bounds
|
---|
142 | if (Main.isDisplayingMapView()) {
|
---|
143 | MapView mv = Main.map.mapView;
|
---|
144 | LatLon pos = mv.getProjection().eastNorth2latlon(mv.getCenter());
|
---|
145 | final Set<ImageryInfo> inViewLayers = new TreeSet<>();
|
---|
146 |
|
---|
147 | for (ImageryInfo i : ImageryLayerInfo.instance.getDefaultLayers()) {
|
---|
148 | if (i.getBounds() != null && i.getBounds().contains(pos)) {
|
---|
149 | inViewLayers.add(i);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | // Do not suggest layers already in use
|
---|
153 | inViewLayers.removeAll(ImageryLayerInfo.instance.getLayers());
|
---|
154 | // For layers containing complex shapes, check that center is in one
|
---|
155 | // of its shapes (fix #7910)
|
---|
156 | for (Iterator<ImageryInfo> iti = inViewLayers.iterator(); iti.hasNext(); ) {
|
---|
157 | List<Shape> shapes = iti.next().getBounds().getShapes();
|
---|
158 | if (shapes != null && !shapes.isEmpty()) {
|
---|
159 | boolean found = false;
|
---|
160 | for (Iterator<Shape> its = shapes.iterator(); its.hasNext() && !found; ) {
|
---|
161 | found = its.next().contains(pos);
|
---|
162 | }
|
---|
163 | if (!found) {
|
---|
164 | iti.remove();
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | if (!inViewLayers.isEmpty()) {
|
---|
169 | addDynamicSeparator();
|
---|
170 | for (ImageryInfo i : inViewLayers) {
|
---|
171 | addDynamic(new AddImageryLayerAction(i));
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | addDynamicSeparator();
|
---|
177 | JMenu subMenu = Main.main.menu.imagerySubMenu;
|
---|
178 | int heightUnrolled = 30*(getItemCount()+subMenu.getItemCount());
|
---|
179 | if (heightUnrolled < Main.panel.getHeight()) {
|
---|
180 | // add all items of submenu if they will fit on screen
|
---|
181 | int n = subMenu.getItemCount();
|
---|
182 | for (int i=0; i<n; i++) {
|
---|
183 | addDynamic(subMenu.getItem(i).getAction());
|
---|
184 | }
|
---|
185 | } else {
|
---|
186 | // or add the submenu itself
|
---|
187 | addDynamic(subMenu);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | private JMenuItem getNewOffsetMenu(){
|
---|
192 | if (!Main.isDisplayingMapView()) {
|
---|
193 | offsetAction.setEnabled(false);
|
---|
194 | return singleOffset;
|
---|
195 | }
|
---|
196 | Collection<ImageryLayer> layers = Main.map.mapView.getLayersOfType(ImageryLayer.class);
|
---|
197 | if (layers.isEmpty()) {
|
---|
198 | offsetAction.setEnabled(false);
|
---|
199 | return singleOffset;
|
---|
200 | }
|
---|
201 | offsetAction.setEnabled(true);
|
---|
202 | JMenu newMenu = new JMenu(trc("layer","Offset"));
|
---|
203 | newMenu.setIcon(ImageProvider.get("mapmode", "adjustimg"));
|
---|
204 | newMenu.setAction(offsetAction);
|
---|
205 | if (layers.size() == 1)
|
---|
206 | return (JMenuItem)layers.iterator().next().getOffsetMenuItem(newMenu);
|
---|
207 | for (ImageryLayer layer : layers) {
|
---|
208 | JMenuItem layerMenu = layer.getOffsetMenuItem();
|
---|
209 | layerMenu.setText(layer.getName());
|
---|
210 | layerMenu.setIcon(layer.getIcon());
|
---|
211 | newMenu.add(layerMenu);
|
---|
212 | }
|
---|
213 | return newMenu;
|
---|
214 | }
|
---|
215 |
|
---|
216 | public void refreshOffsetMenu() {
|
---|
217 | offsetMenuItem = getNewOffsetMenu();
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | public void layerAdded(Layer newLayer) {
|
---|
226 | if (newLayer instanceof ImageryLayer) {
|
---|
227 | refreshOffsetMenu();
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | @Override
|
---|
232 | public void layerRemoved(Layer oldLayer) {
|
---|
233 | if (oldLayer instanceof ImageryLayer) {
|
---|
234 | refreshOffsetMenu();
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Collection to store temporary menu items. They will be deleted
|
---|
240 | * (and possibly recreated) when refreshImageryMenu() is called.
|
---|
241 | * @since 5803
|
---|
242 | */
|
---|
243 | private List <Object> dynamicItems = new ArrayList<>(20);
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Remove all the items in @field dynamicItems collection
|
---|
247 | * @since 5803
|
---|
248 | */
|
---|
249 | private void removeDynamicItems() {
|
---|
250 | for (Object item : dynamicItems) {
|
---|
251 | if (item instanceof JMenuItem) {
|
---|
252 | remove((JMenuItem)item);
|
---|
253 | }
|
---|
254 | if (item instanceof MenuComponent) {
|
---|
255 | remove((MenuComponent)item);
|
---|
256 | }
|
---|
257 | if (item instanceof Component) {
|
---|
258 | remove((Component)item);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | dynamicItems.clear();
|
---|
262 | }
|
---|
263 |
|
---|
264 | private void addDynamicSeparator() {
|
---|
265 | JPopupMenu.Separator s = new JPopupMenu.Separator();
|
---|
266 | dynamicItems.add(s);
|
---|
267 | add(s);
|
---|
268 | }
|
---|
269 |
|
---|
270 | private void addDynamic(Action a) {
|
---|
271 | dynamicItems.add( this.add(a) );
|
---|
272 | }
|
---|
273 |
|
---|
274 | private void addDynamic(JMenuItem it) {
|
---|
275 | dynamicItems.add( this.add(it) );
|
---|
276 | }
|
---|
277 | }
|
---|