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

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

fix #1608 - better icons for JOSM

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