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

Last change on this file since 19125 was 18871, checked in by taylor.smock, 9 months ago

See #23218: Use newer error_prone versions when compiling on Java 11+

error_prone 2.11 dropped support for compiling with Java 8, although it still
supports compiling for Java 8. The "major" new check for us is NotJavadoc since
we used /** in quite a few places which were not javadoc.

Other "new" checks that are of interest:

  • AlreadyChecked: if (foo) { doFoo(); } else if (!foo) { doBar(); }
  • UnnecessaryStringBuilder: Avoid StringBuilder (Java converts + to StringBuilder behind-the-scenes, but may also do something else if it performs better)
  • NonApiType: Avoid specific interface types in function definitions
  • NamedLikeContextualKeyword: Avoid using restricted names for classes and methods
  • UnusedMethod: Unused private methods should be removed

This fixes most of the new error_prone issues and some SonarLint issues.

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