1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.net.Authenticator;
|
---|
12 | import java.net.Inet6Address;
|
---|
13 | import java.net.InetAddress;
|
---|
14 | import java.net.ProxySelector;
|
---|
15 | import java.net.URL;
|
---|
16 | import java.security.AllPermission;
|
---|
17 | import java.security.CodeSource;
|
---|
18 | import java.security.GeneralSecurityException;
|
---|
19 | import java.security.KeyStoreException;
|
---|
20 | import java.security.NoSuchAlgorithmException;
|
---|
21 | import java.security.PermissionCollection;
|
---|
22 | import java.security.Permissions;
|
---|
23 | import java.security.Policy;
|
---|
24 | import java.security.cert.CertificateException;
|
---|
25 | import java.util.ArrayList;
|
---|
26 | import java.util.Arrays;
|
---|
27 | import java.util.Collection;
|
---|
28 | import java.util.Collections;
|
---|
29 | import java.util.List;
|
---|
30 | import java.util.Locale;
|
---|
31 | import java.util.Map;
|
---|
32 | import java.util.Optional;
|
---|
33 | import java.util.Set;
|
---|
34 | import java.util.TreeSet;
|
---|
35 | import java.util.logging.Level;
|
---|
36 | import java.util.stream.Collectors;
|
---|
37 | import java.util.stream.Stream;
|
---|
38 |
|
---|
39 | import javax.net.ssl.SSLSocketFactory;
|
---|
40 | import javax.swing.JOptionPane;
|
---|
41 | import javax.swing.RepaintManager;
|
---|
42 | import javax.swing.SwingUtilities;
|
---|
43 |
|
---|
44 | import org.jdesktop.swinghelper.debug.CheckThreadViolationRepaintManager;
|
---|
45 | import org.openstreetmap.josm.Main;
|
---|
46 | import org.openstreetmap.josm.actions.PreferencesAction;
|
---|
47 | import org.openstreetmap.josm.actions.RestartAction;
|
---|
48 | import org.openstreetmap.josm.data.AutosaveTask;
|
---|
49 | import org.openstreetmap.josm.data.CustomConfigurator;
|
---|
50 | import org.openstreetmap.josm.data.Version;
|
---|
51 | import org.openstreetmap.josm.gui.ProgramArguments.Option;
|
---|
52 | import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
|
---|
53 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
---|
54 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
|
---|
55 | import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
|
---|
56 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
57 | import org.openstreetmap.josm.io.CertificateAmendment;
|
---|
58 | import org.openstreetmap.josm.io.DefaultProxySelector;
|
---|
59 | import org.openstreetmap.josm.io.MessageNotifier;
|
---|
60 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
61 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
62 | import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
|
---|
63 | import org.openstreetmap.josm.io.protocols.data.Handler;
|
---|
64 | import org.openstreetmap.josm.io.remotecontrol.RemoteControl;
|
---|
65 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
66 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
67 | import org.openstreetmap.josm.tools.FontsManager;
|
---|
68 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
69 | import org.openstreetmap.josm.tools.I18n;
|
---|
70 | import org.openstreetmap.josm.tools.Logging;
|
---|
71 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
72 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
73 | import org.openstreetmap.josm.tools.Utils;
|
---|
74 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
75 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
76 | import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Main window class application.
|
---|
80 | *
|
---|
81 | * @author imi
|
---|
82 | */
|
---|
83 | public class MainApplication extends Main {
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Command-line arguments used to run the application.
|
---|
87 | */
|
---|
88 | private static final List<String> COMMAND_LINE_ARGS = new ArrayList<>();
|
---|
89 |
|
---|
90 | private MainFrame mainFrame;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Constructs a new {@code MainApplication} without a window.
|
---|
94 | */
|
---|
95 | public MainApplication() {
|
---|
96 | // Allow subclassing (see JOSM.java)
|
---|
97 | this(null);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Constructs a main frame, ready sized and operating. Does not display the frame.
|
---|
102 | * @param mainFrame The main JFrame of the application
|
---|
103 | * @since 10340
|
---|
104 | */
|
---|
105 | public MainApplication(MainFrame mainFrame) {
|
---|
106 | this.mainFrame = mainFrame;
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | protected void initializeMainWindow() {
|
---|
111 | mainPanel.reAddListeners();
|
---|
112 | if (mainFrame != null) {
|
---|
113 | mainFrame.initialize();
|
---|
114 |
|
---|
115 | menu = mainFrame.getMenu();
|
---|
116 | } else {
|
---|
117 | // required for running some tests.
|
---|
118 | menu = new MainMenu();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | protected void shutdown() {
|
---|
124 | if (mainFrame != null) {
|
---|
125 | mainFrame.storeState();
|
---|
126 | }
|
---|
127 | super.shutdown();
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Returns the command-line arguments used to run the application.
|
---|
132 | * @return the command-line arguments used to run the application
|
---|
133 | * @since 11650
|
---|
134 | */
|
---|
135 | public static List<String> getCommandLineArgs() {
|
---|
136 | return Collections.unmodifiableList(COMMAND_LINE_ARGS);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Displays help on the console
|
---|
141 | * @since 2748
|
---|
142 | */
|
---|
143 | public static void showHelp() {
|
---|
144 | // TODO: put in a platformHook for system that have no console by default
|
---|
145 | System.out.println(getHelp());
|
---|
146 | }
|
---|
147 |
|
---|
148 | static String getHelp() {
|
---|
149 | return tr("Java OpenStreetMap Editor")+" ["
|
---|
150 | +Version.getInstance().getAgentString()+"]\n\n"+
|
---|
151 | tr("usage")+":\n"+
|
---|
152 | "\tjava -jar josm.jar <options>...\n\n"+
|
---|
153 | tr("options")+":\n"+
|
---|
154 | "\t--help|-h "+tr("Show this help")+'\n'+
|
---|
155 | "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+'\n'+
|
---|
156 | "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+'\n'+
|
---|
157 | "\t[--download=]<URL> "+tr("Download the location at the URL (with lat=x&lon=y&zoom=z)")+'\n'+
|
---|
158 | "\t[--download=]<filename> "+tr("Open a file (any file type that can be opened with File/Open)")+'\n'+
|
---|
159 | "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw GPS")+'\n'+
|
---|
160 | "\t--downloadgps=<URL> "+tr("Download the location at the URL (with lat=x&lon=y&zoom=z) as raw GPS")+'\n'+
|
---|
161 | "\t--selection=<searchstring> "+tr("Select with the given search")+'\n'+
|
---|
162 | "\t--[no-]maximize "+tr("Launch in maximized mode")+'\n'+
|
---|
163 | "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
|
---|
164 | "\t--load-preferences=<url-to-xml> "+tr("Changes preferences according to the XML file")+"\n\n"+
|
---|
165 | "\t--set=<key>=<value> "+tr("Set preference key to value")+"\n\n"+
|
---|
166 | "\t--language=<language> "+tr("Set the language")+"\n\n"+
|
---|
167 | "\t--version "+tr("Displays the JOSM version and exits")+"\n\n"+
|
---|
168 | "\t--debug "+tr("Print debugging messages to console")+"\n\n"+
|
---|
169 | "\t--skip-plugins "+tr("Skip loading plugins")+"\n\n"+
|
---|
170 | "\t--offline=<osm_api|josm_website|all> "+tr("Disable access to the given resource(s), separated by comma")+"\n\n"+
|
---|
171 | tr("options provided as Java system properties")+":\n"+
|
---|
172 | align("\t-Djosm.dir.name=JOSM") + tr("Change the JOSM directory name") + "\n\n" +
|
---|
173 | align("\t-Djosm.pref=" + tr("/PATH/TO/JOSM/PREF ")) + tr("Set the preferences directory") + "\n" +
|
---|
174 | align("\t") + tr("Default: {0}", platform.getDefaultPrefDirectory()) + "\n\n" +
|
---|
175 | align("\t-Djosm.userdata=" + tr("/PATH/TO/JOSM/USERDATA")) + tr("Set the user data directory") + "\n" +
|
---|
176 | align("\t") + tr("Default: {0}", platform.getDefaultUserDataDirectory()) + "\n\n" +
|
---|
177 | align("\t-Djosm.cache=" + tr("/PATH/TO/JOSM/CACHE ")) + tr("Set the cache directory") + "\n" +
|
---|
178 | align("\t") + tr("Default: {0}", platform.getDefaultCacheDirectory()) + "\n\n" +
|
---|
179 | align("\t-Djosm.home=" + tr("/PATH/TO/JOSM/HOMEDIR ")) +
|
---|
180 | tr("Set the preferences+data+cache directory (cache directory will be josm.home/cache)")+"\n\n"+
|
---|
181 | tr("-Djosm.home has lower precedence, i.e. the specific setting overrides the general one")+"\n\n"+
|
---|
182 | tr("note: For some tasks, JOSM needs a lot of memory. It can be necessary to add the following\n" +
|
---|
183 | " Java option to specify the maximum size of allocated memory in megabytes")+":\n"+
|
---|
184 | "\t-Xmx...m\n\n"+
|
---|
185 | tr("examples")+":\n"+
|
---|
186 | "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
|
---|
187 | "\tjava -jar josm.jar "+OsmUrlToBounds.getURL(43.2, 11.1, 13)+'\n'+
|
---|
188 | "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
|
---|
189 | "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n"+
|
---|
190 | "\tjava -Djosm.pref=$XDG_CONFIG_HOME -Djosm.userdata=$XDG_DATA_HOME -Djosm.cache=$XDG_CACHE_HOME -jar josm.jar\n"+
|
---|
191 | "\tjava -Djosm.dir.name=josm_dev -jar josm.jar\n"+
|
---|
192 | "\tjava -Djosm.home=/home/user/.josm_dev -jar josm.jar\n"+
|
---|
193 | "\tjava -Xmx1024m -jar josm.jar\n\n"+
|
---|
194 | tr("Parameters --download, --downloadgps, and --selection are processed in this order.")+'\n'+
|
---|
195 | tr("Make sure you load some data if you use --selection.")+'\n';
|
---|
196 | }
|
---|
197 |
|
---|
198 | private static String align(String str) {
|
---|
199 | return str + Stream.generate(() -> " ").limit(Math.max(0, 43 - str.length())).collect(Collectors.joining(""));
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Main application Startup
|
---|
204 | * @param argArray Command-line arguments
|
---|
205 | */
|
---|
206 | public static void main(final String[] argArray) {
|
---|
207 | I18n.init();
|
---|
208 |
|
---|
209 | // construct argument table
|
---|
210 | ProgramArguments args = null;
|
---|
211 | try {
|
---|
212 | args = new ProgramArguments(argArray);
|
---|
213 | } catch (IllegalArgumentException e) {
|
---|
214 | System.err.println(e.getMessage());
|
---|
215 | System.exit(1);
|
---|
216 | return;
|
---|
217 | }
|
---|
218 |
|
---|
219 | Level logLevel = args.getLogLevel();
|
---|
220 | Logging.setLogLevel(logLevel);
|
---|
221 | if (!args.showVersion() && !args.showHelp()) {
|
---|
222 | Main.info(tr("Log level is at {0} ({1}, {2})", logLevel.getLocalizedName(), logLevel.getName(), logLevel.intValue()));
|
---|
223 | }
|
---|
224 |
|
---|
225 | Optional<String> language = args.getSingle(Option.LANGUAGE);
|
---|
226 | I18n.set(language.orElse(null));
|
---|
227 |
|
---|
228 | Policy.setPolicy(new Policy() {
|
---|
229 | // Permissions for plug-ins loaded when josm is started via webstart
|
---|
230 | private PermissionCollection pc;
|
---|
231 |
|
---|
232 | {
|
---|
233 | pc = new Permissions();
|
---|
234 | pc.add(new AllPermission());
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Override
|
---|
238 | public PermissionCollection getPermissions(CodeSource codesource) {
|
---|
239 | return pc;
|
---|
240 | }
|
---|
241 | });
|
---|
242 |
|
---|
243 | Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
|
---|
244 |
|
---|
245 | // initialize the platform hook, and
|
---|
246 | Main.determinePlatformHook();
|
---|
247 | // call the really early hook before we do anything else
|
---|
248 | Main.platform.preStartupHook();
|
---|
249 |
|
---|
250 | if (args.showVersion()) {
|
---|
251 | System.out.println(Version.getInstance().getAgentString());
|
---|
252 | return;
|
---|
253 | } else if (args.showHelp()) {
|
---|
254 | showHelp();
|
---|
255 | return;
|
---|
256 | }
|
---|
257 |
|
---|
258 | COMMAND_LINE_ARGS.addAll(Arrays.asList(argArray));
|
---|
259 |
|
---|
260 | boolean skipLoadingPlugins = args.hasOption(Option.SKIP_PLUGINS);
|
---|
261 | if (skipLoadingPlugins) {
|
---|
262 | Main.info(tr("Plugin loading skipped"));
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (Logging.isLoggingEnabled(Logging.LEVEL_TRACE)) {
|
---|
266 | // Enable debug in OAuth signpost via system preference, but only at trace level
|
---|
267 | Utils.updateSystemProperty("debug", "true");
|
---|
268 | Main.info(tr("Enabled detailed debug level (trace)"));
|
---|
269 | }
|
---|
270 |
|
---|
271 | Main.pref.init(args.hasOption(Option.RESET_PREFERENCES));
|
---|
272 |
|
---|
273 | args.getPreferencesToSet().forEach(Main.pref::put);
|
---|
274 |
|
---|
275 | if (!language.isPresent()) {
|
---|
276 | I18n.set(Main.pref.get("language", null));
|
---|
277 | }
|
---|
278 | Main.pref.updateSystemProperties();
|
---|
279 |
|
---|
280 | checkIPv6();
|
---|
281 |
|
---|
282 | processOffline(args);
|
---|
283 |
|
---|
284 | Main.platform.afterPrefStartupHook();
|
---|
285 |
|
---|
286 | FontsManager.initialize();
|
---|
287 |
|
---|
288 | I18n.setupLanguageFonts();
|
---|
289 |
|
---|
290 | Handler.install();
|
---|
291 |
|
---|
292 | WindowGeometry geometry = WindowGeometry.mainWindow("gui.geometry",
|
---|
293 | args.getSingle(Option.GEOMETRY).orElse(null),
|
---|
294 | !args.hasOption(Option.NO_MAXIMIZE) && Main.pref.getBoolean("gui.maximized", false));
|
---|
295 | final MainFrame mainFrame = new MainFrame(contentPanePrivate, mainPanel, geometry);
|
---|
296 | Main.parent = mainFrame;
|
---|
297 |
|
---|
298 | if (args.hasOption(Option.LOAD_PREFERENCES)) {
|
---|
299 | CustomConfigurator.XMLCommandProcessor config = new CustomConfigurator.XMLCommandProcessor(Main.pref);
|
---|
300 | for (String i : args.get(Option.LOAD_PREFERENCES)) {
|
---|
301 | info("Reading preferences from " + i);
|
---|
302 | try (InputStream is = openStream(new URL(i))) {
|
---|
303 | config.openAndReadXML(is);
|
---|
304 | } catch (IOException ex) {
|
---|
305 | throw BugReport.intercept(ex).put("file", i);
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | try {
|
---|
311 | CertificateAmendment.addMissingCertificates();
|
---|
312 | } catch (IOException | GeneralSecurityException ex) {
|
---|
313 | Main.warn(ex);
|
---|
314 | Main.warn(getErrorMessage(Utils.getRootCause(ex)));
|
---|
315 | }
|
---|
316 | Authenticator.setDefault(DefaultAuthenticator.getInstance());
|
---|
317 | DefaultProxySelector proxySelector = new DefaultProxySelector(ProxySelector.getDefault());
|
---|
318 | ProxySelector.setDefault(proxySelector);
|
---|
319 | OAuthAccessTokenHolder.getInstance().init(Main.pref, CredentialsManager.getInstance());
|
---|
320 |
|
---|
321 | final SplashScreen splash = GuiHelper.runInEDTAndWaitAndReturn(SplashScreen::new);
|
---|
322 | final SplashScreen.SplashProgressMonitor monitor = splash.getProgressMonitor();
|
---|
323 | monitor.beginTask(tr("Initializing"));
|
---|
324 | GuiHelper.runInEDT(() -> splash.setVisible(Main.pref.getBoolean("draw.splashscreen", true)));
|
---|
325 | Main.setInitStatusListener(new InitStatusListener() {
|
---|
326 |
|
---|
327 | @Override
|
---|
328 | public Object updateStatus(String event) {
|
---|
329 | monitor.beginTask(event);
|
---|
330 | return event;
|
---|
331 | }
|
---|
332 |
|
---|
333 | @Override
|
---|
334 | public void finish(Object status) {
|
---|
335 | if (status instanceof String) {
|
---|
336 | monitor.finishTask((String) status);
|
---|
337 | }
|
---|
338 | }
|
---|
339 | });
|
---|
340 |
|
---|
341 | Collection<PluginInformation> pluginsToLoad = null;
|
---|
342 |
|
---|
343 | if (!skipLoadingPlugins) {
|
---|
344 | pluginsToLoad = updateAndLoadEarlyPlugins(splash, monitor);
|
---|
345 | }
|
---|
346 |
|
---|
347 | monitor.indeterminateSubTask(tr("Setting defaults"));
|
---|
348 | preConstructorInit();
|
---|
349 |
|
---|
350 | monitor.indeterminateSubTask(tr("Creating main GUI"));
|
---|
351 | final Main main = new MainApplication(mainFrame);
|
---|
352 | main.initialize();
|
---|
353 |
|
---|
354 | if (!skipLoadingPlugins) {
|
---|
355 | loadLatePlugins(splash, monitor, pluginsToLoad);
|
---|
356 | }
|
---|
357 |
|
---|
358 | // Wait for splash disappearance (fix #9714)
|
---|
359 | GuiHelper.runInEDTAndWait(() -> {
|
---|
360 | splash.setVisible(false);
|
---|
361 | splash.dispose();
|
---|
362 | mainFrame.setVisible(true);
|
---|
363 | });
|
---|
364 |
|
---|
365 | Main.MasterWindowListener.setup();
|
---|
366 |
|
---|
367 | boolean maximized = Main.pref.getBoolean("gui.maximized", false);
|
---|
368 | if ((!args.hasOption(Option.NO_MAXIMIZE) && maximized) || args.hasOption(Option.MAXIMIZE)) {
|
---|
369 | mainFrame.setMaximized(true);
|
---|
370 | }
|
---|
371 | if (main.menu.fullscreenToggleAction != null) {
|
---|
372 | main.menu.fullscreenToggleAction.initial();
|
---|
373 | }
|
---|
374 |
|
---|
375 | SwingUtilities.invokeLater(new GuiFinalizationWorker(args, proxySelector));
|
---|
376 |
|
---|
377 | if (Main.isPlatformWindows()) {
|
---|
378 | try {
|
---|
379 | // Check for insecure certificates to remove.
|
---|
380 | // This is Windows-dependant code but it can't go to preStartupHook (need i18n)
|
---|
381 | // neither startupHook (need to be called before remote control)
|
---|
382 | PlatformHookWindows.removeInsecureCertificates();
|
---|
383 | } catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException e) {
|
---|
384 | error(e);
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | if (RemoteControl.PROP_REMOTECONTROL_ENABLED.get()) {
|
---|
389 | RemoteControl.start();
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (MessageNotifier.PROP_NOTIFIER_ENABLED.get()) {
|
---|
393 | MessageNotifier.start();
|
---|
394 | }
|
---|
395 |
|
---|
396 | if (Main.pref.getBoolean("debug.edt-checker.enable", Version.getInstance().isLocalBuild())) {
|
---|
397 | // Repaint manager is registered so late for a reason - there is lots of violation during startup process
|
---|
398 | // but they don't seem to break anything and are difficult to fix
|
---|
399 | info("Enabled EDT checker, wrongful access to gui from non EDT thread will be printed to console");
|
---|
400 | RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | private static InputStream openStream(URL url) throws IOException {
|
---|
405 | if ("file".equals(url.getProtocol())) {
|
---|
406 | return url.openStream();
|
---|
407 | } else {
|
---|
408 | return HttpClient.create(url).connect().getContent();
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | static Collection<PluginInformation> updateAndLoadEarlyPlugins(SplashScreen splash, SplashProgressMonitor monitor) {
|
---|
413 | Collection<PluginInformation> pluginsToLoad;
|
---|
414 | pluginsToLoad = PluginHandler.buildListOfPluginsToLoad(splash, monitor.createSubTaskMonitor(1, false));
|
---|
415 | if (!pluginsToLoad.isEmpty() && PluginHandler.checkAndConfirmPluginUpdate(splash)) {
|
---|
416 | monitor.subTask(tr("Updating plugins"));
|
---|
417 | pluginsToLoad = PluginHandler.updatePlugins(splash, null, monitor.createSubTaskMonitor(1, false), false);
|
---|
418 | }
|
---|
419 |
|
---|
420 | monitor.indeterminateSubTask(tr("Installing updated plugins"));
|
---|
421 | PluginHandler.installDownloadedPlugins(true);
|
---|
422 |
|
---|
423 | monitor.indeterminateSubTask(tr("Loading early plugins"));
|
---|
424 | PluginHandler.loadEarlyPlugins(splash, pluginsToLoad, monitor.createSubTaskMonitor(1, false));
|
---|
425 | return pluginsToLoad;
|
---|
426 | }
|
---|
427 |
|
---|
428 | static void loadLatePlugins(SplashScreen splash, SplashProgressMonitor monitor, Collection<PluginInformation> pluginsToLoad) {
|
---|
429 | monitor.indeterminateSubTask(tr("Loading plugins"));
|
---|
430 | PluginHandler.loadLatePlugins(splash, pluginsToLoad, monitor.createSubTaskMonitor(1, false));
|
---|
431 | toolbar.refreshToolbarControl();
|
---|
432 | }
|
---|
433 |
|
---|
434 | private static void processOffline(ProgramArguments args) {
|
---|
435 | for (String offlineNames : args.get(Option.OFFLINE)) {
|
---|
436 | for (String s : offlineNames.split(",")) {
|
---|
437 | try {
|
---|
438 | Main.setOffline(OnlineResource.valueOf(s.toUpperCase(Locale.ENGLISH)));
|
---|
439 | } catch (IllegalArgumentException e) {
|
---|
440 | Main.error(e, tr("''{0}'' is not a valid value for argument ''{1}''. Possible values are {2}, possibly delimited by commas.",
|
---|
441 | s.toUpperCase(Locale.ENGLISH), Option.OFFLINE.getName(), Arrays.toString(OnlineResource.values())));
|
---|
442 | System.exit(1);
|
---|
443 | return;
|
---|
444 | }
|
---|
445 | }
|
---|
446 | }
|
---|
447 | Set<OnlineResource> offline = Main.getOfflineResources();
|
---|
448 | if (!offline.isEmpty()) {
|
---|
449 | Main.warn(trn("JOSM is running in offline mode. This resource will not be available: {0}",
|
---|
450 | "JOSM is running in offline mode. These resources will not be available: {0}",
|
---|
451 | offline.size(), offline.size() == 1 ? offline.iterator().next() : Arrays.toString(offline.toArray())));
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Check if IPv6 can be safely enabled and do so. Because this cannot be done after network activation,
|
---|
457 | * disabling or enabling IPV6 may only be done with next start.
|
---|
458 | */
|
---|
459 | private static void checkIPv6() {
|
---|
460 | if ("auto".equals(Main.pref.get("prefer.ipv6", "auto"))) {
|
---|
461 | new Thread((Runnable) () -> { /* this may take some time (DNS, Connect) */
|
---|
462 | boolean hasv6 = false;
|
---|
463 | boolean wasv6 = Main.pref.getBoolean("validated.ipv6", false);
|
---|
464 | try {
|
---|
465 | /* Use the check result from last run of the software, as after the test, value
|
---|
466 | changes have no effect anymore */
|
---|
467 | if (wasv6) {
|
---|
468 | Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true");
|
---|
469 | }
|
---|
470 | for (InetAddress a : InetAddress.getAllByName("josm.openstreetmap.de")) {
|
---|
471 | if (a instanceof Inet6Address) {
|
---|
472 | if (a.isReachable(1000)) {
|
---|
473 | /* be sure it REALLY works */
|
---|
474 | SSLSocketFactory.getDefault().createSocket(a, 443).close();
|
---|
475 | Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true");
|
---|
476 | if (!wasv6) {
|
---|
477 | Main.info(tr("Detected useable IPv6 network, prefering IPv6 over IPv4 after next restart."));
|
---|
478 | } else {
|
---|
479 | Main.info(tr("Detected useable IPv6 network, prefering IPv6 over IPv4."));
|
---|
480 | }
|
---|
481 | hasv6 = true;
|
---|
482 | }
|
---|
483 | break; /* we're done */
|
---|
484 | }
|
---|
485 | }
|
---|
486 | } catch (IOException | SecurityException e) {
|
---|
487 | if (Main.isDebugEnabled()) {
|
---|
488 | Main.debug("Exception while checking IPv6 connectivity: "+e);
|
---|
489 | }
|
---|
490 | Main.trace(e);
|
---|
491 | }
|
---|
492 | if (wasv6 && !hasv6) {
|
---|
493 | Main.info(tr("Detected no useable IPv6 network, prefering IPv4 over IPv6 after next restart."));
|
---|
494 | Main.pref.put("validated.ipv6", hasv6); // be sure it is stored before the restart!
|
---|
495 | new RestartAction().actionPerformed(null);
|
---|
496 | }
|
---|
497 | Main.pref.put("validated.ipv6", hasv6);
|
---|
498 | }, "IPv6-checker").start();
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | private static class GuiFinalizationWorker implements Runnable {
|
---|
503 |
|
---|
504 | private final ProgramArguments args;
|
---|
505 | private final DefaultProxySelector proxySelector;
|
---|
506 |
|
---|
507 | GuiFinalizationWorker(ProgramArguments args, DefaultProxySelector proxySelector) {
|
---|
508 | this.args = args;
|
---|
509 | this.proxySelector = proxySelector;
|
---|
510 | }
|
---|
511 |
|
---|
512 | @Override
|
---|
513 | public void run() {
|
---|
514 |
|
---|
515 | // Handle proxy/network errors early to inform user he should change settings to be able to use JOSM correctly
|
---|
516 | if (!handleProxyErrors()) {
|
---|
517 | handleNetworkErrors();
|
---|
518 | }
|
---|
519 |
|
---|
520 | // Restore autosave layers after crash and start autosave thread
|
---|
521 | handleAutosave();
|
---|
522 |
|
---|
523 | // Handle command line instructions
|
---|
524 | postConstructorProcessCmdLine(args);
|
---|
525 |
|
---|
526 | // Show download dialog if autostart is enabled
|
---|
527 | DownloadDialog.autostartIfNeeded();
|
---|
528 | }
|
---|
529 |
|
---|
530 | private static void handleAutosave() {
|
---|
531 | if (AutosaveTask.PROP_AUTOSAVE_ENABLED.get()) {
|
---|
532 | AutosaveTask autosaveTask = new AutosaveTask();
|
---|
533 | List<File> unsavedLayerFiles = autosaveTask.getUnsavedLayersFiles();
|
---|
534 | if (!unsavedLayerFiles.isEmpty()) {
|
---|
535 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
536 | Main.parent,
|
---|
537 | tr("Unsaved osm data"),
|
---|
538 | new String[] {tr("Restore"), tr("Cancel"), tr("Discard")}
|
---|
539 | );
|
---|
540 | dialog.setContent(
|
---|
541 | trn("JOSM found {0} unsaved osm data layer. ",
|
---|
542 | "JOSM found {0} unsaved osm data layers. ", unsavedLayerFiles.size(), unsavedLayerFiles.size()) +
|
---|
543 | tr("It looks like JOSM crashed last time. Would you like to restore the data?"));
|
---|
544 | dialog.setButtonIcons(new String[] {"ok", "cancel", "dialogs/delete"});
|
---|
545 | int selection = dialog.showDialog().getValue();
|
---|
546 | if (selection == 1) {
|
---|
547 | autosaveTask.recoverUnsavedLayers();
|
---|
548 | } else if (selection == 3) {
|
---|
549 | autosaveTask.discardUnsavedLayers();
|
---|
550 | }
|
---|
551 | }
|
---|
552 | autosaveTask.schedule();
|
---|
553 | }
|
---|
554 | }
|
---|
555 |
|
---|
556 | private static boolean handleNetworkOrProxyErrors(boolean hasErrors, String title, String message) {
|
---|
557 | if (hasErrors) {
|
---|
558 | ExtendedDialog ed = new ExtendedDialog(
|
---|
559 | Main.parent, title,
|
---|
560 | new String[]{tr("Change proxy settings"), tr("Cancel")});
|
---|
561 | ed.setButtonIcons(new String[]{"dialogs/settings", "cancel"}).setCancelButton(2);
|
---|
562 | ed.setMinimumSize(new Dimension(460, 260));
|
---|
563 | ed.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
564 | ed.setContent(message);
|
---|
565 |
|
---|
566 | if (ed.showDialog().getValue() == 1) {
|
---|
567 | PreferencesAction.forPreferenceSubTab(null, null, ProxyPreference.class).run();
|
---|
568 | }
|
---|
569 | }
|
---|
570 | return hasErrors;
|
---|
571 | }
|
---|
572 |
|
---|
573 | private boolean handleProxyErrors() {
|
---|
574 | return handleNetworkOrProxyErrors(proxySelector.hasErrors(), tr("Proxy errors occurred"),
|
---|
575 | tr("JOSM tried to access the following resources:<br>" +
|
---|
576 | "{0}" +
|
---|
577 | "but <b>failed</b> to do so, because of the following proxy errors:<br>" +
|
---|
578 | "{1}" +
|
---|
579 | "Would you like to change your proxy settings now?",
|
---|
580 | Utils.joinAsHtmlUnorderedList(proxySelector.getErrorResources()),
|
---|
581 | Utils.joinAsHtmlUnorderedList(proxySelector.getErrorMessages())
|
---|
582 | ));
|
---|
583 | }
|
---|
584 |
|
---|
585 | private static boolean handleNetworkErrors() {
|
---|
586 | Map<String, Throwable> networkErrors = Main.getNetworkErrors();
|
---|
587 | boolean condition = !networkErrors.isEmpty();
|
---|
588 | if (condition) {
|
---|
589 | Set<String> errors = new TreeSet<>();
|
---|
590 | for (Throwable t : networkErrors.values()) {
|
---|
591 | errors.add(t.toString());
|
---|
592 | }
|
---|
593 | return handleNetworkOrProxyErrors(condition, tr("Network errors occurred"),
|
---|
594 | tr("JOSM tried to access the following resources:<br>" +
|
---|
595 | "{0}" +
|
---|
596 | "but <b>failed</b> to do so, because of the following network errors:<br>" +
|
---|
597 | "{1}" +
|
---|
598 | "It may be due to a missing proxy configuration.<br>" +
|
---|
599 | "Would you like to change your proxy settings now?",
|
---|
600 | Utils.joinAsHtmlUnorderedList(networkErrors.keySet()),
|
---|
601 | Utils.joinAsHtmlUnorderedList(errors)
|
---|
602 | ));
|
---|
603 | }
|
---|
604 | return false;
|
---|
605 | }
|
---|
606 | }
|
---|
607 | }
|
---|