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

Last change on this file since 3815 was 3815, checked in by stoecker, 13 years ago

fixes for applet

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