001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.BorderLayout; 007import java.awt.Component; 008import java.awt.Container; 009import java.awt.Dimension; 010import java.awt.Font; 011import java.awt.GraphicsEnvironment; 012import java.awt.GridBagLayout; 013import java.awt.Rectangle; 014import java.awt.event.ActionEvent; 015import java.awt.event.KeyEvent; 016import java.awt.event.MouseWheelEvent; 017import java.awt.event.MouseWheelListener; 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023import java.util.concurrent.CopyOnWriteArrayList; 024 025import javax.swing.AbstractAction; 026import javax.swing.AbstractButton; 027import javax.swing.Action; 028import javax.swing.BorderFactory; 029import javax.swing.BoxLayout; 030import javax.swing.ButtonGroup; 031import javax.swing.ImageIcon; 032import javax.swing.JButton; 033import javax.swing.JCheckBoxMenuItem; 034import javax.swing.JComponent; 035import javax.swing.JPanel; 036import javax.swing.JPopupMenu; 037import javax.swing.JSplitPane; 038import javax.swing.JToolBar; 039import javax.swing.KeyStroke; 040import javax.swing.SwingUtilities; 041import javax.swing.border.Border; 042import javax.swing.event.PopupMenuEvent; 043import javax.swing.event.PopupMenuListener; 044import javax.swing.plaf.basic.BasicSplitPaneDivider; 045import javax.swing.plaf.basic.BasicSplitPaneUI; 046 047import org.openstreetmap.josm.Main; 048import org.openstreetmap.josm.actions.LassoModeAction; 049import org.openstreetmap.josm.actions.mapmode.DeleteAction; 050import org.openstreetmap.josm.actions.mapmode.DrawAction; 051import org.openstreetmap.josm.actions.mapmode.ExtrudeAction; 052import org.openstreetmap.josm.actions.mapmode.ImproveWayAccuracyAction; 053import org.openstreetmap.josm.actions.mapmode.MapMode; 054import org.openstreetmap.josm.actions.mapmode.ParallelWayAction; 055import org.openstreetmap.josm.actions.mapmode.SelectAction; 056import org.openstreetmap.josm.actions.mapmode.ZoomAction; 057import org.openstreetmap.josm.data.Preferences; 058import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent; 059import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener; 060import org.openstreetmap.josm.data.ViewportData; 061import org.openstreetmap.josm.gui.MapView.LayerChangeListener; 062import org.openstreetmap.josm.gui.dialogs.ChangesetDialog; 063import org.openstreetmap.josm.gui.dialogs.CommandStackDialog; 064import org.openstreetmap.josm.gui.dialogs.ConflictDialog; 065import org.openstreetmap.josm.gui.dialogs.DialogsPanel; 066import org.openstreetmap.josm.gui.dialogs.FilterDialog; 067import org.openstreetmap.josm.gui.dialogs.LayerListDialog; 068import org.openstreetmap.josm.gui.dialogs.MapPaintDialog; 069import org.openstreetmap.josm.gui.dialogs.MinimapDialog; 070import org.openstreetmap.josm.gui.dialogs.NotesDialog; 071import org.openstreetmap.josm.gui.dialogs.RelationListDialog; 072import org.openstreetmap.josm.gui.dialogs.SelectionListDialog; 073import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 074import org.openstreetmap.josm.gui.dialogs.UserListDialog; 075import org.openstreetmap.josm.gui.dialogs.ValidatorDialog; 076import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog; 077import org.openstreetmap.josm.gui.layer.Layer; 078import org.openstreetmap.josm.gui.util.AdvancedKeyPressDetector; 079import org.openstreetmap.josm.tools.Destroyable; 080import org.openstreetmap.josm.tools.GBC; 081import org.openstreetmap.josm.tools.ImageProvider; 082import org.openstreetmap.josm.tools.Shortcut; 083 084 085/** 086 * One Map frame with one dataset behind. This is the container gui class whose 087 * display can be set to the different views. 088 * 089 * @author imi 090 */ 091public class MapFrame extends JPanel implements Destroyable, LayerChangeListener { 092 093 /** 094 * The current mode, this frame operates. 095 */ 096 public MapMode mapMode; 097 098 /** 099 * The view control displayed. 100 */ 101 public final MapView mapView; 102 103 /** 104 * This object allows to detect key press and release events 105 */ 106 public final transient AdvancedKeyPressDetector keyDetector = new AdvancedKeyPressDetector(); 107 108 /** 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. 111 */ 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); 116 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<>(); 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); 126 127 { 128 listAllDialogsAction.setButton(listAllToggleDialogsButton); 129 listAllMapModesAction.setButton(listAllMapModesButton); 130 } 131 132 // Toggle dialogs 133 public ConflictDialog conflictDialog; 134 public FilterDialog filterDialog; 135 public RelationListDialog relationListDialog; 136 public ValidatorDialog validatorDialog; 137 public SelectionListDialog selectionListDialog; 138 public PropertiesDialog propertiesDialog; 139 public NotesDialog noteDialog; 140 141 // Map modes 142 public final SelectAction mapModeSelect; 143 public final DrawAction mapModeDraw; 144 public final ZoomAction mapModeZoom; 145 public LassoModeAction mapModeSelectLasso; 146 147 private final transient Map<Layer, MapMode> lastMapMode = new HashMap<>(); 148 149 /** 150 * The status line below the map 151 */ 152 public MapStatus statusLine; 153 154 /** 155 * The split pane with the mapview (leftPanel) and toggle dialogs (dialogsPanel). 156 */ 157 private final JSplitPane splitPane; 158 private final JPanel leftPanel; 159 private final DialogsPanel dialogsPanel; 160 161 /** 162 * Default width of the toggle dialog area. 163 */ 164 public static final int DEF_TOGGLE_DLG_WIDTH = 330; 165 166 /** 167 * Constructs a new {@code MapFrame}. 168 * @param contentPane The content pane used to register shortcuts in its 169 * {@link javax.swing.InputMap} and {@link javax.swing.ActionMap} 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) { 174 setSize(400, 400); 175 setLayout(new BorderLayout()); 176 177 mapView = new MapView(contentPane, viewportData); 178 if (!GraphicsEnvironment.isHeadless()) { 179 new FileDrop(mapView); 180 } 181 182 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true); 183 184 leftPanel = new JPanel(); 185 leftPanel.setLayout(new GridBagLayout()); 186 leftPanel.add(mapView, GBC.std().fill()); 187 splitPane.setLeftComponent(leftPanel); 188 189 dialogsPanel = new DialogsPanel(splitPane); 190 splitPane.setRightComponent(dialogsPanel); 191 192 /** 193 * All additional space goes to the mapView 194 */ 195 splitPane.setResizeWeight(1.0); 196 197 /** 198 * Some beautifications. 199 */ 200 splitPane.setDividerSize(5); 201 splitPane.setBorder(null); 202 splitPane.setUI(new BasicSplitPaneUI() { 203 @Override 204 public BasicSplitPaneDivider createDefaultDivider() { 205 return new BasicSplitPaneDivider(this) { 206 @Override 207 public void setBorder(Border b) { 208 } 209 }; 210 } 211 }); 212 213 // JSplitPane supports F6 and F8 shortcuts by default, but we need them for Audio actions 214 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), new Object()); 215 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), new Object()); 216 217 add(splitPane, BorderLayout.CENTER); 218 219 dialogsPanel.setLayout(new BoxLayout(dialogsPanel, BoxLayout.Y_AXIS)); 220 dialogsPanel.setPreferredSize(new Dimension(Main.pref.getInteger("toggleDialogs.width", DEF_TOGGLE_DLG_WIDTH), 0)); 221 dialogsPanel.setMinimumSize(new Dimension(24, 0)); 222 mapView.setMinimumSize(new Dimension(10, 0)); 223 224 // toolBarActions, map mode buttons 225 addMapMode(new IconToggleButton(mapModeSelect = new SelectAction(this))); 226 addMapMode(new IconToggleButton(mapModeSelectLasso = new LassoModeAction(), true)); 227 addMapMode(new IconToggleButton(mapModeDraw = new DrawAction(this))); 228 addMapMode(new IconToggleButton(mapModeZoom = new ZoomAction(this))); 229 addMapMode(new IconToggleButton(new DeleteAction(this), true)); 230 addMapMode(new IconToggleButton(new ParallelWayAction(this), true)); 231 addMapMode(new IconToggleButton(new ExtrudeAction(this), true)); 232 addMapMode(new IconToggleButton(new ImproveWayAccuracyAction(Main.map), false)); 233 toolBarActionsGroup.setSelected(allMapModeButtons.get(0).getModel(), true); 234 toolBarActions.setFloatable(false); 235 236 // toolBarToggles, toggle dialog buttons 237 LayerListDialog.createInstance(this); 238 addToggleDialog(LayerListDialog.getInstance()); 239 addToggleDialog(propertiesDialog = new PropertiesDialog()); 240 addToggleDialog(selectionListDialog = new SelectionListDialog()); 241 addToggleDialog(relationListDialog = new RelationListDialog()); 242 addToggleDialog(new MinimapDialog()); 243 addToggleDialog(new CommandStackDialog()); 244 addToggleDialog(new UserListDialog()); 245 addToggleDialog(conflictDialog = new ConflictDialog()); 246 addToggleDialog(validatorDialog = new ValidatorDialog()); 247 addToggleDialog(filterDialog = new FilterDialog()); 248 addToggleDialog(new ChangesetDialog(), true); 249 addToggleDialog(new MapPaintDialog()); 250 addToggleDialog(noteDialog = new NotesDialog()); 251 toolBarToggle.setFloatable(false); 252 253 // status line below the map 254 statusLine = new MapStatus(this); 255 MapView.addLayerChangeListener(this); 256 257 boolean unregisterTab = Shortcut.findShortcut(KeyEvent.VK_TAB, 0) != null; 258 if (unregisterTab) { 259 for (JComponent c: allDialogButtons) c.setFocusTraversalKeysEnabled(false); 260 for (JComponent c: allMapModeButtons) c.setFocusTraversalKeysEnabled(false); 261 } 262 263 if (Main.pref.getBoolean("debug.advanced-keypress-detector.enable", true)) { 264 keyDetector.register(); 265 } 266 } 267 268 public boolean selectSelectTool(boolean onlyIfModeless) { 269 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false)) 270 return false; 271 272 return selectMapMode(mapModeSelect); 273 } 274 275 public boolean selectDrawTool(boolean onlyIfModeless) { 276 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false)) 277 return false; 278 279 return selectMapMode(mapModeDraw); 280 } 281 282 public boolean selectZoomTool(boolean onlyIfModeless) { 283 if (onlyIfModeless && !Main.pref.getBoolean("modeless", false)) 284 return false; 285 286 return selectMapMode(mapModeZoom); 287 } 288 289 /** 290 * Called as some kind of destructor when the last layer has been removed. 291 * Delegates the call to all Destroyables within this component (e.g. MapModes) 292 */ 293 @Override 294 public void destroy() { 295 MapView.removeLayerChangeListener(this); 296 dialogsPanel.destroy(); 297 Main.pref.removePreferenceChangeListener(sidetoolbarPreferencesChangedListener); 298 for (int i = 0; i < toolBarActions.getComponentCount(); ++i) { 299 if (toolBarActions.getComponent(i) instanceof Destroyable) { 300 ((Destroyable) toolBarActions.getComponent(i)).destroy(); 301 } 302 } 303 for (int i = 0; i < toolBarToggle.getComponentCount(); ++i) { 304 if (toolBarToggle.getComponent(i) instanceof Destroyable) { 305 ((Destroyable) toolBarToggle.getComponent(i)).destroy(); 306 } 307 } 308 309 statusLine.destroy(); 310 mapView.destroy(); 311 keyDetector.unregister(); 312 } 313 314 public Action getDefaultButtonAction() { 315 return ((AbstractButton) toolBarActions.getComponent(0)).getAction(); 316 } 317 318 /** 319 * Open all ToggleDialogs that have their preferences property set. Close all others. 320 */ 321 public void initializeDialogsPane() { 322 dialogsPanel.initialize(allDialogs); 323 } 324 325 public IconToggleButton addToggleDialog(final ToggleDialog dlg) { 326 return addToggleDialog(dlg, false); 327 } 328 329 /** 330 * Call this to add new toggle dialogs to the left button-list 331 * @param dlg The toggle dialog. It must not be in the list already. 332 * @return button allowing to toggle the dialog 333 */ 334 public IconToggleButton addToggleDialog(final ToggleDialog dlg, boolean isExpert) { 335 final IconToggleButton button = new IconToggleButton(dlg.getToggleAction(), isExpert); 336 button.setShowHideButtonListener(dlg); 337 button.setInheritsPopupMenu(true); 338 dlg.setButton(button); 339 toolBarToggle.add(button); 340 allDialogs.add(dlg); 341 allDialogButtons.add(button); 342 button.applyButtonHiddenPreferences(); 343 if (dialogsPanel.initialized) { 344 dialogsPanel.add(dlg); 345 } 346 return button; 347 } 348 349 public void addMapMode(IconToggleButton b) { 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"); 354 allMapModeButtons.add(b); 355 toolBarActionsGroup.add(b); 356 toolBarActions.add(b); 357 b.applyButtonHiddenPreferences(); 358 b.setInheritsPopupMenu(true); 359 } 360 361 /** 362 * Fires an property changed event "visible". 363 * @param aFlag {@code true} if display should be visible 364 */ 365 @Override public void setVisible(boolean aFlag) { 366 boolean old = isVisible(); 367 super.setVisible(aFlag); 368 if (old != aFlag) { 369 firePropertyChange("visible", old, aFlag); 370 } 371 } 372 373 /** 374 * Change the operating map mode for the view. Will call unregister on the 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. 378 * @param newMapMode The new mode to set. 379 * @return {@code true} if mode is really selected 380 */ 381 public boolean selectMapMode(MapMode newMapMode) { 382 return selectMapMode(newMapMode, mapView.getActiveLayer()); 383 } 384 385 /** 386 * Another version of the selectMapMode for changing layer action. 387 * Pass newly selected layer to this method. 388 * @param newMapMode The new mode to set. 389 * @param newLayer newly selected layer 390 * @return {@code true} if mode is really selected 391 */ 392 public boolean selectMapMode(MapMode newMapMode, Layer newLayer) { 393 if (newMapMode == null || !newMapMode.layerIsSupported(newLayer)) 394 return false; 395 396 MapMode oldMapMode = this.mapMode; 397 if (newMapMode == oldMapMode) 398 return true; 399 if (oldMapMode != null) { 400 oldMapMode.exitMode(); 401 } 402 this.mapMode = newMapMode; 403 newMapMode.enterMode(); 404 lastMapMode.put(newLayer, newMapMode); 405 fireMapModeChanged(oldMapMode, newMapMode); 406 return true; 407 } 408 409 /** 410 * Fill the given panel by adding all necessary components to the different 411 * locations. 412 * 413 * @param panel The container to fill. Must have a BorderLayout. 414 */ 415 public void fillPanel(Container panel) { 416 panel.add(this, BorderLayout.CENTER); 417 418 /** 419 * sideToolBar: add map modes icons 420 */ 421 if (Main.pref.getBoolean("sidetoolbar.mapmodes.visible", true)) { 422 toolBarActions.setAlignmentX(0.5f); 423 toolBarActions.setBorder(null); 424 toolBarActions.setInheritsPopupMenu(true); 425 sideToolBar.add(toolBarActions); 426 listAllMapModesButton.setAlignmentX(0.5f); 427 listAllMapModesButton.setBorder(null); 428 listAllMapModesButton.setFont(listAllMapModesButton.getFont().deriveFont(Font.PLAIN)); 429 listAllMapModesButton.setInheritsPopupMenu(true); 430 sideToolBar.add(listAllMapModesButton); 431 } 432 433 /** 434 * sideToolBar: add toggle dialogs icons 435 */ 436 if (Main.pref.getBoolean("sidetoolbar.toggledialogs.visible", true)) { 437 ((JToolBar) sideToolBar).addSeparator(new Dimension(0, 18)); 438 toolBarToggle.setAlignmentX(0.5f); 439 toolBarToggle.setBorder(null); 440 toolBarToggle.setInheritsPopupMenu(true); 441 sideToolBar.add(toolBarToggle); 442 listAllToggleDialogsButton.setAlignmentX(0.5f); 443 listAllToggleDialogsButton.setBorder(null); 444 listAllToggleDialogsButton.setFont(listAllToggleDialogsButton.getFont().deriveFont(Font.PLAIN)); 445 listAllToggleDialogsButton.setInheritsPopupMenu(true); 446 sideToolBar.add(listAllToggleDialogsButton); 447 } 448 449 /** 450 * sideToolBar: add dynamic popup menu 451 */ 452 sideToolBar.setComponentPopupMenu(new SideToolbarPopupMenu()); 453 ((JToolBar) sideToolBar).setFloatable(false); 454 sideToolBar.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 1)); 455 456 /** 457 * sideToolBar: decide scroll- and visibility 458 */ 459 if (Main.pref.getBoolean("sidetoolbar.scrollable", true)) { 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)); 470 sidetoolbarPreferencesChangedListener = new Preferences.PreferenceChangedListener() { 471 @Override 472 public void preferenceChanged(PreferenceChangeEvent e) { 473 if ("sidetoolbar.visible".equals(e.getKey())) { 474 sideToolBar.setVisible(Main.pref.getBoolean("sidetoolbar.visible")); 475 } 476 } 477 }; 478 Main.pref.addPreferenceChangeListener(sidetoolbarPreferencesChangedListener); 479 480 /** 481 * sideToolBar: add it to the panel 482 */ 483 panel.add(sideToolBar, BorderLayout.WEST); 484 485 /** 486 * statusLine: add to panel 487 */ 488 if (statusLine != null && Main.pref.getBoolean("statusline.visible", true)) { 489 panel.add(statusLine, BorderLayout.SOUTH); 490 } 491 } 492 493 private final class SideToolbarPopupMenu extends JPopupMenu { 494 private static final int staticMenuEntryCount = 2; 495 private JCheckBoxMenuItem doNotHide = new JCheckBoxMenuItem(new AbstractAction(tr("Do not hide toolbar")) { 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 546 class ListAllButtonsAction extends AbstractAction { 547 548 private JButton button; 549 private transient Collection<? extends HideableButton> buttons; 550 551 ListAllButtonsAction(Collection<? extends HideableButton> buttons) { 552 this.buttons = buttons; 553 } 554 555 public void setButton(JButton button) { 556 this.button = button; 557 final ImageIcon icon = ImageProvider.get("audio-fwd"); 558 putValue(SMALL_ICON, icon); 559 button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight() + 64)); 560 } 561 562 @Override 563 public void actionPerformed(ActionEvent e) { 564 JPopupMenu menu = new JPopupMenu(); 565 for (HideableButton b : buttons) { 566 final HideableButton t = b; 567 menu.add(new JCheckBoxMenuItem(new AbstractAction() { 568 { 569 putValue(NAME, t.getActionName()); 570 putValue(SMALL_ICON, t.getIcon()); 571 putValue(SELECTED_KEY, t.isButtonVisible()); 572 putValue(SHORT_DESCRIPTION, tr("Hide or show this toggle button")); 573 } 574 575 @Override 576 public void actionPerformed(ActionEvent e) { 577 if ((Boolean) getValue(SELECTED_KEY)) { 578 t.showButton(); 579 } else { 580 t.hideButton(); 581 } 582 validateToolBarsVisibility(); 583 } 584 })); 585 } 586 Rectangle bounds = button.getBounds(); 587 menu.show(button, bounds.x + bounds.width, 0); 588 } 589 } 590 591 public void validateToolBarsVisibility() { 592 for (IconToggleButton b : allDialogButtons) { 593 b.applyButtonHiddenPreferences(); 594 } 595 toolBarToggle.repaint(); 596 for (IconToggleButton b : allMapModeButtons) { 597 b.applyButtonHiddenPreferences(); 598 } 599 toolBarActions.repaint(); 600 } 601 602 /** 603 * Replies the instance of a toggle dialog of type <code>type</code> managed by this map frame 604 * 605 * @param <T> toggle dialog type 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 609 * 610 */ 611 public <T> T getToggleDialog(Class<T> type) { 612 return dialogsPanel.getToggleDialog(type); 613 } 614 615 public void setDialogsPanelVisible(boolean visible) { 616 rememberToggleDialogWidth(); 617 dialogsPanel.setVisible(visible); 618 splitPane.setDividerLocation(visible ? splitPane.getWidth()-Main.pref.getInteger("toggleDialogs.width", DEF_TOGGLE_DLG_WIDTH) : 0); 619 splitPane.setDividerSize(visible ? 5 : 0); 620 } 621 622 /** 623 * Remember the current width of the (possibly resized) toggle dialog area 624 */ 625 public void rememberToggleDialogWidth() { 626 if (dialogsPanel.isVisible()) { 627 Main.pref.putInteger("toggleDialogs.width", splitPane.getWidth()-splitPane.getDividerLocation()); 628 } 629 } 630 631 /** 632 * Remove panel from top of MapView by class 633 */ 634 public void removeTopPanel(Class<?> type) { 635 int n = leftPanel.getComponentCount(); 636 for (int i = 0; i < n; i++) { 637 Component c = leftPanel.getComponent(i); 638 if (type.isInstance(c)) { 639 leftPanel.remove(i); 640 leftPanel.doLayout(); 641 return; 642 } 643 } 644 } 645 646 /* 647 * Find panel on top of MapView by class 648 */ 649 public <T> T getTopPanel(Class<T> type) { 650 int n = leftPanel.getComponentCount(); 651 for (int i = 0; i < n; i++) { 652 Component c = leftPanel.getComponent(i); 653 if (type.isInstance(c)) 654 return type.cast(c); 655 } 656 return null; 657 } 658 659 /** 660 * Add component @param c on top of MapView 661 */ 662 public void addTopPanel(Component c) { 663 leftPanel.add(c, GBC.eol().fill(GBC.HORIZONTAL), leftPanel.getComponentCount()-1); 664 leftPanel.doLayout(); 665 c.doLayout(); 666 } 667 668 /** 669 * Interface to notify listeners of the change of the mapMode. 670 */ 671 public interface MapModeChangeListener { 672 void mapModeChange(MapMode oldMapMode, MapMode newMapMode); 673 } 674 675 /** 676 * the mapMode listeners 677 */ 678 private static final CopyOnWriteArrayList<MapModeChangeListener> mapModeChangeListeners = new CopyOnWriteArrayList<>(); 679 680 private transient PreferenceChangedListener sidetoolbarPreferencesChangedListener; 681 /** 682 * Adds a mapMode change listener 683 * 684 * @param listener the listener. Ignored if null or already registered. 685 */ 686 public static void addMapModeChangeListener(MapModeChangeListener listener) { 687 if (listener != null) { 688 mapModeChangeListeners.addIfAbsent(listener); 689 } 690 } 691 692 /** 693 * Removes a mapMode change listener 694 * 695 * @param listener the listener. Ignored if null or already registered. 696 */ 697 public static void removeMapModeChangeListener(MapModeChangeListener listener) { 698 mapModeChangeListeners.remove(listener); 699 } 700 701 protected static void fireMapModeChanged(MapMode oldMapMode, MapMode newMapMode) { 702 for (MapModeChangeListener l : mapModeChangeListeners) { 703 l.mapModeChange(oldMapMode, newMapMode); 704 } 705 } 706 707 @Override 708 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 709 boolean modeChanged = false; 710 if (mapMode == null || !mapMode.layerIsSupported(newLayer)) { 711 MapMode newMapMode = getLastMapMode(newLayer); 712 modeChanged = newMapMode != mapMode; 713 if (newMapMode != null) { 714 // it would be nice to select first supported mode when layer is first selected, 715 // but it don't work well with for example editgpx layer 716 selectMapMode(newMapMode, newLayer); 717 } else if (mapMode != null) { 718 mapMode.exitMode(); // if new mode is null - simply exit from previous mode 719 } 720 } 721 // if this is really a change (and not the first active layer) 722 if (oldLayer != null) { 723 if (!modeChanged && mapMode != null) { 724 // Let mapmodes know about new active layer 725 mapMode.exitMode(); 726 mapMode.enterMode(); 727 } 728 // invalidate repaint cache 729 Main.map.mapView.preferenceChanged(null); 730 } 731 732 // After all listeners notice new layer, some buttons will be disabled/enabled 733 // and possibly need to be hidden/shown. 734 SwingUtilities.invokeLater(new Runnable() { 735 @Override public void run() { 736 validateToolBarsVisibility(); 737 } 738 }); 739 } 740 741 private MapMode getLastMapMode(Layer newLayer) { 742 MapMode mode = lastMapMode.get(newLayer); 743 if (mode == null) { 744 // if no action is selected - try to select default action 745 Action defaultMode = getDefaultButtonAction(); 746 if (defaultMode instanceof MapMode && ((MapMode) defaultMode).layerIsSupported(newLayer)) { 747 mode = (MapMode) defaultMode; 748 } 749 } 750 return mode; 751 } 752 753 @Override 754 public void layerAdded(Layer newLayer) { } 755 756 @Override 757 public void layerRemoved(Layer oldLayer) { 758 lastMapMode.remove(oldLayer); 759 } 760}