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

Last change on this file since 1023 was 1023, checked in by stoecker, 16 years ago

close bug #1622. Keyboard shortcuts and specific OS handling

  • Property svn:eol-style set to native
File size: 16.3 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;
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, remove the map as well.
184 */
185 public final void removeLayer(final Layer layer) {
186 map.mapView.removeLayer(layer);
187 if (layer instanceof OsmDataLayer)
188 ds = new DataSet();
189 if (map.mapView.getAllLayers().isEmpty())
190 setMapFrame(null);
191 }
192
193 public Main() {
194 main = this;
195// platform = determinePlatformHook();
196 platform.startupHook();
197 contentPane.add(panel, BorderLayout.CENTER);
198 panel.add(new GettingStarted(), BorderLayout.CENTER);
199 menu = new MainMenu();
200
201 undoRedo.listenerCommands.add(redoUndoListener);
202
203 // creating toolbar
204 contentPane.add(toolbar.control, BorderLayout.NORTH);
205
206 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ShortCut.registerShortCut("system:help", tr("Help"), KeyEvent.VK_F1, ShortCut.GROUP_DIRECT).getKeyStroke(), "Help");
207 contentPane.getActionMap().put("Help", menu.help);
208
209 TaggingPresetPreference.initialize();
210 MapPaintPreference.initialize();
211
212 toolbar.refreshToolbarControl();
213
214 toolbar.control.updateUI();
215 contentPane.updateUI();
216 }
217
218 /**
219 * Load all plugins specified in preferences. If the parameter is <code>true</code>, all
220 * early plugins are loaded (before constructor).
221 */
222 public static void loadPlugins(boolean early, String lang) {
223 List<String> plugins = new LinkedList<String>();
224 if (Main.pref.hasKey("plugins"))
225 plugins.addAll(Arrays.asList(Main.pref.get("plugins").split(",")));
226 if (System.getProperty("josm.plugins") != null)
227 plugins.addAll(Arrays.asList(System.getProperty("josm.plugins").split(",")));
228
229 String [] oldplugins = new String[]{"mappaint", "unglueplugin"};
230 for(String p : oldplugins)
231 {
232 if(plugins.contains(p))
233 {
234 plugins.remove(p);
235 System.out.println(tr("Warning - loading of {0} plugin was requested. This plugin is no longer required.", p));
236 }
237 }
238 if(lang != null)
239 {
240 for(String p : plugins)
241 {
242 if(p.startsWith("lang-"))
243 {
244 plugins.remove(p);
245 break;
246 }
247 }
248 if(!lang.equals("en"))
249 plugins.add("lang-"+lang);
250 }
251
252 if (plugins.isEmpty())
253 return;
254 SortedMap<Integer, Collection<PluginInformation>> p = new TreeMap<Integer, Collection<PluginInformation>>();
255 for (String pluginName : plugins) {
256 PluginInformation info = PluginInformation.findPlugin(pluginName);
257 if (info != null) {
258 if (info.early != early)
259 continue;
260 if (!p.containsKey(info.stage))
261 p.put(info.stage, new LinkedList<PluginInformation>());
262 p.get(info.stage).add(info);
263 } else {
264 if (early)
265 System.out.println("Plugin not found: "+pluginName); // do not translate
266 else
267 JOptionPane.showMessageDialog(Main.parent, tr("Plugin not found: {0}.", pluginName));
268 }
269 }
270
271 // iterate all plugins and collect all libraries of all plugins:
272 List<URL> allPluginLibraries = new ArrayList<URL>();
273 for (Collection<PluginInformation> c : p.values())
274 for (PluginInformation info : c)
275 allPluginLibraries.addAll(info.libraries);
276 // create a classloader for all plugins:
277 URL[] jarUrls = new URL[allPluginLibraries.size()];
278 jarUrls = allPluginLibraries.toArray(jarUrls);
279 URLClassLoader pluginClassLoader = new URLClassLoader(jarUrls, Main.class.getClassLoader());
280 ImageProvider.sources.add(0, pluginClassLoader);
281
282 for (Collection<PluginInformation> c : p.values()) {
283 for (PluginInformation info : c) {
284 try {
285 Class<?> klass = info.loadClass(pluginClassLoader);
286 if (klass != null) {
287 System.out.println("loading "+info.name);
288 Main.plugins.add(info.load(klass));
289 }
290 } catch (Throwable e) {
291 e.printStackTrace();
292 boolean remove = true;
293 if (early)
294 System.out.println("Could not load plugin: "+info.name+" - deleted from preferences"); // do not translate
295 else {
296 int answer = JOptionPane.showConfirmDialog(Main.parent,
297 tr("Could not load plugin {0}. Delete from preferences?", info.name,
298 JOptionPane.YES_NO_OPTION));
299 if (answer != JOptionPane.OK_OPTION) {
300 remove = false;
301 }
302 }
303 if (remove) {
304 plugins.remove(info.name);
305 String plist = null;
306 for (String pn : plugins) {
307 if (plist==null) plist=""; else plist=plist+",";
308 plist=plist+pn;
309 }
310 Main.pref.put("plugins", plist);
311 }
312 }
313 }
314 }
315 }
316
317 /**
318 * Add a new layer to the map. If no map exists, create one.
319 */
320 public final void addLayer(final Layer layer) {
321 if (map == null) {
322 final MapFrame mapFrame = new MapFrame();
323 setMapFrame(mapFrame);
324 mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction());
325 mapFrame.setVisible(true);
326 mapFrame.setVisibleDialogs();
327 }
328 map.mapView.addLayer(layer);
329 }
330 /**
331 * @return The edit osm layer. If none exists, it will be created.
332 */
333 public final OsmDataLayer editLayer() {
334 if (map == null || map.mapView.editLayer == null)
335 menu.newAction.actionPerformed(null);
336 return map.mapView.editLayer;
337 }
338
339
340
341
342 /**
343 * Use this to register shortcuts to
344 */
345 public static final JPanel contentPane = new JPanel(new BorderLayout());
346
347
348 ////////////////////////////////////////////////////////////////////////////////////////
349 // Implementation part
350 ////////////////////////////////////////////////////////////////////////////////////////
351
352 public static JPanel panel = new JPanel(new BorderLayout());
353
354 protected static Rectangle bounds;
355
356 private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
357 public void commandChanged(final int queueSize, final int redoSize) {
358 menu.undo.setEnabled(queueSize > 0);
359 menu.redo.setEnabled(redoSize > 0);
360 }
361 };
362 /**
363 * Should be called before the main constructor to setup some parameter stuff
364 * @param args The parsed argument list.
365 */
366 public static void preConstructorInit(Map<String, Collection<String>> args) {
367 try {
368 Main.proj = (Projection)Class.forName(Main.pref.get("projection")).newInstance();
369 } catch (final Exception e) {
370 e.printStackTrace();
371 JOptionPane.showMessageDialog(null, tr("The projection could not be read from preferences. Using EPSG:4263."));
372 Main.proj = new Epsg4326();
373 }
374
375 try {
376 UIManager.setLookAndFeel(Main.pref.get("laf"));
377 toolbar = new ToolbarPreferences();
378 contentPane.updateUI();
379 panel.updateUI();
380 } catch (final Exception e) {
381 e.printStackTrace();
382 }
383 UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
384 UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
385 UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
386 UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
387
388 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
389 if (args.containsKey("geometry")) {
390 String geometry = args.get("geometry").iterator().next();
391 final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
392 if (m.matches()) {
393 int w = Integer.valueOf(m.group(1));
394 int h = Integer.valueOf(m.group(2));
395 int x = 0, y = 0;
396 if (m.group(3) != null) {
397 x = Integer.valueOf(m.group(5));
398 y = Integer.valueOf(m.group(7));
399 if (m.group(4).equals("-"))
400 x = screenDimension.width - x - w;
401 if (m.group(6).equals("-"))
402 y = screenDimension.height - y - h;
403 }
404 bounds = new Rectangle(x,y,w,h);
405 } else
406 System.out.println("Ignoring malformed geometry: "+geometry);
407 }
408 if (bounds == null)
409 bounds = !args.containsKey("no-fullscreen") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
410
411 // preinitialize a wait dialog for all early downloads (e.g. via command line)
412 pleaseWaitDlg = new PleaseWaitDialog(null);
413 }
414
415 public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
416 // initialize the pleaseWaitDialog with the application as parent to handle focus stuff
417 pleaseWaitDlg = new PleaseWaitDialog(parent);
418
419 if (args.containsKey("download"))
420 for (String s : args.get("download"))
421 downloadFromParamString(false, s);
422 if (args.containsKey("downloadgps"))
423 for (String s : args.get("downloadgps"))
424 downloadFromParamString(true, s);
425 if (args.containsKey("selection"))
426 for (String s : args.get("selection"))
427 SearchAction.search(s, SearchAction.SearchMode.add, false);
428 }
429
430 public static boolean breakBecauseUnsavedChanges() {
431 ShortCut.savePrefs();
432 if (map != null) {
433 boolean modified = false;
434 boolean uploadedModified = false;
435 for (final Layer l : map.mapView.getAllLayers()) {
436 if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
437 modified = true;
438 uploadedModified = ((OsmDataLayer)l).uploadedModified;
439 break;
440 }
441 }
442 if (modified) {
443 final String msg = uploadedModified ? "\n"+tr("Hint: Some changes came from uploading new data to the server.") : "";
444 final int answer = JOptionPane.showConfirmDialog(
445 parent, tr("There are unsaved changes. Discard the changes and continue?")+msg,
446 tr("Unsaved Changes"), JOptionPane.YES_NO_OPTION);
447 if (answer != JOptionPane.YES_OPTION)
448 return true;
449 }
450 }
451 return false;
452 }
453
454 private static void downloadFromParamString(final boolean rawGps, String s) {
455 if (s.startsWith("http:")) {
456 final Bounds b = BoundingBoxSelection.osmurl2bounds(s);
457 if (b == null)
458 JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed url: \"{0}\"", s));
459 else {
460 //DownloadTask osmTask = main.menu.download.downloadTasks.get(0);
461 DownloadTask osmTask = new DownloadOsmTask();
462 osmTask.download(main.menu.download, b.min.lat(), b.min.lon(), b.max.lat(), b.max.lon());
463 }
464 return;
465 }
466
467 if (s.startsWith("file:")) {
468 try {
469 main.menu.open.openFile(new File(new URI(s)));
470 } catch (URISyntaxException e) {
471 JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed file url: \"{0}\"", s));
472 }
473 return;
474 }
475
476 final StringTokenizer st = new StringTokenizer(s, ",");
477 if (st.countTokens() == 4) {
478 try {
479 DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
480 task.download(main.menu.download, Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
481 return;
482 } catch (final NumberFormatException e) {
483 }
484 }
485
486 main.menu.open.openFile(new File(s));
487 }
488
489 protected static void determinePlatformHook() {
490 String os = System.getProperty("os.name");
491 if (os == null) {
492 System.err.println("Your operating system has no name, so I'm guessing its some kind of *nix.");
493 platform = new PlatformHookUnixoid();
494 } else if (os.toLowerCase().startsWith("windows")) {
495 platform = new PlatformHookWindows();
496 } else if (os.equals("Linux") || os.equals("Solaris") || os.equals("SunOS") || os.equals("AIX") || os.equals("FreeBSD")) {
497 platform = new PlatformHookUnixoid();
498 } else if (os.toLowerCase().startsWith("mac os x")) {
499 platform = new PlatformHookOsx();
500 } else {
501 System.err.println("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
502 platform = new PlatformHookUnixoid();
503 }
504 }
505
506}
Note: See TracBrowser for help on using the repository browser.