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

Last change on this file since 2821 was 2821, checked in by Gubaer, 14 years ago

applied #4344: patch by bomm: simplify command line processing

  • Property svn:eol-style set to native
File size: 9.8 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 /**
104 * Main application Startup
105 */
106 public static void main(final String[] argArray) {
107 I18n.init();
108
109 Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
110
111 // initialize the plaform hook, and
112 Main.determinePlatformHook();
113 // call the really early hook before we anything else
114 Main.platform.preStartupHook();
115
116 // construct argument table
117 final Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
118 for (String arg : argArray) {
119 if ("-h".equals(arg) || "-?".equals(arg)) {
120 arg = "--help";
121 }
122 // handle simple arguments like file names, URLs, bounds
123 if (!arg.startsWith("--")) {
124 arg = "--download="+arg;
125 }
126 int i = arg.indexOf('=');
127 String key = i == -1 ? arg.substring(2) : arg.substring(2,i);
128 String value = i == -1 ? "" : arg.substring(i+1);
129 Collection<String> v = args.get(key);
130 if (v == null) {
131 v = new LinkedList<String>();
132 }
133 v.add(value);
134 args.put(key, v);
135 }
136
137 Main.pref.init(args.containsKey("reset-preferences"));
138
139 // Check if passed as parameter
140 if (args.containsKey("language")) {
141 I18n.set((String)(args.get("language").toArray()[0]));
142 } else {
143 I18n.set(Main.pref.get("language", null));
144 }
145 Main.pref.updateSystemProperties();
146
147 DefaultAuthenticator.createInstance(CredentialsManagerFactory.getCredentialManager());
148 Authenticator.setDefault(DefaultAuthenticator.getInstance());
149 ProxySelector.setDefault(new DefaultProxySelector(ProxySelector.getDefault()));
150 OAuthAccessTokenHolder.getInstance().init(Main.pref, CredentialsManagerFactory.getCredentialManager());
151
152 // asking for help? show help and exit
153 if (args.containsKey("help")) {
154 showHelp();
155 System.exit(0);
156 }
157
158 SplashScreen splash = new SplashScreen();
159 ProgressMonitor monitor = splash.getProgressMonitor();
160 monitor.beginTask(tr("Initializing"));
161 monitor.setTicksCount(7);
162 splash.setVisible(Main.pref.getBoolean("draw.splashscreen", true));
163
164 List<PluginInformation> pluginsToLoad = PluginHandler.buildListOfPluginsToLoad(monitor.createSubTaskMonitor(1, false));
165 if (!pluginsToLoad.isEmpty() && PluginHandler.checkAndConfirmPluginUpdate()) {
166 monitor.subTask(tr("Updating plugins..."));
167 PluginHandler.updatePlugins(pluginsToLoad, monitor.createSubTaskMonitor(1, false));
168 }
169 monitor.worked(1);
170
171 monitor.subTask(tr("Installing updated plugins"));
172 PluginHandler.installDownloadedPlugins();
173 monitor.worked(1);
174
175 monitor.subTask(tr("Loading early plugins"));
176 PluginHandler.loadEarlyPlugins(pluginsToLoad, monitor.createSubTaskMonitor(1, false));
177 monitor.worked(1);
178
179 monitor.subTask(tr("Setting defaults"));
180 preConstructorInit(args);
181 removeObsoletePreferences();
182 monitor.worked(1);
183
184 monitor.indeterminateSubTask(tr("Creating main GUI"));
185 JFrame mainFrame = new JFrame(tr("Java OpenStreetMap Editor"));
186 Main.parent = mainFrame;
187 final Main main = new MainApplication(mainFrame);
188 monitor.worked(1);
189
190 monitor.subTask(tr("Loading plugins"));
191 PluginHandler.loadLatePlugins(pluginsToLoad, monitor.createSubTaskMonitor(1, false));
192 monitor.worked(1);
193 toolbar.refreshToolbarControl();
194 splash.setVisible(false);
195 splash.dispose();
196 mainFrame.setVisible(true);
197
198 if (((!args.containsKey("no-maximize") && !args.containsKey("geometry")
199 && Main.pref.get("gui.geometry").length() == 0) || args.containsKey("maximize"))
200 && Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
201 mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
202 }
203
204 EventQueue.invokeLater(new Runnable() {
205 public void run() {
206 main.postConstructorProcessCmdLine(args);
207 }
208 });
209 }
210
211 /**
212 * Removes obsolete preference settings. If you throw out a once-used preference
213 * setting, add it to the list here with an expiry date (written as comment). If you
214 * see something with an expiry date in the past, remove it from the list.
215 */
216 public static void removeObsoletePreferences() {
217 String[] obsolete = {
218 "proxy.anonymous", // 01/2010 - not needed anymore. Can be removed mid 2010
219 "proxy.enable" // 01/2010 - not needed anymore. Can be removed mid 2010
220 };
221 for (String key : obsolete) {
222 if (Main.pref.hasKey(key)) {
223 Main.pref.removeFromCollection(key, Main.pref.get(key));
224 System.out.println(tr("Preference setting {0} has been removed since it is no longer used.", key));
225 }
226 }
227 }
228}
Note: See TracBrowser for help on using the repository browser.