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

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

see #3475 - patch by Petr Dlouhý - display filtering - disabled by default some time until more mature

  • Property svn:eol-style set to native
File size: 9.3 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.dialogs.CommandStackDialog;
25import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
26import org.openstreetmap.josm.gui.dialogs.HistoryDialog;
27import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
28import org.openstreetmap.josm.gui.dialogs.PropertiesDialog;
29import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
30import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
31import org.openstreetmap.josm.gui.dialogs.FilterDialog;
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 private JPanel toggleDialogs = new JPanel();
73 private 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 LayerListDialog.createInstance(this);
103 addToggleDialog(LayerListDialog.getInstance());
104 addToggleDialog(new PropertiesDialog(this));
105 addToggleDialog(new HistoryDialog());
106 addToggleDialog(new SelectionListDialog());
107 if(Main.pref.getBoolean("displayfilter", false))
108 addToggleDialog(new FilterDialog());
109 addToggleDialog(new UserListDialog());
110 addToggleDialog(conflictDialog = new ConflictDialog());
111 addToggleDialog(new CommandStackDialog(this));
112 addToggleDialog(relationListDialog = new RelationListDialog());
113
114 // status line below the map
115 statusLine = new MapStatus(this);
116 }
117
118 public void selectSelectTool(boolean onlyIfModeless) {
119 if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
120 return;
121
122 selectMapMode((MapMode)getDefaultButtonAction());
123 }
124
125 public void selectDrawTool(boolean onlyIfModeless) {
126 if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
127 return;
128
129 Action drawAction = ((AbstractButton)toolBarActions.getComponent(1)).getAction();
130 selectMapMode((MapMode)drawAction);
131 }
132
133 /**
134 * Called as some kind of destructor when the last layer has been removed.
135 * Delegates the call to all Destroyables within this component (e.g. MapModes)
136 */
137 public void destroy() {
138 for (ToggleDialog t : allDialogs) {
139 t.closeDetachedDialog();
140 }
141 for (int i = 0; i < toolBarActions.getComponentCount(); ++i)
142 if (toolBarActions.getComponent(i) instanceof Destroyable) {
143 ((Destroyable)toolBarActions).destroy();
144 }
145 for (int i = 0; i < toolBarToggle.getComponentCount(); ++i)
146 if (toolBarToggle.getComponent(i) instanceof Destroyable) {
147 ((Destroyable)toolBarToggle).destroy();
148 }
149
150 // remove menu entries
151 Main.main.menu.viewMenu.setVisible(false);
152
153 // MapFrame gets destroyed when the last layer is removed, but the status line background
154 // thread that collects the information doesn't get destroyed automatically.
155 if(statusLine.thread == null) return;
156 try {
157 statusLine.thread.interrupt();
158 } catch (Exception e) {}
159 }
160
161 public Action getDefaultButtonAction() {
162 return ((AbstractButton)toolBarActions.getComponent(0)).getAction();
163 }
164
165 /**
166 * Open all ToggleDialogs that have their preferences property set. Close all others.
167 */
168 public void setVisibleDialogs() {
169 toggleDialogs.removeAll();
170 for (ToggleDialog dialog: allDialogs) {
171 dialog.setVisible(false);
172 toggleDialogs.add(dialog);
173 dialog.setParent(toggleDialogs);
174 if (Main.pref.getBoolean(dialog.getPreferencePrefix()+".visible")) {
175 dialog.showDialog();
176 } else {
177 dialog.hideDialog();
178 }
179 }
180 }
181
182 /**
183 * Call this to add new toggle dialogs to the left button-list
184 * @param dlg The toggle dialog. It must not be in the list already.
185 */
186 public IconToggleButton addToggleDialog(ToggleDialog dlg) {
187 IconToggleButton button = new IconToggleButton(dlg.getToggleAction());
188 toolBarToggle.add(button);
189 allDialogs.add(dlg);
190 return button;
191 }
192
193 public void addMapMode(IconToggleButton b) {
194 toolBarActions.add(b);
195 toolGroup.add(b);
196 }
197
198 /**
199 * Fires an property changed event "visible".
200 */
201 @Override public void setVisible(boolean aFlag) {
202 boolean old = isVisible();
203 super.setVisible(aFlag);
204 if (old != aFlag) {
205 firePropertyChange("visible", old, aFlag);
206 }
207 }
208
209
210
211 /**
212 * Change the operating map mode for the view. Will call unregister on the
213 * old MapMode and register on the new one.
214 * @param mapMode The new mode to set.
215 */
216 public void selectMapMode(MapMode mapMode) {
217 if (mapMode == this.mapMode)
218 return;
219 if (this.mapMode != null) {
220 this.mapMode.exitMode();
221 }
222 this.mapMode = mapMode;
223 mapMode.enterMode();
224 }
225
226 /**
227 * Fill the given panel by adding all necessary components to the different
228 * locations.
229 *
230 * @param panel The container to fill. Must have an BorderLayout.
231 */
232 public void fillPanel(Container panel) {
233 panel.add(this, BorderLayout.CENTER);
234 JToolBar jb = new JToolBar(JToolBar.VERTICAL);
235 jb.setFloatable(false);
236 jb.add(toolBarActions);
237 jb.addSeparator();
238 jb.add(toolBarToggle);
239 if(Main.pref.getBoolean("sidetoolbar.visible", true))
240 {
241 if(Main.pref.getBoolean("sidetoolbar.scrollable", true)) {
242 panel.add(new ScrollViewport(jb, ScrollViewport.VERTICAL_DIRECTION),
243 BorderLayout.WEST);
244 } else {
245 panel.add(jb, BorderLayout.WEST);
246 }
247 }
248 if (statusLine != null && Main.pref.getBoolean("statusline.visible", true)) {
249 panel.add(statusLine, BorderLayout.SOUTH);
250 }
251 }
252
253 /**
254 * Replies the instance of a toggle dialog of type <code>type</code> managed by this
255 * map frame
256 *
257 * @param <T>
258 * @param type the class of the toggle dialog, i.e. UserListDialog.class
259 * @return the instance of a toggle dialog of type <code>type</code> managed by this
260 * map frame; null, if no such dialog exists
261 *
262 */
263 public <T> T getToggleDialog(Class<T> type) {
264 for (ToggleDialog td : allDialogs) {
265 if (type.isInstance(td))
266 return type.cast(td);
267 }
268 return null;
269 }
270}
Note: See TracBrowser for help on using the repository browser.