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

Last change on this file since 8283 was 8201, checked in by simon04, 9 years ago

fix #10632 - Add expand icon and separator in edit toolbar

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