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

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

findbugs - UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR

  • Property svn:eol-style set to native
File size: 29.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Container;
9import java.awt.Dimension;
10import java.awt.Font;
11import java.awt.GraphicsEnvironment;
12import java.awt.GridBagLayout;
13import java.awt.Rectangle;
14import java.awt.event.ActionEvent;
15import java.awt.event.KeyEvent;
16import java.awt.event.MouseWheelEvent;
17import java.awt.event.MouseWheelListener;
18import java.util.ArrayList;
19import java.util.Collection;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23import java.util.concurrent.CopyOnWriteArrayList;
24
25import javax.swing.AbstractAction;
26import javax.swing.AbstractButton;
27import javax.swing.Action;
28import javax.swing.BorderFactory;
29import javax.swing.BoxLayout;
30import javax.swing.ButtonGroup;
31import javax.swing.ImageIcon;
32import javax.swing.JButton;
33import javax.swing.JCheckBoxMenuItem;
34import javax.swing.JComponent;
35import javax.swing.JPanel;
36import javax.swing.JPopupMenu;
37import javax.swing.JSplitPane;
38import javax.swing.JToolBar;
39import javax.swing.KeyStroke;
40import javax.swing.SwingUtilities;
41import javax.swing.border.Border;
42import javax.swing.event.PopupMenuEvent;
43import javax.swing.event.PopupMenuListener;
44import javax.swing.plaf.basic.BasicSplitPaneDivider;
45import javax.swing.plaf.basic.BasicSplitPaneUI;
46
47import org.openstreetmap.josm.Main;
48import org.openstreetmap.josm.actions.LassoModeAction;
49import org.openstreetmap.josm.actions.mapmode.DeleteAction;
50import org.openstreetmap.josm.actions.mapmode.DrawAction;
51import org.openstreetmap.josm.actions.mapmode.ExtrudeAction;
52import org.openstreetmap.josm.actions.mapmode.ImproveWayAccuracyAction;
53import org.openstreetmap.josm.actions.mapmode.MapMode;
54import org.openstreetmap.josm.actions.mapmode.ParallelWayAction;
55import org.openstreetmap.josm.actions.mapmode.SelectAction;
56import org.openstreetmap.josm.actions.mapmode.ZoomAction;
57import org.openstreetmap.josm.data.Preferences;
58import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
59import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
60import org.openstreetmap.josm.data.ViewportData;
61import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
62import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
63import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
64import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
65import org.openstreetmap.josm.gui.dialogs.DialogsPanel;
66import org.openstreetmap.josm.gui.dialogs.FilterDialog;
67import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
68import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
69import org.openstreetmap.josm.gui.dialogs.MinimapDialog;
70import org.openstreetmap.josm.gui.dialogs.NotesDialog;
71import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
72import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
73import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
74import org.openstreetmap.josm.gui.dialogs.UserListDialog;
75import org.openstreetmap.josm.gui.dialogs.ValidatorDialog;
76import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;
77import org.openstreetmap.josm.gui.layer.Layer;
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, LayerChangeListener {
92
93 /**
94 * The current mode, this frame operates.
95 */
96 public MapMode mapMode;
97
98 /**
99 * The view control displayed.
100 */
101 public final MapView mapView;
102
103 /**
104 * This object allows to detect key press and release events
105 */
106 public final transient AdvancedKeyPressDetector keyDetector = new AdvancedKeyPressDetector();
107
108 /**
109 * The toolbar with the action icons. To add new toggle dialog buttons,
110 * use addToggleDialog, to add a new map mode button use addMapMode.
111 */
112 private JComponent sideToolBar = new JToolBar(JToolBar.VERTICAL);
113 private final ButtonGroup toolBarActionsGroup = new ButtonGroup();
114 private final JToolBar toolBarActions = new JToolBar(JToolBar.VERTICAL);
115 private final JToolBar toolBarToggle = new JToolBar(JToolBar.VERTICAL);
116
117 private final List<ToggleDialog> allDialogs = new ArrayList<>();
118 private final List<MapMode> mapModes = new ArrayList<>();
119 private final List<IconToggleButton> allDialogButtons = new ArrayList<>();
120 public final List<IconToggleButton> allMapModeButtons = new ArrayList<>();
121
122 private final ListAllButtonsAction listAllDialogsAction = new ListAllButtonsAction(allDialogButtons);
123 private final ListAllButtonsAction listAllMapModesAction = new ListAllButtonsAction(allMapModeButtons);
124 private final JButton listAllToggleDialogsButton = new JButton(listAllDialogsAction);
125 private final JButton listAllMapModesButton = new JButton(listAllMapModesAction);
126
127 {
128 listAllDialogsAction.setButton(listAllToggleDialogsButton);
129 listAllMapModesAction.setButton(listAllMapModesButton);
130 }
131
132 // Toggle dialogs
133
134 /** Conflict dialog */
135 public final ConflictDialog conflictDialog;
136 /** Filter dialog */
137 public final FilterDialog filterDialog;
138 /** Relation list dialog */
139 public final RelationListDialog relationListDialog;
140 /** Validator dialog */
141 public final ValidatorDialog validatorDialog;
142 /** Selection list dialog */
143 public final SelectionListDialog selectionListDialog;
144 /** Properties dialog */
145 public final PropertiesDialog propertiesDialog;
146 /** Map paint dialog */
147 public final MapPaintDialog mapPaintDialog;
148 /** Notes dialog */
149 public final NotesDialog noteDialog;
150
151 // Map modes
152
153 /** Select mode */
154 public final SelectAction mapModeSelect;
155 /** Draw mode */
156 public final DrawAction mapModeDraw;
157 /** Zoom mode */
158 public final ZoomAction mapModeZoom;
159 /** Select Lasso mode */
160 public LassoModeAction mapModeSelectLasso;
161
162 private final transient Map<Layer, MapMode> lastMapMode = new HashMap<>();
163
164 /**
165 * The status line below the map
166 */
167 public MapStatus statusLine;
168
169 /**
170 * The split pane with the mapview (leftPanel) and toggle dialogs (dialogsPanel).
171 */
172 private final JSplitPane splitPane;
173 private final JPanel leftPanel;
174 private final DialogsPanel dialogsPanel;
175
176 /**
177 * Default width of the toggle dialog area.
178 */
179 public static final int DEF_TOGGLE_DLG_WIDTH = 330;
180
181 /**
182 * Constructs a new {@code MapFrame}.
183 * @param contentPane The content pane used to register shortcuts in its
184 * {@link javax.swing.InputMap} and {@link javax.swing.ActionMap}
185 * @param viewportData the initial viewport of the map. Can be null, then
186 * the viewport is derived from the layer data.
187 */
188 public MapFrame(JPanel contentPane, ViewportData viewportData) {
189 setSize(400, 400);
190 setLayout(new BorderLayout());
191
192 mapView = new MapView(contentPane, viewportData);
193 if (!GraphicsEnvironment.isHeadless()) {
194 new FileDrop(mapView);
195 }
196
197 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
198
199 leftPanel = new JPanel(new GridBagLayout());
200 leftPanel.add(mapView, GBC.std().fill());
201 splitPane.setLeftComponent(leftPanel);
202
203 dialogsPanel = new DialogsPanel(splitPane);
204 splitPane.setRightComponent(dialogsPanel);
205
206 /**
207 * All additional space goes to the mapView
208 */
209 splitPane.setResizeWeight(1.0);
210
211 /**
212 * Some beautifications.
213 */
214 splitPane.setDividerSize(5);
215 splitPane.setBorder(null);
216 splitPane.setUI(new BasicSplitPaneUI() {
217 @Override
218 public BasicSplitPaneDivider createDefaultDivider() {
219 return new BasicSplitPaneDivider(this) {
220 @Override
221 public void setBorder(Border b) {
222 // Do nothing
223 }
224 };
225 }
226 });
227
228 // JSplitPane supports F6 and F8 shortcuts by default, but we need them for Audio actions
229 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), new Object());
230 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), new Object());
231
232 add(splitPane, BorderLayout.CENTER);
233
234 dialogsPanel.setLayout(new BoxLayout(dialogsPanel, BoxLayout.Y_AXIS));
235 dialogsPanel.setPreferredSize(new Dimension(Main.pref.getInteger("toggleDialogs.width", DEF_TOGGLE_DLG_WIDTH), 0));
236 dialogsPanel.setMinimumSize(new Dimension(24, 0));
237 mapView.setMinimumSize(new Dimension(10, 0));
238
239 // toolBarActions, map mode buttons
240 mapModeSelect = new SelectAction(this);
241 mapModeSelectLasso = new LassoModeAction();
242 mapModeDraw = new DrawAction(this);
243 mapModeZoom = new ZoomAction(this);
244
245 addMapMode(new IconToggleButton(mapModeSelect));
246 addMapMode(new IconToggleButton(mapModeSelectLasso, true));
247 addMapMode(new IconToggleButton(mapModeDraw));
248 addMapMode(new IconToggleButton(mapModeZoom, true));
249 addMapMode(new IconToggleButton(new DeleteAction(this), true));
250 addMapMode(new IconToggleButton(new ParallelWayAction(this), true));
251 addMapMode(new IconToggleButton(new ExtrudeAction(this), true));
252 addMapMode(new IconToggleButton(new ImproveWayAccuracyAction(Main.map), false));
253 toolBarActionsGroup.setSelected(allMapModeButtons.get(0).getModel(), true);
254 toolBarActions.setFloatable(false);
255
256 // toolBarToggles, toggle dialog buttons
257 LayerListDialog.createInstance(this);
258 propertiesDialog = new PropertiesDialog();
259 selectionListDialog = new SelectionListDialog();
260 relationListDialog = new RelationListDialog();
261 conflictDialog = new ConflictDialog();
262 validatorDialog = new ValidatorDialog();
263 filterDialog = new FilterDialog();
264 mapPaintDialog = new MapPaintDialog();
265 noteDialog = new NotesDialog();
266
267 addToggleDialog(LayerListDialog.getInstance());
268 addToggleDialog(propertiesDialog);
269 addToggleDialog(selectionListDialog);
270 addToggleDialog(relationListDialog);
271 addToggleDialog(new MinimapDialog());
272 addToggleDialog(new CommandStackDialog());
273 addToggleDialog(new UserListDialog());
274 addToggleDialog(conflictDialog);
275 addToggleDialog(validatorDialog);
276 addToggleDialog(filterDialog);
277 addToggleDialog(new ChangesetDialog(), true);
278 addToggleDialog(mapPaintDialog);
279 addToggleDialog(noteDialog);
280 toolBarToggle.setFloatable(false);
281
282 // status line below the map
283 statusLine = new MapStatus(this);
284 MapView.addLayerChangeListener(this);
285
286 boolean unregisterTab = Shortcut.findShortcut(KeyEvent.VK_TAB, 0) != null;
287 if (unregisterTab) {
288 for (JComponent c: allDialogButtons) {
289 c.setFocusTraversalKeysEnabled(false);
290 }
291 for (JComponent c: allMapModeButtons) {
292 c.setFocusTraversalKeysEnabled(false);
293 }
294 }
295
296 if (Main.pref.getBoolean("debug.advanced-keypress-detector.enable", true)) {
297 keyDetector.register();
298 }
299 }
300
301 public boolean selectSelectTool(boolean onlyIfModeless) {
302 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false))
303 return false;
304
305 return selectMapMode(mapModeSelect);
306 }
307
308 public boolean selectDrawTool(boolean onlyIfModeless) {
309 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false))
310 return false;
311
312 return selectMapMode(mapModeDraw);
313 }
314
315 public boolean selectZoomTool(boolean onlyIfModeless) {
316 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false))
317 return false;
318
319 return selectMapMode(mapModeZoom);
320 }
321
322 /**
323 * Called as some kind of destructor when the last layer has been removed.
324 * Delegates the call to all Destroyables within this component (e.g. MapModes)
325 */
326 @Override
327 public void destroy() {
328 MapView.removeLayerChangeListener(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.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(new MouseWheelListener() {
496 @Override
497 public void mouseWheelMoved(MouseWheelEvent e) {
498 svp.scroll(0, e.getUnitsToScroll() * 5);
499 }
500 });
501 sideToolBar = svp;
502 }
503 sideToolBar.setVisible(Main.pref.getBoolean("sidetoolbar.visible", true));
504 sidetoolbarPreferencesChangedListener = new Preferences.PreferenceChangedListener() {
505 @Override
506 public void preferenceChanged(PreferenceChangeEvent e) {
507 if ("sidetoolbar.visible".equals(e.getKey())) {
508 sideToolBar.setVisible(Main.pref.getBoolean("sidetoolbar.visible"));
509 }
510 }
511 };
512 Main.pref.addPreferenceChangeListener(sidetoolbarPreferencesChangedListener);
513
514 /**
515 * sideToolBar: add it to the panel
516 */
517 panel.add(sideToolBar, BorderLayout.WEST);
518
519 /**
520 * statusLine: add to panel
521 */
522 if (statusLine != null && Main.pref.getBoolean("statusline.visible", true)) {
523 panel.add(statusLine, BorderLayout.SOUTH);
524 }
525 }
526
527 private final class SideToolbarPopupMenu extends JPopupMenu {
528 private static final int staticMenuEntryCount = 2;
529 private final JCheckBoxMenuItem doNotHide = new JCheckBoxMenuItem(new AbstractAction(tr("Do not hide toolbar")) {
530 @Override
531 public void actionPerformed(ActionEvent e) {
532 boolean sel = ((JCheckBoxMenuItem) e.getSource()).getState();
533 Main.pref.put("sidetoolbar.always-visible", sel);
534 }
535 });
536 {
537 addPopupMenuListener(new PopupMenuListener() {
538 @Override
539 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
540 final Object src = ((JPopupMenu) e.getSource()).getInvoker();
541 if (src instanceof IconToggleButton) {
542 insert(new Separator(), 0);
543 insert(new AbstractAction() {
544 {
545 putValue(NAME, tr("Hide this button"));
546 putValue(SHORT_DESCRIPTION, tr("Click the arrow at the bottom to show it again."));
547 }
548
549 @Override
550 public void actionPerformed(ActionEvent e) {
551 ((IconToggleButton) src).setButtonHidden(true);
552 validateToolBarsVisibility();
553 }
554 }, 0);
555 }
556 doNotHide.setSelected(Main.pref.getBoolean("sidetoolbar.always-visible", true));
557 }
558
559 @Override
560 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
561 while (getComponentCount() > staticMenuEntryCount) {
562 remove(0);
563 }
564 }
565
566 @Override
567 public void popupMenuCanceled(PopupMenuEvent e) {
568 // Do nothing
569 }
570 });
571
572 add(new AbstractAction(tr("Hide edit toolbar")) {
573 @Override
574 public void actionPerformed(ActionEvent e) {
575 Main.pref.put("sidetoolbar.visible", false);
576 }
577 });
578 add(doNotHide);
579 }
580 }
581
582 class ListAllButtonsAction extends AbstractAction {
583
584 private JButton button;
585 private final transient Collection<? extends HideableButton> buttons;
586
587 ListAllButtonsAction(Collection<? extends HideableButton> buttons) {
588 this.buttons = buttons;
589 }
590
591 public void setButton(JButton button) {
592 this.button = button;
593 final ImageIcon icon = ImageProvider.get("audio-fwd");
594 putValue(SMALL_ICON, icon);
595 button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight() + 64));
596 }
597
598 @Override
599 public void actionPerformed(ActionEvent e) {
600 JPopupMenu menu = new JPopupMenu();
601 for (HideableButton b : buttons) {
602 final HideableButton t = b;
603 menu.add(new JCheckBoxMenuItem(new AbstractAction() {
604 {
605 putValue(NAME, t.getActionName());
606 putValue(SMALL_ICON, t.getIcon());
607 putValue(SELECTED_KEY, t.isButtonVisible());
608 putValue(SHORT_DESCRIPTION, tr("Hide or show this toggle button"));
609 }
610
611 @Override
612 public void actionPerformed(ActionEvent e) {
613 if ((Boolean) getValue(SELECTED_KEY)) {
614 t.showButton();
615 } else {
616 t.hideButton();
617 }
618 validateToolBarsVisibility();
619 }
620 }));
621 }
622 if (button != null) {
623 Rectangle bounds = button.getBounds();
624 menu.show(button, bounds.x + bounds.width, 0);
625 }
626 }
627 }
628
629 public void validateToolBarsVisibility() {
630 for (IconToggleButton b : allDialogButtons) {
631 b.applyButtonHiddenPreferences();
632 }
633 toolBarToggle.repaint();
634 for (IconToggleButton b : allMapModeButtons) {
635 b.applyButtonHiddenPreferences();
636 }
637 toolBarActions.repaint();
638 }
639
640 /**
641 * Replies the instance of a toggle dialog of type <code>type</code> managed by this map frame
642 *
643 * @param <T> toggle dialog type
644 * @param type the class of the toggle dialog, i.e. UserListDialog.class
645 * @return the instance of a toggle dialog of type <code>type</code> managed by this
646 * map frame; null, if no such dialog exists
647 *
648 */
649 public <T> T getToggleDialog(Class<T> type) {
650 return dialogsPanel.getToggleDialog(type);
651 }
652
653 public void setDialogsPanelVisible(boolean visible) {
654 rememberToggleDialogWidth();
655 dialogsPanel.setVisible(visible);
656 splitPane.setDividerLocation(visible ? splitPane.getWidth()-Main.pref.getInteger("toggleDialogs.width", DEF_TOGGLE_DLG_WIDTH) : 0);
657 splitPane.setDividerSize(visible ? 5 : 0);
658 }
659
660 /**
661 * Remember the current width of the (possibly resized) toggle dialog area
662 */
663 public void rememberToggleDialogWidth() {
664 if (dialogsPanel.isVisible()) {
665 Main.pref.putInteger("toggleDialogs.width", splitPane.getWidth()-splitPane.getDividerLocation());
666 }
667 }
668
669 /**
670 * Remove panel from top of MapView by class
671 * @param type type of panel
672 */
673 public void removeTopPanel(Class<?> type) {
674 int n = leftPanel.getComponentCount();
675 for (int i = 0; i < n; i++) {
676 Component c = leftPanel.getComponent(i);
677 if (type.isInstance(c)) {
678 leftPanel.remove(i);
679 leftPanel.doLayout();
680 return;
681 }
682 }
683 }
684
685 /**
686 * Find panel on top of MapView by class
687 * @param <T> type
688 * @param type type of panel
689 * @return found panel
690 */
691 public <T> T getTopPanel(Class<T> type) {
692 int n = leftPanel.getComponentCount();
693 for (int i = 0; i < n; i++) {
694 Component c = leftPanel.getComponent(i);
695 if (type.isInstance(c))
696 return type.cast(c);
697 }
698 return null;
699 }
700
701 /**
702 * Add component {@code c} on top of MapView
703 * @param c component
704 */
705 public void addTopPanel(Component c) {
706 leftPanel.add(c, GBC.eol().fill(GBC.HORIZONTAL), leftPanel.getComponentCount()-1);
707 leftPanel.doLayout();
708 c.doLayout();
709 }
710
711 /**
712 * Interface to notify listeners of the change of the mapMode.
713 */
714 public interface MapModeChangeListener {
715 /**
716 * Trigerred when map mode changes.
717 * @param oldMapMode old map mode
718 * @param newMapMode new map mode
719 */
720 void mapModeChange(MapMode oldMapMode, MapMode newMapMode);
721 }
722
723 /**
724 * the mapMode listeners
725 */
726 private static final CopyOnWriteArrayList<MapModeChangeListener> mapModeChangeListeners = new CopyOnWriteArrayList<>();
727
728 private transient PreferenceChangedListener sidetoolbarPreferencesChangedListener;
729 /**
730 * Adds a mapMode change listener
731 *
732 * @param listener the listener. Ignored if null or already registered.
733 */
734 public static void addMapModeChangeListener(MapModeChangeListener listener) {
735 if (listener != null) {
736 mapModeChangeListeners.addIfAbsent(listener);
737 }
738 }
739
740 /**
741 * Removes a mapMode change listener
742 *
743 * @param listener the listener. Ignored if null or already registered.
744 */
745 public static void removeMapModeChangeListener(MapModeChangeListener listener) {
746 mapModeChangeListeners.remove(listener);
747 }
748
749 protected static void fireMapModeChanged(MapMode oldMapMode, MapMode newMapMode) {
750 for (MapModeChangeListener l : mapModeChangeListeners) {
751 l.mapModeChange(oldMapMode, newMapMode);
752 }
753 }
754
755 @Override
756 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
757 boolean modeChanged = false;
758 if (mapMode == null || !mapMode.layerIsSupported(newLayer)) {
759 MapMode newMapMode = getLastMapMode(newLayer);
760 modeChanged = newMapMode != mapMode;
761 if (newMapMode != null) {
762 // it would be nice to select first supported mode when layer is first selected,
763 // but it don't work well with for example editgpx layer
764 selectMapMode(newMapMode, newLayer);
765 } else if (mapMode != null) {
766 mapMode.exitMode(); // if new mode is null - simply exit from previous mode
767 }
768 }
769 // if this is really a change (and not the first active layer)
770 if (oldLayer != null) {
771 if (!modeChanged && mapMode != null) {
772 // Let mapmodes know about new active layer
773 mapMode.exitMode();
774 mapMode.enterMode();
775 }
776 // invalidate repaint cache
777 Main.map.mapView.preferenceChanged(null);
778 }
779
780 // After all listeners notice new layer, some buttons will be disabled/enabled
781 // and possibly need to be hidden/shown.
782 SwingUtilities.invokeLater(new Runnable() {
783 @Override public void run() {
784 validateToolBarsVisibility();
785 }
786 });
787 }
788
789 private MapMode getLastMapMode(Layer newLayer) {
790 MapMode mode = lastMapMode.get(newLayer);
791 if (mode == null) {
792 // if no action is selected - try to select default action
793 Action defaultMode = getDefaultButtonAction();
794 if (defaultMode instanceof MapMode && ((MapMode) defaultMode).layerIsSupported(newLayer)) {
795 mode = (MapMode) defaultMode;
796 }
797 }
798 return mode;
799 }
800
801 @Override
802 public void layerAdded(Layer newLayer) {
803 // Do nothing
804 }
805
806 @Override
807 public void layerRemoved(Layer oldLayer) {
808 lastMapMode.remove(oldLayer);
809 }
810}
Note: See TracBrowser for help on using the repository browser.