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

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

refactoring - global simplification of use of setLayout method - simply pass layout to JPanel constructor

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