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

Last change on this file since 15835 was 15781, checked in by simon04, 4 years ago

fix #15334 - Use BasicArrowButton in MapFrame

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