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

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

sonar - various fixes + javadoc

  • Property svn:eol-style set to native
File size: 29.3 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 ConflictDialog conflictDialog;
136 /** Filter dialog */
137 public FilterDialog filterDialog;
138 /** Relation list dialog */
139 public RelationListDialog relationListDialog;
140 /** Validator dialog */
141 public ValidatorDialog validatorDialog;
142 /** Selection list dialog */
143 public SelectionListDialog selectionListDialog;
144 /** Properties dialog */
145 public PropertiesDialog propertiesDialog;
146 /** Map paint dialog */
147 public MapPaintDialog mapPaintDialog;
148 /** Notes dialog */
149 public 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 }
223 };
224 }
225 });
226
227 // JSplitPane supports F6 and F8 shortcuts by default, but we need them for Audio actions
228 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), new Object());
229 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), new Object());
230
231 add(splitPane, BorderLayout.CENTER);
232
233 dialogsPanel.setLayout(new BoxLayout(dialogsPanel, BoxLayout.Y_AXIS));
234 dialogsPanel.setPreferredSize(new Dimension(Main.pref.getInteger("toggleDialogs.width", DEF_TOGGLE_DLG_WIDTH), 0));
235 dialogsPanel.setMinimumSize(new Dimension(24, 0));
236 mapView.setMinimumSize(new Dimension(10, 0));
237
238 // toolBarActions, map mode buttons
239 addMapMode(new IconToggleButton(mapModeSelect = new SelectAction(this)));
240 addMapMode(new IconToggleButton(mapModeSelectLasso = new LassoModeAction(), true));
241 addMapMode(new IconToggleButton(mapModeDraw = new DrawAction(this)));
242 addMapMode(new IconToggleButton(mapModeZoom = new ZoomAction(this)));
243 addMapMode(new IconToggleButton(new DeleteAction(this), true));
244 addMapMode(new IconToggleButton(new ParallelWayAction(this), true));
245 addMapMode(new IconToggleButton(new ExtrudeAction(this), true));
246 addMapMode(new IconToggleButton(new ImproveWayAccuracyAction(Main.map), false));
247 toolBarActionsGroup.setSelected(allMapModeButtons.get(0).getModel(), true);
248 toolBarActions.setFloatable(false);
249
250 // toolBarToggles, toggle dialog buttons
251 LayerListDialog.createInstance(this);
252 addToggleDialog(LayerListDialog.getInstance());
253 addToggleDialog(propertiesDialog = new PropertiesDialog());
254 addToggleDialog(selectionListDialog = new SelectionListDialog());
255 addToggleDialog(relationListDialog = new RelationListDialog());
256 addToggleDialog(new MinimapDialog());
257 addToggleDialog(new CommandStackDialog());
258 addToggleDialog(new UserListDialog());
259 addToggleDialog(conflictDialog = new ConflictDialog());
260 addToggleDialog(validatorDialog = new ValidatorDialog());
261 addToggleDialog(filterDialog = new FilterDialog());
262 addToggleDialog(new ChangesetDialog(), true);
263 addToggleDialog(mapPaintDialog = new MapPaintDialog());
264 addToggleDialog(noteDialog = new NotesDialog());
265 toolBarToggle.setFloatable(false);
266
267 // status line below the map
268 statusLine = new MapStatus(this);
269 MapView.addLayerChangeListener(this);
270
271 boolean unregisterTab = Shortcut.findShortcut(KeyEvent.VK_TAB, 0) != null;
272 if (unregisterTab) {
273 for (JComponent c: allDialogButtons) c.setFocusTraversalKeysEnabled(false);
274 for (JComponent c: allMapModeButtons) c.setFocusTraversalKeysEnabled(false);
275 }
276
277 if (Main.pref.getBoolean("debug.advanced-keypress-detector.enable", true)) {
278 keyDetector.register();
279 }
280 }
281
282 public boolean selectSelectTool(boolean onlyIfModeless) {
283 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false))
284 return false;
285
286 return selectMapMode(mapModeSelect);
287 }
288
289 public boolean selectDrawTool(boolean onlyIfModeless) {
290 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false))
291 return false;
292
293 return selectMapMode(mapModeDraw);
294 }
295
296 public boolean selectZoomTool(boolean onlyIfModeless) {
297 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false))
298 return false;
299
300 return selectMapMode(mapModeZoom);
301 }
302
303 /**
304 * Called as some kind of destructor when the last layer has been removed.
305 * Delegates the call to all Destroyables within this component (e.g. MapModes)
306 */
307 @Override
308 public void destroy() {
309 MapView.removeLayerChangeListener(this);
310 dialogsPanel.destroy();
311 Main.pref.removePreferenceChangeListener(sidetoolbarPreferencesChangedListener);
312 for (int i = 0; i < toolBarActions.getComponentCount(); ++i) {
313 if (toolBarActions.getComponent(i) instanceof Destroyable) {
314 ((Destroyable) toolBarActions.getComponent(i)).destroy();
315 }
316 }
317 for (int i = 0; i < toolBarToggle.getComponentCount(); ++i) {
318 if (toolBarToggle.getComponent(i) instanceof Destroyable) {
319 ((Destroyable) toolBarToggle.getComponent(i)).destroy();
320 }
321 }
322
323 statusLine.destroy();
324 mapView.destroy();
325 keyDetector.unregister();
326 }
327
328 public Action getDefaultButtonAction() {
329 return ((AbstractButton) toolBarActions.getComponent(0)).getAction();
330 }
331
332 /**
333 * Open all ToggleDialogs that have their preferences property set. Close all others.
334 */
335 public void initializeDialogsPane() {
336 dialogsPanel.initialize(allDialogs);
337 }
338
339 public IconToggleButton addToggleDialog(final ToggleDialog dlg) {
340 return addToggleDialog(dlg, false);
341 }
342
343 /**
344 * Call this to add new toggle dialogs to the left button-list
345 * @param dlg The toggle dialog. It must not be in the list already.
346 * @param isExpert {@code true} if it's reserved to expert mode
347 * @return button allowing to toggle the dialog
348 */
349 public IconToggleButton addToggleDialog(final ToggleDialog dlg, boolean isExpert) {
350 final IconToggleButton button = new IconToggleButton(dlg.getToggleAction(), isExpert);
351 button.setShowHideButtonListener(dlg);
352 button.setInheritsPopupMenu(true);
353 dlg.setButton(button);
354 toolBarToggle.add(button);
355 allDialogs.add(dlg);
356 allDialogButtons.add(button);
357 button.applyButtonHiddenPreferences();
358 if (dialogsPanel.initialized) {
359 dialogsPanel.add(dlg);
360 }
361 return button;
362 }
363
364 public void addMapMode(IconToggleButton b) {
365 if (b.getAction() instanceof MapMode) {
366 mapModes.add((MapMode) b.getAction());
367 } else
368 throw new IllegalArgumentException("MapMode action must be subclass of MapMode");
369 allMapModeButtons.add(b);
370 toolBarActionsGroup.add(b);
371 toolBarActions.add(b);
372 b.applyButtonHiddenPreferences();
373 b.setInheritsPopupMenu(true);
374 }
375
376 /**
377 * Fires an property changed event "visible".
378 * @param aFlag {@code true} if display should be visible
379 */
380 @Override public void setVisible(boolean aFlag) {
381 boolean old = isVisible();
382 super.setVisible(aFlag);
383 if (old != aFlag) {
384 firePropertyChange("visible", old, aFlag);
385 }
386 }
387
388 /**
389 * Change the operating map mode for the view. Will call unregister on the
390 * old MapMode and register on the new one. Now this function also verifies
391 * if new map mode is correct mode for current layer and does not change mode
392 * in such cases.
393 * @param newMapMode The new mode to set.
394 * @return {@code true} if mode is really selected
395 */
396 public boolean selectMapMode(MapMode newMapMode) {
397 return selectMapMode(newMapMode, mapView.getActiveLayer());
398 }
399
400 /**
401 * Another version of the selectMapMode for changing layer action.
402 * Pass newly selected layer to this method.
403 * @param newMapMode The new mode to set.
404 * @param newLayer newly selected layer
405 * @return {@code true} if mode is really selected
406 */
407 public boolean selectMapMode(MapMode newMapMode, Layer newLayer) {
408 if (newMapMode == null || !newMapMode.layerIsSupported(newLayer))
409 return false;
410
411 MapMode oldMapMode = this.mapMode;
412 if (newMapMode == oldMapMode)
413 return true;
414 if (oldMapMode != null) {
415 oldMapMode.exitMode();
416 }
417 this.mapMode = newMapMode;
418 newMapMode.enterMode();
419 lastMapMode.put(newLayer, newMapMode);
420 fireMapModeChanged(oldMapMode, newMapMode);
421 return true;
422 }
423
424 /**
425 * Fill the given panel by adding all necessary components to the different
426 * locations.
427 *
428 * @param panel The container to fill. Must have a BorderLayout.
429 */
430 public void fillPanel(Container panel) {
431 panel.add(this, BorderLayout.CENTER);
432
433 /**
434 * sideToolBar: add map modes icons
435 */
436 if (Main.pref.getBoolean("sidetoolbar.mapmodes.visible", true)) {
437 toolBarActions.setAlignmentX(0.5f);
438 toolBarActions.setBorder(null);
439 toolBarActions.setInheritsPopupMenu(true);
440 sideToolBar.add(toolBarActions);
441 listAllMapModesButton.setAlignmentX(0.5f);
442 listAllMapModesButton.setBorder(null);
443 listAllMapModesButton.setFont(listAllMapModesButton.getFont().deriveFont(Font.PLAIN));
444 listAllMapModesButton.setInheritsPopupMenu(true);
445 sideToolBar.add(listAllMapModesButton);
446 }
447
448 /**
449 * sideToolBar: add toggle dialogs icons
450 */
451 if (Main.pref.getBoolean("sidetoolbar.toggledialogs.visible", true)) {
452 ((JToolBar) sideToolBar).addSeparator(new Dimension(0, 18));
453 toolBarToggle.setAlignmentX(0.5f);
454 toolBarToggle.setBorder(null);
455 toolBarToggle.setInheritsPopupMenu(true);
456 sideToolBar.add(toolBarToggle);
457 listAllToggleDialogsButton.setAlignmentX(0.5f);
458 listAllToggleDialogsButton.setBorder(null);
459 listAllToggleDialogsButton.setFont(listAllToggleDialogsButton.getFont().deriveFont(Font.PLAIN));
460 listAllToggleDialogsButton.setInheritsPopupMenu(true);
461 sideToolBar.add(listAllToggleDialogsButton);
462 }
463
464 /**
465 * sideToolBar: add dynamic popup menu
466 */
467 sideToolBar.setComponentPopupMenu(new SideToolbarPopupMenu());
468 ((JToolBar) sideToolBar).setFloatable(false);
469 sideToolBar.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 1));
470
471 /**
472 * sideToolBar: decide scroll- and visibility
473 */
474 if (Main.pref.getBoolean("sidetoolbar.scrollable", true)) {
475 final ScrollViewport svp = new ScrollViewport(sideToolBar, ScrollViewport.VERTICAL_DIRECTION);
476 svp.addMouseWheelListener(new MouseWheelListener() {
477 @Override
478 public void mouseWheelMoved(MouseWheelEvent e) {
479 svp.scroll(0, e.getUnitsToScroll() * 5);
480 }
481 });
482 sideToolBar = svp;
483 }
484 sideToolBar.setVisible(Main.pref.getBoolean("sidetoolbar.visible", true));
485 sidetoolbarPreferencesChangedListener = new Preferences.PreferenceChangedListener() {
486 @Override
487 public void preferenceChanged(PreferenceChangeEvent e) {
488 if ("sidetoolbar.visible".equals(e.getKey())) {
489 sideToolBar.setVisible(Main.pref.getBoolean("sidetoolbar.visible"));
490 }
491 }
492 };
493 Main.pref.addPreferenceChangeListener(sidetoolbarPreferencesChangedListener);
494
495 /**
496 * sideToolBar: add it to the panel
497 */
498 panel.add(sideToolBar, BorderLayout.WEST);
499
500 /**
501 * statusLine: add to panel
502 */
503 if (statusLine != null && Main.pref.getBoolean("statusline.visible", true)) {
504 panel.add(statusLine, BorderLayout.SOUTH);
505 }
506 }
507
508 private final class SideToolbarPopupMenu extends JPopupMenu {
509 private static final int staticMenuEntryCount = 2;
510 private final JCheckBoxMenuItem doNotHide = new JCheckBoxMenuItem(new AbstractAction(tr("Do not hide toolbar")) {
511 @Override
512 public void actionPerformed(ActionEvent e) {
513 boolean sel = ((JCheckBoxMenuItem) e.getSource()).getState();
514 Main.pref.put("sidetoolbar.always-visible", sel);
515 }
516 });
517 {
518 addPopupMenuListener(new PopupMenuListener() {
519 @Override
520 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
521 final Object src = ((JPopupMenu) e.getSource()).getInvoker();
522 if (src instanceof IconToggleButton) {
523 insert(new Separator(), 0);
524 insert(new AbstractAction() {
525 {
526 putValue(NAME, tr("Hide this button"));
527 putValue(SHORT_DESCRIPTION, tr("Click the arrow at the bottom to show it again."));
528 }
529
530 @Override
531 public void actionPerformed(ActionEvent e) {
532 ((IconToggleButton) src).setButtonHidden(true);
533 validateToolBarsVisibility();
534 }
535 }, 0);
536 }
537 doNotHide.setSelected(Main.pref.getBoolean("sidetoolbar.always-visible", true));
538 }
539
540 @Override
541 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
542 while (getComponentCount() > staticMenuEntryCount) {
543 remove(0);
544 }
545 }
546
547 @Override
548 public void popupMenuCanceled(PopupMenuEvent e) {}
549 });
550
551 add(new AbstractAction(tr("Hide edit toolbar")) {
552 @Override
553 public void actionPerformed(ActionEvent e) {
554 Main.pref.put("sidetoolbar.visible", false);
555 }
556 });
557 add(doNotHide);
558 }
559 }
560
561 class ListAllButtonsAction extends AbstractAction {
562
563 private JButton button;
564 private final transient Collection<? extends HideableButton> buttons;
565
566 ListAllButtonsAction(Collection<? extends HideableButton> buttons) {
567 this.buttons = buttons;
568 }
569
570 public void setButton(JButton button) {
571 this.button = button;
572 final ImageIcon icon = ImageProvider.get("audio-fwd");
573 putValue(SMALL_ICON, icon);
574 button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight() + 64));
575 }
576
577 @Override
578 public void actionPerformed(ActionEvent e) {
579 JPopupMenu menu = new JPopupMenu();
580 for (HideableButton b : buttons) {
581 final HideableButton t = b;
582 menu.add(new JCheckBoxMenuItem(new AbstractAction() {
583 {
584 putValue(NAME, t.getActionName());
585 putValue(SMALL_ICON, t.getIcon());
586 putValue(SELECTED_KEY, t.isButtonVisible());
587 putValue(SHORT_DESCRIPTION, tr("Hide or show this toggle button"));
588 }
589
590 @Override
591 public void actionPerformed(ActionEvent e) {
592 if ((Boolean) getValue(SELECTED_KEY)) {
593 t.showButton();
594 } else {
595 t.hideButton();
596 }
597 validateToolBarsVisibility();
598 }
599 }));
600 }
601 Rectangle bounds = button.getBounds();
602 menu.show(button, bounds.x + bounds.width, 0);
603 }
604 }
605
606 public void validateToolBarsVisibility() {
607 for (IconToggleButton b : allDialogButtons) {
608 b.applyButtonHiddenPreferences();
609 }
610 toolBarToggle.repaint();
611 for (IconToggleButton b : allMapModeButtons) {
612 b.applyButtonHiddenPreferences();
613 }
614 toolBarActions.repaint();
615 }
616
617 /**
618 * Replies the instance of a toggle dialog of type <code>type</code> managed by this map frame
619 *
620 * @param <T> toggle dialog type
621 * @param type the class of the toggle dialog, i.e. UserListDialog.class
622 * @return the instance of a toggle dialog of type <code>type</code> managed by this
623 * map frame; null, if no such dialog exists
624 *
625 */
626 public <T> T getToggleDialog(Class<T> type) {
627 return dialogsPanel.getToggleDialog(type);
628 }
629
630 public void setDialogsPanelVisible(boolean visible) {
631 rememberToggleDialogWidth();
632 dialogsPanel.setVisible(visible);
633 splitPane.setDividerLocation(visible ? splitPane.getWidth()-Main.pref.getInteger("toggleDialogs.width", DEF_TOGGLE_DLG_WIDTH) : 0);
634 splitPane.setDividerSize(visible ? 5 : 0);
635 }
636
637 /**
638 * Remember the current width of the (possibly resized) toggle dialog area
639 */
640 public void rememberToggleDialogWidth() {
641 if (dialogsPanel.isVisible()) {
642 Main.pref.putInteger("toggleDialogs.width", splitPane.getWidth()-splitPane.getDividerLocation());
643 }
644 }
645
646 /**
647 * Remove panel from top of MapView by class
648 * @param type type of panel
649 */
650 public void removeTopPanel(Class<?> type) {
651 int n = leftPanel.getComponentCount();
652 for (int i = 0; i < n; i++) {
653 Component c = leftPanel.getComponent(i);
654 if (type.isInstance(c)) {
655 leftPanel.remove(i);
656 leftPanel.doLayout();
657 return;
658 }
659 }
660 }
661
662 /**
663 * Find panel on top of MapView by class
664 * @param <T> type
665 * @param type type of panel
666 * @return found panel
667 */
668 public <T> T getTopPanel(Class<T> type) {
669 int n = leftPanel.getComponentCount();
670 for (int i = 0; i < n; i++) {
671 Component c = leftPanel.getComponent(i);
672 if (type.isInstance(c))
673 return type.cast(c);
674 }
675 return null;
676 }
677
678 /**
679 * Add component {@code c} on top of MapView
680 * @param c component
681 */
682 public void addTopPanel(Component c) {
683 leftPanel.add(c, GBC.eol().fill(GBC.HORIZONTAL), leftPanel.getComponentCount()-1);
684 leftPanel.doLayout();
685 c.doLayout();
686 }
687
688 /**
689 * Interface to notify listeners of the change of the mapMode.
690 */
691 public interface MapModeChangeListener {
692 /**
693 * Trigerred when map mode changes.
694 * @param oldMapMode old map mode
695 * @param newMapMode new map mode
696 */
697 void mapModeChange(MapMode oldMapMode, MapMode newMapMode);
698 }
699
700 /**
701 * the mapMode listeners
702 */
703 private static final CopyOnWriteArrayList<MapModeChangeListener> mapModeChangeListeners = new CopyOnWriteArrayList<>();
704
705 private transient PreferenceChangedListener sidetoolbarPreferencesChangedListener;
706 /**
707 * Adds a mapMode change listener
708 *
709 * @param listener the listener. Ignored if null or already registered.
710 */
711 public static void addMapModeChangeListener(MapModeChangeListener listener) {
712 if (listener != null) {
713 mapModeChangeListeners.addIfAbsent(listener);
714 }
715 }
716
717 /**
718 * Removes a mapMode change listener
719 *
720 * @param listener the listener. Ignored if null or already registered.
721 */
722 public static void removeMapModeChangeListener(MapModeChangeListener listener) {
723 mapModeChangeListeners.remove(listener);
724 }
725
726 protected static void fireMapModeChanged(MapMode oldMapMode, MapMode newMapMode) {
727 for (MapModeChangeListener l : mapModeChangeListeners) {
728 l.mapModeChange(oldMapMode, newMapMode);
729 }
730 }
731
732 @Override
733 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
734 boolean modeChanged = false;
735 if (mapMode == null || !mapMode.layerIsSupported(newLayer)) {
736 MapMode newMapMode = getLastMapMode(newLayer);
737 modeChanged = newMapMode != mapMode;
738 if (newMapMode != null) {
739 // it would be nice to select first supported mode when layer is first selected,
740 // but it don't work well with for example editgpx layer
741 selectMapMode(newMapMode, newLayer);
742 } else if (mapMode != null) {
743 mapMode.exitMode(); // if new mode is null - simply exit from previous mode
744 }
745 }
746 // if this is really a change (and not the first active layer)
747 if (oldLayer != null) {
748 if (!modeChanged && mapMode != null) {
749 // Let mapmodes know about new active layer
750 mapMode.exitMode();
751 mapMode.enterMode();
752 }
753 // invalidate repaint cache
754 Main.map.mapView.preferenceChanged(null);
755 }
756
757 // After all listeners notice new layer, some buttons will be disabled/enabled
758 // and possibly need to be hidden/shown.
759 SwingUtilities.invokeLater(new Runnable() {
760 @Override public void run() {
761 validateToolBarsVisibility();
762 }
763 });
764 }
765
766 private MapMode getLastMapMode(Layer newLayer) {
767 MapMode mode = lastMapMode.get(newLayer);
768 if (mode == null) {
769 // if no action is selected - try to select default action
770 Action defaultMode = getDefaultButtonAction();
771 if (defaultMode instanceof MapMode && ((MapMode) defaultMode).layerIsSupported(newLayer)) {
772 mode = (MapMode) defaultMode;
773 }
774 }
775 return mode;
776 }
777
778 @Override
779 public void layerAdded(Layer newLayer) { }
780
781 @Override
782 public void layerRemoved(Layer oldLayer) {
783 lastMapMode.remove(oldLayer);
784 }
785}
Note: See TracBrowser for help on using the repository browser.