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

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

see #3550 - patch by bastik - resize dialogs on right side

  • Property svn:eol-style set to native
File size: 10.7 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.awt.Dimension;
9import java.util.ArrayList;
10
11import javax.swing.AbstractButton;
12import javax.swing.Action;
13import javax.swing.BoxLayout;
14import javax.swing.ButtonGroup;
15import javax.swing.JSplitPane;
16import javax.swing.JPanel;
17import javax.swing.JToolBar;
18import javax.swing.border.Border;
19import javax.swing.plaf.basic.BasicSplitPaneUI;
20import javax.swing.plaf.basic.BasicSplitPaneDivider;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.actions.mapmode.DeleteAction;
24import org.openstreetmap.josm.actions.mapmode.DrawAction;
25import org.openstreetmap.josm.actions.mapmode.ExtrudeAction;
26import org.openstreetmap.josm.actions.mapmode.MapMode;
27import org.openstreetmap.josm.actions.mapmode.SelectAction;
28import org.openstreetmap.josm.actions.mapmode.ZoomAction;
29import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
30import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
31import org.openstreetmap.josm.gui.dialogs.FilterDialog;
32import org.openstreetmap.josm.gui.dialogs.HistoryDialog;
33import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
34import org.openstreetmap.josm.gui.dialogs.PropertiesDialog;
35import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
36import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
37import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
38import org.openstreetmap.josm.gui.dialogs.UserListDialog;
39import org.openstreetmap.josm.tools.Destroyable;
40
41/**
42 * One Map frame with one dataset behind. This is the container gui class whose
43 * display can be set to the different views.
44 *
45 * @author imi
46 */
47public class MapFrame extends JPanel implements Destroyable {
48
49 /**
50 * The current mode, this frame operates.
51 */
52 public MapMode mapMode;
53 /**
54 * The view control displayed.
55 */
56 public MapView mapView;
57 /**
58 * The toolbar with the action icons. To add new toggle dialog actions, use addToggleDialog
59 * instead of adding directly to this list. To add a new mode use addMapMode.
60 */
61 private JToolBar toolBarActions = new JToolBar(JToolBar.VERTICAL);
62 private JToolBar toolBarToggle = new JToolBar(JToolBar.VERTICAL);
63 /**
64 * The status line below the map
65 */
66 public MapStatus statusLine;
67
68 public ConflictDialog conflictDialog;
69 /**
70 * The dialog that shows all relations and lets the user edit them.
71 */
72 public RelationListDialog relationListDialog;
73 /**
74 * The panel list of all toggle dialog icons. To add new toggle dialog actions, use addToggleDialog
75 * instead of adding directly to this list.
76 */
77 private JPanel toggleDialogs = new JPanel();
78 private ArrayList<ToggleDialog> allDialogs = new ArrayList<ToggleDialog>();
79
80 public final ButtonGroup toolGroup = new ButtonGroup();
81
82 /**
83 * Default width of the toggle dialog area.
84 */
85 public final int DEF_TOGGLE_DLG_WIDTH = 330;
86
87 public MapFrame() {
88 setSize(400,400);
89 setLayout(new BorderLayout());
90
91 mapView = new MapView();
92
93 new FileDrop(mapView);
94
95 // show menu entry
96 Main.main.menu.viewMenu.setVisible(true);
97
98 // toolbar
99 toolBarActions.setFloatable(false);
100 addMapMode(new IconToggleButton(new SelectAction(this)));
101 addMapMode(new IconToggleButton(new DrawAction(this)));
102 addMapMode(new IconToggleButton(new ExtrudeAction(this)));
103 addMapMode(new IconToggleButton(new ZoomAction(this)));
104 addMapMode(new IconToggleButton(new DeleteAction(this)));
105
106 toolGroup.setSelected(((AbstractButton)toolBarActions.getComponent(0)).getModel(), true);
107
108 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
109 mapView, toggleDialogs);
110
111 /**
112 * All additional space goes to the mapView
113 */
114 splitPane.setResizeWeight(1.0);
115
116 /**
117 * Some beautifications.
118 */
119 splitPane.setDividerSize(5);
120 splitPane.setBorder(null);
121 splitPane.setUI(new BasicSplitPaneUI() {
122 public BasicSplitPaneDivider createDefaultDivider() {
123 return new BasicSplitPaneDivider(this) {
124 public void setBorder(Border b) {
125 }
126 };
127 }
128 });
129
130 add(splitPane, BorderLayout.CENTER);
131
132 toggleDialogs.setLayout(new BoxLayout(toggleDialogs, BoxLayout.Y_AXIS));
133 toggleDialogs.setPreferredSize(new Dimension(Main.pref.getInteger("toggleDialogs.width",DEF_TOGGLE_DLG_WIDTH), 0));
134
135 toggleDialogs.setMinimumSize(new Dimension(24, 0));
136 mapView.setMinimumSize(new Dimension(10,0));
137
138 toolBarToggle.setFloatable(false);
139 LayerListDialog.createInstance(this);
140 addToggleDialog(LayerListDialog.getInstance());
141 addToggleDialog(new PropertiesDialog(this));
142 addToggleDialog(new HistoryDialog());
143 addToggleDialog(new SelectionListDialog());
144 if(Main.pref.getBoolean("displayfilter", false))
145 addToggleDialog(new FilterDialog());
146 addToggleDialog(new UserListDialog());
147 addToggleDialog(conflictDialog = new ConflictDialog());
148 addToggleDialog(new CommandStackDialog(this));
149 addToggleDialog(relationListDialog = new RelationListDialog());
150
151 // status line below the map
152 statusLine = new MapStatus(this);
153 }
154
155 public void selectSelectTool(boolean onlyIfModeless) {
156 if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
157 return;
158
159 selectMapMode((MapMode)getDefaultButtonAction());
160 }
161
162 public void selectDrawTool(boolean onlyIfModeless) {
163 if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
164 return;
165
166 Action drawAction = ((AbstractButton)toolBarActions.getComponent(1)).getAction();
167 selectMapMode((MapMode)drawAction);
168 }
169
170 /**
171 * Called as some kind of destructor when the last layer has been removed.
172 * Delegates the call to all Destroyables within this component (e.g. MapModes)
173 */
174 public void destroy() {
175 for (ToggleDialog t : allDialogs) {
176 t.closeDetachedDialog();
177 }
178 for (int i = 0; i < toolBarActions.getComponentCount(); ++i)
179 if (toolBarActions.getComponent(i) instanceof Destroyable) {
180 ((Destroyable)toolBarActions).destroy();
181 }
182 for (int i = 0; i < toolBarToggle.getComponentCount(); ++i)
183 if (toolBarToggle.getComponent(i) instanceof Destroyable) {
184 ((Destroyable)toolBarToggle).destroy();
185 }
186
187 // remove menu entries
188 Main.main.menu.viewMenu.setVisible(false);
189
190 // MapFrame gets destroyed when the last layer is removed, but the status line background
191 // thread that collects the information doesn't get destroyed automatically.
192 if(statusLine.thread == null) return;
193 try {
194 statusLine.thread.interrupt();
195 } catch (Exception e) {}
196 }
197
198 public Action getDefaultButtonAction() {
199 return ((AbstractButton)toolBarActions.getComponent(0)).getAction();
200 }
201
202 /**
203 * Open all ToggleDialogs that have their preferences property set. Close all others.
204 */
205 public void setVisibleDialogs() {
206 toggleDialogs.removeAll();
207 for (ToggleDialog dialog: allDialogs) {
208 dialog.setVisible(false);
209 toggleDialogs.add(dialog);
210 dialog.setParent(toggleDialogs);
211 if (Main.pref.getBoolean(dialog.getPreferencePrefix()+".visible")) {
212 dialog.showDialog();
213 } else {
214 dialog.hideDialog();
215 }
216 }
217 }
218
219 /**
220 * Call this to add new toggle dialogs to the left button-list
221 * @param dlg The toggle dialog. It must not be in the list already.
222 */
223 public IconToggleButton addToggleDialog(ToggleDialog dlg) {
224 IconToggleButton button = new IconToggleButton(dlg.getToggleAction());
225 toolBarToggle.add(button);
226 allDialogs.add(dlg);
227 return button;
228 }
229
230 public void addMapMode(IconToggleButton b) {
231 toolBarActions.add(b);
232 toolGroup.add(b);
233 }
234
235 /**
236 * Fires an property changed event "visible".
237 */
238 @Override public void setVisible(boolean aFlag) {
239 boolean old = isVisible();
240 super.setVisible(aFlag);
241 if (old != aFlag) {
242 firePropertyChange("visible", old, aFlag);
243 }
244 }
245
246
247
248 /**
249 * Change the operating map mode for the view. Will call unregister on the
250 * old MapMode and register on the new one.
251 * @param mapMode The new mode to set.
252 */
253 public void selectMapMode(MapMode mapMode) {
254 if (mapMode == this.mapMode)
255 return;
256 if (this.mapMode != null) {
257 this.mapMode.exitMode();
258 }
259 this.mapMode = mapMode;
260 mapMode.enterMode();
261 }
262
263 /**
264 * Fill the given panel by adding all necessary components to the different
265 * locations.
266 *
267 * @param panel The container to fill. Must have an BorderLayout.
268 */
269 public void fillPanel(Container panel) {
270 panel.add(this, BorderLayout.CENTER);
271 JToolBar jb = new JToolBar(JToolBar.VERTICAL);
272 jb.setFloatable(false);
273 jb.add(toolBarActions);
274 jb.addSeparator();
275 jb.add(toolBarToggle);
276 if(Main.pref.getBoolean("sidetoolbar.visible", true))
277 {
278 if(Main.pref.getBoolean("sidetoolbar.scrollable", true)) {
279 panel.add(new ScrollViewport(jb, ScrollViewport.VERTICAL_DIRECTION),
280 BorderLayout.WEST);
281 } else {
282 panel.add(jb, BorderLayout.WEST);
283 }
284 }
285 if (statusLine != null && Main.pref.getBoolean("statusline.visible", true)) {
286 panel.add(statusLine, BorderLayout.SOUTH);
287 }
288 }
289
290 /**
291 * Replies the instance of a toggle dialog of type <code>type</code> managed by this
292 * map frame
293 *
294 * @param <T>
295 * @param type the class of the toggle dialog, i.e. UserListDialog.class
296 * @return the instance of a toggle dialog of type <code>type</code> managed by this
297 * map frame; null, if no such dialog exists
298 *
299 */
300 public <T> T getToggleDialog(Class<T> type) {
301 for (ToggleDialog td : allDialogs) {
302 if (type.isInstance(td))
303 return type.cast(td);
304 }
305 return null;
306 }
307
308 /**
309 * Returns the current width of the (possibly resized) toggle dialog area
310 */
311 public int getToggleDlgWidth() {
312 return toggleDialogs.getWidth();
313 }
314}
Note: See TracBrowser for help on using the repository browser.