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

Last change on this file since 5903 was 5897, checked in by Don-vip, 11 years ago

see #8582 - Use Main.removeLayer() everywhere

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