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

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

fix #13380 - Create removeToggleDialog method in MapFrame

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