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

Last change on this file since 8855 was 8836, checked in by Don-vip, 9 years ago

fix Checkstyle issues

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