source: josm/trunk/src/org/openstreetmap/josm/gui/MainApplication.java@ 3378

Last change on this file since 3378 was 3378, checked in by jttt, 14 years ago

Fix #2662 Auto-save

  • Property svn:eol-style set to native
File size: 12.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2//Licence: GPL
3package org.openstreetmap.josm.gui;
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.EventQueue;
8import java.awt.Toolkit;
9import java.awt.event.WindowAdapter;
10import java.awt.event.WindowEvent;
11import java.net.Authenticator;
12import java.net.ProxySelector;
13import java.security.AllPermission;
14import java.security.CodeSource;
15import java.security.PermissionCollection;
16import java.security.Permissions;
17import java.security.Policy;
18import java.util.Collection;
19import java.util.HashMap;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
23
24import javax.swing.JFrame;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.data.AutosaveTask;
28import org.openstreetmap.josm.gui.layer.OsmDataLayer;
29import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
30import org.openstreetmap.josm.gui.progress.ProgressMonitor;
31import org.openstreetmap.josm.io.DefaultProxySelector;
32import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
33import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
34import org.openstreetmap.josm.plugins.PluginHandler;
35import org.openstreetmap.josm.plugins.PluginInformation;
36import org.openstreetmap.josm.tools.BugReportExceptionHandler;
37import org.openstreetmap.josm.tools.I18n;
38import org.openstreetmap.josm.tools.ImageProvider;
39
40/**
41 * Main window class application.
42 *
43 * @author imi
44 */
45public class MainApplication extends Main {
46 /**
47 * Allow subclassing (see JOSM.java)
48 */
49 public MainApplication() {}
50
51 /**
52 * Construct an main frame, ready sized and operating. Does not
53 * display the frame.
54 */
55 public MainApplication(JFrame mainFrame) {
56 super();
57 mainFrame.setContentPane(contentPanePrivate);
58 mainFrame.setJMenuBar(menu);
59 mainFrame.setBounds(bounds);
60 mainFrame.setIconImage(ImageProvider.get("logo.png").getImage());
61 mainFrame.addWindowListener(new WindowAdapter(){
62 @Override public void windowClosing(final WindowEvent arg0) {
63 Main.exitJosm(true);
64 }
65 });
66 mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
67 }
68
69 /**
70 * Displays help on the console
71 *
72 */
73 public static void showHelp() {
74 // TODO: put in a platformHook for system that have no console by default
75 System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
76 tr("usage")+":\n"+
77 "\tjava -jar josm.jar <options>...\n\n"+
78 tr("options")+":\n"+
79 "\t--help|-?|-h "+tr("Show this help")+"\n"+
80 "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
81 "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
82 "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
83 "\t[--download=]<filename> "+tr("Open a file (any file type that can be opened with File/Open)")+"\n"+
84 "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
85 "\t--downloadgps=<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z) as raw gps")+"\n"+
86 "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
87 "\t--[no-]maximize "+tr("Launch in maximized mode")+"\n"+
88 "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
89 "\t--language=<language> "+tr("Set the language")+"\n\n"+
90 tr("options provided as Java system properties")+":\n"+
91 "\t-Djosm.home="+tr("/PATH/TO/JOSM/FOLDER/ ")+tr("Change the folder for all user settings")+"\n\n"+
92 tr("note: For some tasks, JOSM needs a lot of memory. It can be necessary to add the following\n" +
93 " Java option to specify the maximum size of allocated memory in megabytes")+":\n"+
94 "\t-Xmx...m\n\n"+
95 tr("examples")+":\n"+
96 "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
97 "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
98 "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
99 "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n"+
100 "\tjava -Djosm.home=/home/user/.josm_dev -jar josm.jar\n"+
101 "\tjava -Xmx400m -jar josm.jar\n\n"+
102 tr("Parameters --download, --downloadgps, and --selection are processed in this order.")+"\n"+
103 tr("Make sure you load some data if you use --selection.")+"\n"
104 );
105 }
106
107 private static Map<String, Collection<String>> buildCommandLineArgumentMap(String[] args) {
108 Map<String, Collection<String>> argMap = new HashMap<String, Collection<String>>();
109 for (String arg : args) {
110 if ("-h".equals(arg) || "-?".equals(arg)) {
111 arg = "--help";
112 }
113 // handle simple arguments like file names, URLs, bounds
114 if (!arg.startsWith("--")) {
115 arg = "--download="+arg;
116 }
117 int i = arg.indexOf('=');
118 String key = i == -1 ? arg.substring(2) : arg.substring(2,i);
119 String value = i == -1 ? "" : arg.substring(i+1);
120 Collection<String> v = argMap.get(key);
121 if (v == null) {
122 v = new LinkedList<String>();
123 }
124 v.add(value);
125 argMap.put(key, v);
126 }
127 return argMap;
128 }
129
130 /**
131 * Main application Startup
132 */
133 public static void main(final String[] argArray) {
134 I18n.init();
135
136 Policy.setPolicy(new Policy() {
137 // Permissions for plug-ins loaded when josm is started via webstart
138 private PermissionCollection pc;
139
140 {
141 pc = new Permissions();
142 pc.add(new AllPermission());
143 }
144
145 @Override
146 public void refresh() { }
147
148 @Override
149 public PermissionCollection getPermissions(CodeSource codesource) {
150 return pc;
151 }
152 });
153
154 Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
155 // http://stuffthathappens.com/blog/2007/10/15/one-more-note-on-uncaught-exception-handlers/
156 System.setProperty("sun.awt.exception.handler", BugReportExceptionHandler.class.getName());
157
158 // initialize the plaform hook, and
159 Main.determinePlatformHook();
160 // call the really early hook before we anything else
161 Main.platform.preStartupHook();
162
163 // construct argument table
164 final Map<String, Collection<String>> args = buildCommandLineArgumentMap(argArray);
165
166 Main.pref.init(args.containsKey("reset-preferences"));
167
168 // Check if passed as parameter
169 if (args.containsKey("language")) {
170 I18n.set((String)(args.get("language").toArray()[0]));
171 } else {
172 I18n.set(Main.pref.get("language", null));
173 }
174 Main.pref.updateSystemProperties();
175
176 DefaultAuthenticator.createInstance(CredentialsManagerFactory.getCredentialManager());
177 Authenticator.setDefault(DefaultAuthenticator.getInstance());
178 ProxySelector.setDefault(new DefaultProxySelector(ProxySelector.getDefault()));
179 OAuthAccessTokenHolder.getInstance().init(Main.pref, CredentialsManagerFactory.getCredentialManager());
180
181 // asking for help? show help and exit
182 if (args.containsKey("help")) {
183 showHelp();
184 System.exit(0);
185 }
186
187 SplashScreen splash = new SplashScreen();
188 ProgressMonitor monitor = splash.getProgressMonitor();
189 monitor.beginTask(tr("Initializing"));
190 monitor.setTicksCount(7);
191 splash.setVisible(Main.pref.getBoolean("draw.splashscreen", true));
192
193 List<PluginInformation> pluginsToLoad = PluginHandler.buildListOfPluginsToLoad(splash,monitor.createSubTaskMonitor(1, false));
194 if (!pluginsToLoad.isEmpty() && PluginHandler.checkAndConfirmPluginUpdate(splash)) {
195 monitor.subTask(tr("Updating plugins..."));
196 pluginsToLoad = PluginHandler.updatePlugins(splash,pluginsToLoad, monitor.createSubTaskMonitor(1, false));
197 }
198 monitor.worked(1);
199
200 monitor.subTask(tr("Installing updated plugins"));
201 PluginHandler.installDownloadedPlugins(true);
202 monitor.worked(1);
203
204 monitor.subTask(tr("Loading early plugins"));
205 PluginHandler.loadEarlyPlugins(splash,pluginsToLoad, monitor.createSubTaskMonitor(1, false));
206 monitor.worked(1);
207
208 monitor.subTask(tr("Setting defaults"));
209 preConstructorInit(args);
210 removeObsoletePreferences();
211 monitor.worked(1);
212
213 monitor.indeterminateSubTask(tr("Creating main GUI"));
214 JFrame mainFrame = new JFrame(tr("Java OpenStreetMap Editor"));
215 Main.parent = mainFrame;
216 Main.addListener();
217 final Main main = new MainApplication(mainFrame);
218 monitor.worked(1);
219
220 monitor.subTask(tr("Loading plugins"));
221 PluginHandler.loadLatePlugins(splash,pluginsToLoad, monitor.createSubTaskMonitor(1, false));
222 monitor.worked(1);
223 toolbar.refreshToolbarControl();
224 splash.setVisible(false);
225 splash.dispose();
226 mainFrame.setVisible(true);
227
228 boolean maximized = Boolean.parseBoolean(Main.pref.get("gui.maximized"));
229 if ((!args.containsKey("no-maximize") && maximized) || args.containsKey("maximize")) {
230 if (Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
231 // Main.debug("Main window maximized");
232 Main.windowState = JFrame.MAXIMIZED_BOTH;
233 mainFrame.setExtendedState(Main.windowState);
234 } else {
235 Main.debug("Main window: maximizing not supported");
236 }
237 } else {
238 // Main.debug("Main window not maximized");
239 }
240
241 AutosaveTask autosaveTask = new AutosaveTask();
242 List<OsmDataLayer> unsavedLayers = autosaveTask.getUnsavedLayers();
243 if (!unsavedLayers.isEmpty()) {
244 ExtendedDialog dialog = new ExtendedDialog(
245 Main.parent,
246 tr("Unsaved osm data"),
247 new String[] {tr("Restore"), tr("Cancel")}
248 );
249 dialog.setContent(tr("JOSM found {0} unsaved osm data layers. It looks like JOSM crashed last time. Do you want to restore data?",
250 unsavedLayers.size()));
251 dialog.setButtonIcons(new String[] {"ok.png", "cancel.png"});
252 dialog.showDialog();
253 if (dialog.getValue() == 1) {
254 for (OsmDataLayer layer: unsavedLayers) {
255 Main.main.addLayer(layer);
256 }
257 }
258
259
260 }
261 autosaveTask.schedule();
262
263
264 EventQueue.invokeLater(new Runnable() {
265 public void run() {
266 main.postConstructorProcessCmdLine(args);
267 }
268 });
269 }
270
271 /**
272 * Removes obsolete preference settings. If you throw out a once-used preference
273 * setting, add it to the list here with an expiry date (written as comment). If you
274 * see something with an expiry date in the past, remove it from the list.
275 */
276 public static void removeObsoletePreferences() {
277 String[] obsolete = {
278 "proxy.anonymous", // 01/2010 - not needed anymore. Can be removed mid 2010
279 "proxy.enable" // 01/2010 - not needed anymore. Can be removed mid 2010
280 };
281 for (String key : obsolete) {
282 if (Main.pref.hasKey(key)) {
283 Main.pref.removeFromCollection(key, Main.pref.get(key));
284 System.out.println(tr("Preference setting {0} has been removed since it is no longer used.", key));
285 }
286 }
287 }
288}
Note: See TracBrowser for help on using the repository browser.