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

Last change on this file since 13802 was 13795, checked in by Don-vip, 6 years ago

fix #13561, fix #16265 - fix mapmode change issues with "Adjust imagery offset"

  • Property svn:eol-style set to native
File size: 32.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Container;
9import java.awt.Dimension;
10import java.awt.Font;
11import java.awt.GridBagLayout;
12import java.awt.Rectangle;
13import java.awt.event.ActionEvent;
14import java.awt.event.KeyEvent;
15import java.util.ArrayList;
16import java.util.Collection;
17import java.util.HashMap;
18import java.util.List;
19import java.util.Map;
20import java.util.concurrent.CopyOnWriteArrayList;
21
22import javax.swing.AbstractAction;
23import javax.swing.AbstractButton;
24import javax.swing.Action;
25import javax.swing.BorderFactory;
26import javax.swing.BoxLayout;
27import javax.swing.ButtonGroup;
28import javax.swing.ImageIcon;
29import javax.swing.JButton;
30import javax.swing.JCheckBoxMenuItem;
31import javax.swing.JComponent;
32import javax.swing.JPanel;
33import javax.swing.JPopupMenu;
34import javax.swing.JSplitPane;
35import javax.swing.JToggleButton;
36import javax.swing.JToolBar;
37import javax.swing.KeyStroke;
38import javax.swing.border.Border;
39import javax.swing.event.PopupMenuEvent;
40import javax.swing.event.PopupMenuListener;
41import javax.swing.plaf.basic.BasicSplitPaneDivider;
42import javax.swing.plaf.basic.BasicSplitPaneUI;
43
44import org.openstreetmap.josm.actions.LassoModeAction;
45import org.openstreetmap.josm.actions.mapmode.DeleteAction;
46import org.openstreetmap.josm.actions.mapmode.DrawAction;
47import org.openstreetmap.josm.actions.mapmode.ExtrudeAction;
48import org.openstreetmap.josm.actions.mapmode.ImproveWayAccuracyAction;
49import org.openstreetmap.josm.actions.mapmode.MapMode;
50import org.openstreetmap.josm.actions.mapmode.ParallelWayAction;
51import org.openstreetmap.josm.actions.mapmode.SelectAction;
52import org.openstreetmap.josm.actions.mapmode.ZoomAction;
53import org.openstreetmap.josm.data.ViewportData;
54import org.openstreetmap.josm.data.preferences.BooleanProperty;
55import org.openstreetmap.josm.data.preferences.IntegerProperty;
56import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
57import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
58import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
59import org.openstreetmap.josm.gui.dialogs.DialogsPanel;
60import org.openstreetmap.josm.gui.dialogs.FilterDialog;
61import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
62import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
63import org.openstreetmap.josm.gui.dialogs.MinimapDialog;
64import org.openstreetmap.josm.gui.dialogs.NotesDialog;
65import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
66import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
67import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
68import org.openstreetmap.josm.gui.dialogs.UserListDialog;
69import org.openstreetmap.josm.gui.dialogs.ValidatorDialog;
70import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;
71import org.openstreetmap.josm.gui.layer.Layer;
72import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
73import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
74import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
75import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
76import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
77import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
78import org.openstreetmap.josm.gui.util.AdvancedKeyPressDetector;
79import org.openstreetmap.josm.spi.preferences.Config;
80import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener;
81import org.openstreetmap.josm.tools.Destroyable;
82import org.openstreetmap.josm.tools.GBC;
83import org.openstreetmap.josm.tools.ImageProvider;
84import org.openstreetmap.josm.tools.Shortcut;
85
86/**
87 * One Map frame with one dataset behind. This is the container gui class whose
88 * display can be set to the different views.
89 *
90 * @author imi
91 */
92public class MapFrame extends JPanel implements Destroyable, ActiveLayerChangeListener, LayerChangeListener {
93 /**
94 * Default width of the toggle dialog area.
95 */
96 public static final int DEF_TOGGLE_DLG_WIDTH = 330;
97
98 private static final IntegerProperty TOGGLE_DIALOGS_WIDTH = new IntegerProperty("toggleDialogs.width", DEF_TOGGLE_DLG_WIDTH);
99 /**
100 * Do not require to switch modes (potlatch style workflow) for drawing/selecting map modes.
101 * @since 12347
102 */
103 public static final BooleanProperty MODELESS = new BooleanProperty("modeless", false);
104 /**
105 * The current mode, this frame operates.
106 */
107 public MapMode mapMode;
108
109 /**
110 * The view control displayed.
111 */
112 public final MapView mapView;
113
114 /**
115 * This object allows to detect key press and release events
116 */
117 public final transient AdvancedKeyPressDetector keyDetector = new AdvancedKeyPressDetector();
118
119 /**
120 * The toolbar with the action icons. To add new toggle dialog buttons,
121 * use addToggleDialog, to add a new map mode button use addMapMode.
122 */
123 private JComponent sideToolBar = new JToolBar(JToolBar.VERTICAL);
124 private final ButtonGroup toolBarActionsGroup = new ButtonGroup();
125 private final JToolBar toolBarActions = new JToolBar(JToolBar.VERTICAL);
126 private final JToolBar toolBarToggle = new JToolBar(JToolBar.VERTICAL);
127
128 private final List<ToggleDialog> allDialogs = new ArrayList<>();
129 private final List<IconToggleButton> allDialogButtons = new ArrayList<>();
130 /**
131 * All map mode buttons. Should only be read form the outside
132 */
133 public final List<IconToggleButton> allMapModeButtons = new ArrayList<>();
134
135 private final ListAllButtonsAction listAllDialogsAction = new ListAllButtonsAction(allDialogButtons);
136 private final ListAllButtonsAction listAllMapModesAction = new ListAllButtonsAction(allMapModeButtons);
137 private final JButton listAllToggleDialogsButton = new JButton(listAllDialogsAction);
138 private final JButton listAllMapModesButton = new JButton(listAllMapModesAction);
139
140 {
141 listAllDialogsAction.setButton(listAllToggleDialogsButton);
142 listAllMapModesAction.setButton(listAllMapModesButton);
143 }
144
145 // Toggle dialogs
146
147 /** Conflict dialog */
148 public final ConflictDialog conflictDialog;
149 /** Filter dialog */
150 public final FilterDialog filterDialog;
151 /** Relation list dialog */
152 public final RelationListDialog relationListDialog;
153 /** Validator dialog */
154 public final ValidatorDialog validatorDialog;
155 /** Selection list dialog */
156 public final SelectionListDialog selectionListDialog;
157 /** Properties dialog */
158 public final PropertiesDialog propertiesDialog;
159 /** Map paint dialog */
160 public final MapPaintDialog mapPaintDialog;
161 /** Notes dialog */
162 public final NotesDialog noteDialog;
163
164 // Map modes
165
166 /** Select mode */
167 public final SelectAction mapModeSelect;
168 /** Draw mode */
169 public final DrawAction mapModeDraw;
170 /** Zoom mode */
171 public final ZoomAction mapModeZoom;
172 /** Delete mode */
173 public final DeleteAction mapModeDelete;
174 /** Select Lasso mode */
175 public LassoModeAction mapModeSelectLasso;
176
177 private final transient Map<Layer, MapMode> lastMapMode = new HashMap<>();
178
179 /**
180 * The status line below the map
181 */
182 public MapStatus statusLine;
183
184 /**
185 * The split pane with the mapview (leftPanel) and toggle dialogs (dialogsPanel).
186 */
187 private final JSplitPane splitPane;
188 private final JPanel leftPanel;
189 private final DialogsPanel dialogsPanel;
190
191 /**
192 * Constructs a new {@code MapFrame}.
193 * @param viewportData the initial viewport of the map. Can be null, then
194 * the viewport is derived from the layer data.
195 * @since 11713
196 */
197 public MapFrame(ViewportData viewportData) {
198 setSize(400, 400);
199 setLayout(new BorderLayout());
200
201 mapView = new MapView(MainApplication.getLayerManager(), viewportData);
202
203 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
204
205 leftPanel = new JPanel(new GridBagLayout());
206 leftPanel.add(mapView, GBC.std().fill());
207 splitPane.setLeftComponent(leftPanel);
208
209 dialogsPanel = new DialogsPanel(splitPane);
210 splitPane.setRightComponent(dialogsPanel);
211
212 /**
213 * All additional space goes to the mapView
214 */
215 splitPane.setResizeWeight(1.0);
216
217 /**
218 * Some beautifications.
219 */
220 splitPane.setDividerSize(5);
221 splitPane.setBorder(null);
222 splitPane.setUI(new NoBorderSplitPaneUI());
223
224 // JSplitPane supports F6 and F8 shortcuts by default, but we need them for Audio actions
225 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), new Object());
226 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 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 LassoModeAction();
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 dialogsPanel.destroy();
344 Config.getPref().removePreferenceChangeListener(sidetoolbarPreferencesChangedListener);
345 for (int i = 0; i < toolBarActions.getComponentCount(); ++i) {
346 if (toolBarActions.getComponent(i) instanceof Destroyable) {
347 ((Destroyable) toolBarActions.getComponent(i)).destroy();
348 }
349 }
350 toolBarActions.removeAll();
351 for (int i = 0; i < toolBarToggle.getComponentCount(); ++i) {
352 if (toolBarToggle.getComponent(i) instanceof Destroyable) {
353 ((Destroyable) toolBarToggle.getComponent(i)).destroy();
354 }
355 }
356 toolBarToggle.removeAll();
357
358 statusLine.destroy();
359 mapView.destroy();
360 keyDetector.unregister();
361
362 allDialogs.clear();
363 allDialogButtons.clear();
364 allMapModeButtons.clear();
365 }
366
367 /**
368 * Gets the action of the default (first) map mode
369 * @return That action
370 */
371 public Action getDefaultButtonAction() {
372 return ((AbstractButton) toolBarActions.getComponent(0)).getAction();
373 }
374
375 /**
376 * Open all ToggleDialogs that have their preferences property set. Close all others.
377 */
378 public void initializeDialogsPane() {
379 dialogsPanel.initialize(allDialogs);
380 }
381
382 /**
383 * Adds a new toggle dialog to the left button list. It is displayed in expert and normal mode
384 * @param dlg The dialog
385 * @return The button
386 */
387 public IconToggleButton addToggleDialog(final ToggleDialog dlg) {
388 return addToggleDialog(dlg, false);
389 }
390
391 /**
392 * Call this to add new toggle dialogs to the left button-list
393 * @param dlg The toggle dialog. It must not be in the list already.
394 * @param isExpert {@code true} if it's reserved to expert mode
395 * @return button allowing to toggle the dialog
396 */
397 public IconToggleButton addToggleDialog(final ToggleDialog dlg, boolean isExpert) {
398 final IconToggleButton button = new IconToggleButton(dlg.getToggleAction(), isExpert);
399 button.setShowHideButtonListener(dlg);
400 button.setInheritsPopupMenu(true);
401 dlg.setButton(button);
402 toolBarToggle.add(button);
403 allDialogs.add(dlg);
404 allDialogButtons.add(button);
405 button.applyButtonHiddenPreferences();
406 if (dialogsPanel.initialized) {
407 dialogsPanel.add(dlg);
408 }
409 return button;
410 }
411
412 /**
413 * Call this to remove existing toggle dialog from the left button-list
414 * @param dlg The toggle dialog. It must be already in the list.
415 * @since 10851
416 */
417 public void removeToggleDialog(final ToggleDialog dlg) {
418 final JToggleButton button = dlg.getButton();
419 if (button != null) {
420 allDialogButtons.remove(button);
421 toolBarToggle.remove(button);
422 }
423 dialogsPanel.remove(dlg);
424 allDialogs.remove(dlg);
425 }
426
427 /**
428 * Adds a new map mode button
429 * @param b The map mode button with a {@link MapMode} action.
430 */
431 public void addMapMode(IconToggleButton b) {
432 if (!(b.getAction() instanceof MapMode))
433 throw new IllegalArgumentException("MapMode action must be subclass of MapMode");
434 allMapModeButtons.add(b);
435 toolBarActionsGroup.add(b);
436 toolBarActions.add(b);
437 b.applyButtonHiddenPreferences();
438 b.setInheritsPopupMenu(true);
439 }
440
441 /**
442 * Fires an property changed event "visible".
443 * @param aFlag {@code true} if display should be visible
444 */
445 @Override public void setVisible(boolean aFlag) {
446 boolean old = isVisible();
447 super.setVisible(aFlag);
448 if (old != aFlag) {
449 firePropertyChange("visible", old, aFlag);
450 }
451 }
452
453 /**
454 * Change the operating map mode for the view. Will call unregister on the
455 * old MapMode and register on the new one. Now this function also verifies
456 * if new map mode is correct mode for current layer and does not change mode
457 * in such cases.
458 * @param newMapMode The new mode to set.
459 * @return {@code true} if mode is really selected
460 */
461 public boolean selectMapMode(MapMode newMapMode) {
462 return selectMapMode(newMapMode, mapView.getLayerManager().getActiveLayer());
463 }
464
465 /**
466 * Another version of the selectMapMode for changing layer action.
467 * Pass newly selected layer to this method.
468 * @param newMapMode The new mode to set.
469 * @param newLayer newly selected layer
470 * @return {@code true} if mode is really selected
471 */
472 public boolean selectMapMode(MapMode newMapMode, Layer newLayer) {
473 MapMode oldMapMode = this.mapMode;
474 if (newMapMode == oldMapMode)
475 return true;
476 if (newMapMode == null || !newMapMode.layerIsSupported(newLayer)) {
477 newMapMode = null;
478 }
479
480 if (oldMapMode != null) {
481 oldMapMode.exitMode();
482 }
483 this.mapMode = newMapMode;
484 if (newMapMode != null) {
485 newMapMode.enterMode();
486 }
487 lastMapMode.put(newLayer, newMapMode);
488 fireMapModeChanged(oldMapMode, newMapMode);
489 return newMapMode != null;
490 }
491
492 /**
493 * Fill the given panel by adding all necessary components to the different
494 * locations.
495 *
496 * @param panel The container to fill. Must have a BorderLayout.
497 */
498 public void fillPanel(Container panel) {
499 panel.add(this, BorderLayout.CENTER);
500
501 /**
502 * sideToolBar: add map modes icons
503 */
504 if (Config.getPref().getBoolean("sidetoolbar.mapmodes.visible", true)) {
505 toolBarActions.setAlignmentX(0.5f);
506 toolBarActions.setBorder(null);
507 toolBarActions.setInheritsPopupMenu(true);
508 sideToolBar.add(toolBarActions);
509 listAllMapModesButton.setAlignmentX(0.5f);
510 listAllMapModesButton.setBorder(null);
511 listAllMapModesButton.setFont(listAllMapModesButton.getFont().deriveFont(Font.PLAIN));
512 listAllMapModesButton.setInheritsPopupMenu(true);
513 sideToolBar.add(listAllMapModesButton);
514 }
515
516 /**
517 * sideToolBar: add toggle dialogs icons
518 */
519 if (Config.getPref().getBoolean("sidetoolbar.toggledialogs.visible", true)) {
520 ((JToolBar) sideToolBar).addSeparator(new Dimension(0, 18));
521 toolBarToggle.setAlignmentX(0.5f);
522 toolBarToggle.setBorder(null);
523 toolBarToggle.setInheritsPopupMenu(true);
524 sideToolBar.add(toolBarToggle);
525 listAllToggleDialogsButton.setAlignmentX(0.5f);
526 listAllToggleDialogsButton.setBorder(null);
527 listAllToggleDialogsButton.setFont(listAllToggleDialogsButton.getFont().deriveFont(Font.PLAIN));
528 listAllToggleDialogsButton.setInheritsPopupMenu(true);
529 sideToolBar.add(listAllToggleDialogsButton);
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 public void setButton(JButton button) {
650 this.button = button;
651 final ImageIcon icon = ImageProvider.get("audio-fwd");
652 putValue(SMALL_ICON, icon);
653 button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight() + 64));
654 }
655
656 @Override
657 public void actionPerformed(ActionEvent e) {
658 JPopupMenu menu = new JPopupMenu();
659 for (HideableButton b : buttons) {
660 final HideableButton t = b;
661 menu.add(new JCheckBoxMenuItem(new AbstractAction() {
662 {
663 putValue(NAME, t.getActionName());
664 putValue(SMALL_ICON, t.getIcon());
665 putValue(SELECTED_KEY, t.isButtonVisible());
666 putValue(SHORT_DESCRIPTION, tr("Hide or show this toggle button"));
667 }
668
669 @Override
670 public void actionPerformed(ActionEvent e) {
671 if ((Boolean) getValue(SELECTED_KEY)) {
672 t.showButton();
673 } else {
674 t.hideButton();
675 }
676 validateToolBarsVisibility();
677 }
678 }));
679 }
680 if (button != null) {
681 Rectangle bounds = button.getBounds();
682 menu.show(button, bounds.x + bounds.width, 0);
683 }
684 }
685 }
686
687 /**
688 * Validate the visibility of all tool bars and hide the ones that should be hidden
689 */
690 public void validateToolBarsVisibility() {
691 for (IconToggleButton b : allDialogButtons) {
692 b.applyButtonHiddenPreferences();
693 }
694 toolBarToggle.repaint();
695 for (IconToggleButton b : allMapModeButtons) {
696 b.applyButtonHiddenPreferences();
697 }
698 toolBarActions.repaint();
699 }
700
701 /**
702 * Replies the instance of a toggle dialog of type <code>type</code> managed by this map frame
703 *
704 * @param <T> toggle dialog type
705 * @param type the class of the toggle dialog, i.e. UserListDialog.class
706 * @return the instance of a toggle dialog of type <code>type</code> managed by this
707 * map frame; null, if no such dialog exists
708 *
709 */
710 public <T> T getToggleDialog(Class<T> type) {
711 return dialogsPanel.getToggleDialog(type);
712 }
713
714 /**
715 * Shows or hides the side dialog panel
716 * @param visible The new visibility
717 */
718 public void setDialogsPanelVisible(boolean visible) {
719 rememberToggleDialogWidth();
720 dialogsPanel.setVisible(visible);
721 splitPane.setDividerLocation(visible ? splitPane.getWidth() - TOGGLE_DIALOGS_WIDTH.get() : 0);
722 splitPane.setDividerSize(visible ? 5 : 0);
723 }
724
725 /**
726 * Remember the current width of the (possibly resized) toggle dialog area
727 */
728 public void rememberToggleDialogWidth() {
729 if (dialogsPanel.isVisible()) {
730 TOGGLE_DIALOGS_WIDTH.put(splitPane.getWidth() - splitPane.getDividerLocation());
731 }
732 }
733
734 /**
735 * Remove panel from top of MapView by class
736 * @param type type of panel
737 */
738 public void removeTopPanel(Class<?> type) {
739 int n = leftPanel.getComponentCount();
740 for (int i = 0; i < n; i++) {
741 Component c = leftPanel.getComponent(i);
742 if (type.isInstance(c)) {
743 leftPanel.remove(i);
744 leftPanel.doLayout();
745 return;
746 }
747 }
748 }
749
750 /**
751 * Find panel on top of MapView by class
752 * @param <T> type
753 * @param type type of panel
754 * @return found panel
755 */
756 public <T> T getTopPanel(Class<T> type) {
757 int n = leftPanel.getComponentCount();
758 for (int i = 0; i < n; i++) {
759 Component c = leftPanel.getComponent(i);
760 if (type.isInstance(c))
761 return type.cast(c);
762 }
763 return null;
764 }
765
766 /**
767 * Add component {@code c} on top of MapView
768 * @param c component
769 */
770 public void addTopPanel(Component c) {
771 leftPanel.add(c, GBC.eol().fill(GBC.HORIZONTAL), leftPanel.getComponentCount()-1);
772 leftPanel.doLayout();
773 c.doLayout();
774 }
775
776 /**
777 * Interface to notify listeners of the change of the mapMode.
778 * @since 10600 (functional interface)
779 */
780 @FunctionalInterface
781 public interface MapModeChangeListener {
782 /**
783 * Trigerred when map mode changes.
784 * @param oldMapMode old map mode
785 * @param newMapMode new map mode
786 */
787 void mapModeChange(MapMode oldMapMode, MapMode newMapMode);
788 }
789
790 /**
791 * the mapMode listeners
792 */
793 private static final CopyOnWriteArrayList<MapModeChangeListener> mapModeChangeListeners = new CopyOnWriteArrayList<>();
794
795 private transient PreferenceChangedListener sidetoolbarPreferencesChangedListener;
796 /**
797 * Adds a mapMode change listener
798 *
799 * @param listener the listener. Ignored if null or already registered.
800 */
801 public static void addMapModeChangeListener(MapModeChangeListener listener) {
802 if (listener != null) {
803 mapModeChangeListeners.addIfAbsent(listener);
804 }
805 }
806
807 /**
808 * Removes a mapMode change listener
809 *
810 * @param listener the listener. Ignored if null or already registered.
811 */
812 public static void removeMapModeChangeListener(MapModeChangeListener listener) {
813 mapModeChangeListeners.remove(listener);
814 }
815
816 protected static void fireMapModeChanged(MapMode oldMapMode, MapMode newMapMode) {
817 for (MapModeChangeListener l : mapModeChangeListeners) {
818 l.mapModeChange(oldMapMode, newMapMode);
819 }
820 }
821
822 @Override
823 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
824 boolean modeChanged = false;
825 Layer newLayer = e.getSource().getActiveLayer();
826 if (mapMode == null || !mapMode.layerIsSupported(newLayer)) {
827 MapMode newMapMode = getLastMapMode(newLayer);
828 modeChanged = newMapMode != mapMode;
829 if (newMapMode != null) {
830 // it would be nice to select first supported mode when layer is first selected,
831 // but it don't work well with for example editgpx layer
832 selectMapMode(newMapMode, newLayer);
833 } else if (mapMode != null) {
834 mapMode.exitMode(); // if new mode is null - simply exit from previous mode
835 mapMode = null;
836 }
837 }
838 // if this is really a change (and not the first active layer)
839 if (e.getPreviousActiveLayer() != null && !modeChanged && mapMode != null) {
840 // Let mapmodes know about new active layer
841 mapMode.exitMode();
842 mapMode.enterMode();
843 }
844
845 // After all listeners notice new layer, some buttons will be disabled/enabled
846 // and possibly need to be hidden/shown.
847 validateToolBarsVisibility();
848 }
849
850 private MapMode getLastMapMode(Layer newLayer) {
851 MapMode mode = lastMapMode.get(newLayer);
852 if (mode == null) {
853 // if no action is selected - try to select default action
854 Action defaultMode = getDefaultButtonAction();
855 if (defaultMode instanceof MapMode && ((MapMode) defaultMode).layerIsSupported(newLayer)) {
856 mode = (MapMode) defaultMode;
857 }
858 }
859 return mode;
860 }
861
862 @Override
863 public void layerAdded(LayerAddEvent e) {
864 // ignored
865 }
866
867 @Override
868 public void layerRemoving(LayerRemoveEvent e) {
869 lastMapMode.remove(e.getRemovedLayer());
870 }
871
872 @Override
873 public void layerOrderChanged(LayerOrderChangeEvent e) {
874 // ignored
875 }
876
877}
Note: See TracBrowser for help on using the repository browser.