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

Last change on this file since 10604 was 10604, checked in by Don-vip, 8 years ago

fix #12478, fix #12565, fix #11114 - Use ​Swing Copy/Paste instead of CopyAction/PasteAction with custom buffer (patch by michael2402, modified) - gsoc-core

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