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

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

checkstyle: enable relevant whitespace checks and fix them

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