source: josm/trunk/src/org/openstreetmap/josm/Main.java@ 729

Last change on this file since 729 was 655, checked in by ramack, 16 years ago

patch by bruce89, closes #812; thanks bruce

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