1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Container;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.Rectangle;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.event.KeyEvent;
|
---|
13 | import java.awt.event.MouseWheelEvent;
|
---|
14 | import java.awt.event.MouseWheelListener;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Map;
|
---|
20 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
21 |
|
---|
22 | import javax.swing.AbstractAction;
|
---|
23 | import javax.swing.AbstractButton;
|
---|
24 | import javax.swing.Action;
|
---|
25 | import javax.swing.BoxLayout;
|
---|
26 | import javax.swing.ButtonGroup;
|
---|
27 | import javax.swing.JButton;
|
---|
28 | import javax.swing.JCheckBoxMenuItem;
|
---|
29 | import javax.swing.JComponent;
|
---|
30 | import javax.swing.JPanel;
|
---|
31 | import javax.swing.JPopupMenu;
|
---|
32 | import javax.swing.JSplitPane;
|
---|
33 | import javax.swing.JToolBar;
|
---|
34 | import javax.swing.KeyStroke;
|
---|
35 | import javax.swing.SwingUtilities;
|
---|
36 | import javax.swing.border.Border;
|
---|
37 | import javax.swing.plaf.basic.BasicSplitPaneDivider;
|
---|
38 | import javax.swing.plaf.basic.BasicSplitPaneUI;
|
---|
39 |
|
---|
40 | import org.openstreetmap.josm.Main;
|
---|
41 | import org.openstreetmap.josm.actions.mapmode.DeleteAction;
|
---|
42 | import org.openstreetmap.josm.actions.mapmode.DrawAction;
|
---|
43 | import org.openstreetmap.josm.actions.mapmode.ExtrudeAction;
|
---|
44 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
45 | import org.openstreetmap.josm.actions.mapmode.ParallelWayAction;
|
---|
46 | import org.openstreetmap.josm.actions.mapmode.SelectAction;
|
---|
47 | import org.openstreetmap.josm.actions.mapmode.ZoomAction;
|
---|
48 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
49 | import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
|
---|
50 | import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
|
---|
51 | import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
|
---|
52 | import org.openstreetmap.josm.gui.dialogs.DialogsPanel;
|
---|
53 | import org.openstreetmap.josm.gui.dialogs.FilterDialog;
|
---|
54 | import org.openstreetmap.josm.gui.dialogs.HistoryDialog;
|
---|
55 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
56 | import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
|
---|
57 | import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
|
---|
58 | import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
|
---|
59 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
60 | import org.openstreetmap.josm.gui.dialogs.UserListDialog;
|
---|
61 | import org.openstreetmap.josm.gui.dialogs.ValidatorDialog;
|
---|
62 | import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;
|
---|
63 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
64 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
65 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * One Map frame with one dataset behind. This is the container gui class whose
|
---|
69 | * display can be set to the different views.
|
---|
70 | *
|
---|
71 | * @author imi
|
---|
72 | */
|
---|
73 | public class MapFrame extends JPanel implements Destroyable, LayerChangeListener {
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * The current mode, this frame operates.
|
---|
77 | */
|
---|
78 | public MapMode mapMode;
|
---|
79 |
|
---|
80 | private final List<MapMode> mapModes = new ArrayList<MapMode>();
|
---|
81 | /**
|
---|
82 | * The view control displayed.
|
---|
83 | */
|
---|
84 | public MapView mapView;
|
---|
85 | /**
|
---|
86 | * The toolbar with the action icons. To add new toggle dialog actions, use addToggleDialog
|
---|
87 | * instead of adding directly to this list. To add a new mode use addMapMode.
|
---|
88 | */
|
---|
89 | private JToolBar toolBarActions = new JToolBar(JToolBar.VERTICAL);
|
---|
90 | private JToolBar toolBarToggle = new JToolBar(JToolBar.VERTICAL);
|
---|
91 | /**
|
---|
92 | * The status line below the map
|
---|
93 | */
|
---|
94 | public MapStatus statusLine;
|
---|
95 |
|
---|
96 | public ConflictDialog conflictDialog;
|
---|
97 | public FilterDialog filterDialog;
|
---|
98 | public RelationListDialog relationListDialog;
|
---|
99 | public ValidatorDialog validatorDialog;
|
---|
100 | public SelectionListDialog selectionListDialog;
|
---|
101 | public PropertiesDialog propertiesDialog;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * The panel list of all toggle dialog icons. To add new toggle dialog actions, use addToggleDialog
|
---|
105 | * instead of adding directly to this list.
|
---|
106 | */
|
---|
107 | private List<ToggleDialog> allDialogs = new ArrayList<ToggleDialog>();
|
---|
108 | private final DialogsPanel dialogsPanel;
|
---|
109 |
|
---|
110 | public final ButtonGroup toolGroup = new ButtonGroup();
|
---|
111 |
|
---|
112 | private List<IconToggleButton> allDialogButtons = new ArrayList<IconToggleButton>();
|
---|
113 | private List<IconToggleButton> allMapModeButtons = new ArrayList<IconToggleButton>();
|
---|
114 |
|
---|
115 | private final ListAllButtonsAction listAllDialogsAction = new ListAllButtonsAction(allDialogButtons);
|
---|
116 | private final ListAllButtonsAction listAllMapModesAction = new ListAllButtonsAction(allMapModeButtons);
|
---|
117 | private final JButton listAllToggleDialogsButton = new JButton(listAllDialogsAction);
|
---|
118 | private final JButton listAllMapModesButton = new JButton(listAllMapModesAction);
|
---|
119 | {
|
---|
120 | listAllDialogsAction.setButton(listAllToggleDialogsButton);
|
---|
121 | listAllMapModesAction.setButton(listAllMapModesButton);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Default width of the toggle dialog area.
|
---|
126 | */
|
---|
127 | public static final int DEF_TOGGLE_DLG_WIDTH = 330;
|
---|
128 |
|
---|
129 | private final Map<Layer, MapMode> lastMapMode = new HashMap<Layer, MapMode>();
|
---|
130 |
|
---|
131 | public MapFrame(JPanel contentPane) {
|
---|
132 | setSize(400,400);
|
---|
133 | setLayout(new BorderLayout());
|
---|
134 |
|
---|
135 | mapView = new MapView(contentPane);
|
---|
136 |
|
---|
137 | new FileDrop(mapView);
|
---|
138 |
|
---|
139 | // show menu entry
|
---|
140 | Main.main.menu.viewMenu.setVisible(true);
|
---|
141 |
|
---|
142 | // toolbar
|
---|
143 | toolBarActions.setFloatable(false);
|
---|
144 | addMapMode(new IconToggleButton(new SelectAction(this)));
|
---|
145 | addMapMode(new IconToggleButton(new DrawAction(this)));
|
---|
146 | addMapMode(new IconToggleButton(new ZoomAction(this)));
|
---|
147 | addMapMode(new IconToggleButton(new DeleteAction(this), !Main.pref.getBoolean("expert", false)));
|
---|
148 | addMapMode(new IconToggleButton(new ExtrudeAction(this), !Main.pref.getBoolean("expert", false)));
|
---|
149 | addMapMode(new IconToggleButton(new ParallelWayAction(this), !Main.pref.getBoolean("expert", false)));
|
---|
150 |
|
---|
151 | toolGroup.setSelected(((AbstractButton)toolBarActions.getComponent(0)).getModel(), true);
|
---|
152 |
|
---|
153 | JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
|
---|
154 | dialogsPanel = new DialogsPanel(splitPane);
|
---|
155 | splitPane.setLeftComponent(mapView);
|
---|
156 | splitPane.setRightComponent(dialogsPanel);
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * All additional space goes to the mapView
|
---|
160 | */
|
---|
161 | splitPane.setResizeWeight(1.0);
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Some beautifications.
|
---|
165 | */
|
---|
166 | splitPane.setDividerSize(5);
|
---|
167 | splitPane.setBorder(null);
|
---|
168 | splitPane.setUI(new BasicSplitPaneUI() {
|
---|
169 | @Override
|
---|
170 | public BasicSplitPaneDivider createDefaultDivider() {
|
---|
171 | return new BasicSplitPaneDivider(this) {
|
---|
172 | @Override
|
---|
173 | public void setBorder(Border b) {
|
---|
174 | }
|
---|
175 | };
|
---|
176 | }
|
---|
177 | });
|
---|
178 |
|
---|
179 | // JSplitPane supports F6 and F8 shortcuts by default, but we need them for Audio actions
|
---|
180 | splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), new Object());
|
---|
181 | splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), new Object());
|
---|
182 |
|
---|
183 | add(splitPane, BorderLayout.CENTER);
|
---|
184 |
|
---|
185 | dialogsPanel.setLayout(new BoxLayout(dialogsPanel, BoxLayout.Y_AXIS));
|
---|
186 | dialogsPanel.setPreferredSize(new Dimension(Main.pref.getInteger("toggleDialogs.width",DEF_TOGGLE_DLG_WIDTH), 0));
|
---|
187 |
|
---|
188 | dialogsPanel.setMinimumSize(new Dimension(24, 0));
|
---|
189 | mapView.setMinimumSize(new Dimension(10,0));
|
---|
190 |
|
---|
191 | toolBarToggle.setFloatable(false);
|
---|
192 | LayerListDialog.createInstance(this);
|
---|
193 | addToggleDialog(LayerListDialog.getInstance());
|
---|
194 | addToggleDialog(propertiesDialog = new PropertiesDialog(this));
|
---|
195 | addToggleDialog(selectionListDialog = new SelectionListDialog());
|
---|
196 | addToggleDialog(relationListDialog = new RelationListDialog());
|
---|
197 | addToggleDialog(new CommandStackDialog(this));
|
---|
198 | addToggleDialog(new UserListDialog());
|
---|
199 | addToggleDialog(new HistoryDialog());
|
---|
200 | addToggleDialog(conflictDialog = new ConflictDialog());
|
---|
201 | addToggleDialog(validatorDialog = new ValidatorDialog());
|
---|
202 | addToggleDialog(filterDialog = new FilterDialog());
|
---|
203 | addToggleDialog(new ChangesetDialog(this));
|
---|
204 | addToggleDialog(new MapPaintDialog());
|
---|
205 |
|
---|
206 | // status line below the map
|
---|
207 | statusLine = new MapStatus(this);
|
---|
208 | MapView.addLayerChangeListener(this);
|
---|
209 | }
|
---|
210 |
|
---|
211 | public void selectSelectTool(boolean onlyIfModeless) {
|
---|
212 | if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
|
---|
213 | return;
|
---|
214 |
|
---|
215 | selectMapMode((MapMode)getDefaultButtonAction());
|
---|
216 | }
|
---|
217 |
|
---|
218 | public void selectDrawTool(boolean onlyIfModeless) {
|
---|
219 | if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
|
---|
220 | return;
|
---|
221 |
|
---|
222 | Action drawAction = ((AbstractButton)toolBarActions.getComponent(1)).getAction();
|
---|
223 | selectMapMode((MapMode)drawAction);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Called as some kind of destructor when the last layer has been removed.
|
---|
228 | * Delegates the call to all Destroyables within this component (e.g. MapModes)
|
---|
229 | */
|
---|
230 | public void destroy() {
|
---|
231 | MapView.removeLayerChangeListener(this);
|
---|
232 | dialogsPanel.destroy();
|
---|
233 | for (int i = 0; i < toolBarActions.getComponentCount(); ++i) {
|
---|
234 | if (toolBarActions.getComponent(i) instanceof Destroyable) {
|
---|
235 | ((Destroyable)toolBarActions.getComponent(i)).destroy();
|
---|
236 | }
|
---|
237 | }
|
---|
238 | for (int i = 0; i < toolBarToggle.getComponentCount(); ++i) {
|
---|
239 | if (toolBarToggle.getComponent(i) instanceof Destroyable) {
|
---|
240 | ((Destroyable)toolBarToggle.getComponent(i)).destroy();
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | // remove menu entries
|
---|
245 | Main.main.menu.viewMenu.setVisible(false);
|
---|
246 |
|
---|
247 | // MapFrame gets destroyed when the last layer is removed, but the status line background
|
---|
248 | // thread that collects the information doesn't get destroyed automatically.
|
---|
249 | if(statusLine.thread != null) {
|
---|
250 | try {
|
---|
251 | statusLine.thread.interrupt();
|
---|
252 | } catch (Exception e) {
|
---|
253 | e.printStackTrace();
|
---|
254 | }
|
---|
255 | }
|
---|
256 | mapView.destroy();
|
---|
257 | }
|
---|
258 |
|
---|
259 | public Action getDefaultButtonAction() {
|
---|
260 | return ((AbstractButton)toolBarActions.getComponent(0)).getAction();
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Open all ToggleDialogs that have their preferences property set. Close all others.
|
---|
265 | */
|
---|
266 | public void initializeDialogsPane() {
|
---|
267 | dialogsPanel.initialize(allDialogs);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Call this to add new toggle dialogs to the left button-list
|
---|
272 | * @param dlg The toggle dialog. It must not be in the list already.
|
---|
273 | */
|
---|
274 | public IconToggleButton addToggleDialog(final ToggleDialog dlg) {
|
---|
275 | final IconToggleButton button = new IconToggleButton(dlg.getToggleAction());
|
---|
276 | button.setShowHideButtonListener(dlg);
|
---|
277 | addHideContextMenu(button);
|
---|
278 | dlg.setButton(button);
|
---|
279 | toolBarToggle.add(button);
|
---|
280 | allDialogs.add(dlg);
|
---|
281 | allDialogButtons.add(button);
|
---|
282 | button.applyButtonHiddenPreferences();
|
---|
283 | if (dialogsPanel.initialized) {
|
---|
284 | dialogsPanel.add(dlg);
|
---|
285 | }
|
---|
286 | return button;
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 |
|
---|
291 | public void addMapMode(IconToggleButton b) {
|
---|
292 | toolBarActions.add(b);
|
---|
293 | toolGroup.add(b);
|
---|
294 | allMapModeButtons.add(b);
|
---|
295 | if (b.getAction() instanceof MapMode) {
|
---|
296 | mapModes.add((MapMode) b.getAction());
|
---|
297 | } else
|
---|
298 | throw new IllegalArgumentException("MapMode action must be subclass of MapMode");
|
---|
299 | addHideContextMenu(b);
|
---|
300 | b.applyButtonHiddenPreferences();
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Fires an property changed event "visible".
|
---|
305 | */
|
---|
306 | @Override public void setVisible(boolean aFlag) {
|
---|
307 | boolean old = isVisible();
|
---|
308 | super.setVisible(aFlag);
|
---|
309 | if (old != aFlag) {
|
---|
310 | firePropertyChange("visible", old, aFlag);
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Change the operating map mode for the view. Will call unregister on the
|
---|
316 | * old MapMode and register on the new one. Now this function also verifies
|
---|
317 | * if new map mode is correct mode for current layer and does not change mode
|
---|
318 | * in such cases.
|
---|
319 | * @param mapMode The new mode to set.
|
---|
320 | */
|
---|
321 | public void selectMapMode(MapMode newMapMode) {
|
---|
322 | selectMapMode(newMapMode, mapView.getActiveLayer());
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Another version of the selectMapMode for changing layer action.
|
---|
327 | * Pass newly selected layer to this method.
|
---|
328 | * @param newMapMode
|
---|
329 | * @param newLayer
|
---|
330 | */
|
---|
331 | public void selectMapMode(MapMode newMapMode, Layer newLayer) {
|
---|
332 | if (newMapMode == null || !newMapMode.layerIsSupported(newLayer))
|
---|
333 | return;
|
---|
334 |
|
---|
335 | MapMode oldMapMode = this.mapMode;
|
---|
336 | if (newMapMode == oldMapMode)
|
---|
337 | return;
|
---|
338 | if (oldMapMode != null) {
|
---|
339 | oldMapMode.exitMode();
|
---|
340 | }
|
---|
341 | this.mapMode = newMapMode;
|
---|
342 | newMapMode.enterMode();
|
---|
343 | lastMapMode.put(newLayer, newMapMode);
|
---|
344 | fireMapModeChanged(oldMapMode, newMapMode);
|
---|
345 | }
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Fill the given panel by adding all necessary components to the different
|
---|
349 | * locations.
|
---|
350 | *
|
---|
351 | * @param panel The container to fill. Must have an BorderLayout.
|
---|
352 | */
|
---|
353 | public void fillPanel(Container panel) {
|
---|
354 | panel.add(this, BorderLayout.CENTER);
|
---|
355 | JToolBar jb = new JToolBar(JToolBar.VERTICAL);
|
---|
356 | jb.setFloatable(false);
|
---|
357 | toolBarActions.setAlignmentX(0.5f);
|
---|
358 | jb.add(toolBarActions);
|
---|
359 | listAllMapModesButton.setAlignmentX(0.5f);
|
---|
360 | listAllMapModesButton.setBorder(null);
|
---|
361 | listAllMapModesButton.setFont(listAllMapModesButton.getFont().deriveFont(Font.PLAIN));
|
---|
362 | jb.add(listAllMapModesButton);
|
---|
363 |
|
---|
364 | if(Main.pref.getBoolean("sidetoolbar.togglevisible", true)) {
|
---|
365 | jb.addSeparator(new Dimension(0,18));
|
---|
366 | toolBarToggle.setAlignmentX(0.5f);
|
---|
367 | jb.add(toolBarToggle);
|
---|
368 | listAllToggleDialogsButton.setAlignmentX(0.5f);
|
---|
369 | listAllToggleDialogsButton.setBorder(null);
|
---|
370 | listAllToggleDialogsButton.setFont(listAllToggleDialogsButton.getFont().deriveFont(Font.PLAIN));
|
---|
371 | jb.add(listAllToggleDialogsButton);
|
---|
372 | }
|
---|
373 |
|
---|
374 | if(Main.pref.getBoolean("sidetoolbar.visible", true))
|
---|
375 | {
|
---|
376 | if(Main.pref.getBoolean("sidetoolbar.scrollable", true)) {
|
---|
377 | final ScrollViewport svp = new ScrollViewport(jb, ScrollViewport.VERTICAL_DIRECTION);
|
---|
378 | panel.add(svp, BorderLayout.WEST);
|
---|
379 | jb.addMouseWheelListener(new MouseWheelListener() {
|
---|
380 | public void mouseWheelMoved(MouseWheelEvent e) {
|
---|
381 | svp.scroll(0,e.getUnitsToScroll() * 5);
|
---|
382 | }
|
---|
383 | });
|
---|
384 | } else {
|
---|
385 | panel.add(jb, BorderLayout.WEST);
|
---|
386 | }
|
---|
387 | }
|
---|
388 | if (statusLine != null && Main.pref.getBoolean("statusline.visible", true)) {
|
---|
389 | panel.add(statusLine, BorderLayout.SOUTH);
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | private void addHideContextMenu(final IconToggleButton b) {
|
---|
394 | //context menu
|
---|
395 | b.addMouseListener(new PopupMenuLauncher(new JPopupMenu() {
|
---|
396 | {
|
---|
397 | add(new AbstractAction() {
|
---|
398 | {
|
---|
399 | putValue(NAME, tr("Hide this button"));
|
---|
400 | putValue(SHORT_DESCRIPTION, tr("Click the arrow at the bottom to show it again."));
|
---|
401 | }
|
---|
402 | @Override
|
---|
403 | public void actionPerformed(ActionEvent e) {
|
---|
404 | b.setButtonHidden(true);
|
---|
405 | validateToolBarsVisibility();
|
---|
406 | }
|
---|
407 | });
|
---|
408 | }
|
---|
409 | }));
|
---|
410 | }
|
---|
411 |
|
---|
412 | class ListAllButtonsAction extends AbstractAction {
|
---|
413 |
|
---|
414 | private JButton button;
|
---|
415 | private Collection<? extends HideableButton> buttons;
|
---|
416 |
|
---|
417 |
|
---|
418 | public ListAllButtonsAction(Collection<? extends HideableButton> buttons) {
|
---|
419 | this.buttons = buttons;
|
---|
420 | putValue(NAME, ">>");
|
---|
421 | }
|
---|
422 |
|
---|
423 | public void setButton(JButton button) {
|
---|
424 | this.button = button;
|
---|
425 | }
|
---|
426 |
|
---|
427 | @Override
|
---|
428 | public void actionPerformed(ActionEvent e) {
|
---|
429 | JPopupMenu menu = new JPopupMenu();
|
---|
430 | for (HideableButton b : buttons) {
|
---|
431 | final HideableButton t = b;
|
---|
432 | menu.add(new JCheckBoxMenuItem(new AbstractAction() {
|
---|
433 | {
|
---|
434 | putValue(NAME, t.getActionName());
|
---|
435 | putValue(SMALL_ICON, t.getIcon());
|
---|
436 | putValue(SELECTED_KEY, t.isButtonVisible());
|
---|
437 | putValue(SHORT_DESCRIPTION, tr("Hide or show this toggle button"));
|
---|
438 | }
|
---|
439 | @Override
|
---|
440 | public void actionPerformed(ActionEvent e) {
|
---|
441 | if ((Boolean) getValue(SELECTED_KEY)) t.showButton(); else t.hideButton();
|
---|
442 | validateToolBarsVisibility();
|
---|
443 | }
|
---|
444 | }));
|
---|
445 | }
|
---|
446 | Rectangle bounds = button.getBounds();
|
---|
447 | menu.show(button, bounds.x + bounds.width, 0);
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | public void validateToolBarsVisibility() {
|
---|
452 | for (IconToggleButton b : allDialogButtons) {
|
---|
453 | b.applyButtonHiddenPreferences();
|
---|
454 | }
|
---|
455 | toolBarToggle.repaint();
|
---|
456 | for (IconToggleButton b : allMapModeButtons) {
|
---|
457 | b.applyButtonHiddenPreferences();
|
---|
458 | }
|
---|
459 | toolBarActions.repaint();
|
---|
460 | }
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Replies the instance of a toggle dialog of type <code>type</code> managed by this
|
---|
464 | * map frame
|
---|
465 | *
|
---|
466 | * @param <T>
|
---|
467 | * @param type the class of the toggle dialog, i.e. UserListDialog.class
|
---|
468 | * @return the instance of a toggle dialog of type <code>type</code> managed by this
|
---|
469 | * map frame; null, if no such dialog exists
|
---|
470 | *
|
---|
471 | */
|
---|
472 | public <T> T getToggleDialog(Class<T> type) {
|
---|
473 | return dialogsPanel.getToggleDialog(type);
|
---|
474 | }
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * Returns the current width of the (possibly resized) toggle dialog area
|
---|
478 | */
|
---|
479 | public int getToggleDlgWidth() {
|
---|
480 | return dialogsPanel.getWidth();
|
---|
481 | }
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Interface to notify listeners of the change of the mapMode.
|
---|
485 | */
|
---|
486 | public interface MapModeChangeListener {
|
---|
487 | void mapModeChange(MapMode oldMapMode, MapMode newMapMode);
|
---|
488 | }
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * the mapMode listeners
|
---|
492 | */
|
---|
493 | private static final CopyOnWriteArrayList<MapModeChangeListener> mapModeChangeListeners = new CopyOnWriteArrayList<MapModeChangeListener>();
|
---|
494 | /**
|
---|
495 | * Adds a mapMode change listener
|
---|
496 | *
|
---|
497 | * @param listener the listener. Ignored if null or already registered.
|
---|
498 | */
|
---|
499 | public static void addMapModeChangeListener(MapModeChangeListener listener) {
|
---|
500 | if (listener != null) {
|
---|
501 | mapModeChangeListeners.addIfAbsent(listener);
|
---|
502 | }
|
---|
503 | }
|
---|
504 | /**
|
---|
505 | * Removes a mapMode change listener
|
---|
506 | *
|
---|
507 | * @param listener the listener. Ignored if null or already registered.
|
---|
508 | */
|
---|
509 | public static void removeMapModeChangeListener(MapModeChangeListener listener) {
|
---|
510 | mapModeChangeListeners.remove(listener);
|
---|
511 | }
|
---|
512 |
|
---|
513 | protected static void fireMapModeChanged(MapMode oldMapMode, MapMode newMapMode) {
|
---|
514 | for (MapModeChangeListener l : mapModeChangeListeners) {
|
---|
515 | l.mapModeChange(oldMapMode, newMapMode);
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | @Override
|
---|
520 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
521 | boolean modeChanged = false;
|
---|
522 | if (mapMode == null || !mapMode.layerIsSupported(newLayer)) {
|
---|
523 | MapMode newMapMode = getLastMapMode(newLayer);
|
---|
524 | modeChanged = newMapMode != mapMode;
|
---|
525 | if (newMapMode != null) {
|
---|
526 | selectMapMode(newMapMode, newLayer); // it would be nice to select first supported mode when layer is first selected, but it don't work well with for example editgpx layer
|
---|
527 | } else if (mapMode != null) {
|
---|
528 | mapMode.exitMode(); // if new mode is null - simply exit from previous mode
|
---|
529 | }
|
---|
530 | }
|
---|
531 | if (!modeChanged && mapMode != null) {
|
---|
532 | // Let mapmodes know about new active layer
|
---|
533 | mapMode.exitMode();
|
---|
534 | mapMode.enterMode();
|
---|
535 | }
|
---|
536 | // invalidate repaint cache
|
---|
537 | Main.map.mapView.preferenceChanged(null);
|
---|
538 |
|
---|
539 | // After all listeners notice new layer, some buttons will be disabled/enabled
|
---|
540 | // and possibly need to be hidden/shown.
|
---|
541 | SwingUtilities.invokeLater(new Runnable() {
|
---|
542 | public void run() {
|
---|
543 | validateToolBarsVisibility();
|
---|
544 | }
|
---|
545 | });
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | private MapMode getLastMapMode(Layer newLayer) {
|
---|
550 | MapMode mode = lastMapMode.get(newLayer);
|
---|
551 | if (mode == null) {
|
---|
552 | // if no action is selected - try to select default action
|
---|
553 | Action defaultMode = getDefaultButtonAction();
|
---|
554 | if (defaultMode instanceof MapMode & ((MapMode)defaultMode).layerIsSupported(newLayer)) {
|
---|
555 | mode = (MapMode) defaultMode;
|
---|
556 | }
|
---|
557 | }
|
---|
558 | return mode;
|
---|
559 | }
|
---|
560 |
|
---|
561 | @Override
|
---|
562 | public void layerAdded(Layer newLayer) { }
|
---|
563 |
|
---|
564 | @Override
|
---|
565 | public void layerRemoved(Layer oldLayer) {
|
---|
566 | lastMapMode.remove(oldLayer);
|
---|
567 | }
|
---|
568 | }
|
---|