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