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

Last change on this file since 3287 was 3287, checked in by stoecker, 14 years ago

fix #4706 and fix #5066 - plugin handling

  • Property svn:eol-style set to native
File size: 11.2 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.gui.preferences.server.OAuthAccessTokenHolder;
28import org.openstreetmap.josm.gui.progress.ProgressMonitor;
29import org.openstreetmap.josm.io.DefaultProxySelector;
30import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
31import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
32import org.openstreetmap.josm.plugins.PluginHandler;
33import org.openstreetmap.josm.plugins.PluginInformation;
34import org.openstreetmap.josm.tools.BugReportExceptionHandler;
35import org.openstreetmap.josm.tools.I18n;
36import org.openstreetmap.josm.tools.ImageProvider;
37
38/**
39 * Main window class application.
40 *
41 * @author imi
42 */
43public class MainApplication extends Main {
44 /**
45 * Allow subclassing (see JOSM.java)
46 */
47 public MainApplication() {}
48
49 /**
50 * Construct an main frame, ready sized and operating. Does not
51 * display the frame.
52 */
53 public MainApplication(JFrame mainFrame) {
54 super();
55 mainFrame.setContentPane(contentPanePrivate);
56 mainFrame.setJMenuBar(menu);
57 mainFrame.setBounds(bounds);
58 mainFrame.setIconImage(ImageProvider.get("logo.png").getImage());
59 mainFrame.addWindowListener(new WindowAdapter(){
60 @Override public void windowClosing(final WindowEvent arg0) {
61 if (!Main.saveUnsavedModifications())
62 return;
63 Main.saveGuiGeometry();
64 System.exit(0);
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 EventQueue.invokeLater(new Runnable() {
243 public void run() {
244 main.postConstructorProcessCmdLine(args);
245 }
246 });
247 }
248
249 /**
250 * Removes obsolete preference settings. If you throw out a once-used preference
251 * setting, add it to the list here with an expiry date (written as comment). If you
252 * see something with an expiry date in the past, remove it from the list.
253 */
254 public static void removeObsoletePreferences() {
255 String[] obsolete = {
256 "proxy.anonymous", // 01/2010 - not needed anymore. Can be removed mid 2010
257 "proxy.enable" // 01/2010 - not needed anymore. Can be removed mid 2010
258 };
259 for (String key : obsolete) {
260 if (Main.pref.hasKey(key)) {
261 Main.pref.removeFromCollection(key, Main.pref.get(key));
262 System.out.println(tr("Preference setting {0} has been removed since it is no longer used.", key));
263 }
264 }
265 }
266}
Note: See TracBrowser for help on using the repository browser.