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

Last change on this file since 623 was 529, checked in by gebner, 16 years ago

Part one of patch by Dave Hansen <dave@…>

  • Remove unused imports
  • Main.debug
  • Make attribute merging aware of TIGER-import attributes
  • Better upload progress information
  • Retry uploads
File size: 15.0 KB
RevLine 
[298]1// License: GPL. Copyright 2007 by Immanuel Scholz and others
[98]2package org.openstreetmap.josm;
[301]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;
[486]55import org.openstreetmap.josm.gui.preferences.MapPaintPreference;
[301]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;
[98]61
62abstract public class Main {
63 /**
64 * Global parent component for all dialogs and message boxes
65 */
66 public static Component parent;
67 /**
[284]68 * Global application.
[98]69 */
70 public static Main main;
71 /**
72 * The worker thread slave. This is for executing all long and intensive
73 * calculations. The executed runnables are guaranteed to be executed seperatly
74 * and sequenciel.
75 */
76 public final static Executor worker = Executors.newSingleThreadExecutor();
77 /**
78 * Global application preferences
79 */
80 public static Preferences pref = new Preferences();
81 /**
82 * The global dataset.
83 */
[404]84 public static DataSet ds = new DataSet();
85 /**
86 * The global paste buffer.
87 */
88 public static DataSet pasteBuffer = new DataSet();
[98]89 /**
90 * The projection method used.
91 */
92 public static Projection proj;
93 /**
94 * The MapFrame. Use setMapFrame to set or clear it.
95 */
96 public static MapFrame map;
[144]97 /**
98 * All installed and loaded plugins (resp. their main classes)
99 */
[149]100 public final static Collection<PluginProxy> plugins = new LinkedList<PluginProxy>();
[153]101 /**
102 * The dialog that gets displayed during background task execution.
103 */
104 public static PleaseWaitDialog pleaseWaitDlg;
[159]105
[172]106 /**
107 * True, when in applet mode
108 */
109 public static boolean applet = false;
[159]110
[155]111 /**
[172]112 * The toolbar preference control to register new actions.
113 */
114 public static ToolbarPreferences toolbar = new ToolbarPreferences();
115
116
[301]117 public UndoRedoHandler undoRedo = new UndoRedoHandler();
118
[172]119 /**
[159]120 * The main menu bar at top of screen.
[155]121 */
[159]122 public final MainMenu menu;
[98]123
[529]124 /**
125 * Print a debug message if debugging is on.
126 */
127 static public int debug_level = 1;
128 static public final void debug(String msg) {
129 if (debug_level <= 0)
130 return;
131 System.out.println(msg);
132 }
[301]133
[98]134 /**
135 * Set or clear (if passed <code>null</code>) the map.
136 */
137 public final void setMapFrame(final MapFrame map) {
[145]138 MapFrame old = Main.map;
[98]139 Main.map = map;
140 panel.setVisible(false);
141 panel.removeAll();
[301]142 if (map != null)
[98]143 map.fillPanel(panel);
[301]144 else {
[208]145 old.destroy();
[266]146 panel.add(new GettingStarted(), BorderLayout.CENTER);
147 }
148 panel.setVisible(true);
[98]149 redoUndoListener.commandChanged(0,0);
[144]150
[149]151 for (PluginProxy plugin : plugins)
[153]152 plugin.mapFrameInitialized(old, map);
[98]153 }
154
155 /**
[103]156 * Set the layer menu (changed when active layer changes).
157 */
158 public final void setLayerMenu(Component[] entries) {
[463]159 //if (entries == null || entries.length == 0)
160 //menu.layerMenu.setVisible(false);
161 //else {
162 //menu.layerMenu.removeAll();
163 //for (Component c : entries)
164 //menu.layerMenu.add(c);
165 //menu.layerMenu.setVisible(true);
166 //}
[103]167 }
[119]168
[103]169 /**
[98]170 * Remove the specified layer from the map. If it is the last layer, remove the map as well.
171 */
172 public final void removeLayer(final Layer layer) {
[100]173 map.mapView.removeLayer(layer);
[290]174 if (layer instanceof OsmDataLayer)
175 ds = new DataSet();
[100]176 if (map.mapView.getAllLayers().isEmpty())
[98]177 setMapFrame(null);
178 }
[129]179
180
[98]181 public Main() {
182 main = this;
183 contentPane.add(panel, BorderLayout.CENTER);
[266]184 panel.add(new GettingStarted(), BorderLayout.CENTER);
[159]185 menu = new MainMenu();
[301]186
187 undoRedo.listenerCommands.add(redoUndoListener);
188
[98]189 // creating toolbar
[172]190 contentPane.add(toolbar.control, BorderLayout.NORTH);
[98]191
[178]192 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "Help");
193 contentPane.getActionMap().put("Help", menu.help);
[155]194
[486]195 TaggingPresetPreference.initialize();
196 MapPaintPreference.initialize();
[172]197
[178]198 toolbar.refreshToolbarControl();
199
200 toolbar.control.updateUI();
201 contentPane.updateUI();
[155]202 }
[149]203
[155]204 /**
[207]205 * Load all plugins specified in preferences. If the parameter is <code>true</code>, all
206 * early plugins are loaded (before constructor).
[155]207 */
[207]208 public static void loadPlugins(boolean early) {
[243]209 List<String> plugins = new LinkedList<String>();
210 if (Main.pref.hasKey("plugins"))
211 plugins.addAll(Arrays.asList(Main.pref.get("plugins").split(",")));
212 if (System.getProperty("josm.plugins") != null)
[486]213 plugins.addAll(Arrays.asList(System.getProperty("josm.plugins").split(",")));
214
215 // we remove mappaint from the preferences on startup but this is just
216 // in case it crept in through the properties:
217 if (plugins.contains("mappaint")) {
218 plugins.remove("mappaint");
219 System.out.println("Warning - loading of mappaint plugin was requested. This JOSM version has built-in mappaint support. The plugin is not required.");
220 }
221
[243]222 if (plugins.isEmpty())
[207]223 return;
224 SortedMap<Integer, Collection<PluginInformation>> p = new TreeMap<Integer, Collection<PluginInformation>>();
[243]225 for (String pluginName : plugins) {
226 PluginInformation info = PluginInformation.findPlugin(pluginName);
227 if (info != null) {
[207]228 if (info.early != early)
229 continue;
230 if (!p.containsKey(info.stage))
231 p.put(info.stage, new LinkedList<PluginInformation>());
232 p.get(info.stage).add(info);
233 } else {
234 if (early)
235 System.out.println("Plugin not found: "+pluginName); // do not translate
236 else
237 JOptionPane.showMessageDialog(Main.parent, tr("Plugin not found: {0}.", pluginName));
238 }
239 }
[250]240
241 // iterate all plugins and collect all libraries of all plugins:
242 List<URL> allPluginLibraries = new ArrayList<URL>();
[267]243 for (Collection<PluginInformation> c : p.values())
244 for (PluginInformation info : c)
245 allPluginLibraries.addAll(info.libraries);
[250]246 // create a classloader for all plugins:
247 URL[] jarUrls = new URL[allPluginLibraries.size()];
248 jarUrls = allPluginLibraries.toArray(jarUrls);
249 URLClassLoader pluginClassLoader = new URLClassLoader(jarUrls, Main.class.getClassLoader());
[293]250 ImageProvider.sources.add(0, pluginClassLoader);
[267]251
[250]252 for (Collection<PluginInformation> c : p.values()) {
253 for (PluginInformation info : c) {
[144]254 try {
[267]255 Class<?> klass = info.loadClass(pluginClassLoader);
[293]256 if (klass != null) {
257 System.out.println("loading "+info.name);
258 Main.plugins.add(info.load(klass));
259 }
[350]260 } catch (Throwable e) {
[149]261 e.printStackTrace();
[518]262 boolean remove = true;
[207]263 if (early)
[350]264 System.out.println("Could not load plugin: "+info.name+" - deleted from preferences"); // do not translate
[518]265 else {
266 int answer = JOptionPane.showConfirmDialog(Main.parent,
267 tr("Could not load plugin {0}. Delete from preferences?", info.name,
268 JOptionPane.YES_NO_OPTION));
269 if (answer != JOptionPane.OK_OPTION) {
270 remove = false;
271 }
[350]272 }
[518]273 if (remove) {
274 plugins.remove(info.name);
275 String plist = null;
276 for (String pn : plugins) {
277 if (plist==null) plist=""; else plist=plist+",";
278 plist=plist+pn;
279 }
280 Main.pref.put("plugins", plist);
281 }
[149]282 }
[144]283 }
284 }
[178]285 }
[154]286
[98]287 /**
288 * Add a new layer to the map. If no map exist, create one.
289 */
290 public final void addLayer(final Layer layer) {
291 if (map == null) {
[103]292 final MapFrame mapFrame = new MapFrame();
[98]293 setMapFrame(mapFrame);
[101]294 mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction());
[98]295 mapFrame.setVisible(true);
296 mapFrame.setVisibleDialogs();
[103]297 }
298 map.mapView.addLayer(layer);
[98]299 }
300 /**
301 * @return The edit osm layer. If none exist, it will be created.
302 */
303 public final OsmDataLayer editLayer() {
304 if (map == null || map.mapView.editLayer == null)
[290]305 menu.newAction.actionPerformed(null);
[98]306 return map.mapView.editLayer;
307 }
308
309
310
311
312 /**
313 * Use this to register shortcuts to
314 */
[99]315 public static final JPanel contentPane = new JPanel(new BorderLayout());
[98]316
317
318 ////////////////////////////////////////////////////////////////////////////////////////
319 // Implementation part
320 ////////////////////////////////////////////////////////////////////////////////////////
321
[153]322 public static JPanel panel = new JPanel(new BorderLayout());
[99]323
[98]324 protected static Rectangle bounds;
325
326 private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
327 public void commandChanged(final int queueSize, final int redoSize) {
[159]328 menu.undo.setEnabled(queueSize > 0);
329 menu.redo.setEnabled(redoSize > 0);
[98]330 }
[301]331 };
[98]332 /**
333 * Should be called before the main constructor to setup some parameter stuff
334 * @param args The parsed argument list.
335 */
336 public static void preConstructorInit(Map<String, Collection<String>> args) {
337 try {
338 Main.proj = (Projection)Class.forName(Main.pref.get("projection")).newInstance();
339 } catch (final Exception e) {
340 e.printStackTrace();
[104]341 JOptionPane.showMessageDialog(null, tr("The projection could not be read from preferences. Using EPSG:4263."));
[98]342 Main.proj = new Epsg4326();
343 }
344
345 try {
346 UIManager.setLookAndFeel(Main.pref.get("laf"));
347 contentPane.updateUI();
348 panel.updateUI();
349 } catch (final Exception e) {
350 e.printStackTrace();
351 }
352 UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
353 UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
354 UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
355 UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
356
357 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
358 if (args.containsKey("geometry")) {
359 String geometry = args.get("geometry").iterator().next();
360 final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
361 if (m.matches()) {
362 int w = Integer.valueOf(m.group(1));
363 int h = Integer.valueOf(m.group(2));
364 int x = 0, y = 0;
365 if (m.group(3) != null) {
366 x = Integer.valueOf(m.group(5));
367 y = Integer.valueOf(m.group(7));
368 if (m.group(4).equals("-"))
369 x = screenDimension.width - x - w;
370 if (m.group(6).equals("-"))
371 y = screenDimension.height - y - h;
372 }
373 bounds = new Rectangle(x,y,w,h);
374 } else
375 System.out.println("Ignoring malformed geometry: "+geometry);
376 }
377 if (bounds == null)
378 bounds = !args.containsKey("no-fullscreen") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
[153]379
[211]380 // preinitialize a wait dialog for all early downloads (e.g. via command line)
381 pleaseWaitDlg = new PleaseWaitDialog(null);
[98]382 }
383
384 public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
[211]385 // initialize the pleaseWaitDialog with the application as parent to handle focus stuff
386 pleaseWaitDlg = new PleaseWaitDialog(parent);
387
[98]388 if (args.containsKey("download"))
389 for (String s : args.get("download"))
390 downloadFromParamString(false, s);
391 if (args.containsKey("downloadgps"))
392 for (String s : args.get("downloadgps"))
393 downloadFromParamString(true, s);
394 if (args.containsKey("selection"))
395 for (String s : args.get("selection"))
[207]396 SearchAction.search(s, SearchAction.SearchMode.add, false);
[98]397 }
398
[169]399 public static boolean breakBecauseUnsavedChanges() {
[178]400 if (map != null) {
401 boolean modified = false;
402 boolean uploadedModified = false;
403 for (final Layer l : map.mapView.getAllLayers()) {
404 if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
405 modified = true;
406 uploadedModified = ((OsmDataLayer)l).uploadedModified;
407 break;
408 }
409 }
410 if (modified) {
411 final String msg = uploadedModified ? "\n"+tr("Hint: Some changes came from uploading new data to the server.") : "";
412 final int answer = JOptionPane.showConfirmDialog(
413 parent, tr("There are unsaved changes. Discard the changes and continue?")+msg,
414 tr("Unsaved Changes"), JOptionPane.YES_NO_OPTION);
415 if (answer != JOptionPane.YES_OPTION)
416 return true;
417 }
418 }
419 return false;
420 }
[172]421
[98]422 private static void downloadFromParamString(final boolean rawGps, String s) {
423 if (s.startsWith("http:")) {
[237]424 final Bounds b = BoundingBoxSelection.osmurl2bounds(s);
[98]425 if (b == null)
[110]426 JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed url: \"{0}\"", s));
[153]427 else {
[237]428 //DownloadTask osmTask = main.menu.download.downloadTasks.get(0);
429 DownloadTask osmTask = new DownloadOsmTask();
[159]430 osmTask.download(main.menu.download, b.min.lat(), b.min.lon(), b.max.lat(), b.max.lon());
[153]431 }
[98]432 return;
433 }
434
435 if (s.startsWith("file:")) {
436 try {
[159]437 main.menu.open.openFile(new File(new URI(s)));
[98]438 } catch (URISyntaxException e) {
[110]439 JOptionPane.showMessageDialog(Main.parent, tr("Ignoring malformed file url: \"{0}\"", s));
[98]440 }
441 return;
442 }
443
444 final StringTokenizer st = new StringTokenizer(s, ",");
445 if (st.countTokens() == 4) {
446 try {
[237]447 DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
[159]448 task.download(main.menu.download, Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
[98]449 return;
450 } catch (final NumberFormatException e) {
451 }
452 }
453
[159]454 main.menu.open.openFile(new File(s));
[98]455 }
456}
Note: See TracBrowser for help on using the repository browser.