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

Last change on this file since 3775 was 3715, checked in by Upliner, 13 years ago

Added imagery plugin to josm core. Imagery plugin is union of wmsplugin and slippymap plugins. It includes code by Tim Waters, Petr Dlouhý, Frederik Ramm and others. Also enables the remotecontol which was integrated in [3707].

  • Property svn:eol-style set to native
File size: 14.9 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.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.Toolkit;
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.JLabel;
28import javax.swing.JOptionPane;
29import javax.swing.JPanel;
30import javax.swing.JTextArea;
31import javax.swing.SwingUtilities;
32
33import org.openstreetmap.josm.Main;
34import org.openstreetmap.josm.data.AutosaveTask;
35import org.openstreetmap.josm.data.Preferences;
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.CredentialsManagerFactory;
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 mainFrame.setBounds(bounds);
68 mainFrame.setIconImage(ImageProvider.get("logo.png").getImage());
69 mainFrame.addWindowListener(new WindowAdapter(){
70 @Override public void windowClosing(final WindowEvent arg0) {
71 Main.exitJosm(true);
72 }
73 });
74 mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
75 }
76
77 /**
78 * Displays help on the console
79 *
80 */
81 public static void showHelp() {
82 // TODO: put in a platformHook for system that have no console by default
83 System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
84 tr("usage")+":\n"+
85 "\tjava -jar josm.jar <options>...\n\n"+
86 tr("options")+":\n"+
87 "\t--help|-?|-h "+tr("Show this help")+"\n"+
88 "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
89 "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
90 "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
91 "\t[--download=]<filename> "+tr("Open a file (any file type that can be opened with File/Open)")+"\n"+
92 "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
93 "\t--downloadgps=<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z) as raw gps")+"\n"+
94 "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
95 "\t--[no-]maximize "+tr("Launch in maximized mode")+"\n"+
96 "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
97 "\t--language=<language> "+tr("Set the language")+"\n\n"+
98 tr("options provided as Java system properties")+":\n"+
99 "\t-Djosm.home="+tr("/PATH/TO/JOSM/FOLDER/ ")+tr("Change the folder for all user settings")+"\n\n"+
100 tr("note: For some tasks, JOSM needs a lot of memory. It can be necessary to add the following\n" +
101 " Java option to specify the maximum size of allocated memory in megabytes")+":\n"+
102 "\t-Xmx...m\n\n"+
103 tr("examples")+":\n"+
104 "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
105 "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
106 "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
107 "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n"+
108 "\tjava -Djosm.home=/home/user/.josm_dev -jar josm.jar\n"+
109 "\tjava -Xmx400m -jar josm.jar\n\n"+
110 tr("Parameters --download, --downloadgps, and --selection are processed in this order.")+"\n"+
111 tr("Make sure you load some data if you use --selection.")+"\n"
112 );
113 }
114
115 private static Map<String, Collection<String>> buildCommandLineArgumentMap(String[] args) {
116 Map<String, Collection<String>> argMap = new HashMap<String, Collection<String>>();
117 for (String arg : args) {
118 if ("-h".equals(arg) || "-?".equals(arg)) {
119 arg = "--help";
120 }
121 // handle simple arguments like file names, URLs, bounds
122 if (!arg.startsWith("--")) {
123 arg = "--download="+arg;
124 }
125 int i = arg.indexOf('=');
126 String key = i == -1 ? arg.substring(2) : arg.substring(2,i);
127 String value = i == -1 ? "" : arg.substring(i+1);
128 Collection<String> v = argMap.get(key);
129 if (v == null) {
130 v = new LinkedList<String>();
131 }
132 v.add(value);
133 argMap.put(key, v);
134 }
135 return argMap;
136 }
137
138 /**
139 * Main application Startup
140 */
141 public static void main(final String[] argArray) {
142 I18n.init();
143 checkJava6();
144 Main.pref = new Preferences();
145
146 Policy.setPolicy(new Policy() {
147 // Permissions for plug-ins loaded when josm is started via webstart
148 private PermissionCollection pc;
149
150 {
151 pc = new Permissions();
152 pc.add(new AllPermission());
153 }
154
155 @Override
156 public void refresh() { }
157
158 @Override
159 public PermissionCollection getPermissions(CodeSource codesource) {
160 return pc;
161 }
162 });
163
164 Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
165 // http://stuffthathappens.com/blog/2007/10/15/one-more-note-on-uncaught-exception-handlers/
166 System.setProperty("sun.awt.exception.handler", BugReportExceptionHandler.class.getName());
167
168 // initialize the plaform hook, and
169 Main.determinePlatformHook();
170 // call the really early hook before we anything else
171 Main.platform.preStartupHook();
172
173 // construct argument table
174 final Map<String, Collection<String>> args = buildCommandLineArgumentMap(argArray);
175
176 Main.pref.init(args.containsKey("reset-preferences"));
177
178 // Check if passed as parameter
179 if (args.containsKey("language")) {
180 I18n.set((String)(args.get("language").toArray()[0]));
181 } else {
182 I18n.set(Main.pref.get("language", null));
183 }
184 Main.pref.updateSystemProperties();
185
186 DefaultAuthenticator.createInstance(CredentialsManagerFactory.getCredentialManager());
187 Authenticator.setDefault(DefaultAuthenticator.getInstance());
188 ProxySelector.setDefault(new DefaultProxySelector(ProxySelector.getDefault()));
189 OAuthAccessTokenHolder.getInstance().init(Main.pref, CredentialsManagerFactory.getCredentialManager());
190
191 // asking for help? show help and exit
192 if (args.containsKey("help")) {
193 showHelp();
194 System.exit(0);
195 }
196
197 SplashScreen splash = new SplashScreen();
198 ProgressMonitor monitor = splash.getProgressMonitor();
199 monitor.beginTask(tr("Initializing"));
200 monitor.setTicksCount(7);
201 splash.setVisible(Main.pref.getBoolean("draw.splashscreen", true));
202
203 List<PluginInformation> pluginsToLoad = PluginHandler.buildListOfPluginsToLoad(splash,monitor.createSubTaskMonitor(1, false));
204 if (!pluginsToLoad.isEmpty() && PluginHandler.checkAndConfirmPluginUpdate(splash)) {
205 monitor.subTask(tr("Updating plugins..."));
206 pluginsToLoad = PluginHandler.updatePlugins(splash,pluginsToLoad, monitor.createSubTaskMonitor(1, false));
207 }
208 monitor.worked(1);
209
210 monitor.subTask(tr("Installing updated plugins"));
211 PluginHandler.installDownloadedPlugins(true);
212 monitor.worked(1);
213
214 monitor.subTask(tr("Loading early plugins"));
215 PluginHandler.loadEarlyPlugins(splash,pluginsToLoad, monitor.createSubTaskMonitor(1, false));
216 monitor.worked(1);
217
218 monitor.subTask(tr("Setting defaults"));
219 preConstructorInit(args);
220 removeObsoletePreferences();
221 monitor.worked(1);
222
223 if (RemoteControl.PROP_REMOTECONTROL_ENABLED.get()) {
224 RemoteControl.start();
225 }
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 /**
287 * Removes obsolete preference settings. If you throw out a once-used preference
288 * setting, add it to the list here with an expiry date (written as comment). If you
289 * see something with an expiry date in the past, remove it from the list.
290 */
291 public static void removeObsoletePreferences() {
292
293 String[] obsolete = {
294 "proxy.anonymous", // 01/2010 - not needed anymore. Can be removed mid 2010
295 "proxy.enable" // 01/2010 - not needed anymore. Can be removed mid 2010
296 };
297 for (String key : obsolete) {
298 if (Main.pref.hasKey(key)) {
299 Main.pref.removeFromCollection(key, Main.pref.get(key));
300 System.out.println(tr("Preference setting {0} has been removed since it is no longer used.", key));
301 }
302 }
303 }
304
305 private static void checkJava6() {
306 String version = System.getProperty("java.version");
307 if (version != null) {
308 if (version.startsWith("1.6") || version.startsWith("6") ||
309 version.startsWith("1.7") || version.startsWith("7"))
310 return;
311 if (version.startsWith("1.5") || version.startsWith("5")) {
312 JLabel ho = new JLabel("<html>"+
313 tr("<h2>JOSM requires Java version 6.</h2>"+
314 "Detected Java version: {0}.<br>"+
315 "You can <ul><li>update your Java (JRE) or</li>"+
316 "<li>use an earlier (Java 5 compatible) version of JOSM.</li></ul>"+
317 "More Info:", version)+"</html>");
318 JTextArea link = new JTextArea("http://josm.openstreetmap.de/wiki/Help/SystemRequirements");
319 link.setEditable(false);
320 link.setBackground(panel.getBackground());
321 JPanel panel = new JPanel(new GridBagLayout());
322 GridBagConstraints gbc = new GridBagConstraints();
323 gbc.gridwidth = GridBagConstraints.REMAINDER;
324 gbc.anchor = GridBagConstraints.WEST;
325 gbc.weightx = 1.0;
326 panel.add(ho, gbc);
327 panel.add(link, gbc);
328 final String EXIT = tr("Exit JOSM");
329 final String CONTINUE = tr("Continue, try anyway");
330 int ret = JOptionPane.showOptionDialog(null, panel, tr("Error"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, new String[] {EXIT, CONTINUE}, EXIT);
331 if (ret == 0) {
332 System.exit(0);
333 }
334 return;
335 }
336 }
337 System.err.println("Error: Could not recognize Java Version: "+version);
338 }
339}
Note: See TracBrowser for help on using the repository browser.