1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Toolkit;
|
---|
10 | import java.awt.event.WindowAdapter;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.io.File;
|
---|
13 | import java.net.Authenticator;
|
---|
14 | import java.net.ProxySelector;
|
---|
15 | import java.security.AllPermission;
|
---|
16 | import java.security.CodeSource;
|
---|
17 | import java.security.PermissionCollection;
|
---|
18 | import java.security.Permissions;
|
---|
19 | import java.security.Policy;
|
---|
20 | import java.util.Collection;
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.LinkedList;
|
---|
23 | import java.util.List;
|
---|
24 | import java.util.Map;
|
---|
25 |
|
---|
26 | import javax.swing.JFrame;
|
---|
27 | import javax.swing.JLabel;
|
---|
28 | import javax.swing.JOptionPane;
|
---|
29 | import javax.swing.JPanel;
|
---|
30 | import javax.swing.JTextArea;
|
---|
31 | import javax.swing.SwingUtilities;
|
---|
32 |
|
---|
33 | import org.openstreetmap.josm.Main;
|
---|
34 | import org.openstreetmap.josm.data.AutosaveTask;
|
---|
35 | import org.openstreetmap.josm.data.Preferences;
|
---|
36 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
|
---|
37 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
38 | import org.openstreetmap.josm.io.DefaultProxySelector;
|
---|
39 | import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
|
---|
40 | import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
|
---|
41 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
42 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
43 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
44 | import org.openstreetmap.josm.tools.I18n;
|
---|
45 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Main window class application.
|
---|
49 | *
|
---|
50 | * @author imi
|
---|
51 | */
|
---|
52 | public class MainApplication extends Main {
|
---|
53 | /**
|
---|
54 | * Allow subclassing (see JOSM.java)
|
---|
55 | */
|
---|
56 | public MainApplication() {}
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Construct an main frame, ready sized and operating. Does not
|
---|
60 | * display the frame.
|
---|
61 | */
|
---|
62 | public MainApplication(JFrame mainFrame) {
|
---|
63 | super();
|
---|
64 | mainFrame.setContentPane(contentPanePrivate);
|
---|
65 | mainFrame.setJMenuBar(menu);
|
---|
66 | mainFrame.setBounds(bounds);
|
---|
67 | mainFrame.setIconImage(ImageProvider.get("logo.png").getImage());
|
---|
68 | mainFrame.addWindowListener(new WindowAdapter(){
|
---|
69 | @Override public void windowClosing(final WindowEvent arg0) {
|
---|
70 | Main.exitJosm(true);
|
---|
71 | }
|
---|
72 | });
|
---|
73 | mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Displays help on the console
|
---|
78 | *
|
---|
79 | */
|
---|
80 | public static void showHelp() {
|
---|
81 | // TODO: put in a platformHook for system that have no console by default
|
---|
82 | System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
|
---|
83 | tr("usage")+":\n"+
|
---|
84 | "\tjava -jar josm.jar <options>...\n\n"+
|
---|
85 | tr("options")+":\n"+
|
---|
86 | "\t--help|-?|-h "+tr("Show this help")+"\n"+
|
---|
87 | "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
|
---|
88 | "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
|
---|
89 | "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
|
---|
90 | "\t[--download=]<filename> "+tr("Open a file (any file type that can be opened with File/Open)")+"\n"+
|
---|
91 | "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
|
---|
92 | "\t--downloadgps=<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z) as raw gps")+"\n"+
|
---|
93 | "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
|
---|
94 | "\t--[no-]maximize "+tr("Launch in maximized mode")+"\n"+
|
---|
95 | "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
|
---|
96 | "\t--language=<language> "+tr("Set the language")+"\n\n"+
|
---|
97 | tr("options provided as Java system properties")+":\n"+
|
---|
98 | "\t-Djosm.home="+tr("/PATH/TO/JOSM/FOLDER/ ")+tr("Change the folder for all user settings")+"\n\n"+
|
---|
99 | tr("note: For some tasks, JOSM needs a lot of memory. It can be necessary to add the following\n" +
|
---|
100 | " Java option to specify the maximum size of allocated memory in megabytes")+":\n"+
|
---|
101 | "\t-Xmx...m\n\n"+
|
---|
102 | tr("examples")+":\n"+
|
---|
103 | "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
|
---|
104 | "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
|
---|
105 | "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
|
---|
106 | "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n"+
|
---|
107 | "\tjava -Djosm.home=/home/user/.josm_dev -jar josm.jar\n"+
|
---|
108 | "\tjava -Xmx400m -jar josm.jar\n\n"+
|
---|
109 | tr("Parameters --download, --downloadgps, and --selection are processed in this order.")+"\n"+
|
---|
110 | tr("Make sure you load some data if you use --selection.")+"\n"
|
---|
111 | );
|
---|
112 | }
|
---|
113 |
|
---|
114 | private static Map<String, Collection<String>> buildCommandLineArgumentMap(String[] args) {
|
---|
115 | Map<String, Collection<String>> argMap = new HashMap<String, Collection<String>>();
|
---|
116 | for (String arg : args) {
|
---|
117 | if ("-h".equals(arg) || "-?".equals(arg)) {
|
---|
118 | arg = "--help";
|
---|
119 | }
|
---|
120 | // handle simple arguments like file names, URLs, bounds
|
---|
121 | if (!arg.startsWith("--")) {
|
---|
122 | arg = "--download="+arg;
|
---|
123 | }
|
---|
124 | int i = arg.indexOf('=');
|
---|
125 | String key = i == -1 ? arg.substring(2) : arg.substring(2,i);
|
---|
126 | String value = i == -1 ? "" : arg.substring(i+1);
|
---|
127 | Collection<String> v = argMap.get(key);
|
---|
128 | if (v == null) {
|
---|
129 | v = new LinkedList<String>();
|
---|
130 | }
|
---|
131 | v.add(value);
|
---|
132 | argMap.put(key, v);
|
---|
133 | }
|
---|
134 | return argMap;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Main application Startup
|
---|
139 | */
|
---|
140 | public static void main(final String[] argArray) {
|
---|
141 | I18n.init();
|
---|
142 | checkJava6();
|
---|
143 | Main.pref = new Preferences();
|
---|
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 | SwingUtilities.invokeLater(new Runnable() {
|
---|
251 | public void run() {
|
---|
252 | if (AutosaveTask.PROP_AUTOSAVE_ENABLED.get()) {
|
---|
253 | AutosaveTask autosaveTask = new AutosaveTask();
|
---|
254 | List<File> unsavedLayerFiles = autosaveTask.getUnsavedLayersFiles();
|
---|
255 | if (!unsavedLayerFiles.isEmpty()) {
|
---|
256 | System.err.println("autosave debug: unsavedLayerFiles="+unsavedLayerFiles);
|
---|
257 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
258 | Main.parent,
|
---|
259 | tr("Unsaved osm data"),
|
---|
260 | new String[] {tr("Restore"), tr("Cancel"), tr("Discard")}
|
---|
261 | );
|
---|
262 | dialog.setContent(
|
---|
263 | trn("JOSM found {0} unsaved osm data layer. ",
|
---|
264 | "JOSM found {0} unsaved osm data layers. ", unsavedLayerFiles.size(), unsavedLayerFiles.size()) +
|
---|
265 | tr("It looks like JOSM crashed last time. Do you like to restore the data?"));
|
---|
266 | dialog.setButtonIcons(new String[] {"ok", "cancel", "dialogs/remove"});
|
---|
267 | int selection = dialog.showDialog().getValue();
|
---|
268 | System.err.println("autosave debug: user selection="+selection);
|
---|
269 | if (selection == 1) {
|
---|
270 | autosaveTask.recoverUnsavedLayers();
|
---|
271 | } else if (selection == 3) {
|
---|
272 | System.err.println("autosave debug: discard autosaved layers");
|
---|
273 | autosaveTask.dicardUnsavedLayers();
|
---|
274 | System.err.println("autosave debug: discard autosaved layers [DONE]");
|
---|
275 | }
|
---|
276 | }
|
---|
277 | autosaveTask.schedule();
|
---|
278 | }
|
---|
279 |
|
---|
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 | }
|
---|