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

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

fix commandline file loading - closes #4288 - patch by bomm

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