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