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

Last change on this file since 11846 was 11713, checked in by Don-vip, 7 years ago

add Ant target to run PMD (only few rules for now), fix violations

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