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

Last change on this file since 2039 was 2039, checked in by Gubaer, 15 years ago

applied #3377: patch by xeen: Less warnings for JOSM

  • Property svn:eol-style set to native
File size: 20.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;
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.Rectangle;
10import java.awt.Toolkit;
11import java.awt.event.KeyEvent;
12import java.io.File;
13import java.net.URI;
14import java.net.URISyntaxException;
15import java.util.ArrayList;
16import java.util.Collection;
17import java.util.List;
18import java.util.Map;
19import java.util.StringTokenizer;
20import java.util.concurrent.ExecutorService;
21import java.util.concurrent.Executors;
22import java.util.regex.Matcher;
23import java.util.regex.Pattern;
24
25import javax.swing.JComponent;
26import javax.swing.JFrame;
27import javax.swing.JLabel;
28import javax.swing.JOptionPane;
29import javax.swing.JPanel;
30import javax.swing.UIManager;
31
32import org.openstreetmap.josm.actions.SaveAction;
33import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
34import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
35import org.openstreetmap.josm.actions.mapmode.MapMode;
36import org.openstreetmap.josm.actions.search.SearchAction;
37import org.openstreetmap.josm.data.Bounds;
38import org.openstreetmap.josm.data.Preferences;
39import org.openstreetmap.josm.data.UndoRedoHandler;
40import org.openstreetmap.josm.data.coor.CoordinateFormat;
41import org.openstreetmap.josm.data.osm.DataSet;
42import org.openstreetmap.josm.data.projection.Mercator;
43import org.openstreetmap.josm.data.projection.Projection;
44import org.openstreetmap.josm.gui.ExtendedDialog;
45import org.openstreetmap.josm.gui.GettingStarted;
46import org.openstreetmap.josm.gui.MainMenu;
47import org.openstreetmap.josm.gui.MapFrame;
48import org.openstreetmap.josm.gui.SplashScreen;
49import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
50import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
51import org.openstreetmap.josm.gui.io.SaveLayersDialog;
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.PluginHandler;
59import org.openstreetmap.josm.tools.ImageProvider;
60import org.openstreetmap.josm.tools.OsmUrlToBounds;
61import org.openstreetmap.josm.tools.PlatformHook;
62import org.openstreetmap.josm.tools.PlatformHookOsx;
63import org.openstreetmap.josm.tools.PlatformHookUnixoid;
64import org.openstreetmap.josm.tools.PlatformHookWindows;
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 ExecutorService worker = Executors.newSingleThreadExecutor();
82 /**
83 * Global application preferences
84 */
85 public static Preferences pref = new Preferences();
86
87 /**
88 * The global paste buffer.
89 */
90 public static DataSet pasteBuffer = new DataSet();
91 public static Layer pasteSource;
92 /**
93 * The projection method used.
94 */
95 public static Projection proj;
96 /**
97 * The MapFrame. Use setMapFrame to set or clear it.
98 */
99 public static MapFrame map;
100 /**
101 * The dialog that gets displayed during background task execution.
102 */
103 //public static PleaseWaitDialog pleaseWaitDlg;
104
105 /**
106 * True, when in applet mode
107 */
108 public static boolean applet = false;
109
110 /**
111 * The toolbar preference control to register new actions.
112 */
113 public static ToolbarPreferences toolbar;
114
115
116 public UndoRedoHandler undoRedo = new UndoRedoHandler();
117
118 /**
119 * The main menu bar at top of screen.
120 */
121 public final MainMenu menu;
122
123 /**
124 * The MOTD Layer.
125 */
126 private GettingStarted gettingStarted=new GettingStarted();
127
128 /**
129 * Print a debug message if debugging is on.
130 */
131 static public int debug_level = 1;
132 static public final void debug(String msg) {
133 if (debug_level <= 0)
134 return;
135 System.out.println(msg);
136 }
137
138 /**
139 * Platform specific code goes in here.
140 * Plugins may replace it, however, some hooks will be called before any plugins have been loeaded.
141 * So if you need to hook into those early ones, split your class and send the one with the early hooks
142 * to the JOSM team for inclusion.
143 */
144 public static PlatformHook platform;
145
146 /**
147 * Set or clear (if passed <code>null</code>) the map.
148 */
149 public final void setMapFrame(final MapFrame map) {
150 MapFrame old = Main.map;
151 Main.map = map;
152 panel.setVisible(false);
153 panel.removeAll();
154 if (map != null) {
155 map.fillPanel(panel);
156 } else {
157 old.destroy();
158 panel.add(gettingStarted, BorderLayout.CENTER);
159 }
160 panel.setVisible(true);
161 redoUndoListener.commandChanged(0,0);
162
163 PluginHandler.setMapFrame(old, map);
164 }
165
166 /**
167 * Remove the specified layer from the map. If it is the last layer,
168 * remove the map as well.
169 */
170 public final void removeLayer(final Layer layer) {
171 if (map != null) {
172 map.mapView.removeLayer(layer);
173 if (map.mapView.getAllLayers().isEmpty()) {
174 setMapFrame(null);
175 }
176 }
177 }
178
179 public Main() {
180 this(null);
181 }
182
183 public Main(SplashScreen splash) {
184 main = this;
185 // platform = determinePlatformHook();
186 platform.startupHook();
187 contentPane.add(panel, BorderLayout.CENTER);
188 panel.add(gettingStarted, BorderLayout.CENTER);
189
190 if(splash != null) {
191 splash.setStatus(tr("Creating main GUI"));
192 }
193 menu = new MainMenu();
194
195 undoRedo.listenerCommands.add(redoUndoListener);
196
197 // creating toolbar
198 contentPane.add(toolbar.control, BorderLayout.NORTH);
199
200 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
201 .put(Shortcut.registerShortcut("system:help", tr("Help"),
202 KeyEvent.VK_F1, Shortcut.GROUP_DIRECT).getKeyStroke(), "Help");
203 contentPane.getActionMap().put("Help", menu.help);
204
205 TaggingPresetPreference.initialize();
206 MapPaintPreference.initialize();
207
208 toolbar.refreshToolbarControl();
209
210 toolbar.control.updateUI();
211 contentPane.updateUI();
212 }
213
214 /**
215 * Add a new layer to the map. If no map exists, create one.
216 */
217 public final void addLayer(final Layer layer) {
218 if (map == null) {
219 final MapFrame mapFrame = new MapFrame();
220 setMapFrame(mapFrame);
221 mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction());
222 mapFrame.setVisible(true);
223 mapFrame.setVisibleDialogs();
224 // bootstrapping problem: make sure the layer list dialog is going to
225 // listen to change events of the very first layer
226 //
227 layer.addPropertyChangeListener(LayerListDialog.getInstance().getModel());
228 }
229 map.mapView.addLayer(layer);
230 }
231
232 /**
233 * Replies true if there is an edit layer
234 *
235 * @return true if there is an edit layer
236 */
237 public boolean hasEditLayer() {
238 if (map == null) return false;
239 if (map.mapView == null) return false;
240 if (map.mapView.getEditLayer() == null) return false;
241 return true;
242 }
243
244 /**
245 * Replies the current edit layer
246 *
247 * @return the current edit layer. null, if no current edit layer exists
248 */
249 public OsmDataLayer getEditLayer() {
250 if (map == null) return null;
251 if (map.mapView == null) return null;
252 return map.mapView.getEditLayer();
253 }
254
255 /**
256 * Replies the current data set.
257 *
258 * @return the current data set. null, if no current data set exists
259 */
260 public DataSet getCurrentDataSet() {
261 if (!hasEditLayer()) return null;
262 return getEditLayer().data;
263 }
264
265 /**
266 * Use this to register shortcuts to
267 */
268 public static final JPanel contentPane = new JPanel(new BorderLayout());
269
270 ///////////////////////////////////////////////////////////////////////////
271 // Implementation part
272 ///////////////////////////////////////////////////////////////////////////
273
274 public static JPanel panel = new JPanel(new BorderLayout());
275
276 protected static Rectangle bounds;
277
278 private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
279 public void commandChanged(final int queueSize, final int redoSize) {
280 menu.undo.setEnabled(queueSize > 0);
281 menu.redo.setEnabled(redoSize > 0);
282 }
283 };
284
285 static public void setProjection(String name)
286 {
287 Bounds b = (map != null && map.mapView != null) ? map.mapView.getRealBounds() : null;
288 Projection oldProj = Main.proj;
289 try {
290 Main.proj = (Projection)Class.forName(name).newInstance();
291 } catch (final Exception e) {
292 JOptionPane.showMessageDialog(
293 Main.parent,
294 tr("The projection {0} could not be activated. Using Mercator", name),
295 tr("Error"),
296 JOptionPane.ERROR_MESSAGE
297 );
298 Main.proj = new Mercator();
299 }
300 if(!Main.proj.equals(oldProj))
301 {
302 if(b != null) {
303 map.mapView.zoomTo(b);
304 /* TODO - remove layers with fixed projection */
305 }
306 }
307 }
308
309 /**
310 * Should be called before the main constructor to setup some parameter stuff
311 * @param args The parsed argument list.
312 */
313 public static void preConstructorInit(Map<String, Collection<String>> args) {
314 setProjection(Main.pref.get("projection", Mercator.class.getName()));
315
316 try {
317 try {
318 String laf = Main.pref.get("laf");
319 if(laf != null && laf.length() > 0) {
320 UIManager.setLookAndFeel(laf);
321 }
322 }
323 catch (final javax.swing.UnsupportedLookAndFeelException e) {
324 System.out.println("Look and Feel not supported: " + Main.pref.get("laf"));
325 }
326 toolbar = new ToolbarPreferences();
327 contentPane.updateUI();
328 panel.updateUI();
329 } catch (final Exception e) {
330 e.printStackTrace();
331 }
332 UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
333 UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
334 UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
335 UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
336
337 // init default coordinate format
338 //
339 try {
340 //CoordinateFormat format = CoordinateFormat.valueOf(Main.pref.get("coordinates"));
341 CoordinateFormat.setCoordinateFormat(CoordinateFormat.valueOf(Main.pref.get("coordinates")));
342 } catch (IllegalArgumentException iae) {
343 CoordinateFormat.setCoordinateFormat(CoordinateFormat.DECIMAL_DEGREES);
344 }
345
346
347 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
348 String geometry = Main.pref.get("gui.geometry");
349 if (args.containsKey("geometry")) {
350 geometry = args.get("geometry").iterator().next();
351 }
352 if (geometry.length() != 0) {
353 final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
354 if (m.matches()) {
355 int w = Integer.valueOf(m.group(1));
356 int h = Integer.valueOf(m.group(2));
357 int x = 0, y = 0;
358 if (m.group(3) != null) {
359 x = Integer.valueOf(m.group(5));
360 y = Integer.valueOf(m.group(7));
361 if (m.group(4).equals("-")) {
362 x = screenDimension.width - x - w;
363 }
364 if (m.group(6).equals("-")) {
365 y = screenDimension.height - y - h;
366 }
367 }
368 bounds = new Rectangle(x,y,w,h);
369 if(!Main.pref.get("gui.geometry").equals(geometry)) {
370 // remember this geometry
371 Main.pref.put("gui.geometry", geometry);
372 }
373 } else {
374 System.out.println("Ignoring malformed geometry: "+geometry);
375 }
376 }
377 if (bounds == null) {
378 bounds = !args.containsKey("no-maximize") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
379 }
380 }
381
382 public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
383 if (args.containsKey("download")) {
384 for (String s : args.get("download")) {
385 downloadFromParamString(false, s);
386 }
387 }
388 if (args.containsKey("downloadgps")) {
389 for (String s : args.get("downloadgps")) {
390 downloadFromParamString(true, s);
391 }
392 }
393 if (args.containsKey("selection")) {
394 for (String s : args.get("selection")) {
395 SearchAction.search(s, SearchAction.SearchMode.add, false, false);
396 }
397 }
398 }
399
400 public static boolean saveUnsavedModifications() {
401 if (map == null) return true;
402 SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
403 List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<OsmDataLayer>();
404 for (OsmDataLayer l: Main.map.mapView.getLayersOfType(OsmDataLayer.class)) {
405 if (l.requiresSaveToFile() || l.requiresUploadToServer()) {
406 layersWithUnmodifiedChanges.add(l);
407 }
408 }
409 dialog.prepareForSavingAndUpdatingLayersBeforeExit();
410 if (!layersWithUnmodifiedChanges.isEmpty()) {
411 dialog.getModel().populate(layersWithUnmodifiedChanges);
412 dialog.setVisible(true);
413 switch(dialog.getUserAction()) {
414 case CANCEL: return false;
415 case PROCEED: return true;
416 default: return false;
417 }
418 }
419 return true;
420 }
421
422 /**
423 * Saves all {@see OsmDataLayer}s with an associated file and with unsaved
424 * data modifications.
425 *
426 * @return true, if the save operation was successful; false, otherwise
427 */
428 public static boolean saveUnsavedModifications_old() {
429 Shortcut.savePrefs();
430 if (map == null)
431 return true; // nothing to save, return success
432
433 int numUnsavedLayers = 0;
434 for (final OsmDataLayer l : map.mapView.getLayersOfType(OsmDataLayer.class)) {
435 if (l.requiresSaveToFile()) {
436 numUnsavedLayers++;
437 }
438 }
439 if (numUnsavedLayers == 0)
440 return true; // nothing to save, return success
441
442 String msg = trn(
443 "There are unsaved changes in {0} layer. Discard the changes and continue?",
444 "There are unsaved changes in {0} layers. Discard the changes and continue?",
445 numUnsavedLayers,
446 numUnsavedLayers
447 );
448
449 ExtendedDialog ed = new ExtendedDialog(parent,
450 tr("Unsaved Changes"),
451 new String[] {tr("Save and Exit"), tr("Discard and Exit"), tr("Cancel")});
452 ed.setButtonIcons(new String[] {"save.png", "exit.png", "cancel.png"});
453 ed.setContent(new JLabel(msg));
454 ed.showDialog();
455
456 switch(ed.getValue()) {
457 case 2: /* discard and exit */ return true;
458 case 3: /* cancel */ return false;
459 }
460 boolean savefailed = false;
461 for (OsmDataLayer l : map.mapView.getLayersOfType(OsmDataLayer.class)) {
462 if(!new SaveAction().doSave(l)) {
463 savefailed = true;
464 }
465 }
466 return !savefailed;
467 }
468
469 private static void downloadFromParamString(final boolean rawGps, String s) {
470 if (s.startsWith("http:")) {
471 final Bounds b = OsmUrlToBounds.parse(s);
472 if (b == null) {
473 JOptionPane.showMessageDialog(
474 Main.parent,
475 tr("Ignoring malformed URL: \"{0}\"", s),
476 tr("Warning"),
477 JOptionPane.WARNING_MESSAGE
478 );
479 } else {
480 //DownloadTask osmTask = main.menu.download.downloadTasks.get(0);
481 DownloadTask osmTask = new DownloadOsmTask();
482 osmTask.download(main.menu.download, b.min.lat(), b.min.lon(), b.max.lat(), b.max.lon(), null);
483 }
484 return;
485 }
486
487 if (s.startsWith("file:")) {
488 try {
489 main.menu.openFile.openFile(new File(new URI(s)));
490 } catch (URISyntaxException e) {
491 JOptionPane.showMessageDialog(
492 Main.parent,
493 tr("Ignoring malformed file URL: \"{0}\"", s),
494 tr("Warning"),
495 JOptionPane.WARNING_MESSAGE
496 );
497 }
498 return;
499 }
500
501 final StringTokenizer st = new StringTokenizer(s, ",");
502 if (st.countTokens() == 4) {
503 try {
504 DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
505 task.download(main.menu.download, Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), null);
506 return;
507 } catch (final NumberFormatException e) {
508 }
509 }
510
511 main.menu.openFile.openFile(new File(s));
512 }
513
514 public static void determinePlatformHook() {
515 String os = System.getProperty("os.name");
516 if (os == null) {
517 System.err.println("Your operating system has no name, so I'm guessing its some kind of *nix.");
518 platform = new PlatformHookUnixoid();
519 } else if (os.toLowerCase().startsWith("windows")) {
520 platform = new PlatformHookWindows();
521 } else if (os.equals("Linux") || os.equals("Solaris") ||
522 os.equals("SunOS") || os.equals("AIX") ||
523 os.equals("FreeBSD") || os.equals("NetBSD") || os.equals("OpenBSD")) {
524 platform = new PlatformHookUnixoid();
525 } else if (os.toLowerCase().startsWith("mac os x")) {
526 platform = new PlatformHookOsx();
527 } else {
528 System.err.println("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
529 platform = new PlatformHookUnixoid();
530 }
531 }
532
533 static public void saveGuiGeometry() {
534 // save the current window geometry
535 String newGeometry = "";
536 try {
537 if (((JFrame)parent).getExtendedState() == JFrame.NORMAL) {
538 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
539 Rectangle bounds = parent.getBounds();
540 int width = (int)bounds.getWidth();
541 int height = (int)bounds.getHeight();
542 int x = (int)bounds.getX();
543 int y = (int)bounds.getY();
544 if (width > screenDimension.width) {
545 width = screenDimension.width;
546 }
547 if (height > screenDimension.height) {
548 width = screenDimension.height;
549 }
550 if (x < 0) {
551 x = 0;
552 }
553 if (y < 0) {
554 y = 0;
555 }
556 newGeometry = width + "x" + height + "+" + x + "+" + y;
557 }
558 }
559 catch (Exception e) {
560 System.out.println("Failed to save GUI geometry: " + e);
561 }
562 pref.put("gui.geometry", newGeometry);
563 }
564
565
566}
Note: See TracBrowser for help on using the repository browser.