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