source: josm/trunk/src/org/openstreetmap/josm/gui/MapFrame.java@ 5670

Last change on this file since 5670 was 5670, checked in by bastiK, 11 years ago

session: save & restore map position and scale (see #4029)

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