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

Last change on this file since 7206 was 7075, checked in by Don-vip, 10 years ago

Robustness to allow to run more unit tests in headless mode

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