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

Last change on this file since 5091 was 5091, checked in by akks, 12 years ago

Warning against misaligned imagery for new users - see #7450.
Added addTopPanel function in MapFrame for other possible enhancements

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