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