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

Last change on this file since 12940 was 12881, checked in by bastiK, 7 years ago

see #15229 - move remaining classes to spi.preferences package, to make it self-contained

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