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

Last change on this file since 4689 was 4681, checked in by simon04, 12 years ago

see #6329 - improve splash screen: show execution time of items, display more detailed information

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