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

Last change on this file since 4840 was 4840, checked in by bastiK, 12 years ago

expert mode rework:

  • toggling of expert mode does not require restart
  • checkbox at a more prominent places (MainMenu > View and in the preference dialog at the bottom)
  • Property svn:eol-style set to native
File size: 20.8 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Container;
8import java.awt.Dimension;
9import java.awt.Font;
10import java.awt.Rectangle;
11import java.awt.event.ActionEvent;
12import java.awt.event.KeyEvent;
13import java.awt.event.MouseWheelEvent;
14import java.awt.event.MouseWheelListener;
15import java.util.ArrayList;
16import java.util.Collection;
17import java.util.HashMap;
18import java.util.List;
19import java.util.Map;
20import java.util.concurrent.CopyOnWriteArrayList;
21
22import javax.swing.AbstractAction;
23import javax.swing.AbstractButton;
24import javax.swing.Action;
25import javax.swing.BoxLayout;
26import javax.swing.ButtonGroup;
27import javax.swing.JButton;
28import javax.swing.JCheckBoxMenuItem;
29import javax.swing.JComponent;
30import javax.swing.JPanel;
31import javax.swing.JPopupMenu;
32import javax.swing.JSplitPane;
33import javax.swing.JToolBar;
34import javax.swing.KeyStroke;
35import javax.swing.SwingUtilities;
36import javax.swing.border.Border;
37import javax.swing.plaf.basic.BasicSplitPaneDivider;
38import javax.swing.plaf.basic.BasicSplitPaneUI;
39
40import org.openstreetmap.josm.Main;
41import org.openstreetmap.josm.actions.mapmode.DeleteAction;
42import org.openstreetmap.josm.actions.mapmode.DrawAction;
43import org.openstreetmap.josm.actions.mapmode.ExtrudeAction;
44import org.openstreetmap.josm.actions.mapmode.MapMode;
45import org.openstreetmap.josm.actions.mapmode.ParallelWayAction;
46import org.openstreetmap.josm.actions.mapmode.SelectAction;
47import org.openstreetmap.josm.actions.mapmode.ZoomAction;
48import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
49import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
50import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
51import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
52import org.openstreetmap.josm.gui.dialogs.DialogsPanel;
53import org.openstreetmap.josm.gui.dialogs.FilterDialog;
54import org.openstreetmap.josm.gui.dialogs.HistoryDialog;
55import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
56import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
57import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
58import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
59import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
60import org.openstreetmap.josm.gui.dialogs.UserListDialog;
61import org.openstreetmap.josm.gui.dialogs.ValidatorDialog;
62import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;
63import org.openstreetmap.josm.gui.layer.Layer;
64import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
65import org.openstreetmap.josm.tools.Destroyable;
66
67/**
68 * One Map frame with one dataset behind. This is the container gui class whose
69 * display can be set to the different views.
70 *
71 * @author imi
72 */
73public class MapFrame extends JPanel implements Destroyable, LayerChangeListener {
74
75 /**
76 * The current mode, this frame operates.
77 */
78 public MapMode mapMode;
79
80 private final List<MapMode> mapModes = new ArrayList<MapMode>();
81 /**
82 * The view control displayed.
83 */
84 public MapView mapView;
85 /**
86 * The toolbar with the action icons. To add new toggle dialog actions, use addToggleDialog
87 * instead of adding directly to this list. To add a new mode use addMapMode.
88 */
89 private JToolBar toolBarActions = new JToolBar(JToolBar.VERTICAL);
90 private JToolBar toolBarToggle = new JToolBar(JToolBar.VERTICAL);
91 /**
92 * The status line below the map
93 */
94 public MapStatus statusLine;
95
96 public ConflictDialog conflictDialog;
97 public FilterDialog filterDialog;
98 public RelationListDialog relationListDialog;
99 public ValidatorDialog validatorDialog;
100 public SelectionListDialog selectionListDialog;
101 public PropertiesDialog propertiesDialog;
102
103 /**
104 * The panel list of all toggle dialog icons. To add new toggle dialog actions, use addToggleDialog
105 * instead of adding directly to this list.
106 */
107 private List<ToggleDialog> allDialogs = new ArrayList<ToggleDialog>();
108 private final DialogsPanel dialogsPanel;
109
110 public final ButtonGroup toolGroup = new ButtonGroup();
111
112 private List<IconToggleButton> allDialogButtons = new ArrayList<IconToggleButton>();
113 private List<IconToggleButton> allMapModeButtons = new ArrayList<IconToggleButton>();
114
115 private final ListAllButtonsAction listAllDialogsAction = new ListAllButtonsAction(allDialogButtons);
116 private final ListAllButtonsAction listAllMapModesAction = new ListAllButtonsAction(allMapModeButtons);
117 private final JButton listAllToggleDialogsButton = new JButton(listAllDialogsAction);
118 private final JButton listAllMapModesButton = new JButton(listAllMapModesAction);
119 {
120 listAllDialogsAction.setButton(listAllToggleDialogsButton);
121 listAllMapModesAction.setButton(listAllMapModesButton);
122 }
123
124 /**
125 * Default width of the toggle dialog area.
126 */
127 public static final int DEF_TOGGLE_DLG_WIDTH = 330;
128
129 private final Map<Layer, MapMode> lastMapMode = new HashMap<Layer, MapMode>();
130
131 public MapFrame(JPanel contentPane) {
132 setSize(400,400);
133 setLayout(new BorderLayout());
134
135 mapView = new MapView(contentPane);
136
137 new FileDrop(mapView);
138
139 // toolbar
140 toolBarActions.setFloatable(false);
141 addMapMode(new IconToggleButton(new SelectAction(this)));
142 addMapMode(new IconToggleButton(new DrawAction(this)));
143 addMapMode(new IconToggleButton(new ZoomAction(this)));
144 addMapMode(new IconToggleButton(new DeleteAction(this), true));
145 addMapMode(new IconToggleButton(new ExtrudeAction(this), true));
146 addMapMode(new IconToggleButton(new ParallelWayAction(this), true));
147
148 toolGroup.setSelected(((AbstractButton)toolBarActions.getComponent(0)).getModel(), true);
149
150 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
151 dialogsPanel = new DialogsPanel(splitPane);
152 splitPane.setLeftComponent(mapView);
153 splitPane.setRightComponent(dialogsPanel);
154
155 /**
156 * All additional space goes to the mapView
157 */
158 splitPane.setResizeWeight(1.0);
159
160 /**
161 * Some beautifications.
162 */
163 splitPane.setDividerSize(5);
164 splitPane.setBorder(null);
165 splitPane.setUI(new BasicSplitPaneUI() {
166 @Override
167 public BasicSplitPaneDivider createDefaultDivider() {
168 return new BasicSplitPaneDivider(this) {
169 @Override
170 public void setBorder(Border b) {
171 }
172 };
173 }
174 });
175
176 // JSplitPane supports F6 and F8 shortcuts by default, but we need them for Audio actions
177 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), new Object());
178 splitPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), new Object());
179
180 add(splitPane, BorderLayout.CENTER);
181
182 dialogsPanel.setLayout(new BoxLayout(dialogsPanel, BoxLayout.Y_AXIS));
183 dialogsPanel.setPreferredSize(new Dimension(Main.pref.getInteger("toggleDialogs.width",DEF_TOGGLE_DLG_WIDTH), 0));
184
185 dialogsPanel.setMinimumSize(new Dimension(24, 0));
186 mapView.setMinimumSize(new Dimension(10,0));
187
188 toolBarToggle.setFloatable(false);
189 LayerListDialog.createInstance(this);
190 addToggleDialog(LayerListDialog.getInstance());
191 addToggleDialog(propertiesDialog = new PropertiesDialog(this));
192 addToggleDialog(selectionListDialog = new SelectionListDialog());
193 addToggleDialog(relationListDialog = new RelationListDialog());
194 addToggleDialog(new CommandStackDialog(this));
195 addToggleDialog(new UserListDialog());
196 addToggleDialog(new HistoryDialog());
197 addToggleDialog(conflictDialog = new ConflictDialog());
198 addToggleDialog(validatorDialog = new ValidatorDialog());
199 addToggleDialog(filterDialog = new FilterDialog());
200 addToggleDialog(new ChangesetDialog(this));
201 addToggleDialog(new MapPaintDialog());
202
203 // status line below the map
204 statusLine = new MapStatus(this);
205 MapView.addLayerChangeListener(this);
206 }
207
208 public void selectSelectTool(boolean onlyIfModeless) {
209 if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
210 return;
211
212 selectMapMode((MapMode)getDefaultButtonAction());
213 }
214
215 public void selectDrawTool(boolean onlyIfModeless) {
216 if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
217 return;
218
219 Action drawAction = ((AbstractButton)toolBarActions.getComponent(1)).getAction();
220 selectMapMode((MapMode)drawAction);
221 }
222
223 /**
224 * Called as some kind of destructor when the last layer has been removed.
225 * Delegates the call to all Destroyables within this component (e.g. MapModes)
226 */
227 public void destroy() {
228 MapView.removeLayerChangeListener(this);
229 dialogsPanel.destroy();
230 for (int i = 0; i < toolBarActions.getComponentCount(); ++i) {
231 if (toolBarActions.getComponent(i) instanceof Destroyable) {
232 ((Destroyable)toolBarActions.getComponent(i)).destroy();
233 }
234 }
235 for (int i = 0; i < toolBarToggle.getComponentCount(); ++i) {
236 if (toolBarToggle.getComponent(i) instanceof Destroyable) {
237 ((Destroyable)toolBarToggle.getComponent(i)).destroy();
238 }
239 }
240
241 // MapFrame gets destroyed when the last layer is removed, but the status line background
242 // thread that collects the information doesn't get destroyed automatically.
243 if(statusLine.thread != null) {
244 try {
245 statusLine.thread.interrupt();
246 } catch (Exception e) {
247 e.printStackTrace();
248 }
249 }
250 mapView.destroy();
251 }
252
253 public Action getDefaultButtonAction() {
254 return ((AbstractButton)toolBarActions.getComponent(0)).getAction();
255 }
256
257 /**
258 * Open all ToggleDialogs that have their preferences property set. Close all others.
259 */
260 public void initializeDialogsPane() {
261 dialogsPanel.initialize(allDialogs);
262 }
263
264 /**
265 * Call this to add new toggle dialogs to the left button-list
266 * @param dlg The toggle dialog. It must not be in the list already.
267 */
268 public IconToggleButton addToggleDialog(final ToggleDialog dlg) {
269 final IconToggleButton button = new IconToggleButton(dlg.getToggleAction());
270 button.setShowHideButtonListener(dlg);
271 addHideContextMenu(button);
272 dlg.setButton(button);
273 toolBarToggle.add(button);
274 allDialogs.add(dlg);
275 allDialogButtons.add(button);
276 button.applyButtonHiddenPreferences();
277 if (dialogsPanel.initialized) {
278 dialogsPanel.add(dlg);
279 }
280 return button;
281 }
282
283
284
285 public void addMapMode(IconToggleButton b) {
286 toolBarActions.add(b);
287 toolGroup.add(b);
288 allMapModeButtons.add(b);
289 if (b.getAction() instanceof MapMode) {
290 mapModes.add((MapMode) b.getAction());
291 } else
292 throw new IllegalArgumentException("MapMode action must be subclass of MapMode");
293 addHideContextMenu(b);
294 b.applyButtonHiddenPreferences();
295 }
296
297 /**
298 * Fires an property changed event "visible".
299 */
300 @Override public void setVisible(boolean aFlag) {
301 boolean old = isVisible();
302 super.setVisible(aFlag);
303 if (old != aFlag) {
304 firePropertyChange("visible", old, aFlag);
305 }
306 }
307
308 /**
309 * Change the operating map mode for the view. Will call unregister on the
310 * old MapMode and register on the new one. Now this function also verifies
311 * if new map mode is correct mode for current layer and does not change mode
312 * in such cases.
313 * @param mapMode The new mode to set.
314 */
315 public void selectMapMode(MapMode newMapMode) {
316 selectMapMode(newMapMode, mapView.getActiveLayer());
317 }
318
319 /**
320 * Another version of the selectMapMode for changing layer action.
321 * Pass newly selected layer to this method.
322 * @param newMapMode
323 * @param newLayer
324 */
325 public void selectMapMode(MapMode newMapMode, Layer newLayer) {
326 if (newMapMode == null || !newMapMode.layerIsSupported(newLayer))
327 return;
328
329 MapMode oldMapMode = this.mapMode;
330 if (newMapMode == oldMapMode)
331 return;
332 if (oldMapMode != null) {
333 oldMapMode.exitMode();
334 }
335 this.mapMode = newMapMode;
336 newMapMode.enterMode();
337 lastMapMode.put(newLayer, newMapMode);
338 fireMapModeChanged(oldMapMode, newMapMode);
339 }
340
341 /**
342 * Fill the given panel by adding all necessary components to the different
343 * locations.
344 *
345 * @param panel The container to fill. Must have an BorderLayout.
346 */
347 public void fillPanel(Container panel) {
348 panel.add(this, BorderLayout.CENTER);
349 JToolBar jb = new JToolBar(JToolBar.VERTICAL);
350 jb.setFloatable(false);
351 toolBarActions.setAlignmentX(0.5f);
352 jb.add(toolBarActions);
353 listAllMapModesButton.setAlignmentX(0.5f);
354 listAllMapModesButton.setBorder(null);
355 listAllMapModesButton.setFont(listAllMapModesButton.getFont().deriveFont(Font.PLAIN));
356 jb.add(listAllMapModesButton);
357
358 if(Main.pref.getBoolean("sidetoolbar.togglevisible", true)) {
359 jb.addSeparator(new Dimension(0,18));
360 toolBarToggle.setAlignmentX(0.5f);
361 jb.add(toolBarToggle);
362 listAllToggleDialogsButton.setAlignmentX(0.5f);
363 listAllToggleDialogsButton.setBorder(null);
364 listAllToggleDialogsButton.setFont(listAllToggleDialogsButton.getFont().deriveFont(Font.PLAIN));
365 jb.add(listAllToggleDialogsButton);
366 }
367
368 if(Main.pref.getBoolean("sidetoolbar.visible", true))
369 {
370 if(Main.pref.getBoolean("sidetoolbar.scrollable", true)) {
371 final ScrollViewport svp = new ScrollViewport(jb, ScrollViewport.VERTICAL_DIRECTION);
372 panel.add(svp, BorderLayout.WEST);
373 jb.addMouseWheelListener(new MouseWheelListener() {
374 public void mouseWheelMoved(MouseWheelEvent e) {
375 svp.scroll(0,e.getUnitsToScroll() * 5);
376 }
377 });
378 } else {
379 panel.add(jb, BorderLayout.WEST);
380 }
381 }
382 if (statusLine != null && Main.pref.getBoolean("statusline.visible", true)) {
383 panel.add(statusLine, BorderLayout.SOUTH);
384 }
385 }
386
387 private void addHideContextMenu(final IconToggleButton b) {
388 //context menu
389 b.addMouseListener(new PopupMenuLauncher(new JPopupMenu() {
390 {
391 add(new AbstractAction() {
392 {
393 putValue(NAME, tr("Hide this button"));
394 putValue(SHORT_DESCRIPTION, tr("Click the arrow at the bottom to show it again."));
395 }
396 @Override
397 public void actionPerformed(ActionEvent e) {
398 b.setButtonHidden(true);
399 validateToolBarsVisibility();
400 }
401 });
402 }
403 }));
404 }
405
406 class ListAllButtonsAction extends AbstractAction {
407
408 private JButton button;
409 private Collection<? extends HideableButton> buttons;
410
411
412 public ListAllButtonsAction(Collection<? extends HideableButton> buttons) {
413 this.buttons = buttons;
414 putValue(NAME, ">>");
415 }
416
417 public void setButton(JButton button) {
418 this.button = button;
419 }
420
421 @Override
422 public void actionPerformed(ActionEvent e) {
423 JPopupMenu menu = new JPopupMenu();
424 for (HideableButton b : buttons) {
425 final HideableButton t = b;
426 menu.add(new JCheckBoxMenuItem(new AbstractAction() {
427 {
428 putValue(NAME, t.getActionName());
429 putValue(SMALL_ICON, t.getIcon());
430 putValue(SELECTED_KEY, t.isButtonVisible());
431 putValue(SHORT_DESCRIPTION, tr("Hide or show this toggle button"));
432 }
433 @Override
434 public void actionPerformed(ActionEvent e) {
435 if ((Boolean) getValue(SELECTED_KEY)) {
436 t.showButton();
437 } else {
438 t.hideButton();
439 }
440 validateToolBarsVisibility();
441 }
442 }));
443 }
444 Rectangle bounds = button.getBounds();
445 menu.show(button, bounds.x + bounds.width, 0);
446 }
447 }
448
449 public void validateToolBarsVisibility() {
450 for (IconToggleButton b : allDialogButtons) {
451 b.applyButtonHiddenPreferences();
452 }
453 toolBarToggle.repaint();
454 for (IconToggleButton b : allMapModeButtons) {
455 b.applyButtonHiddenPreferences();
456 }
457 toolBarActions.repaint();
458 }
459
460 /**
461 * Replies the instance of a toggle dialog of type <code>type</code> managed by this
462 * map frame
463 *
464 * @param <T>
465 * @param type the class of the toggle dialog, i.e. UserListDialog.class
466 * @return the instance of a toggle dialog of type <code>type</code> managed by this
467 * map frame; null, if no such dialog exists
468 *
469 */
470 public <T> T getToggleDialog(Class<T> type) {
471 return dialogsPanel.getToggleDialog(type);
472 }
473
474 /**
475 * Returns the current width of the (possibly resized) toggle dialog area
476 */
477 public int getToggleDlgWidth() {
478 return dialogsPanel.getWidth();
479 }
480
481 /**
482 * Interface to notify listeners of the change of the mapMode.
483 */
484 public interface MapModeChangeListener {
485 void mapModeChange(MapMode oldMapMode, MapMode newMapMode);
486 }
487
488 /**
489 * the mapMode listeners
490 */
491 private static final CopyOnWriteArrayList<MapModeChangeListener> mapModeChangeListeners = new CopyOnWriteArrayList<MapModeChangeListener>();
492 /**
493 * Adds a mapMode change listener
494 *
495 * @param listener the listener. Ignored if null or already registered.
496 */
497 public static void addMapModeChangeListener(MapModeChangeListener listener) {
498 if (listener != null) {
499 mapModeChangeListeners.addIfAbsent(listener);
500 }
501 }
502 /**
503 * Removes a mapMode change listener
504 *
505 * @param listener the listener. Ignored if null or already registered.
506 */
507 public static void removeMapModeChangeListener(MapModeChangeListener listener) {
508 mapModeChangeListeners.remove(listener);
509 }
510
511 protected static void fireMapModeChanged(MapMode oldMapMode, MapMode newMapMode) {
512 for (MapModeChangeListener l : mapModeChangeListeners) {
513 l.mapModeChange(oldMapMode, newMapMode);
514 }
515 }
516
517 @Override
518 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
519 boolean modeChanged = false;
520 if (mapMode == null || !mapMode.layerIsSupported(newLayer)) {
521 MapMode newMapMode = getLastMapMode(newLayer);
522 modeChanged = newMapMode != mapMode;
523 if (newMapMode != null) {
524 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
525 } else if (mapMode != null) {
526 mapMode.exitMode(); // if new mode is null - simply exit from previous mode
527 }
528 }
529 if (!modeChanged && mapMode != null) {
530 // Let mapmodes know about new active layer
531 mapMode.exitMode();
532 mapMode.enterMode();
533 }
534 // invalidate repaint cache
535 Main.map.mapView.preferenceChanged(null);
536
537 // After all listeners notice new layer, some buttons will be disabled/enabled
538 // and possibly need to be hidden/shown.
539 SwingUtilities.invokeLater(new Runnable() {
540 public void run() {
541 validateToolBarsVisibility();
542 }
543 });
544 }
545
546
547 private MapMode getLastMapMode(Layer newLayer) {
548 MapMode mode = lastMapMode.get(newLayer);
549 if (mode == null) {
550 // if no action is selected - try to select default action
551 Action defaultMode = getDefaultButtonAction();
552 if (defaultMode instanceof MapMode & ((MapMode)defaultMode).layerIsSupported(newLayer)) {
553 mode = (MapMode) defaultMode;
554 }
555 }
556 return mode;
557 }
558
559 @Override
560 public void layerAdded(Layer newLayer) { }
561
562 @Override
563 public void layerRemoved(Layer oldLayer) {
564 lastMapMode.remove(oldLayer);
565 }
566}
Note: See TracBrowser for help on using the repository browser.