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

Last change on this file since 3461 was 3461, checked in by bastiK, 14 years ago

added gui preference for autosave; fixed #5359 - Button to delete autosaved data

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