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

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

cleanup language code handling

  • Property svn:eol-style set to native
File size: 20.9 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 UIManager.setLookAndFeel(Main.pref.get("laf"));
395 toolbar = new ToolbarPreferences();
396 contentPane.updateUI();
397 panel.updateUI();
398 } catch (final Exception e) {
399 e.printStackTrace();
400 }
401 UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
402 UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
403 UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
404 UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
405
406 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
407 if (args.containsKey("geometry")) {
408 String geometry = args.get("geometry").iterator().next();
409 final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
410 if (m.matches()) {
411 int w = Integer.valueOf(m.group(1));
412 int h = Integer.valueOf(m.group(2));
413 int x = 0, y = 0;
414 if (m.group(3) != null) {
415 x = Integer.valueOf(m.group(5));
416 y = Integer.valueOf(m.group(7));
417 if (m.group(4).equals("-"))
418 x = screenDimension.width - x - w;
419 if (m.group(6).equals("-"))
420 y = screenDimension.height - y - h;
421 }
422 bounds = new Rectangle(x,y,w,h);
423 } else
424 System.out.println("Ignoring malformed geometry: "+geometry);
425 }
426 if (bounds == null)
427 bounds = !args.containsKey("no-fullscreen") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
428
429 // preinitialize a wait dialog for all early downloads (e.g. via command line)
430 pleaseWaitDlg = new PleaseWaitDialog(null);
431 }
432
433 public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
434 // initialize the pleaseWaitDialog with the application as parent to handle focus stuff
435 pleaseWaitDlg = new PleaseWaitDialog(parent);
436
437 if (args.containsKey("download"))
438 for (String s : args.get("download"))
439 downloadFromParamString(false, s);
440 if (args.containsKey("downloadgps"))
441 for (String s : args.get("downloadgps"))
442 downloadFromParamString(true, s);
443 if (args.containsKey("selection"))
444 for (String s : args.get("selection"))
445 SearchAction.search(s, SearchAction.SearchMode.add, false);
446 }
447
448 public static boolean breakBecauseUnsavedChanges() {
449 Shortcut.savePrefs();
450 if (map != null) {
451 boolean modified = false;
452 boolean uploadedModified = false;
453 for (final Layer l : map.mapView.getAllLayers()) {
454 if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
455 modified = true;
456 uploadedModified = ((OsmDataLayer)l).uploadedModified;
457 break;
458 }
459 }
460 if (modified) {
461 final String msg = uploadedModified ? "\n"+tr("Hint: Some changes came from uploading new data to the server.") : "";
462 final int answer = JOptionPane.showConfirmDialog(
463 parent, tr("There are unsaved changes. Discard the changes and continue?")+msg,
464 tr("Unsaved Changes"), JOptionPane.YES_NO_OPTION);
465 if (answer != JOptionPane.YES_OPTION)
466 return true;
467 }
468 }
469 return false;
470 }
471
472 private static void downloadFromParamString(final boolean rawGps, String s) {
473 if (s.startsWith("http:")) {
474 final Bounds b = BoundingBoxSelection.osmurl2bounds(s);
475 if (b == null)
476 JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed url: \"{0}\"", s));
477 else {
478 //DownloadTask osmTask = main.menu.download.downloadTasks.get(0);
479 DownloadTask osmTask = new DownloadOsmTask();
480 osmTask.download(main.menu.download, b.min.lat(), b.min.lon(), b.max.lat(), b.max.lon());
481 }
482 return;
483 }
484
485 if (s.startsWith("file:")) {
486 try {
487 main.menu.openFile.openFile(new File(new URI(s)));
488 } catch (URISyntaxException e) {
489 JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed file url: \"{0}\"", s));
490 }
491 return;
492 }
493
494 final StringTokenizer st = new StringTokenizer(s, ",");
495 if (st.countTokens() == 4) {
496 try {
497 DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
498 task.download(main.menu.download, Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
499 return;
500 } catch (final NumberFormatException e) {
501 }
502 }
503
504 main.menu.openFile.openFile(new File(s));
505 }
506
507 protected static void determinePlatformHook() {
508 String os = System.getProperty("os.name");
509 if (os == null) {
510 System.err.println("Your operating system has no name, so I'm guessing its some kind of *nix.");
511 platform = new PlatformHookUnixoid();
512 } else if (os.toLowerCase().startsWith("windows")) {
513 platform = new PlatformHookWindows();
514 } else if (os.equals("Linux") || os.equals("Solaris") ||
515 os.equals("SunOS") || os.equals("AIX") ||
516 os.equals("FreeBSD") || os.equals("NetBSD") || os.equals("OpenBSD")) {
517 platform = new PlatformHookUnixoid();
518 } else if (os.toLowerCase().startsWith("mac os x")) {
519 platform = new PlatformHookOsx();
520 } else {
521 System.err.println("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
522 platform = new PlatformHookUnixoid();
523 }
524 }
525
526 static public String getLanguageCodeU()
527 {
528 String languageCode = Locale.getDefault().getLanguage();
529 return languageCode.substring(0,1).toUpperCase() + languageCode.substring(1) + ":";
530 }
531 static public String getLanguageCode()
532 {
533 return Locale.getDefault().getLanguage() + ":";
534 }
535}
Note: See TracBrowser for help on using the repository browser.