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

Last change on this file since 1405 was 1405, checked in by stoecker, 15 years ago

final patch for modeless drawing by xeen. Closes #1937

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1// License: GPL. See LICENSE file for details.
2
3package org.openstreetmap.josm.gui;
4
5import java.awt.BorderLayout;
6import java.awt.Component;
7import java.awt.Container;
8import java.util.ArrayList;
9
10import javax.swing.AbstractButton;
11import javax.swing.Action;
12import javax.swing.BoxLayout;
13import javax.swing.ButtonGroup;
14import javax.swing.JPanel;
15import javax.swing.JToolBar;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.actions.mapmode.DeleteAction;
19import org.openstreetmap.josm.actions.mapmode.DrawAction;
20import org.openstreetmap.josm.actions.mapmode.ExtrudeAction;
21import org.openstreetmap.josm.actions.mapmode.MapMode;
22import org.openstreetmap.josm.actions.mapmode.SelectAction;
23import org.openstreetmap.josm.actions.mapmode.ZoomAction;
24import org.openstreetmap.josm.gui.ScrollViewport;
25import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
26import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
27import org.openstreetmap.josm.gui.dialogs.HistoryDialog;
28import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
29import org.openstreetmap.josm.gui.dialogs.PropertiesDialog;
30import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
31import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
32import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
33import org.openstreetmap.josm.gui.dialogs.UserListDialog;
34import org.openstreetmap.josm.tools.Destroyable;
35
36/**
37 * One Map frame with one dataset behind. This is the container gui class whose
38 * display can be set to the different views.
39 *
40 * @author imi
41 */
42public class MapFrame extends JPanel implements Destroyable {
43
44 /**
45 * The current mode, this frame operates.
46 */
47 public MapMode mapMode;
48 /**
49 * The view control displayed.
50 */
51 public MapView mapView;
52 /**
53 * The toolbar with the action icons. To add new toggle dialog actions, use addToggleDialog
54 * instead of adding directly to this list. To add a new mode use addMapMode.
55 */
56 private JToolBar toolBarActions = new JToolBar(JToolBar.VERTICAL);
57 private JToolBar toolBarToggle = new JToolBar(JToolBar.VERTICAL);
58 /**
59 * The status line below the map
60 */
61 public MapStatus statusLine;
62
63 public ConflictDialog conflictDialog;
64 /**
65 * The dialog that shows all relations and lets the user edit them.
66 */
67 public RelationListDialog relationListDialog;
68 /**
69 * The panel list of all toggle dialog icons. To add new toggle dialog actions, use addToggleDialog
70 * instead of adding directly to this list.
71 */
72 public JPanel toggleDialogs = new JPanel();
73 public ArrayList<ToggleDialog> allDialogs = new ArrayList<ToggleDialog>();
74
75 public final ButtonGroup toolGroup = new ButtonGroup();
76
77 public MapFrame() {
78 setSize(400,400);
79 setLayout(new BorderLayout());
80
81 add(mapView = new MapView(), BorderLayout.CENTER);
82
83 new FileDrop(mapView);
84
85 // show menu entry
86 Main.main.menu.viewMenu.setVisible(true);
87
88 // toolbar
89 toolBarActions.setFloatable(false);
90 addMapMode(new IconToggleButton(new SelectAction(this)));
91 addMapMode(new IconToggleButton(new DrawAction(this)));
92 addMapMode(new IconToggleButton(new ExtrudeAction(this)));
93 addMapMode(new IconToggleButton(new ZoomAction(this)));
94 addMapMode(new IconToggleButton(new DeleteAction(this)));
95
96 toolGroup.setSelected(((AbstractButton)toolBarActions.getComponent(0)).getModel(), true);
97
98 add(toggleDialogs, BorderLayout.EAST);
99 toggleDialogs.setLayout(new BoxLayout(toggleDialogs, BoxLayout.Y_AXIS));
100
101 toolBarToggle.setFloatable(false);
102 addToggleDialog(new LayerListDialog(this));
103 addToggleDialog(new PropertiesDialog(this));
104 addToggleDialog(new HistoryDialog());
105 addToggleDialog(new SelectionListDialog());
106 addToggleDialog(new UserListDialog());
107 addToggleDialog(conflictDialog = new ConflictDialog());
108 addToggleDialog(new CommandStackDialog(this));
109 addToggleDialog(relationListDialog = new RelationListDialog());
110
111 // status line below the map
112 statusLine = new MapStatus(this);
113 }
114
115 public void selectSelectTool(boolean onlyIfModeless) {
116 if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
117 return;
118
119 selectMapMode((MapMode)getDefaultButtonAction());
120 }
121
122 public void selectDrawTool(boolean onlyIfModeless) {
123 if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
124 return;
125
126 Action drawAction = ((AbstractButton)toolBarActions.getComponent(1)).getAction();
127 selectMapMode((MapMode)drawAction);
128 }
129
130 /**
131 * Called as some kind of destructor when the last layer has been removed.
132 * Delegates the call to all Destroyables within this component (e.g. MapModes)
133 */
134 public void destroy() {
135 for (ToggleDialog t : allDialogs)
136 t.close();
137 for (int i = 0; i < toolBarActions.getComponentCount(); ++i)
138 if (toolBarActions.getComponent(i) instanceof Destroyable)
139 ((Destroyable)toolBarActions).destroy();
140 for (int i = 0; i < toolBarToggle.getComponentCount(); ++i)
141 if (toolBarToggle.getComponent(i) instanceof Destroyable)
142 ((Destroyable)toolBarToggle).destroy();
143
144 // remove menu entries
145 Main.main.menu.viewMenu.setVisible(false);
146 }
147
148 public Action getDefaultButtonAction() {
149 return ((AbstractButton)toolBarActions.getComponent(0)).getAction();
150 }
151
152 /**
153 * Open all ToggleDialogs that have their preferences property set. Close all others.
154 */
155 public void setVisibleDialogs() {
156 for (Component c : toggleDialogs.getComponents()) {
157 if (c instanceof ToggleDialog) {
158 boolean sel = Main.pref.getBoolean(((ToggleDialog)c).prefName+".visible");
159 ((ToggleDialog)c).action.button.setSelected(sel);
160 c.setVisible(sel);
161 }
162 }
163 }
164
165 /**
166 * Call this to add new toggle dialogs to the left button-list
167 * @param dlg The toggle dialog. It must not be in the list already.
168 */
169 public IconToggleButton addToggleDialog(ToggleDialog dlg) {
170 IconToggleButton button = new IconToggleButton(dlg.action);
171 dlg.action.button = button;
172 dlg.parent = toggleDialogs;
173 toolBarToggle.add(button);
174 toggleDialogs.add(dlg);
175 allDialogs.add(dlg);
176 return button;
177 }
178
179 public void addMapMode(IconToggleButton b) {
180 toolBarActions.add(b);
181 toolGroup.add((AbstractButton)b);
182 }
183
184 /**
185 * Fires an property changed event "visible".
186 */
187 @Override public void setVisible(boolean aFlag) {
188 boolean old = isVisible();
189 super.setVisible(aFlag);
190 if (old != aFlag)
191 firePropertyChange("visible", old, aFlag);
192 }
193
194
195
196 /**
197 * Change the operating map mode for the view. Will call unregister on the
198 * old MapMode and register on the new one.
199 * @param mapMode The new mode to set.
200 */
201 public void selectMapMode(MapMode mapMode) {
202 if (mapMode == this.mapMode)
203 return;
204 if (this.mapMode != null)
205 this.mapMode.exitMode();
206 this.mapMode = mapMode;
207 mapMode.enterMode();
208 }
209
210 /**
211 * Fill the given panel by adding all necessary components to the different
212 * locations.
213 *
214 * @param panel The container to fill. Must have an BorderLayout.
215 */
216 public void fillPanel(Container panel) {
217 panel.add(this, BorderLayout.CENTER);
218 JToolBar jb = new JToolBar(JToolBar.VERTICAL);
219 jb.setFloatable(false);
220 jb.add(toolBarActions);
221 jb.addSeparator();
222 jb.add(toolBarToggle);
223 if(Main.pref.getBoolean("sidetoolbar.visible", true))
224 {
225 if(Main.pref.getBoolean("sidetoolbar.scrollable", true))
226 panel.add(new ScrollViewport(jb, ScrollViewport.VERTICAL_DIRECTION),
227 BorderLayout.WEST);
228 else
229 panel.add(jb, BorderLayout.WEST);
230 }
231 if (statusLine != null && Main.pref.getBoolean("statusline.visible", true))
232 panel.add(statusLine, BorderLayout.SOUTH);
233 }
234}
Note: See TracBrowser for help on using the repository browser.