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

Last change on this file since 8612 was 8537, checked in by akks, 9 years ago

add debug.advanced-keypress-detector.enable option for debugging #11209

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