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

Last change on this file since 4932 was 4932, checked in by stoecker, 12 years ago

fix 6833 - use WindowGeometry for toggle dialogs and mainwindow replacing old custom methods, improve dual screen handling

  • Property svn:eol-style set to native
File size: 14.2 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.Image;
8import java.awt.Toolkit;
9import java.awt.event.ActionEvent;
10import java.awt.event.WindowAdapter;
11import java.awt.event.WindowEvent;
12import java.io.File;
13import java.net.Authenticator;
14import java.net.ProxySelector;
15import java.security.AllPermission;
16import java.security.CodeSource;
17import java.security.PermissionCollection;
18import java.security.Permissions;
19import java.security.Policy;
20import java.util.Collection;
21import java.util.HashMap;
22import java.util.LinkedList;
23import java.util.List;
24import java.util.Map;
25
26import javax.swing.JFrame;
27import javax.swing.RepaintManager;
28import javax.swing.SwingUtilities;
29
30import org.jdesktop.swinghelper.debug.CheckThreadViolationRepaintManager;
31import org.openstreetmap.josm.Main;
32import org.openstreetmap.josm.data.AutosaveTask;
33import org.openstreetmap.josm.data.Preferences;
34import org.openstreetmap.josm.data.Version;
35import org.openstreetmap.josm.gui.download.DownloadDialog;
36import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
37import org.openstreetmap.josm.gui.progress.ProgressMonitor;
38import org.openstreetmap.josm.io.DefaultProxySelector;
39import org.openstreetmap.josm.io.auth.CredentialsManager;
40import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
41import org.openstreetmap.josm.io.remotecontrol.RemoteControl;
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 geometry.applySafe(mainFrame);
68 LinkedList<Image> l = new LinkedList<Image>();
69 l.add(ImageProvider.get("logo_16x16x32").getImage());
70 l.add(ImageProvider.get("logo_16x16x8").getImage());
71 l.add(ImageProvider.get("logo_32x32x32").getImage());
72 l.add(ImageProvider.get("logo_32x32x8").getImage());
73 l.add(ImageProvider.get("logo_48x48x32").getImage());
74 l.add(ImageProvider.get("logo_48x48x8").getImage());
75 l.add(ImageProvider.get("logo").getImage());
76 mainFrame.setIconImages(l);
77 mainFrame.addWindowListener(new WindowAdapter(){
78 @Override public void windowClosing(final WindowEvent arg0) {
79 Main.exitJosm(true);
80 }
81 });
82 mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
83 }
84
85 /**
86 * Displays help on the console
87 *
88 */
89 public static void showHelp() {
90 // TODO: put in a platformHook for system that have no console by default
91 System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
92 tr("usage")+":\n"+
93 "\tjava -jar josm.jar <options>...\n\n"+
94 tr("options")+":\n"+
95 "\t--help|-?|-h "+tr("Show this help")+"\n"+
96 "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
97 "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
98 "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
99 "\t[--download=]<filename> "+tr("Open a file (any file type that can be opened with File/Open)")+"\n"+
100 "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
101 "\t--downloadgps=<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z) as raw gps")+"\n"+
102 "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
103 "\t--[no-]maximize "+tr("Launch in maximized mode")+"\n"+
104 "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
105 "\t--set=<key>=<value> "+tr("Set preference key to value")+"\n\n"+
106 "\t--language=<language> "+tr("Set the language")+"\n\n"+
107 tr("options provided as Java system properties")+":\n"+
108 "\t-Djosm.home="+tr("/PATH/TO/JOSM/FOLDER/ ")+tr("Change the folder for all user settings")+"\n\n"+
109 tr("note: For some tasks, JOSM needs a lot of memory. It can be necessary to add the following\n" +
110 " Java option to specify the maximum size of allocated memory in megabytes")+":\n"+
111 "\t-Xmx...m\n\n"+
112 tr("examples")+":\n"+
113 "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
114 "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
115 "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
116 "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n"+
117 "\tjava -Djosm.home=/home/user/.josm_dev -jar josm.jar\n"+
118 "\tjava -Xmx400m -jar josm.jar\n\n"+
119 tr("Parameters --download, --downloadgps, and --selection are processed in this order.")+"\n"+
120 tr("Make sure you load some data if you use --selection.")+"\n"
121 );
122 }
123
124 private static Map<String, Collection<String>> buildCommandLineArgumentMap(String[] args) {
125 Map<String, Collection<String>> argMap = new HashMap<String, Collection<String>>();
126 for (String arg : args) {
127 if ("-h".equals(arg) || "-?".equals(arg)) {
128 arg = "--help";
129 } else if ("-v".equals(arg)) {
130 arg = "--version";
131 }
132 // handle simple arguments like file names, URLs, bounds
133 if (!arg.startsWith("--")) {
134 arg = "--download="+arg;
135 }
136 int i = arg.indexOf('=');
137 String key = i == -1 ? arg.substring(2) : arg.substring(2,i);
138 String value = i == -1 ? "" : arg.substring(i+1);
139 Collection<String> v = argMap.get(key);
140 if (v == null) {
141 v = new LinkedList<String>();
142 }
143 v.add(value);
144 argMap.put(key, v);
145 }
146 return argMap;
147 }
148
149 /**
150 * Main application Startup
151 */
152 public static void main(final String[] argArray) {
153 I18n.init();
154 Main.checkJava6();
155 Main.pref = new Preferences();
156
157 Policy.setPolicy(new Policy() {
158 // Permissions for plug-ins loaded when josm is started via webstart
159 private PermissionCollection pc;
160
161 {
162 pc = new Permissions();
163 pc.add(new AllPermission());
164 }
165
166 @Override
167 public void refresh() { }
168
169 @Override
170 public PermissionCollection getPermissions(CodeSource codesource) {
171 return pc;
172 }
173 });
174
175 Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
176 // http://stuffthathappens.com/blog/2007/10/15/one-more-note-on-uncaught-exception-handlers/
177 System.setProperty("sun.awt.exception.handler", BugReportExceptionHandler.class.getName());
178
179 // initialize the platform hook, and
180 Main.determinePlatformHook();
181 // call the really early hook before we do anything else
182 Main.platform.preStartupHook();
183
184 // construct argument table
185 final Map<String, Collection<String>> args = buildCommandLineArgumentMap(argArray);
186
187 if (args.containsKey("version")) {
188 System.out.println(Version.getInstance().getAgentString());
189 System.exit(0);
190 } else {
191 System.out.println(Version.getInstance().getReleaseAttributes());
192 }
193
194 Main.pref.init(args.containsKey("reset-preferences"));
195
196 // Check if passed as parameter
197 if (args.containsKey("language")) {
198 I18n.set(args.get("language").iterator().next());
199 } else {
200 I18n.set(Main.pref.get("language", null));
201 }
202 Main.pref.updateSystemProperties();
203
204 if (args.containsKey("set")) {
205 for (String i : args.get("set")) {
206 String[] kv = i.split("=", 2);
207 Main.pref.put(kv[0], "null".equals(kv[1]) ? null : kv[1]);
208 }
209 }
210
211 DefaultAuthenticator.createInstance();
212 Authenticator.setDefault(DefaultAuthenticator.getInstance());
213 ProxySelector.setDefault(new DefaultProxySelector(ProxySelector.getDefault()));
214 OAuthAccessTokenHolder.getInstance().init(Main.pref, CredentialsManager.getInstance());
215
216 // asking for help? show help and exit
217 if (args.containsKey("help")) {
218 showHelp();
219 System.exit(0);
220 }
221
222 SplashScreen splash = new SplashScreen();
223 final ProgressMonitor monitor = splash.getProgressMonitor();
224 monitor.beginTask(tr("Initializing"));
225 splash.setVisible(Main.pref.getBoolean("draw.splashscreen", true));
226 Main.setInitStatusListener(new InitStatusListener() {
227
228 @Override
229 public void updateStatus(String event) {
230 monitor.indeterminateSubTask(event);
231 }
232 });
233
234 List<PluginInformation> pluginsToLoad = PluginHandler.buildListOfPluginsToLoad(splash,monitor.createSubTaskMonitor(1, false));
235 if (!pluginsToLoad.isEmpty() && PluginHandler.checkAndConfirmPluginUpdate(splash)) {
236 monitor.subTask(tr("Updating plugins"));
237 pluginsToLoad = PluginHandler.updatePlugins(splash,pluginsToLoad, monitor.createSubTaskMonitor(1, false));
238 }
239
240 monitor.indeterminateSubTask(tr("Installing updated plugins"));
241 PluginHandler.installDownloadedPlugins(true);
242
243 monitor.indeterminateSubTask(tr("Loading early plugins"));
244 PluginHandler.loadEarlyPlugins(splash,pluginsToLoad, monitor.createSubTaskMonitor(1, false));
245
246 monitor.indeterminateSubTask(tr("Setting defaults"));
247 preConstructorInit(args);
248
249 monitor.indeterminateSubTask(tr("Creating main GUI"));
250 JFrame mainFrame = new JFrame(tr("Java OpenStreetMap Editor"));
251 Main.parent = mainFrame;
252 Main.addListener();
253 final Main main = new MainApplication(mainFrame);
254
255 monitor.indeterminateSubTask(tr("Loading plugins"));
256 PluginHandler.loadLatePlugins(splash,pluginsToLoad, monitor.createSubTaskMonitor(1, false));
257 toolbar.refreshToolbarControl();
258 splash.setVisible(false);
259 splash.dispose();
260 mainFrame.setVisible(true);
261
262 boolean maximized = Boolean.parseBoolean(Main.pref.get("gui.maximized"));
263 if ((!args.containsKey("no-maximize") && maximized) || args.containsKey("maximize")) {
264 if (Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
265 // Main.debug("Main window maximized");
266 Main.windowState = JFrame.MAXIMIZED_BOTH;
267 mainFrame.setExtendedState(Main.windowState);
268 } else {
269 Main.debug("Main window: maximizing not supported");
270 }
271 } else {
272 // Main.debug("Main window not maximized");
273 }
274 if(main.menu.fullscreenToggleAction != null) {
275 main.menu.fullscreenToggleAction.initial();
276 }
277
278 SwingUtilities.invokeLater(new Runnable() {
279 public void run() {
280 if (AutosaveTask.PROP_AUTOSAVE_ENABLED.get()) {
281 AutosaveTask autosaveTask = new AutosaveTask();
282 List<File> unsavedLayerFiles = autosaveTask.getUnsavedLayersFiles();
283 if (!unsavedLayerFiles.isEmpty()) {
284 ExtendedDialog dialog = new ExtendedDialog(
285 Main.parent,
286 tr("Unsaved osm data"),
287 new String[] {tr("Restore"), tr("Cancel"), tr("Discard")}
288 );
289 dialog.setContent(
290 trn("JOSM found {0} unsaved osm data layer. ",
291 "JOSM found {0} unsaved osm data layers. ", unsavedLayerFiles.size(), unsavedLayerFiles.size()) +
292 tr("It looks like JOSM crashed last time. Would you like to restore the data?"));
293 dialog.setButtonIcons(new String[] {"ok", "cancel", "dialogs/remove"});
294 int selection = dialog.showDialog().getValue();
295 if (selection == 1) {
296 autosaveTask.recoverUnsavedLayers();
297 } else if (selection == 3) {
298 autosaveTask.dicardUnsavedLayers();
299 }
300 }
301 autosaveTask.schedule();
302 }
303
304 main.postConstructorProcessCmdLine(args);
305
306 DownloadDialog.autostartIfNeeded();
307 }
308 });
309
310 if (RemoteControl.PROP_REMOTECONTROL_ENABLED.get()) {
311 RemoteControl.start();
312 }
313
314 if (Main.pref.getBoolean("debug.edt-checker.enable", Version.getInstance().isLocalBuild())) {
315 // Repaint manager is registered so late for a reason - there is lots of violation during startup process but they don't seem to break anything and are difficult to fix
316 System.out.println("Enabled EDT checker, wrongful access to gui from non EDT thread will be printed to console");
317 RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
318 }
319 }
320}
Note: See TracBrowser for help on using the repository browser.