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

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

see #15334 - MapFrame: black BasicArrowButton

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