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