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

Last change on this file since 3002 was 2876, checked in by bastiK, 14 years ago

fix #3745 - fullscreen geometry not restored (patch by bomm)

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