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