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

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

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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