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

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

fixed relation handling, applied language patches

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