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

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

fix #13001 - Add MainPanel + some new methods (patch by michael2402, modified) - gsoc-core

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