1 | // License: GPL. See LICENSE file for details.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui;
|
---|
4 |
|
---|
5 | import java.awt.BorderLayout;
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Container;
|
---|
8 | import java.util.ArrayList;
|
---|
9 |
|
---|
10 | import javax.swing.AbstractButton;
|
---|
11 | import javax.swing.Action;
|
---|
12 | import javax.swing.BoxLayout;
|
---|
13 | import javax.swing.ButtonGroup;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 | import javax.swing.JToolBar;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.actions.mapmode.DeleteAction;
|
---|
19 | import org.openstreetmap.josm.actions.mapmode.DrawAction;
|
---|
20 | import org.openstreetmap.josm.actions.mapmode.ExtrudeAction;
|
---|
21 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
22 | import org.openstreetmap.josm.actions.mapmode.SelectAction;
|
---|
23 | import org.openstreetmap.josm.actions.mapmode.ZoomAction;
|
---|
24 | import org.openstreetmap.josm.gui.ScrollViewport;
|
---|
25 | import org.openstreetmap.josm.gui.dialogs.CommandStackDialog;
|
---|
26 | import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
|
---|
27 | import org.openstreetmap.josm.gui.dialogs.HistoryDialog;
|
---|
28 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
29 | import org.openstreetmap.josm.gui.dialogs.PropertiesDialog;
|
---|
30 | import org.openstreetmap.josm.gui.dialogs.RelationListDialog;
|
---|
31 | import org.openstreetmap.josm.gui.dialogs.SelectionListDialog;
|
---|
32 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
33 | import org.openstreetmap.josm.gui.dialogs.UserListDialog;
|
---|
34 | import 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 | */
|
---|
42 | public 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 | LayerListDialog.createInstance(this);
|
---|
103 | addToggleDialog(LayerListDialog.getInstance());
|
---|
104 | addToggleDialog(new PropertiesDialog(this));
|
---|
105 | addToggleDialog(new HistoryDialog());
|
---|
106 | addToggleDialog(new SelectionListDialog());
|
---|
107 | addToggleDialog(new UserListDialog());
|
---|
108 | addToggleDialog(conflictDialog = new ConflictDialog());
|
---|
109 | addToggleDialog(new CommandStackDialog(this));
|
---|
110 | addToggleDialog(relationListDialog = new RelationListDialog());
|
---|
111 |
|
---|
112 | // status line below the map
|
---|
113 | statusLine = new MapStatus(this);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void selectSelectTool(boolean onlyIfModeless) {
|
---|
117 | if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
|
---|
118 | return;
|
---|
119 |
|
---|
120 | selectMapMode((MapMode)getDefaultButtonAction());
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void selectDrawTool(boolean onlyIfModeless) {
|
---|
124 | if(onlyIfModeless && !Main.pref.getBoolean("modeless", false))
|
---|
125 | return;
|
---|
126 |
|
---|
127 | Action drawAction = ((AbstractButton)toolBarActions.getComponent(1)).getAction();
|
---|
128 | selectMapMode((MapMode)drawAction);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Called as some kind of destructor when the last layer has been removed.
|
---|
133 | * Delegates the call to all Destroyables within this component (e.g. MapModes)
|
---|
134 | */
|
---|
135 | public void destroy() {
|
---|
136 | for (ToggleDialog t : allDialogs) {
|
---|
137 | t.close();
|
---|
138 | }
|
---|
139 | for (int i = 0; i < toolBarActions.getComponentCount(); ++i)
|
---|
140 | if (toolBarActions.getComponent(i) instanceof Destroyable) {
|
---|
141 | ((Destroyable)toolBarActions).destroy();
|
---|
142 | }
|
---|
143 | for (int i = 0; i < toolBarToggle.getComponentCount(); ++i)
|
---|
144 | if (toolBarToggle.getComponent(i) instanceof Destroyable) {
|
---|
145 | ((Destroyable)toolBarToggle).destroy();
|
---|
146 | }
|
---|
147 |
|
---|
148 | // remove menu entries
|
---|
149 | Main.main.menu.viewMenu.setVisible(false);
|
---|
150 |
|
---|
151 | // MapFrame gets destroyed when the last layer is removed, but the status line background
|
---|
152 | // thread that collects the information doesn't get destroyed automatically.
|
---|
153 | if(statusLine.thread == null) return;
|
---|
154 | try {
|
---|
155 | statusLine.thread.interrupt();
|
---|
156 | } catch (Exception e) {}
|
---|
157 | }
|
---|
158 |
|
---|
159 | public Action getDefaultButtonAction() {
|
---|
160 | return ((AbstractButton)toolBarActions.getComponent(0)).getAction();
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Open all ToggleDialogs that have their preferences property set. Close all others.
|
---|
165 | */
|
---|
166 | public void setVisibleDialogs() {
|
---|
167 | for (Component c : toggleDialogs.getComponents()) {
|
---|
168 | if (c instanceof ToggleDialog) {
|
---|
169 | boolean sel = Main.pref.getBoolean(((ToggleDialog)c).prefName+".visible");
|
---|
170 | ((ToggleDialog)c).action.button.setSelected(sel);
|
---|
171 | c.setVisible(sel);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Call this to add new toggle dialogs to the left button-list
|
---|
178 | * @param dlg The toggle dialog. It must not be in the list already.
|
---|
179 | */
|
---|
180 | public IconToggleButton addToggleDialog(ToggleDialog dlg) {
|
---|
181 | IconToggleButton button = new IconToggleButton(dlg.action);
|
---|
182 | dlg.action.button = button;
|
---|
183 | dlg.parent = toggleDialogs;
|
---|
184 | toolBarToggle.add(button);
|
---|
185 | toggleDialogs.add(dlg);
|
---|
186 | allDialogs.add(dlg);
|
---|
187 | return button;
|
---|
188 | }
|
---|
189 |
|
---|
190 | public void addMapMode(IconToggleButton b) {
|
---|
191 | toolBarActions.add(b);
|
---|
192 | toolGroup.add(b);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Fires an property changed event "visible".
|
---|
197 | */
|
---|
198 | @Override public void setVisible(boolean aFlag) {
|
---|
199 | boolean old = isVisible();
|
---|
200 | super.setVisible(aFlag);
|
---|
201 | if (old != aFlag) {
|
---|
202 | firePropertyChange("visible", old, aFlag);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Change the operating map mode for the view. Will call unregister on the
|
---|
210 | * old MapMode and register on the new one.
|
---|
211 | * @param mapMode The new mode to set.
|
---|
212 | */
|
---|
213 | public void selectMapMode(MapMode mapMode) {
|
---|
214 | if (mapMode == this.mapMode)
|
---|
215 | return;
|
---|
216 | if (this.mapMode != null) {
|
---|
217 | this.mapMode.exitMode();
|
---|
218 | }
|
---|
219 | this.mapMode = mapMode;
|
---|
220 | mapMode.enterMode();
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Fill the given panel by adding all necessary components to the different
|
---|
225 | * locations.
|
---|
226 | *
|
---|
227 | * @param panel The container to fill. Must have an BorderLayout.
|
---|
228 | */
|
---|
229 | public void fillPanel(Container panel) {
|
---|
230 | panel.add(this, BorderLayout.CENTER);
|
---|
231 | JToolBar jb = new JToolBar(JToolBar.VERTICAL);
|
---|
232 | jb.setFloatable(false);
|
---|
233 | jb.add(toolBarActions);
|
---|
234 | jb.addSeparator();
|
---|
235 | jb.add(toolBarToggle);
|
---|
236 | if(Main.pref.getBoolean("sidetoolbar.visible", true))
|
---|
237 | {
|
---|
238 | if(Main.pref.getBoolean("sidetoolbar.scrollable", true)) {
|
---|
239 | panel.add(new ScrollViewport(jb, ScrollViewport.VERTICAL_DIRECTION),
|
---|
240 | BorderLayout.WEST);
|
---|
241 | } else {
|
---|
242 | panel.add(jb, BorderLayout.WEST);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | if (statusLine != null && Main.pref.getBoolean("statusline.visible", true)) {
|
---|
246 | panel.add(statusLine, BorderLayout.SOUTH);
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|