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

Last change on this file since 7217 was 7217, checked in by akks, 10 years ago

see #10104: refactor key press/release detection introducing Main.map.keyDetector

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