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

Last change on this file since 1104 was 1104, checked in by framm, 15 years ago
  • add extra message to plugin update warning telling people how to circumvent it
  • Property svn:eol-style set to native
File size: 20.2 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.UIManager;
34
35import org.openstreetmap.josm.actions.AboutAction;
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;
61import org.openstreetmap.josm.tools.PlatformHook;
62import org.openstreetmap.josm.tools.PlatformHookUnixoid;
63import org.openstreetmap.josm.tools.PlatformHookWindows;
64import org.openstreetmap.josm.tools.PlatformHookOsx;
65import org.openstreetmap.josm.tools.Shortcut;
66
67abstract public class Main {
68 /**
69 * Global parent component for all dialogs and message boxes
70 */
71 public static Component parent;
72 /**
73 * Global application.
74 */
75 public static Main main;
76 /**
77 * The worker thread slave. This is for executing all long and intensive
78 * calculations. The executed runnables are guaranteed to be executed separately
79 * and sequential.
80 */
81 public final static Executor worker = Executors.newSingleThreadExecutor();
82 /**
83 * Global application preferences
84 */
85 public static Preferences pref = new Preferences();
86 /**
87 * The global dataset.
88 */
89 public static DataSet ds = new DataSet();
90 /**
91 * The global paste buffer.
92 */
93 public static DataSet pasteBuffer = new DataSet();
94 /**
95 * The projection method used.
96 */
97 public static Projection proj;
98 /**
99 * The MapFrame. Use setMapFrame to set or clear it.
100 */
101 public static MapFrame map;
102 /**
103 * All installed and loaded plugins (resp. their main classes)
104 */
105 public final static Collection<PluginProxy> plugins = new LinkedList<PluginProxy>();
106 /**
107 * The dialog that gets displayed during background task execution.
108 */
109 public static PleaseWaitDialog pleaseWaitDlg;
110
111 /**
112 * True, when in applet mode
113 */
114 public static boolean applet = false;
115
116 /**
117 * The toolbar preference control to register new actions.
118 */
119 public static ToolbarPreferences toolbar;
120
121
122 public UndoRedoHandler undoRedo = new UndoRedoHandler();
123
124 /**
125 * The main menu bar at top of screen.
126 */
127 public final MainMenu menu;
128
129 /**
130 * Print a debug message if debugging is on.
131 */
132 static public int debug_level = 1;
133 static public final void debug(String msg) {
134 if (debug_level <= 0)
135 return;
136 System.out.println(msg);
137 }
138
139 /**
140 * Platform specific code goes in here.
141 * Plugins may replace it, however, some hooks will be called before any plugins have been loeaded.
142 * So if you need to hook into those early ones, split your class and send the one with the early hooks
143 * to the JOSM team for inclusion.
144 */
145 public static PlatformHook platform;
146
147 /**
148 * Set or clear (if passed <code>null</code>) the map.
149 */
150 public final void setMapFrame(final MapFrame map) {
151 MapFrame old = Main.map;
152 Main.map = map;
153 panel.setVisible(false);
154 panel.removeAll();
155 if (map != null)
156 map.fillPanel(panel);
157 else {
158 old.destroy();
159 panel.add(new GettingStarted(), BorderLayout.CENTER);
160 }
161 panel.setVisible(true);
162 redoUndoListener.commandChanged(0,0);
163
164 for (PluginProxy plugin : plugins)
165 plugin.mapFrameInitialized(old, map);
166 }
167
168 /**
169 * Set the layer menu (changed when active layer changes).
170 */
171 public final void setLayerMenu(Component[] entries) {
172 //if (entries == null || entries.length == 0)
173 //menu.layerMenu.setVisible(false);
174 //else {
175 //menu.layerMenu.removeAll();
176 //for (Component c : entries)
177 //menu.layerMenu.add(c);
178 //menu.layerMenu.setVisible(true);
179 //}
180 }
181
182 /**
183 * Remove the specified layer from the map. If it is the last layer,
184 * remove the map as well.
185 */
186 public final void removeLayer(final Layer layer) {
187 map.mapView.removeLayer(layer);
188 if (layer instanceof OsmDataLayer)
189 ds = new DataSet();
190 if (map.mapView.getAllLayers().isEmpty())
191 setMapFrame(null);
192 }
193
194 public Main() {
195 main = this;
196// platform = determinePlatformHook();
197 platform.startupHook();
198 contentPane.add(panel, BorderLayout.CENTER);
199 panel.add(new GettingStarted(), BorderLayout.CENTER);
200 menu = new MainMenu();
201
202 undoRedo.listenerCommands.add(redoUndoListener);
203
204 // creating toolbar
205 contentPane.add(toolbar.control, BorderLayout.NORTH);
206
207 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1, Shortcut.GROUP_DIRECT).getKeyStroke(), "Help");
208 contentPane.getActionMap().put("Help", menu.help);
209
210 TaggingPresetPreference.initialize();
211 MapPaintPreference.initialize();
212
213 toolbar.refreshToolbarControl();
214
215 toolbar.control.updateUI();
216 contentPane.updateUI();
217 }
218
219 /**
220 * Load all plugins specified in preferences. If the parameter is
221 * <code>true</code>, all early plugins are loaded (before constructor).
222 */
223 public static void loadPlugins(boolean early) {
224 List<String> plugins = new LinkedList<String>();
225 Collection<String> cp = Main.pref.getCollection("plugins", null);
226 if (cp != null)
227 plugins.addAll(cp);
228 if (System.getProperty("josm.plugins") != null)
229 plugins.addAll(Arrays.asList(System.getProperty("josm.plugins").split(",")));
230
231 String [] oldplugins = new String[] {"mappaint", "unglueplugin", "lang-de","lang-en_GB","lang-fr","lang-it","lang-pl","lang-ro","lang-ru"};
232 for (String p : oldplugins) {
233 if (plugins.contains(p)) {
234 plugins.remove(p);
235 Main.pref.removeFromCollection("plugins", p);
236 System.out.println(tr("Warning - loading of {0} plugin was requested. This plugin is no longer required.", p));
237 }
238 }
239
240 if (plugins.isEmpty())
241 return;
242
243 SortedMap<Integer, Collection<PluginInformation>> p = new TreeMap<Integer, Collection<PluginInformation>>();
244 for (String pluginName : plugins) {
245 PluginInformation info = PluginInformation.findPlugin(pluginName);
246 if (info != null) {
247 if (info.early != early)
248 continue;
249 if (info.mainversion != null && info.mainversion.compareTo(AboutAction.version) > 0) {
250 JOptionPane.showMessageDialog(Main.parent, tr("Plugin requires JOSM update: {0}.", pluginName));
251 continue;
252 }
253 if (!p.containsKey(info.stage))
254 p.put(info.stage, new LinkedList<PluginInformation>());
255 p.get(info.stage).add(info);
256 } else {
257 if (early)
258 System.out.println("Plugin not found: "+pluginName); // do not translate
259 else
260 JOptionPane.showMessageDialog(Main.parent, tr("Plugin not found: {0}.", pluginName));
261 }
262 }
263
264 if (!early) {
265 long tim = System.currentTimeMillis();
266 long last = Main.pref.getLong("pluginmanager.lastupdate", 0);
267 Integer maxTime = Main.pref.getInteger("pluginmanager.warntime", 30);
268 long d = (tim - last)/(24*60*60*1000l);
269 if ((last <= 0) || (maxTime <= 0)) {
270 Main.pref.put("pluginmanager.lastupdate",Long.toString(tim));
271 } else if (d > maxTime) {
272 JOptionPane.showMessageDialog(Main.parent,
273 "<html>" +
274 tr("Last plugin update more than {0} days ago.", d) +
275 "<br><em>" +
276 tr("(You can change the number of days after which this warning appears<br>by setting the config option 'pluginmanager.warntime'.)") +
277 "</html>");
278 }
279 }
280
281 // iterate all plugins and collect all libraries of all plugins:
282 List<URL> allPluginLibraries = new ArrayList<URL>();
283 for (Collection<PluginInformation> c : p.values())
284 for (PluginInformation info : c)
285 allPluginLibraries.addAll(info.libraries);
286 // create a classloader for all plugins:
287 URL[] jarUrls = new URL[allPluginLibraries.size()];
288 jarUrls = allPluginLibraries.toArray(jarUrls);
289 URLClassLoader pluginClassLoader = new URLClassLoader(jarUrls, Main.class.getClassLoader());
290 ImageProvider.sources.add(0, pluginClassLoader);
291
292 for (Collection<PluginInformation> c : p.values()) {
293 for (PluginInformation info : c) {
294 try {
295 Class<?> klass = info.loadClass(pluginClassLoader);
296 if (klass != null) {
297 System.out.println("loading "+info.name);
298 Main.plugins.add(info.load(klass));
299 }
300 } catch (Throwable e) {
301 e.printStackTrace();
302 boolean remove = true;
303 if (early)
304 System.out.println("Could not load plugin: "+info.name+" - deleted from preferences"); // do not translate
305 else {
306 int answer = JOptionPane.showConfirmDialog(Main.parent,
307 tr("Could not load plugin {0}. Delete from preferences?", info.name,
308 JOptionPane.YES_NO_OPTION));
309 if (answer != JOptionPane.OK_OPTION) {
310 remove = false;
311 }
312 }
313 if (remove) {
314 plugins.remove(info.name);
315 String plist = null;
316 for (String pn : plugins) {
317 if (plist==null) plist=""; else plist=plist+",";
318 plist=plist+pn;
319 }
320 Main.pref.put("plugins", plist);
321 }
322 }
323 }
324 }
325 }
326
327 /**
328 * Add a new layer to the map. If no map exists, create one.
329 */
330 public final void addLayer(final Layer layer) {
331 if (map == null) {
332 final MapFrame mapFrame = new MapFrame();
333 setMapFrame(mapFrame);
334 mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction());
335 mapFrame.setVisible(true);
336 mapFrame.setVisibleDialogs();
337 }
338 map.mapView.addLayer(layer);
339 }
340 /**
341 * @return The edit osm layer. If none exists, it will be created.
342 */
343 public final OsmDataLayer editLayer() {
344 if (map == null || map.mapView.editLayer == null)
345 menu.newAction.actionPerformed(null);
346 return map.mapView.editLayer;
347 }
348
349 /**
350 * Use this to register shortcuts to
351 */
352 public static final JPanel contentPane = new JPanel(new BorderLayout());
353
354
355 ///////////////////////////////////////////////////////////////////////////
356 // Implementation part
357 ///////////////////////////////////////////////////////////////////////////
358
359 public static JPanel panel = new JPanel(new BorderLayout());
360
361 protected static Rectangle bounds;
362
363 private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
364 public void commandChanged(final int queueSize, final int redoSize) {
365 menu.undo.setEnabled(queueSize > 0);
366 menu.redo.setEnabled(redoSize > 0);
367 }
368 };
369 /**
370 * Should be called before the main constructor to setup some parameter stuff
371 * @param args The parsed argument list.
372 */
373 public static void preConstructorInit(Map<String, Collection<String>> args) {
374 try {
375 Main.proj = (Projection)Class.forName(Main.pref.get("projection")).newInstance();
376 } catch (final Exception e) {
377 e.printStackTrace();
378 JOptionPane.showMessageDialog(null, tr("The projection could not be read from preferences. Using EPSG:4263."));
379 Main.proj = new Epsg4326();
380 }
381
382 try {
383 UIManager.setLookAndFeel(Main.pref.get("laf"));
384 toolbar = new ToolbarPreferences();
385 contentPane.updateUI();
386 panel.updateUI();
387 } catch (final Exception e) {
388 e.printStackTrace();
389 }
390 UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
391 UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
392 UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
393 UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
394
395 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
396 if (args.containsKey("geometry")) {
397 String geometry = args.get("geometry").iterator().next();
398 final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
399 if (m.matches()) {
400 int w = Integer.valueOf(m.group(1));
401 int h = Integer.valueOf(m.group(2));
402 int x = 0, y = 0;
403 if (m.group(3) != null) {
404 x = Integer.valueOf(m.group(5));
405 y = Integer.valueOf(m.group(7));
406 if (m.group(4).equals("-"))
407 x = screenDimension.width - x - w;
408 if (m.group(6).equals("-"))
409 y = screenDimension.height - y - h;
410 }
411 bounds = new Rectangle(x,y,w,h);
412 } else
413 System.out.println("Ignoring malformed geometry: "+geometry);
414 }
415 if (bounds == null)
416 bounds = !args.containsKey("no-fullscreen") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
417
418 // preinitialize a wait dialog for all early downloads (e.g. via command line)
419 pleaseWaitDlg = new PleaseWaitDialog(null);
420 }
421
422 public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
423 // initialize the pleaseWaitDialog with the application as parent to handle focus stuff
424 pleaseWaitDlg = new PleaseWaitDialog(parent);
425
426 if (args.containsKey("download"))
427 for (String s : args.get("download"))
428 downloadFromParamString(false, s);
429 if (args.containsKey("downloadgps"))
430 for (String s : args.get("downloadgps"))
431 downloadFromParamString(true, s);
432 if (args.containsKey("selection"))
433 for (String s : args.get("selection"))
434 SearchAction.search(s, SearchAction.SearchMode.add, false);
435 }
436
437 public static boolean breakBecauseUnsavedChanges() {
438 Shortcut.savePrefs();
439 if (map != null) {
440 boolean modified = false;
441 boolean uploadedModified = false;
442 for (final Layer l : map.mapView.getAllLayers()) {
443 if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
444 modified = true;
445 uploadedModified = ((OsmDataLayer)l).uploadedModified;
446 break;
447 }
448 }
449 if (modified) {
450 final String msg = uploadedModified ? "\n"+tr("Hint: Some changes came from uploading new data to the server.") : "";
451 final int answer = JOptionPane.showConfirmDialog(
452 parent, tr("There are unsaved changes. Discard the changes and continue?")+msg,
453 tr("Unsaved Changes"), JOptionPane.YES_NO_OPTION);
454 if (answer != JOptionPane.YES_OPTION)
455 return true;
456 }
457 }
458 return false;
459 }
460
461 private static void downloadFromParamString(final boolean rawGps, String s) {
462 if (s.startsWith("http:")) {
463 final Bounds b = BoundingBoxSelection.osmurl2bounds(s);
464 if (b == null)
465 JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed url: \"{0}\"", s));
466 else {
467 //DownloadTask osmTask = main.menu.download.downloadTasks.get(0);
468 DownloadTask osmTask = new DownloadOsmTask();
469 osmTask.download(main.menu.download, b.min.lat(), b.min.lon(), b.max.lat(), b.max.lon());
470 }
471 return;
472 }
473
474 if (s.startsWith("file:")) {
475 try {
476 main.menu.open.openFile(new File(new URI(s)));
477 } catch (URISyntaxException e) {
478 JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed file url: \"{0}\"", s));
479 }
480 return;
481 }
482
483 final StringTokenizer st = new StringTokenizer(s, ",");
484 if (st.countTokens() == 4) {
485 try {
486 DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
487 task.download(main.menu.download, Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
488 return;
489 } catch (final NumberFormatException e) {
490 }
491 }
492
493 main.menu.open.openFile(new File(s));
494 }
495
496 protected static void determinePlatformHook() {
497 String os = System.getProperty("os.name");
498 if (os == null) {
499 System.err.println("Your operating system has no name, so I'm guessing its some kind of *nix.");
500 platform = new PlatformHookUnixoid();
501 } else if (os.toLowerCase().startsWith("windows")) {
502 platform = new PlatformHookWindows();
503 } else if (os.equals("Linux") || os.equals("Solaris") ||
504 os.equals("SunOS") || os.equals("AIX") ||
505 os.equals("FreeBSD") || os.equals("NetBSD") || os.equals("OpenBSD")) {
506 platform = new PlatformHookUnixoid();
507 } else if (os.toLowerCase().startsWith("mac os x")) {
508 platform = new PlatformHookOsx();
509 } else {
510 System.err.println("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
511 platform = new PlatformHookUnixoid();
512 }
513 }
514
515}
Note: See TracBrowser for help on using the repository browser.