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

Last change on this file since 12368 was 12368, checked in by michael2402, 7 years ago

Fix checkstyle

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