1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.BorderLayout;
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.Rectangle;
|
---|
9 | import java.awt.Toolkit;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.io.File;
|
---|
12 | import java.net.URI;
|
---|
13 | import java.net.URISyntaxException;
|
---|
14 | import java.net.URL;
|
---|
15 | import java.net.URLClassLoader;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Arrays;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.SortedMap;
|
---|
23 | import java.util.StringTokenizer;
|
---|
24 | import java.util.TreeMap;
|
---|
25 | import java.util.concurrent.Executor;
|
---|
26 | import java.util.concurrent.Executors;
|
---|
27 | import java.util.regex.Matcher;
|
---|
28 | import java.util.regex.Pattern;
|
---|
29 |
|
---|
30 | import javax.swing.JComponent;
|
---|
31 | import javax.swing.JOptionPane;
|
---|
32 | import javax.swing.JPanel;
|
---|
33 | import javax.swing.KeyStroke;
|
---|
34 | import javax.swing.UIManager;
|
---|
35 |
|
---|
36 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
37 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
38 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
39 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
40 | import org.openstreetmap.josm.data.Bounds;
|
---|
41 | import org.openstreetmap.josm.data.Preferences;
|
---|
42 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
43 | import org.openstreetmap.josm.data.projection.Epsg4326;
|
---|
44 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
45 | import org.openstreetmap.josm.gui.GettingStarted;
|
---|
46 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
47 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
48 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
49 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
50 | import org.openstreetmap.josm.gui.download.BoundingBoxSelection;
|
---|
51 | import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
|
---|
52 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
53 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
54 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
55 | import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
|
---|
56 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
57 | import org.openstreetmap.josm.plugins.PluginException;
|
---|
58 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
59 | import org.openstreetmap.josm.plugins.PluginProxy;
|
---|
60 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
61 |
|
---|
62 | abstract public class Main {
|
---|
63 | /**
|
---|
64 | * Global parent component for all dialogs and message boxes
|
---|
65 | */
|
---|
66 | public static Component parent;
|
---|
67 | /**
|
---|
68 | * Global application.
|
---|
69 | */
|
---|
70 | public static Main main;
|
---|
71 | /**
|
---|
72 | * The worker thread slave. This is for executing all long and intensive
|
---|
73 | * calculations. The executed runnables are guaranteed to be executed seperatly
|
---|
74 | * and sequenciel.
|
---|
75 | */
|
---|
76 | public final static Executor worker = Executors.newSingleThreadExecutor();
|
---|
77 | /**
|
---|
78 | * Global application preferences
|
---|
79 | */
|
---|
80 | public static Preferences pref = new Preferences();
|
---|
81 | /**
|
---|
82 | * The global dataset.
|
---|
83 | */
|
---|
84 | public static DataSet ds = new DataSet();
|
---|
85 | /**
|
---|
86 | * The projection method used.
|
---|
87 | */
|
---|
88 | public static Projection proj;
|
---|
89 | /**
|
---|
90 | * The MapFrame. Use setMapFrame to set or clear it.
|
---|
91 | */
|
---|
92 | public static MapFrame map;
|
---|
93 | /**
|
---|
94 | * All installed and loaded plugins (resp. their main classes)
|
---|
95 | */
|
---|
96 | public final static Collection<PluginProxy> plugins = new LinkedList<PluginProxy>();
|
---|
97 | /**
|
---|
98 | * The dialog that gets displayed during background task execution.
|
---|
99 | */
|
---|
100 | public static PleaseWaitDialog pleaseWaitDlg;
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * True, when in applet mode
|
---|
104 | */
|
---|
105 | public static boolean applet = false;
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * The toolbar preference control to register new actions.
|
---|
109 | */
|
---|
110 | public static ToolbarPreferences toolbar = new ToolbarPreferences();
|
---|
111 |
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * The main menu bar at top of screen.
|
---|
115 | */
|
---|
116 | public final MainMenu menu;
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Set or clear (if passed <code>null</code>) the map.
|
---|
121 | */
|
---|
122 | public final void setMapFrame(final MapFrame map) {
|
---|
123 | MapFrame old = Main.map;
|
---|
124 | Main.map = map;
|
---|
125 | panel.setVisible(false);
|
---|
126 | panel.removeAll();
|
---|
127 | if (map != null) {
|
---|
128 | map.fillPanel(panel);
|
---|
129 | map.mapView.addLayerChangeListener(new LayerChangeListener(){
|
---|
130 | public void activeLayerChange(final Layer oldLayer, final Layer newLayer) {
|
---|
131 | setLayerMenu(newLayer.getMenuEntries());
|
---|
132 | }
|
---|
133 | public void layerAdded(final Layer newLayer) {
|
---|
134 | if (newLayer instanceof OsmDataLayer)
|
---|
135 | Main.main.editLayer().listenerCommands.add(redoUndoListener);
|
---|
136 | }
|
---|
137 | public void layerRemoved(final Layer oldLayer) {
|
---|
138 | if (oldLayer instanceof OsmDataLayer)
|
---|
139 | Main.main.editLayer().listenerCommands.add(redoUndoListener);
|
---|
140 | if (map.mapView.getAllLayers().isEmpty())
|
---|
141 | setLayerMenu(null);
|
---|
142 | }
|
---|
143 | });
|
---|
144 | if (map.mapView.editLayer != null)
|
---|
145 | map.mapView.editLayer.listenerCommands.add(redoUndoListener);
|
---|
146 | } else {
|
---|
147 | old.destroy();
|
---|
148 | panel.add(new GettingStarted(), BorderLayout.CENTER);
|
---|
149 | }
|
---|
150 | panel.setVisible(true);
|
---|
151 | redoUndoListener.commandChanged(0,0);
|
---|
152 |
|
---|
153 | for (PluginProxy plugin : plugins)
|
---|
154 | plugin.mapFrameInitialized(old, map);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Set the layer menu (changed when active layer changes).
|
---|
159 | */
|
---|
160 | public final void setLayerMenu(Component[] entries) {
|
---|
161 | if (entries == null || entries.length == 0)
|
---|
162 | menu.layerMenu.setVisible(false);
|
---|
163 | else {
|
---|
164 | menu.layerMenu.removeAll();
|
---|
165 | for (Component c : entries)
|
---|
166 | menu.layerMenu.add(c);
|
---|
167 | menu.layerMenu.setVisible(true);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Remove the specified layer from the map. If it is the last layer, remove the map as well.
|
---|
173 | */
|
---|
174 | public final void removeLayer(final Layer layer) {
|
---|
175 | map.mapView.removeLayer(layer);
|
---|
176 | if (layer instanceof OsmDataLayer)
|
---|
177 | ds = new DataSet();
|
---|
178 | if (map.mapView.getAllLayers().isEmpty())
|
---|
179 | setMapFrame(null);
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | public Main() {
|
---|
184 | main = this;
|
---|
185 | contentPane.add(panel, BorderLayout.CENTER);
|
---|
186 | panel.add(new GettingStarted(), BorderLayout.CENTER);
|
---|
187 | menu = new MainMenu();
|
---|
188 |
|
---|
189 | // creating toolbar
|
---|
190 | contentPane.add(toolbar.control, BorderLayout.NORTH);
|
---|
191 |
|
---|
192 | contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "Help");
|
---|
193 | contentPane.getActionMap().put("Help", menu.help);
|
---|
194 |
|
---|
195 | TaggingPresetPreference.initialize();
|
---|
196 |
|
---|
197 | toolbar.refreshToolbarControl();
|
---|
198 |
|
---|
199 | toolbar.control.updateUI();
|
---|
200 | contentPane.updateUI();
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Load all plugins specified in preferences. If the parameter is <code>true</code>, all
|
---|
205 | * early plugins are loaded (before constructor).
|
---|
206 | */
|
---|
207 | public static void loadPlugins(boolean early) {
|
---|
208 | List<String> plugins = new LinkedList<String>();
|
---|
209 | if (Main.pref.hasKey("plugins"))
|
---|
210 | plugins.addAll(Arrays.asList(Main.pref.get("plugins").split(",")));
|
---|
211 | if (System.getProperty("josm.plugins") != null)
|
---|
212 | plugins.addAll(Arrays.asList(System.getProperty("josm.plugins").split(",")));
|
---|
213 | if (plugins.isEmpty())
|
---|
214 | return;
|
---|
215 | SortedMap<Integer, Collection<PluginInformation>> p = new TreeMap<Integer, Collection<PluginInformation>>();
|
---|
216 | for (String pluginName : plugins) {
|
---|
217 | PluginInformation info = PluginInformation.findPlugin(pluginName);
|
---|
218 | if (info != null) {
|
---|
219 | if (info.early != early)
|
---|
220 | continue;
|
---|
221 | if (!p.containsKey(info.stage))
|
---|
222 | p.put(info.stage, new LinkedList<PluginInformation>());
|
---|
223 | p.get(info.stage).add(info);
|
---|
224 | } else {
|
---|
225 | if (early)
|
---|
226 | System.out.println("Plugin not found: "+pluginName); // do not translate
|
---|
227 | else
|
---|
228 | JOptionPane.showMessageDialog(Main.parent, tr("Plugin not found: {0}.", pluginName));
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | // iterate all plugins and collect all libraries of all plugins:
|
---|
233 | List<URL> allPluginLibraries = new ArrayList<URL>();
|
---|
234 | for (Collection<PluginInformation> c : p.values())
|
---|
235 | for (PluginInformation info : c)
|
---|
236 | allPluginLibraries.addAll(info.libraries);
|
---|
237 | // create a classloader for all plugins:
|
---|
238 | URL[] jarUrls = new URL[allPluginLibraries.size()];
|
---|
239 | jarUrls = allPluginLibraries.toArray(jarUrls);
|
---|
240 | URLClassLoader pluginClassLoader = new URLClassLoader(jarUrls, Main.class.getClassLoader());
|
---|
241 | ImageProvider.sources.add(0, pluginClassLoader);
|
---|
242 |
|
---|
243 | for (Collection<PluginInformation> c : p.values()) {
|
---|
244 | for (PluginInformation info : c) {
|
---|
245 | try {
|
---|
246 | Class<?> klass = info.loadClass(pluginClassLoader);
|
---|
247 | if (klass != null) {
|
---|
248 | System.out.println("loading "+info.name);
|
---|
249 | Main.plugins.add(info.load(klass));
|
---|
250 | }
|
---|
251 | } catch (PluginException e) {
|
---|
252 | e.printStackTrace();
|
---|
253 | if (early)
|
---|
254 | System.out.println("Could not load plugin: "+info.name); // do not translate
|
---|
255 | else
|
---|
256 | JOptionPane.showMessageDialog(Main.parent, tr("Could not load plugin {0}.", info.name));
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Add a new layer to the map. If no map exist, create one.
|
---|
264 | */
|
---|
265 | public final void addLayer(final Layer layer) {
|
---|
266 | if (map == null) {
|
---|
267 | final MapFrame mapFrame = new MapFrame();
|
---|
268 | setMapFrame(mapFrame);
|
---|
269 | mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction());
|
---|
270 | mapFrame.setVisible(true);
|
---|
271 | mapFrame.setVisibleDialogs();
|
---|
272 | }
|
---|
273 | map.mapView.addLayer(layer);
|
---|
274 | }
|
---|
275 | /**
|
---|
276 | * @return The edit osm layer. If none exist, it will be created.
|
---|
277 | */
|
---|
278 | public final OsmDataLayer editLayer() {
|
---|
279 | if (map == null || map.mapView.editLayer == null)
|
---|
280 | menu.newAction.actionPerformed(null);
|
---|
281 | return map.mapView.editLayer;
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 |
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Use this to register shortcuts to
|
---|
289 | */
|
---|
290 | public static final JPanel contentPane = new JPanel(new BorderLayout());
|
---|
291 |
|
---|
292 |
|
---|
293 | ////////////////////////////////////////////////////////////////////////////////////////
|
---|
294 | // Implementation part
|
---|
295 | ////////////////////////////////////////////////////////////////////////////////////////
|
---|
296 |
|
---|
297 | public static JPanel panel = new JPanel(new BorderLayout());
|
---|
298 |
|
---|
299 | protected static Rectangle bounds;
|
---|
300 |
|
---|
301 | private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
|
---|
302 | public void commandChanged(final int queueSize, final int redoSize) {
|
---|
303 | menu.undo.setEnabled(queueSize > 0);
|
---|
304 | menu.redo.setEnabled(redoSize > 0);
|
---|
305 | }
|
---|
306 | };
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Should be called before the main constructor to setup some parameter stuff
|
---|
310 | * @param args The parsed argument list.
|
---|
311 | */
|
---|
312 | public static void preConstructorInit(Map<String, Collection<String>> args) {
|
---|
313 | try {
|
---|
314 | Main.proj = (Projection)Class.forName(Main.pref.get("projection")).newInstance();
|
---|
315 | } catch (final Exception e) {
|
---|
316 | e.printStackTrace();
|
---|
317 | JOptionPane.showMessageDialog(null, tr("The projection could not be read from preferences. Using EPSG:4263."));
|
---|
318 | Main.proj = new Epsg4326();
|
---|
319 | }
|
---|
320 |
|
---|
321 | try {
|
---|
322 | UIManager.setLookAndFeel(Main.pref.get("laf"));
|
---|
323 | contentPane.updateUI();
|
---|
324 | panel.updateUI();
|
---|
325 | } catch (final Exception e) {
|
---|
326 | e.printStackTrace();
|
---|
327 | }
|
---|
328 | UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
|
---|
329 | UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
|
---|
330 | UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
|
---|
331 | UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
|
---|
332 |
|
---|
333 | Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
334 | if (args.containsKey("geometry")) {
|
---|
335 | String geometry = args.get("geometry").iterator().next();
|
---|
336 | final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
|
---|
337 | if (m.matches()) {
|
---|
338 | int w = Integer.valueOf(m.group(1));
|
---|
339 | int h = Integer.valueOf(m.group(2));
|
---|
340 | int x = 0, y = 0;
|
---|
341 | if (m.group(3) != null) {
|
---|
342 | x = Integer.valueOf(m.group(5));
|
---|
343 | y = Integer.valueOf(m.group(7));
|
---|
344 | if (m.group(4).equals("-"))
|
---|
345 | x = screenDimension.width - x - w;
|
---|
346 | if (m.group(6).equals("-"))
|
---|
347 | y = screenDimension.height - y - h;
|
---|
348 | }
|
---|
349 | bounds = new Rectangle(x,y,w,h);
|
---|
350 | } else
|
---|
351 | System.out.println("Ignoring malformed geometry: "+geometry);
|
---|
352 | }
|
---|
353 | if (bounds == null)
|
---|
354 | bounds = !args.containsKey("no-fullscreen") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
|
---|
355 |
|
---|
356 | // preinitialize a wait dialog for all early downloads (e.g. via command line)
|
---|
357 | pleaseWaitDlg = new PleaseWaitDialog(null);
|
---|
358 | }
|
---|
359 |
|
---|
360 | public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
|
---|
361 | // initialize the pleaseWaitDialog with the application as parent to handle focus stuff
|
---|
362 | pleaseWaitDlg = new PleaseWaitDialog(parent);
|
---|
363 |
|
---|
364 | if (args.containsKey("download"))
|
---|
365 | for (String s : args.get("download"))
|
---|
366 | downloadFromParamString(false, s);
|
---|
367 | if (args.containsKey("downloadgps"))
|
---|
368 | for (String s : args.get("downloadgps"))
|
---|
369 | downloadFromParamString(true, s);
|
---|
370 | if (args.containsKey("selection"))
|
---|
371 | for (String s : args.get("selection"))
|
---|
372 | SearchAction.search(s, SearchAction.SearchMode.add, false);
|
---|
373 | }
|
---|
374 |
|
---|
375 | public static boolean breakBecauseUnsavedChanges() {
|
---|
376 | if (map != null) {
|
---|
377 | boolean modified = false;
|
---|
378 | boolean uploadedModified = false;
|
---|
379 | for (final Layer l : map.mapView.getAllLayers()) {
|
---|
380 | if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
|
---|
381 | modified = true;
|
---|
382 | uploadedModified = ((OsmDataLayer)l).uploadedModified;
|
---|
383 | break;
|
---|
384 | }
|
---|
385 | }
|
---|
386 | if (modified) {
|
---|
387 | final String msg = uploadedModified ? "\n"+tr("Hint: Some changes came from uploading new data to the server.") : "";
|
---|
388 | final int answer = JOptionPane.showConfirmDialog(
|
---|
389 | parent, tr("There are unsaved changes. Discard the changes and continue?")+msg,
|
---|
390 | tr("Unsaved Changes"), JOptionPane.YES_NO_OPTION);
|
---|
391 | if (answer != JOptionPane.YES_OPTION)
|
---|
392 | return true;
|
---|
393 | }
|
---|
394 | }
|
---|
395 | return false;
|
---|
396 | }
|
---|
397 |
|
---|
398 | private static void downloadFromParamString(final boolean rawGps, String s) {
|
---|
399 | if (s.startsWith("http:")) {
|
---|
400 | final Bounds b = BoundingBoxSelection.osmurl2bounds(s);
|
---|
401 | if (b == null)
|
---|
402 | JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed url: \"{0}\"", s));
|
---|
403 | else {
|
---|
404 | //DownloadTask osmTask = main.menu.download.downloadTasks.get(0);
|
---|
405 | DownloadTask osmTask = new DownloadOsmTask();
|
---|
406 | osmTask.download(main.menu.download, b.min.lat(), b.min.lon(), b.max.lat(), b.max.lon());
|
---|
407 | }
|
---|
408 | return;
|
---|
409 | }
|
---|
410 |
|
---|
411 | if (s.startsWith("file:")) {
|
---|
412 | try {
|
---|
413 | main.menu.open.openFile(new File(new URI(s)));
|
---|
414 | } catch (URISyntaxException e) {
|
---|
415 | JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed file url: \"{0}\"", s));
|
---|
416 | }
|
---|
417 | return;
|
---|
418 | }
|
---|
419 |
|
---|
420 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
421 | if (st.countTokens() == 4) {
|
---|
422 | try {
|
---|
423 | //DownloadTask task = main.menu.download.downloadTasks.get(rawGps ? 1 : 0);
|
---|
424 | DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
|
---|
425 | task.download(main.menu.download, Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
|
---|
426 | return;
|
---|
427 | } catch (final NumberFormatException e) {
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | main.menu.open.openFile(new File(s));
|
---|
432 | }
|
---|
433 | }
|
---|