1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.Window;
|
---|
11 | import java.awt.event.ComponentEvent;
|
---|
12 | import java.awt.event.ComponentListener;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.awt.event.WindowAdapter;
|
---|
15 | import java.awt.event.WindowEvent;
|
---|
16 | import java.io.File;
|
---|
17 | import java.lang.ref.WeakReference;
|
---|
18 | import java.net.URI;
|
---|
19 | import java.net.URISyntaxException;
|
---|
20 | import java.net.URL;
|
---|
21 | import java.text.MessageFormat;
|
---|
22 | import java.util.ArrayList;
|
---|
23 | import java.util.Arrays;
|
---|
24 | import java.util.Collection;
|
---|
25 | import java.util.Collections;
|
---|
26 | import java.util.HashMap;
|
---|
27 | import java.util.HashSet;
|
---|
28 | import java.util.Iterator;
|
---|
29 | import java.util.List;
|
---|
30 | import java.util.Map;
|
---|
31 | import java.util.Objects;
|
---|
32 | import java.util.Set;
|
---|
33 | import java.util.StringTokenizer;
|
---|
34 | import java.util.concurrent.Callable;
|
---|
35 | import java.util.concurrent.ExecutorService;
|
---|
36 | import java.util.concurrent.Executors;
|
---|
37 | import java.util.concurrent.Future;
|
---|
38 | import java.util.logging.Handler;
|
---|
39 | import java.util.logging.Level;
|
---|
40 | import java.util.logging.LogRecord;
|
---|
41 | import java.util.logging.Logger;
|
---|
42 |
|
---|
43 | import javax.swing.Action;
|
---|
44 | import javax.swing.InputMap;
|
---|
45 | import javax.swing.JComponent;
|
---|
46 | import javax.swing.JFrame;
|
---|
47 | import javax.swing.JOptionPane;
|
---|
48 | import javax.swing.JPanel;
|
---|
49 | import javax.swing.JTextArea;
|
---|
50 | import javax.swing.KeyStroke;
|
---|
51 | import javax.swing.LookAndFeel;
|
---|
52 | import javax.swing.UIManager;
|
---|
53 | import javax.swing.UnsupportedLookAndFeelException;
|
---|
54 |
|
---|
55 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
|
---|
56 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
57 | import org.openstreetmap.josm.actions.OpenFileAction;
|
---|
58 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
59 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
60 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
61 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
62 | import org.openstreetmap.josm.actions.mapmode.DrawAction;
|
---|
63 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
64 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
65 | import org.openstreetmap.josm.data.Bounds;
|
---|
66 | import org.openstreetmap.josm.data.Preferences;
|
---|
67 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
68 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
69 | import org.openstreetmap.josm.data.ViewportData;
|
---|
70 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
71 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
72 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
73 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
74 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
75 | import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
|
---|
76 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
77 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
78 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
79 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
80 | import org.openstreetmap.josm.gui.GettingStarted;
|
---|
81 | import org.openstreetmap.josm.gui.MainApplication.Option;
|
---|
82 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
83 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
84 | import org.openstreetmap.josm.gui.MapFrameListener;
|
---|
85 | import org.openstreetmap.josm.gui.MapView;
|
---|
86 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
87 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
88 | import org.openstreetmap.josm.gui.io.SaveLayersDialog;
|
---|
89 | import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer;
|
---|
90 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
91 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
92 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
93 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
94 | import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
|
---|
95 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
96 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
97 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
98 | import org.openstreetmap.josm.gui.progress.ProgressMonitorExecutor;
|
---|
99 | import org.openstreetmap.josm.gui.tagging.TaggingPresets;
|
---|
100 | import org.openstreetmap.josm.gui.util.RedirectInputMap;
|
---|
101 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
102 | import org.openstreetmap.josm.io.FileWatcher;
|
---|
103 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
104 | import org.openstreetmap.josm.io.OsmApi;
|
---|
105 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
106 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
107 | import org.openstreetmap.josm.tools.I18n;
|
---|
108 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
109 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
110 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
111 | import org.openstreetmap.josm.tools.PlatformHook;
|
---|
112 | import org.openstreetmap.josm.tools.PlatformHookOsx;
|
---|
113 | import org.openstreetmap.josm.tools.PlatformHookUnixoid;
|
---|
114 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
115 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
116 | import org.openstreetmap.josm.tools.Utils;
|
---|
117 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Abstract class holding various static global variables and methods used in large parts of JOSM application.
|
---|
121 | * @since 98
|
---|
122 | */
|
---|
123 | public abstract class Main {
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * The JOSM website URL.
|
---|
127 | * @since 6897 (was public from 6143 to 6896)
|
---|
128 | */
|
---|
129 | private static final String JOSM_WEBSITE = "https://josm.openstreetmap.de";
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * The OSM website URL.
|
---|
133 | * @since 6897 (was public from 6453 to 6896)
|
---|
134 | */
|
---|
135 | private static final String OSM_WEBSITE = "https://www.openstreetmap.org";
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Replies true if JOSM currently displays a map view. False, if it doesn't, i.e. if
|
---|
139 | * it only shows the MOTD panel.
|
---|
140 | *
|
---|
141 | * @return <code>true</code> if JOSM currently displays a map view
|
---|
142 | */
|
---|
143 | public static boolean isDisplayingMapView() {
|
---|
144 | if (map == null) return false;
|
---|
145 | if (map.mapView == null) return false;
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Global parent component for all dialogs and message boxes
|
---|
151 | */
|
---|
152 | public static Component parent;
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Global application.
|
---|
156 | */
|
---|
157 | public static volatile Main main;
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Command-line arguments used to run the application.
|
---|
161 | */
|
---|
162 | protected static final List<String> COMMAND_LINE_ARGS = new ArrayList<>();
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * The worker thread slave. This is for executing all long and intensive
|
---|
166 | * calculations. The executed runnables are guaranteed to be executed separately
|
---|
167 | * and sequential.
|
---|
168 | */
|
---|
169 | public static final ExecutorService worker = new ProgressMonitorExecutor();
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Global application preferences
|
---|
173 | */
|
---|
174 | public static Preferences pref;
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * The global paste buffer.
|
---|
178 | */
|
---|
179 | public static final PrimitiveDeepCopy pasteBuffer = new PrimitiveDeepCopy();
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * The layer source from which {@link Main#pasteBuffer} data comes from.
|
---|
183 | */
|
---|
184 | public static Layer pasteSource;
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * The MapFrame. Use {@link Main#setMapFrame} to set or clear it.
|
---|
188 | */
|
---|
189 | public static MapFrame map;
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * The toolbar preference control to register new actions.
|
---|
193 | */
|
---|
194 | public static volatile ToolbarPreferences toolbar;
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * The commands undo/redo handler.
|
---|
198 | */
|
---|
199 | public final UndoRedoHandler undoRedo = new UndoRedoHandler();
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * The progress monitor being currently displayed.
|
---|
203 | */
|
---|
204 | public static PleaseWaitProgressMonitor currentProgressMonitor;
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * The main menu bar at top of screen.
|
---|
208 | */
|
---|
209 | public MainMenu menu;
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * The data validation handler.
|
---|
213 | */
|
---|
214 | public OsmValidator validator;
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * The file watcher service.
|
---|
218 | */
|
---|
219 | public static final FileWatcher fileWatcher = new FileWatcher();
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * The MOTD Layer.
|
---|
223 | */
|
---|
224 | public final GettingStarted gettingStarted = new GettingStarted();
|
---|
225 |
|
---|
226 | private static final Collection<MapFrameListener> mapFrameListeners = new ArrayList<>();
|
---|
227 |
|
---|
228 | protected static final Map<String, Throwable> NETWORK_ERRORS = new HashMap<>();
|
---|
229 |
|
---|
230 | // First lines of last 5 error and warning messages, used for bug reports
|
---|
231 | private static final List<String> ERRORS_AND_WARNINGS = Collections.<String>synchronizedList(new ArrayList<String>());
|
---|
232 |
|
---|
233 | private static final Set<OnlineResource> OFFLINE_RESOURCES = new HashSet<>();
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Logging level (5 = trace, 4 = debug, 3 = info, 2 = warn, 1 = error, 0 = none).
|
---|
237 | * @since 6248
|
---|
238 | */
|
---|
239 | public static int logLevel = 3;
|
---|
240 |
|
---|
241 | private static void rememberWarnErrorMsg(String msg) {
|
---|
242 | // Only remember first line of message
|
---|
243 | int idx = msg.indexOf('\n');
|
---|
244 | if (idx > 0) {
|
---|
245 | ERRORS_AND_WARNINGS.add(msg.substring(0, idx));
|
---|
246 | } else {
|
---|
247 | ERRORS_AND_WARNINGS.add(msg);
|
---|
248 | }
|
---|
249 | // Only keep 5 lines to avoid memory leak and incomplete stacktraces in bug reports
|
---|
250 | while (ERRORS_AND_WARNINGS.size() > 5) {
|
---|
251 | ERRORS_AND_WARNINGS.remove(0);
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Replies the first lines of last 10 error and warning messages, used for bug reports
|
---|
257 | * @return the first lines of last 10 error and warning messages
|
---|
258 | * @since 7420
|
---|
259 | */
|
---|
260 | public static final Collection<String> getLastErrorAndWarnings() {
|
---|
261 | return Collections.unmodifiableList(ERRORS_AND_WARNINGS);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Prints an error message if logging is on.
|
---|
266 | * @param msg The message to print.
|
---|
267 | * @since 6248
|
---|
268 | */
|
---|
269 | public static void error(String msg) {
|
---|
270 | if (logLevel < 1)
|
---|
271 | return;
|
---|
272 | if (msg != null && !msg.isEmpty()) {
|
---|
273 | System.err.println(tr("ERROR: {0}", msg));
|
---|
274 | rememberWarnErrorMsg("E: "+msg);
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Prints a warning message if logging is on.
|
---|
280 | * @param msg The message to print.
|
---|
281 | */
|
---|
282 | public static void warn(String msg) {
|
---|
283 | if (logLevel < 2)
|
---|
284 | return;
|
---|
285 | if (msg != null && !msg.isEmpty()) {
|
---|
286 | System.err.println(tr("WARNING: {0}", msg));
|
---|
287 | rememberWarnErrorMsg("W: "+msg);
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Prints an informational message if logging is on.
|
---|
293 | * @param msg The message to print.
|
---|
294 | */
|
---|
295 | public static void info(String msg) {
|
---|
296 | if (logLevel < 3)
|
---|
297 | return;
|
---|
298 | if (msg != null && !msg.isEmpty()) {
|
---|
299 | System.out.println(tr("INFO: {0}", msg));
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Prints a debug message if logging is on.
|
---|
305 | * @param msg The message to print.
|
---|
306 | */
|
---|
307 | public static void debug(String msg) {
|
---|
308 | if (logLevel < 4)
|
---|
309 | return;
|
---|
310 | if (msg != null && !msg.isEmpty()) {
|
---|
311 | System.out.println(tr("DEBUG: {0}", msg));
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Prints a trace message if logging is on.
|
---|
317 | * @param msg The message to print.
|
---|
318 | */
|
---|
319 | public static void trace(String msg) {
|
---|
320 | if (logLevel < 5)
|
---|
321 | return;
|
---|
322 | if (msg != null && !msg.isEmpty()) {
|
---|
323 | System.out.print("TRACE: ");
|
---|
324 | System.out.println(msg);
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Determines if debug log level is enabled.
|
---|
330 | * Useful to avoid costly construction of debug messages when not enabled.
|
---|
331 | * @return {@code true} if log level is at least debug, {@code false} otherwise
|
---|
332 | * @since 6852
|
---|
333 | */
|
---|
334 | public static boolean isDebugEnabled() {
|
---|
335 | return logLevel >= 4;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Determines if trace log level is enabled.
|
---|
340 | * Useful to avoid costly construction of trace messages when not enabled.
|
---|
341 | * @return {@code true} if log level is at least trace, {@code false} otherwise
|
---|
342 | * @since 6852
|
---|
343 | */
|
---|
344 | public static boolean isTraceEnabled() {
|
---|
345 | return logLevel >= 5;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Prints a formatted error message if logging is on. Calls {@link MessageFormat#format}
|
---|
350 | * function to format text.
|
---|
351 | * @param msg The formatted message to print.
|
---|
352 | * @param objects The objects to insert into format string.
|
---|
353 | * @since 6248
|
---|
354 | */
|
---|
355 | public static void error(String msg, Object... objects) {
|
---|
356 | error(MessageFormat.format(msg, objects));
|
---|
357 | }
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Prints a formatted warning message if logging is on. Calls {@link MessageFormat#format}
|
---|
361 | * function to format text.
|
---|
362 | * @param msg The formatted message to print.
|
---|
363 | * @param objects The objects to insert into format string.
|
---|
364 | */
|
---|
365 | public static void warn(String msg, Object... objects) {
|
---|
366 | warn(MessageFormat.format(msg, objects));
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Prints a formatted informational message if logging is on. Calls {@link MessageFormat#format}
|
---|
371 | * function to format text.
|
---|
372 | * @param msg The formatted message to print.
|
---|
373 | * @param objects The objects to insert into format string.
|
---|
374 | */
|
---|
375 | public static void info(String msg, Object... objects) {
|
---|
376 | info(MessageFormat.format(msg, objects));
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Prints a formatted debug message if logging is on. Calls {@link MessageFormat#format}
|
---|
381 | * function to format text.
|
---|
382 | * @param msg The formatted message to print.
|
---|
383 | * @param objects The objects to insert into format string.
|
---|
384 | */
|
---|
385 | public static void debug(String msg, Object... objects) {
|
---|
386 | debug(MessageFormat.format(msg, objects));
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Prints a formatted trace message if logging is on. Calls {@link MessageFormat#format}
|
---|
391 | * function to format text.
|
---|
392 | * @param msg The formatted message to print.
|
---|
393 | * @param objects The objects to insert into format string.
|
---|
394 | */
|
---|
395 | public static void trace(String msg, Object... objects) {
|
---|
396 | trace(MessageFormat.format(msg, objects));
|
---|
397 | }
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * Prints an error message for the given Throwable.
|
---|
401 | * @param t The throwable object causing the error
|
---|
402 | * @since 6248
|
---|
403 | */
|
---|
404 | public static void error(Throwable t) {
|
---|
405 | error(t, true);
|
---|
406 | }
|
---|
407 |
|
---|
408 | /**
|
---|
409 | * Prints a warning message for the given Throwable.
|
---|
410 | * @param t The throwable object causing the error
|
---|
411 | * @since 6248
|
---|
412 | */
|
---|
413 | public static void warn(Throwable t) {
|
---|
414 | warn(t, true);
|
---|
415 | }
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Prints an error message for the given Throwable.
|
---|
419 | * @param t The throwable object causing the error
|
---|
420 | * @param stackTrace {@code true}, if the stacktrace should be displayed
|
---|
421 | * @since 6642
|
---|
422 | */
|
---|
423 | public static void error(Throwable t, boolean stackTrace) {
|
---|
424 | error(getErrorMessage(t));
|
---|
425 | if (stackTrace) {
|
---|
426 | t.printStackTrace();
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Prints a warning message for the given Throwable.
|
---|
432 | * @param t The throwable object causing the error
|
---|
433 | * @param stackTrace {@code true}, if the stacktrace should be displayed
|
---|
434 | * @since 6642
|
---|
435 | */
|
---|
436 | public static void warn(Throwable t, boolean stackTrace) {
|
---|
437 | warn(getErrorMessage(t));
|
---|
438 | if (stackTrace) {
|
---|
439 | t.printStackTrace();
|
---|
440 | }
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Returns a human-readable message of error, also usable for developers.
|
---|
445 | * @param t The error
|
---|
446 | * @return The human-readable error message
|
---|
447 | * @since 6642
|
---|
448 | */
|
---|
449 | public static String getErrorMessage(Throwable t) {
|
---|
450 | if (t == null) {
|
---|
451 | return null;
|
---|
452 | }
|
---|
453 | StringBuilder sb = new StringBuilder(t.getClass().getName());
|
---|
454 | String msg = t.getMessage();
|
---|
455 | if (msg != null) {
|
---|
456 | sb.append(": ").append(msg.trim());
|
---|
457 | }
|
---|
458 | Throwable cause = t.getCause();
|
---|
459 | if (cause != null && !cause.equals(t)) {
|
---|
460 | sb.append(". ").append(tr("Cause: ")).append(getErrorMessage(cause));
|
---|
461 | }
|
---|
462 | return sb.toString();
|
---|
463 | }
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Platform specific code goes in here.
|
---|
467 | * Plugins may replace it, however, some hooks will be called before any plugins have been loeaded.
|
---|
468 | * So if you need to hook into those early ones, split your class and send the one with the early hooks
|
---|
469 | * to the JOSM team for inclusion.
|
---|
470 | */
|
---|
471 | public static volatile PlatformHook platform;
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Whether or not the java vm is openjdk
|
---|
475 | * We use this to work around openjdk bugs
|
---|
476 | */
|
---|
477 | public static boolean isOpenjdk;
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Initializes {@code Main.pref} in normal application context.
|
---|
481 | * @since 6471
|
---|
482 | */
|
---|
483 | public static void initApplicationPreferences() {
|
---|
484 | Main.pref = new Preferences();
|
---|
485 | }
|
---|
486 |
|
---|
487 | /**
|
---|
488 | * Set or clear (if passed <code>null</code>) the map.
|
---|
489 | * @param map The map to set {@link Main#map} to. Can be null.
|
---|
490 | */
|
---|
491 | public final void setMapFrame(final MapFrame map) {
|
---|
492 | MapFrame old = Main.map;
|
---|
493 | panel.setVisible(false);
|
---|
494 | panel.removeAll();
|
---|
495 | if (map != null) {
|
---|
496 | map.fillPanel(panel);
|
---|
497 | } else {
|
---|
498 | old.destroy();
|
---|
499 | panel.add(gettingStarted, BorderLayout.CENTER);
|
---|
500 | }
|
---|
501 | panel.setVisible(true);
|
---|
502 | redoUndoListener.commandChanged(0,0);
|
---|
503 |
|
---|
504 | Main.map = map;
|
---|
505 |
|
---|
506 | for (MapFrameListener listener : mapFrameListeners ) {
|
---|
507 | listener.mapFrameInitialized(old, map);
|
---|
508 | }
|
---|
509 | if (map == null && currentProgressMonitor != null) {
|
---|
510 | currentProgressMonitor.showForegroundDialog();
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 | /**
|
---|
515 | * Remove the specified layer from the map. If it is the last layer,
|
---|
516 | * remove the map as well.
|
---|
517 | * @param layer The layer to remove
|
---|
518 | */
|
---|
519 | public final synchronized void removeLayer(final Layer layer) {
|
---|
520 | if (map != null) {
|
---|
521 | map.mapView.removeLayer(layer);
|
---|
522 | if (isDisplayingMapView() && map.mapView.getAllLayers().isEmpty()) {
|
---|
523 | setMapFrame(null);
|
---|
524 | }
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | private static volatile InitStatusListener initListener = null;
|
---|
529 |
|
---|
530 | public static interface InitStatusListener {
|
---|
531 |
|
---|
532 | void updateStatus(String event);
|
---|
533 | }
|
---|
534 |
|
---|
535 | public static void setInitStatusListener(InitStatusListener listener) {
|
---|
536 | initListener = listener;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Constructs new {@code Main} object. A lot of global variables are initialized here.
|
---|
541 | */
|
---|
542 | public Main() {
|
---|
543 | main = this;
|
---|
544 | isOpenjdk = System.getProperty("java.vm.name").toUpperCase().indexOf("OPENJDK") != -1;
|
---|
545 |
|
---|
546 | if (initListener != null) {
|
---|
547 | initListener.updateStatus(tr("Executing platform startup hook"));
|
---|
548 | }
|
---|
549 | platform.startupHook();
|
---|
550 |
|
---|
551 | if (initListener != null) {
|
---|
552 | initListener.updateStatus(tr("Building main menu"));
|
---|
553 | }
|
---|
554 | contentPanePrivate.add(panel, BorderLayout.CENTER);
|
---|
555 | panel.add(gettingStarted, BorderLayout.CENTER);
|
---|
556 | menu = new MainMenu();
|
---|
557 |
|
---|
558 | undoRedo.addCommandQueueListener(redoUndoListener);
|
---|
559 |
|
---|
560 | // creating toolbar
|
---|
561 | contentPanePrivate.add(toolbar.control, BorderLayout.NORTH);
|
---|
562 |
|
---|
563 | registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"),
|
---|
564 | KeyEvent.VK_F1, Shortcut.DIRECT));
|
---|
565 |
|
---|
566 | // contains several initialization tasks to be executed (in parallel) by a ExecutorService
|
---|
567 | List<Callable<Void>> tasks = new ArrayList<>();
|
---|
568 |
|
---|
569 | tasks.add(new InitializationTask(tr("Initializing OSM API")) {
|
---|
570 |
|
---|
571 | @Override
|
---|
572 | public void initialize() throws Exception {
|
---|
573 | // We try to establish an API connection early, so that any API
|
---|
574 | // capabilities are already known to the editor instance. However
|
---|
575 | // if it goes wrong that's not critical at this stage.
|
---|
576 | try {
|
---|
577 | OsmApi.getOsmApi().initialize(null, true);
|
---|
578 | } catch (Exception e) {
|
---|
579 | Main.warn(getErrorMessage(Utils.getRootCause(e)));
|
---|
580 | }
|
---|
581 | }
|
---|
582 | });
|
---|
583 |
|
---|
584 | tasks.add(new InitializationTask(tr("Initializing validator")) {
|
---|
585 |
|
---|
586 | @Override
|
---|
587 | public void initialize() throws Exception {
|
---|
588 | validator = new OsmValidator();
|
---|
589 | MapView.addLayerChangeListener(validator);
|
---|
590 | }
|
---|
591 | });
|
---|
592 |
|
---|
593 | tasks.add(new InitializationTask(tr("Initializing presets")) {
|
---|
594 |
|
---|
595 | @Override
|
---|
596 | public void initialize() throws Exception {
|
---|
597 | TaggingPresets.initialize();
|
---|
598 | }
|
---|
599 | });
|
---|
600 |
|
---|
601 | tasks.add(new InitializationTask(tr("Initializing map styles")) {
|
---|
602 |
|
---|
603 | @Override
|
---|
604 | public void initialize() throws Exception {
|
---|
605 | MapPaintPreference.initialize();
|
---|
606 | }
|
---|
607 | });
|
---|
608 |
|
---|
609 | tasks.add(new InitializationTask(tr("Loading imagery preferences")) {
|
---|
610 |
|
---|
611 | @Override
|
---|
612 | public void initialize() throws Exception {
|
---|
613 | ImageryPreference.initialize();
|
---|
614 | }
|
---|
615 | });
|
---|
616 |
|
---|
617 | try {
|
---|
618 | for (Future<Void> i : Executors.newFixedThreadPool(
|
---|
619 | Runtime.getRuntime().availableProcessors()).invokeAll(tasks)) {
|
---|
620 | i.get();
|
---|
621 | }
|
---|
622 | } catch (Exception ex) {
|
---|
623 | throw new RuntimeException(ex);
|
---|
624 | }
|
---|
625 |
|
---|
626 | // hooks for the jmapviewer component
|
---|
627 | FeatureAdapter.registerBrowserAdapter(new FeatureAdapter.BrowserAdapter() {
|
---|
628 | @Override
|
---|
629 | public void openLink(String url) {
|
---|
630 | OpenBrowser.displayUrl(url);
|
---|
631 | }
|
---|
632 | });
|
---|
633 | FeatureAdapter.registerTranslationAdapter(I18n.getTranslationAdapter());
|
---|
634 | FeatureAdapter.registerLoggingAdapter(new FeatureAdapter.LoggingAdapter() {
|
---|
635 | @Override
|
---|
636 | public Logger getLogger(String name) {
|
---|
637 | Logger logger = Logger.getAnonymousLogger();
|
---|
638 | logger.setUseParentHandlers(false);
|
---|
639 | logger.setLevel(Level.ALL);
|
---|
640 | if (logger.getHandlers().length == 0) {
|
---|
641 | logger.addHandler(new Handler() {
|
---|
642 | @Override
|
---|
643 | public void publish(LogRecord record) {
|
---|
644 | String msg = MessageFormat.format(record.getMessage(), record.getParameters());
|
---|
645 | if (record.getLevel().intValue() >= Level.SEVERE.intValue()) {
|
---|
646 | Main.error(msg);
|
---|
647 | } else if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
|
---|
648 | Main.warn(msg);
|
---|
649 | } else if (record.getLevel().intValue() >= Level.INFO.intValue()) {
|
---|
650 | Main.info(msg);
|
---|
651 | } else if (record.getLevel().intValue() >= Level.FINE.intValue()) {
|
---|
652 | Main.debug(msg);
|
---|
653 | } else {
|
---|
654 | Main.trace(msg);
|
---|
655 | }
|
---|
656 | }
|
---|
657 |
|
---|
658 | @Override
|
---|
659 | public void flush() {
|
---|
660 | }
|
---|
661 |
|
---|
662 | @Override
|
---|
663 | public void close() {
|
---|
664 | }
|
---|
665 | });
|
---|
666 | }
|
---|
667 | return logger;
|
---|
668 | }
|
---|
669 | });
|
---|
670 |
|
---|
671 | if (initListener != null) {
|
---|
672 | initListener.updateStatus(tr("Updating user interface"));
|
---|
673 | }
|
---|
674 |
|
---|
675 | toolbar.refreshToolbarControl();
|
---|
676 |
|
---|
677 | toolbar.control.updateUI();
|
---|
678 | contentPanePrivate.updateUI();
|
---|
679 | }
|
---|
680 |
|
---|
681 | private abstract class InitializationTask implements Callable<Void> {
|
---|
682 |
|
---|
683 | private final String name;
|
---|
684 |
|
---|
685 | protected InitializationTask(String name) {
|
---|
686 | this.name = name;
|
---|
687 | }
|
---|
688 |
|
---|
689 | public abstract void initialize() throws Exception;
|
---|
690 |
|
---|
691 | @Override
|
---|
692 | public Void call() throws Exception {
|
---|
693 | if (initListener != null) {
|
---|
694 | initListener.updateStatus(name);
|
---|
695 | }
|
---|
696 | final long startTime = System.currentTimeMillis();
|
---|
697 | initialize();
|
---|
698 | if (isDebugEnabled()) {
|
---|
699 | final long elapsedTime = System.currentTimeMillis() - startTime;
|
---|
700 | Main.debug(tr("{0} completed in {1}", name, Utils.getDurationString(elapsedTime)));
|
---|
701 | }
|
---|
702 | return null;
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | /**
|
---|
707 | * Add a new layer to the map.
|
---|
708 | *
|
---|
709 | * If no map exists, create one.
|
---|
710 | *
|
---|
711 | * @param layer the layer
|
---|
712 | *
|
---|
713 | * @see #addLayer(Layer, ProjectionBounds)
|
---|
714 | * @see #addLayer(Layer, ViewportData)
|
---|
715 | */
|
---|
716 | public final void addLayer(final Layer layer) {
|
---|
717 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
718 | layer.visitBoundingBox(v);
|
---|
719 | addLayer(layer, v.getBounds());
|
---|
720 | }
|
---|
721 |
|
---|
722 | /**
|
---|
723 | * Add a new layer to the map.
|
---|
724 | *
|
---|
725 | * If no map exists, create one.
|
---|
726 | *
|
---|
727 | * @param layer the layer
|
---|
728 | * @param bounds the bounds of the layer (target zoom area); can be null, then
|
---|
729 | * the viewport isn't changed
|
---|
730 | */
|
---|
731 | public final synchronized void addLayer(final Layer layer, ProjectionBounds bounds) {
|
---|
732 | addLayer(layer, bounds == null ? null : new ViewportData(bounds));
|
---|
733 | }
|
---|
734 |
|
---|
735 | /**
|
---|
736 | * Add a new layer to the map.
|
---|
737 | *
|
---|
738 | * If no map exists, create one.
|
---|
739 | *
|
---|
740 | * @param layer the layer
|
---|
741 | * @param viewport the viewport to zoom to; can be null, then the viewport
|
---|
742 | * isn't changed
|
---|
743 | */
|
---|
744 | public final synchronized void addLayer(final Layer layer, ViewportData viewport) {
|
---|
745 | boolean noMap = map == null;
|
---|
746 | if (noMap) {
|
---|
747 | createMapFrame(layer, viewport);
|
---|
748 | }
|
---|
749 | layer.hookUpMapView();
|
---|
750 | map.mapView.addLayer(layer);
|
---|
751 | if (noMap) {
|
---|
752 | Main.map.setVisible(true);
|
---|
753 | } else if (viewport != null) {
|
---|
754 | Main.map.mapView.zoomTo(viewport);
|
---|
755 | }
|
---|
756 | }
|
---|
757 |
|
---|
758 | public synchronized void createMapFrame(Layer firstLayer, ViewportData viewportData) {
|
---|
759 | MapFrame mapFrame = new MapFrame(contentPanePrivate, viewportData);
|
---|
760 | setMapFrame(mapFrame);
|
---|
761 | if (firstLayer != null) {
|
---|
762 | mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction(), firstLayer);
|
---|
763 | }
|
---|
764 | mapFrame.initializeDialogsPane();
|
---|
765 | // bootstrapping problem: make sure the layer list dialog is going to
|
---|
766 | // listen to change events of the very first layer
|
---|
767 | //
|
---|
768 | if (firstLayer != null) {
|
---|
769 | firstLayer.addPropertyChangeListener(LayerListDialog.getInstance().getModel());
|
---|
770 | }
|
---|
771 | }
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * Replies <code>true</code> if there is an edit layer
|
---|
775 | *
|
---|
776 | * @return <code>true</code> if there is an edit layer
|
---|
777 | */
|
---|
778 | public boolean hasEditLayer() {
|
---|
779 | if (getEditLayer() == null) return false;
|
---|
780 | return true;
|
---|
781 | }
|
---|
782 |
|
---|
783 | /**
|
---|
784 | * Replies the current edit layer
|
---|
785 | *
|
---|
786 | * @return the current edit layer. <code>null</code>, if no current edit layer exists
|
---|
787 | */
|
---|
788 | public OsmDataLayer getEditLayer() {
|
---|
789 | if (!isDisplayingMapView()) return null;
|
---|
790 | return map.mapView.getEditLayer();
|
---|
791 | }
|
---|
792 |
|
---|
793 | /**
|
---|
794 | * Replies the current data set.
|
---|
795 | *
|
---|
796 | * @return the current data set. <code>null</code>, if no current data set exists
|
---|
797 | */
|
---|
798 | public DataSet getCurrentDataSet() {
|
---|
799 | if (!hasEditLayer()) return null;
|
---|
800 | return getEditLayer().data;
|
---|
801 | }
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * Replies the current selected primitives, from a end-user point of view.
|
---|
805 | * It is not always technically the same collection of primitives than {@link DataSet#getSelected()}.
|
---|
806 | * Indeed, if the user is currently in drawing mode, only the way currently being drawn is returned,
|
---|
807 | * see {@link DrawAction#getInProgressSelection()}.
|
---|
808 | *
|
---|
809 | * @return The current selected primitives, from a end-user point of view. Can be {@code null}.
|
---|
810 | * @since 6546
|
---|
811 | */
|
---|
812 | public Collection<OsmPrimitive> getInProgressSelection() {
|
---|
813 | if (map != null && map.mapMode instanceof DrawAction) {
|
---|
814 | return ((DrawAction) map.mapMode).getInProgressSelection();
|
---|
815 | } else {
|
---|
816 | DataSet ds = getCurrentDataSet();
|
---|
817 | if (ds == null) return null;
|
---|
818 | return ds.getSelected();
|
---|
819 | }
|
---|
820 | }
|
---|
821 |
|
---|
822 | /**
|
---|
823 | * Returns the currently active layer
|
---|
824 | *
|
---|
825 | * @return the currently active layer. <code>null</code>, if currently no active layer exists
|
---|
826 | */
|
---|
827 | public Layer getActiveLayer() {
|
---|
828 | if (!isDisplayingMapView()) return null;
|
---|
829 | return map.mapView.getActiveLayer();
|
---|
830 | }
|
---|
831 |
|
---|
832 | protected static final JPanel contentPanePrivate = new JPanel(new BorderLayout());
|
---|
833 |
|
---|
834 | public static void redirectToMainContentPane(JComponent source) {
|
---|
835 | RedirectInputMap.redirect(source, contentPanePrivate);
|
---|
836 | }
|
---|
837 |
|
---|
838 | public static void registerActionShortcut(JosmAction action) {
|
---|
839 | registerActionShortcut(action, action.getShortcut());
|
---|
840 | }
|
---|
841 |
|
---|
842 | public static void registerActionShortcut(Action action, Shortcut shortcut) {
|
---|
843 | KeyStroke keyStroke = shortcut.getKeyStroke();
|
---|
844 | if (keyStroke == null)
|
---|
845 | return;
|
---|
846 |
|
---|
847 | InputMap inputMap = contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
|
---|
848 | Object existing = inputMap.get(keyStroke);
|
---|
849 | if (existing != null && !existing.equals(action)) {
|
---|
850 | info(String.format("Keystroke %s is already assigned to %s, will be overridden by %s", keyStroke, existing, action));
|
---|
851 | }
|
---|
852 | inputMap.put(keyStroke, action);
|
---|
853 |
|
---|
854 | contentPanePrivate.getActionMap().put(action, action);
|
---|
855 | }
|
---|
856 |
|
---|
857 | public static void unregisterShortcut(Shortcut shortcut) {
|
---|
858 | contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortcut.getKeyStroke());
|
---|
859 | }
|
---|
860 |
|
---|
861 | public static void unregisterActionShortcut(JosmAction action) {
|
---|
862 | unregisterActionShortcut(action, action.getShortcut());
|
---|
863 | }
|
---|
864 |
|
---|
865 | public static void unregisterActionShortcut(Action action, Shortcut shortcut) {
|
---|
866 | unregisterShortcut(shortcut);
|
---|
867 | contentPanePrivate.getActionMap().remove(action);
|
---|
868 | }
|
---|
869 |
|
---|
870 | /**
|
---|
871 | * Replies the registered action for the given shortcut
|
---|
872 | * @param shortcut The shortcut to look for
|
---|
873 | * @return the registered action for the given shortcut
|
---|
874 | * @since 5696
|
---|
875 | */
|
---|
876 | public static Action getRegisteredActionShortcut(Shortcut shortcut) {
|
---|
877 | KeyStroke keyStroke = shortcut.getKeyStroke();
|
---|
878 | if (keyStroke == null)
|
---|
879 | return null;
|
---|
880 | Object action = contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).get(keyStroke);
|
---|
881 | if (action instanceof Action)
|
---|
882 | return (Action) action;
|
---|
883 | return null;
|
---|
884 | }
|
---|
885 |
|
---|
886 | ///////////////////////////////////////////////////////////////////////////
|
---|
887 | // Implementation part
|
---|
888 | ///////////////////////////////////////////////////////////////////////////
|
---|
889 |
|
---|
890 | /**
|
---|
891 | * Global panel.
|
---|
892 | */
|
---|
893 | public static final JPanel panel = new JPanel(new BorderLayout());
|
---|
894 |
|
---|
895 | protected static volatile WindowGeometry geometry;
|
---|
896 | protected static int windowState = JFrame.NORMAL;
|
---|
897 |
|
---|
898 | private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
|
---|
899 | @Override
|
---|
900 | public void commandChanged(final int queueSize, final int redoSize) {
|
---|
901 | menu.undo.setEnabled(queueSize > 0);
|
---|
902 | menu.redo.setEnabled(redoSize > 0);
|
---|
903 | }
|
---|
904 | };
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * Should be called before the main constructor to setup some parameter stuff
|
---|
908 | * @param args The parsed argument list.
|
---|
909 | */
|
---|
910 | public static void preConstructorInit(Map<Option, Collection<String>> args) {
|
---|
911 | ProjectionPreference.setProjection();
|
---|
912 |
|
---|
913 | try {
|
---|
914 | String defaultlaf = platform.getDefaultStyle();
|
---|
915 | String laf = Main.pref.get("laf", defaultlaf);
|
---|
916 | try {
|
---|
917 | UIManager.setLookAndFeel(laf);
|
---|
918 | } catch (final NoClassDefFoundError | ClassNotFoundException e) {
|
---|
919 | // Try to find look and feel in plugin classloaders
|
---|
920 | Class<?> klass = null;
|
---|
921 | for (ClassLoader cl : PluginHandler.getResourceClassLoaders()) {
|
---|
922 | try {
|
---|
923 | klass = cl.loadClass(laf);
|
---|
924 | break;
|
---|
925 | } catch (ClassNotFoundException ex) {
|
---|
926 | // Do nothing
|
---|
927 | }
|
---|
928 | }
|
---|
929 | if (klass != null && LookAndFeel.class.isAssignableFrom(klass)) {
|
---|
930 | try {
|
---|
931 | UIManager.setLookAndFeel((LookAndFeel) klass.newInstance());
|
---|
932 | } catch (Exception ex) {
|
---|
933 | warn("Cannot set Look and Feel: " + laf + ": "+ex.getMessage());
|
---|
934 | }
|
---|
935 | } else {
|
---|
936 | info("Look and Feel not found: " + laf);
|
---|
937 | Main.pref.put("laf", defaultlaf);
|
---|
938 | }
|
---|
939 | } catch (final UnsupportedLookAndFeelException e) {
|
---|
940 | info("Look and Feel not supported: " + laf);
|
---|
941 | Main.pref.put("laf", defaultlaf);
|
---|
942 | }
|
---|
943 | toolbar = new ToolbarPreferences();
|
---|
944 | contentPanePrivate.updateUI();
|
---|
945 | panel.updateUI();
|
---|
946 | } catch (final Exception e) {
|
---|
947 | error(e);
|
---|
948 | }
|
---|
949 | UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
|
---|
950 | UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
|
---|
951 | UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
|
---|
952 | UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
|
---|
953 |
|
---|
954 | I18n.translateJavaInternalMessages();
|
---|
955 |
|
---|
956 | // init default coordinate format
|
---|
957 | //
|
---|
958 | try {
|
---|
959 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.valueOf(Main.pref.get("coordinates")));
|
---|
960 | } catch (IllegalArgumentException iae) {
|
---|
961 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.DECIMAL_DEGREES);
|
---|
962 | }
|
---|
963 |
|
---|
964 | geometry = WindowGeometry.mainWindow("gui.geometry",
|
---|
965 | args.containsKey(Option.GEOMETRY) ? args.get(Option.GEOMETRY).iterator().next() : null,
|
---|
966 | !args.containsKey(Option.NO_MAXIMIZE) && Main.pref.getBoolean("gui.maximized", false));
|
---|
967 | }
|
---|
968 |
|
---|
969 | protected static void postConstructorProcessCmdLine(Map<Option, Collection<String>> args) {
|
---|
970 | if (args.containsKey(Option.DOWNLOAD)) {
|
---|
971 | List<File> fileList = new ArrayList<>();
|
---|
972 | for (String s : args.get(Option.DOWNLOAD)) {
|
---|
973 | File f = null;
|
---|
974 | switch(paramType(s)) {
|
---|
975 | case httpUrl:
|
---|
976 | downloadFromParamHttp(false, s);
|
---|
977 | break;
|
---|
978 | case bounds:
|
---|
979 | downloadFromParamBounds(false, s);
|
---|
980 | break;
|
---|
981 | case fileUrl:
|
---|
982 | try {
|
---|
983 | f = new File(new URI(s));
|
---|
984 | } catch (URISyntaxException e) {
|
---|
985 | JOptionPane.showMessageDialog(
|
---|
986 | Main.parent,
|
---|
987 | tr("Ignoring malformed file URL: \"{0}\"", s),
|
---|
988 | tr("Warning"),
|
---|
989 | JOptionPane.WARNING_MESSAGE
|
---|
990 | );
|
---|
991 | }
|
---|
992 | if (f!=null) {
|
---|
993 | fileList.add(f);
|
---|
994 | }
|
---|
995 | break;
|
---|
996 | case fileName:
|
---|
997 | f = new File(s);
|
---|
998 | fileList.add(f);
|
---|
999 | break;
|
---|
1000 | }
|
---|
1001 | }
|
---|
1002 | if(!fileList.isEmpty()) {
|
---|
1003 | OpenFileAction.openFiles(fileList, true);
|
---|
1004 | }
|
---|
1005 | }
|
---|
1006 | if (args.containsKey(Option.DOWNLOADGPS)) {
|
---|
1007 | for (String s : args.get(Option.DOWNLOADGPS)) {
|
---|
1008 | switch(paramType(s)) {
|
---|
1009 | case httpUrl:
|
---|
1010 | downloadFromParamHttp(true, s);
|
---|
1011 | break;
|
---|
1012 | case bounds:
|
---|
1013 | downloadFromParamBounds(true, s);
|
---|
1014 | break;
|
---|
1015 | case fileUrl:
|
---|
1016 | case fileName:
|
---|
1017 | JOptionPane.showMessageDialog(
|
---|
1018 | Main.parent,
|
---|
1019 | tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
|
---|
1020 | tr("Warning"),
|
---|
1021 | JOptionPane.WARNING_MESSAGE
|
---|
1022 | );
|
---|
1023 | }
|
---|
1024 | }
|
---|
1025 | }
|
---|
1026 | if (args.containsKey(Option.SELECTION)) {
|
---|
1027 | for (String s : args.get(Option.SELECTION)) {
|
---|
1028 | SearchAction.search(s, SearchAction.SearchMode.add);
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | /**
|
---|
1034 | * Asks user to perform "save layer" operations (save on disk and/or upload data to server) for all {@link AbstractModifiableLayer} before JOSM exits.
|
---|
1035 | * @return {@code true} if there was nothing to save, or if the user wants to proceed to save operations. {@code false} if the user cancels.
|
---|
1036 | * @since 2025
|
---|
1037 | */
|
---|
1038 | public static boolean saveUnsavedModifications() {
|
---|
1039 | if (!isDisplayingMapView()) return true;
|
---|
1040 | return saveUnsavedModifications(map.mapView.getLayersOfType(AbstractModifiableLayer.class), true);
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | /**
|
---|
1044 | * Asks user to perform "save layer" operations (save on disk and/or upload data to server) before data layers deletion.
|
---|
1045 | *
|
---|
1046 | * @param selectedLayers The layers to check. Only instances of {@link AbstractModifiableLayer} are considered.
|
---|
1047 | * @param exit {@code true} if JOSM is exiting, {@code false} otherwise.
|
---|
1048 | * @return {@code true} if there was nothing to save, or if the user wants to proceed to save operations. {@code false} if the user cancels.
|
---|
1049 | * @since 5519
|
---|
1050 | */
|
---|
1051 | public static boolean saveUnsavedModifications(Iterable<? extends Layer> selectedLayers, boolean exit) {
|
---|
1052 | SaveLayersDialog dialog = new SaveLayersDialog(parent);
|
---|
1053 | List<AbstractModifiableLayer> layersWithUnmodifiedChanges = new ArrayList<>();
|
---|
1054 | for (Layer l: selectedLayers) {
|
---|
1055 | if (!(l instanceof AbstractModifiableLayer)) {
|
---|
1056 | continue;
|
---|
1057 | }
|
---|
1058 | AbstractModifiableLayer odl = (AbstractModifiableLayer)l;
|
---|
1059 | if ((odl.requiresSaveToFile() || (odl.requiresUploadToServer() && !odl.isUploadDiscouraged())) && odl.isModified()) {
|
---|
1060 | layersWithUnmodifiedChanges.add(odl);
|
---|
1061 | }
|
---|
1062 | }
|
---|
1063 | if (exit) {
|
---|
1064 | dialog.prepareForSavingAndUpdatingLayersBeforeExit();
|
---|
1065 | } else {
|
---|
1066 | dialog.prepareForSavingAndUpdatingLayersBeforeDelete();
|
---|
1067 | }
|
---|
1068 | if (!layersWithUnmodifiedChanges.isEmpty()) {
|
---|
1069 | dialog.getModel().populate(layersWithUnmodifiedChanges);
|
---|
1070 | dialog.setVisible(true);
|
---|
1071 | switch(dialog.getUserAction()) {
|
---|
1072 | case PROCEED: return true;
|
---|
1073 | case CANCEL:
|
---|
1074 | default: return false;
|
---|
1075 | }
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | return true;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | /**
|
---|
1082 | * Closes JOSM and optionally terminates the Java Virtual Machine (JVM). If there are some unsaved data layers, asks first for user confirmation.
|
---|
1083 | * @param exit If {@code true}, the JVM is terminated by running {@link System#exit} with a given return code.
|
---|
1084 | * @param exitCode The return code
|
---|
1085 | * @return {@code true} if JOSM has been closed, {@code false} if the user has cancelled the operation.
|
---|
1086 | * @since 3378
|
---|
1087 | */
|
---|
1088 | public static boolean exitJosm(boolean exit, int exitCode) {
|
---|
1089 | JCSCacheManager.shutdown();
|
---|
1090 | if (Main.saveUnsavedModifications()) {
|
---|
1091 | geometry.remember("gui.geometry");
|
---|
1092 | if (map != null) {
|
---|
1093 | map.rememberToggleDialogWidth();
|
---|
1094 | }
|
---|
1095 | pref.put("gui.maximized", (windowState & JFrame.MAXIMIZED_BOTH) != 0);
|
---|
1096 | // Remove all layers because somebody may rely on layerRemoved events (like AutosaveTask)
|
---|
1097 | if (Main.isDisplayingMapView()) {
|
---|
1098 | Collection<Layer> layers = new ArrayList<>(Main.map.mapView.getAllLayers());
|
---|
1099 | for (Layer l: layers) {
|
---|
1100 | Main.main.removeLayer(l);
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | if (exit) {
|
---|
1104 | System.exit(exitCode);
|
---|
1105 | }
|
---|
1106 | return true;
|
---|
1107 | }
|
---|
1108 | return false;
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | /**
|
---|
1112 | * The type of a command line parameter, to be used in switch statements.
|
---|
1113 | * @see #paramType
|
---|
1114 | */
|
---|
1115 | private enum DownloadParamType { httpUrl, fileUrl, bounds, fileName }
|
---|
1116 |
|
---|
1117 | /**
|
---|
1118 | * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
|
---|
1119 | * @param s A parameter string
|
---|
1120 | * @return The guessed parameter type
|
---|
1121 | */
|
---|
1122 | private static DownloadParamType paramType(String s) {
|
---|
1123 | if(s.startsWith("http:") || s.startsWith("https:")) return DownloadParamType.httpUrl;
|
---|
1124 | if(s.startsWith("file:")) return DownloadParamType.fileUrl;
|
---|
1125 | String coorPattern = "\\s*[+-]?[0-9]+(\\.[0-9]+)?\\s*";
|
---|
1126 | if(s.matches(coorPattern+"(,"+coorPattern+"){3}")) return DownloadParamType.bounds;
|
---|
1127 | // everything else must be a file name
|
---|
1128 | return DownloadParamType.fileName;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | /**
|
---|
1132 | * Download area specified on the command line as OSM URL.
|
---|
1133 | * @param rawGps Flag to download raw GPS tracks
|
---|
1134 | * @param s The URL parameter
|
---|
1135 | */
|
---|
1136 | private static void downloadFromParamHttp(final boolean rawGps, String s) {
|
---|
1137 | final Bounds b = OsmUrlToBounds.parse(s);
|
---|
1138 | if (b == null) {
|
---|
1139 | JOptionPane.showMessageDialog(
|
---|
1140 | Main.parent,
|
---|
1141 | tr("Ignoring malformed URL: \"{0}\"", s),
|
---|
1142 | tr("Warning"),
|
---|
1143 | JOptionPane.WARNING_MESSAGE
|
---|
1144 | );
|
---|
1145 | } else {
|
---|
1146 | downloadFromParamBounds(rawGps, b);
|
---|
1147 | }
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /**
|
---|
1151 | * Download area specified on the command line as bounds string.
|
---|
1152 | * @param rawGps Flag to download raw GPS tracks
|
---|
1153 | * @param s The bounds parameter
|
---|
1154 | */
|
---|
1155 | private static void downloadFromParamBounds(final boolean rawGps, String s) {
|
---|
1156 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
1157 | if (st.countTokens() == 4) {
|
---|
1158 | Bounds b = new Bounds(
|
---|
1159 | new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken())),
|
---|
1160 | new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()))
|
---|
1161 | );
|
---|
1162 | downloadFromParamBounds(rawGps, b);
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | /**
|
---|
1167 | * Download area specified as Bounds value.
|
---|
1168 | * @param rawGps Flag to download raw GPS tracks
|
---|
1169 | * @param b The bounds value
|
---|
1170 | * @see #downloadFromParamBounds(boolean, String)
|
---|
1171 | * @see #downloadFromParamHttp
|
---|
1172 | */
|
---|
1173 | private static void downloadFromParamBounds(final boolean rawGps, Bounds b) {
|
---|
1174 | DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
|
---|
1175 | // asynchronously launch the download task ...
|
---|
1176 | Future<?> future = task.download(true, b, null);
|
---|
1177 | // ... and the continuation when the download is finished (this will wait for the download to finish)
|
---|
1178 | Main.worker.execute(new PostDownloadHandler(task, future));
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | /**
|
---|
1182 | * Identifies the current operating system family and initializes the platform hook accordingly.
|
---|
1183 | * @since 1849
|
---|
1184 | */
|
---|
1185 | public static void determinePlatformHook() {
|
---|
1186 | String os = System.getProperty("os.name");
|
---|
1187 | if (os == null) {
|
---|
1188 | warn("Your operating system has no name, so I'm guessing its some kind of *nix.");
|
---|
1189 | platform = new PlatformHookUnixoid();
|
---|
1190 | } else if (os.toLowerCase().startsWith("windows")) {
|
---|
1191 | platform = new PlatformHookWindows();
|
---|
1192 | } else if ("Linux".equals(os) || "Solaris".equals(os) ||
|
---|
1193 | "SunOS".equals(os) || "AIX".equals(os) ||
|
---|
1194 | "FreeBSD".equals(os) || "NetBSD".equals(os) || "OpenBSD".equals(os)) {
|
---|
1195 | platform = new PlatformHookUnixoid();
|
---|
1196 | } else if (os.toLowerCase().startsWith("mac os x")) {
|
---|
1197 | platform = new PlatformHookOsx();
|
---|
1198 | } else {
|
---|
1199 | warn("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
|
---|
1200 | platform = new PlatformHookUnixoid();
|
---|
1201 | }
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | private static class WindowPositionSizeListener extends WindowAdapter implements
|
---|
1205 | ComponentListener {
|
---|
1206 | @Override
|
---|
1207 | public void windowStateChanged(WindowEvent e) {
|
---|
1208 | Main.windowState = e.getNewState();
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | @Override
|
---|
1212 | public void componentHidden(ComponentEvent e) {
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | @Override
|
---|
1216 | public void componentMoved(ComponentEvent e) {
|
---|
1217 | handleComponentEvent(e);
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | @Override
|
---|
1221 | public void componentResized(ComponentEvent e) {
|
---|
1222 | handleComponentEvent(e);
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | @Override
|
---|
1226 | public void componentShown(ComponentEvent e) {
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | private void handleComponentEvent(ComponentEvent e) {
|
---|
1230 | Component c = e.getComponent();
|
---|
1231 | if (c instanceof JFrame && c.isVisible()) {
|
---|
1232 | if(Main.windowState == JFrame.NORMAL) {
|
---|
1233 | Main.geometry = new WindowGeometry((JFrame) c);
|
---|
1234 | } else {
|
---|
1235 | Main.geometry.fixScreen((JFrame) c);
|
---|
1236 | }
|
---|
1237 | }
|
---|
1238 | }
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | protected static void addListener() {
|
---|
1242 | parent.addComponentListener(new WindowPositionSizeListener());
|
---|
1243 | ((JFrame)parent).addWindowStateListener(new WindowPositionSizeListener());
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | /**
|
---|
1247 | * Determines if JOSM currently runs with Java 8 or later.
|
---|
1248 | * @return {@code true} if the current JVM is at least Java 8, {@code false} otherwise
|
---|
1249 | * @since 7894
|
---|
1250 | */
|
---|
1251 | public static boolean isJava8orLater() {
|
---|
1252 | String version = System.getProperty("java.version");
|
---|
1253 | return version != null && !version.matches("^(1\\.)?[7].*");
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | /**
|
---|
1257 | * Checks that JOSM is at least running with Java 7.
|
---|
1258 | * @since 7001
|
---|
1259 | */
|
---|
1260 | public static void checkJavaVersion() {
|
---|
1261 | String version = System.getProperty("java.version");
|
---|
1262 | if (version != null) {
|
---|
1263 | if (version.matches("^(1\\.)?[789].*"))
|
---|
1264 | return;
|
---|
1265 | if (version.matches("^(1\\.)?[56].*")) {
|
---|
1266 | JMultilineLabel ho = new JMultilineLabel("<html>"+
|
---|
1267 | tr("<h2>JOSM requires Java version {0}.</h2>"+
|
---|
1268 | "Detected Java version: {1}.<br>"+
|
---|
1269 | "You can <ul><li>update your Java (JRE) or</li>"+
|
---|
1270 | "<li>use an earlier (Java {2} compatible) version of JOSM.</li></ul>"+
|
---|
1271 | "More Info:", "7", version, "6")+"</html>");
|
---|
1272 | JTextArea link = new JTextArea(HelpUtil.getWikiBaseHelpUrl()+"/Help/SystemRequirements");
|
---|
1273 | link.setEditable(false);
|
---|
1274 | link.setBackground(panel.getBackground());
|
---|
1275 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
1276 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
1277 | gbc.gridwidth = GridBagConstraints.REMAINDER;
|
---|
1278 | gbc.anchor = GridBagConstraints.WEST;
|
---|
1279 | gbc.weightx = 1.0;
|
---|
1280 | panel.add(ho, gbc);
|
---|
1281 | panel.add(link, gbc);
|
---|
1282 | final String EXIT = tr("Exit JOSM");
|
---|
1283 | final String CONTINUE = tr("Continue, try anyway");
|
---|
1284 | int ret = JOptionPane.showOptionDialog(null, panel, tr("Error"), JOptionPane.YES_NO_OPTION,
|
---|
1285 | JOptionPane.ERROR_MESSAGE, null, new String[] {EXIT, CONTINUE}, EXIT);
|
---|
1286 | if (ret == 0) {
|
---|
1287 | System.exit(0);
|
---|
1288 | }
|
---|
1289 | return;
|
---|
1290 | }
|
---|
1291 | }
|
---|
1292 | error("Could not recognize Java Version: "+version);
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | /* ----------------------------------------------------------------------------------------- */
|
---|
1296 | /* projection handling - Main is a registry for a single, global projection instance */
|
---|
1297 | /* */
|
---|
1298 | /* TODO: For historical reasons the registry is implemented by Main. An alternative approach */
|
---|
1299 | /* would be a singleton org.openstreetmap.josm.data.projection.ProjectionRegistry class. */
|
---|
1300 | /* ----------------------------------------------------------------------------------------- */
|
---|
1301 | /**
|
---|
1302 | * The projection method used.
|
---|
1303 | * use {@link #getProjection()} and {@link #setProjection(Projection)} for access.
|
---|
1304 | * Use {@link #setProjection(Projection)} in order to trigger a projection change event.
|
---|
1305 | */
|
---|
1306 | private static volatile Projection proj;
|
---|
1307 |
|
---|
1308 | /**
|
---|
1309 | * Replies the current projection.
|
---|
1310 | *
|
---|
1311 | * @return the currently active projection
|
---|
1312 | */
|
---|
1313 | public static Projection getProjection() {
|
---|
1314 | return proj;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | /**
|
---|
1318 | * Sets the current projection
|
---|
1319 | *
|
---|
1320 | * @param p the projection
|
---|
1321 | */
|
---|
1322 | public static void setProjection(Projection p) {
|
---|
1323 | CheckParameterUtil.ensureParameterNotNull(p);
|
---|
1324 | Projection oldValue = proj;
|
---|
1325 | Bounds b = isDisplayingMapView() ? map.mapView.getRealBounds() : null;
|
---|
1326 | proj = p;
|
---|
1327 | fireProjectionChanged(oldValue, proj, b);
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | /*
|
---|
1331 | * Keep WeakReferences to the listeners. This relieves clients from the burden of
|
---|
1332 | * explicitly removing the listeners and allows us to transparently register every
|
---|
1333 | * created dataset as projection change listener.
|
---|
1334 | */
|
---|
1335 | private static final List<WeakReference<ProjectionChangeListener>> listeners = new ArrayList<>();
|
---|
1336 |
|
---|
1337 | private static void fireProjectionChanged(Projection oldValue, Projection newValue, Bounds oldBounds) {
|
---|
1338 | if (newValue == null ^ oldValue == null
|
---|
1339 | || (newValue != null && oldValue != null && !Objects.equals(newValue.toCode(), oldValue.toCode()))) {
|
---|
1340 |
|
---|
1341 | synchronized(Main.class) {
|
---|
1342 | Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
|
---|
1343 | while (it.hasNext()){
|
---|
1344 | WeakReference<ProjectionChangeListener> wr = it.next();
|
---|
1345 | ProjectionChangeListener listener = wr.get();
|
---|
1346 | if (listener == null) {
|
---|
1347 | it.remove();
|
---|
1348 | continue;
|
---|
1349 | }
|
---|
1350 | listener.projectionChanged(oldValue, newValue);
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 | if (newValue != null && oldBounds != null) {
|
---|
1354 | Main.map.mapView.zoomTo(oldBounds);
|
---|
1355 | }
|
---|
1356 | /* TODO - remove layers with fixed projection */
|
---|
1357 | }
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | /**
|
---|
1361 | * Register a projection change listener.
|
---|
1362 | *
|
---|
1363 | * @param listener the listener. Ignored if <code>null</code>.
|
---|
1364 | */
|
---|
1365 | public static void addProjectionChangeListener(ProjectionChangeListener listener) {
|
---|
1366 | if (listener == null) return;
|
---|
1367 | synchronized (Main.class) {
|
---|
1368 | for (WeakReference<ProjectionChangeListener> wr : listeners) {
|
---|
1369 | // already registered ? => abort
|
---|
1370 | if (wr.get() == listener) return;
|
---|
1371 | }
|
---|
1372 | listeners.add(new WeakReference<>(listener));
|
---|
1373 | }
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | /**
|
---|
1377 | * Removes a projection change listener.
|
---|
1378 | *
|
---|
1379 | * @param listener the listener. Ignored if <code>null</code>.
|
---|
1380 | */
|
---|
1381 | public static void removeProjectionChangeListener(ProjectionChangeListener listener) {
|
---|
1382 | if (listener == null) return;
|
---|
1383 | synchronized(Main.class){
|
---|
1384 | Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
|
---|
1385 | while (it.hasNext()){
|
---|
1386 | WeakReference<ProjectionChangeListener> wr = it.next();
|
---|
1387 | // remove the listener - and any other listener which got garbage
|
---|
1388 | // collected in the meantime
|
---|
1389 | if (wr.get() == null || wr.get() == listener) {
|
---|
1390 | it.remove();
|
---|
1391 | }
|
---|
1392 | }
|
---|
1393 | }
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | /**
|
---|
1397 | * Listener for window switch events.
|
---|
1398 | *
|
---|
1399 | * These are events, when the user activates a window of another application
|
---|
1400 | * or comes back to JOSM. Window switches from one JOSM window to another
|
---|
1401 | * are not reported.
|
---|
1402 | */
|
---|
1403 | public static interface WindowSwitchListener {
|
---|
1404 | /**
|
---|
1405 | * Called when the user activates a window of another application.
|
---|
1406 | */
|
---|
1407 | void toOtherApplication();
|
---|
1408 | /**
|
---|
1409 | * Called when the user comes from a window of another application
|
---|
1410 | * back to JOSM.
|
---|
1411 | */
|
---|
1412 | void fromOtherApplication();
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | private static final List<WeakReference<WindowSwitchListener>> windowSwitchListeners = new ArrayList<>();
|
---|
1416 |
|
---|
1417 | /**
|
---|
1418 | * Register a window switch listener.
|
---|
1419 | *
|
---|
1420 | * @param listener the listener. Ignored if <code>null</code>.
|
---|
1421 | */
|
---|
1422 | public static void addWindowSwitchListener(WindowSwitchListener listener) {
|
---|
1423 | if (listener == null) return;
|
---|
1424 | synchronized (Main.class) {
|
---|
1425 | for (WeakReference<WindowSwitchListener> wr : windowSwitchListeners) {
|
---|
1426 | // already registered ? => abort
|
---|
1427 | if (wr.get() == listener) return;
|
---|
1428 | }
|
---|
1429 | boolean wasEmpty = windowSwitchListeners.isEmpty();
|
---|
1430 | windowSwitchListeners.add(new WeakReference<>(listener));
|
---|
1431 | if (wasEmpty) {
|
---|
1432 | // The following call will have no effect, when there is no window
|
---|
1433 | // at the time. Therefore, MasterWindowListener.setup() will also be
|
---|
1434 | // called, as soon as the main window is shown.
|
---|
1435 | MasterWindowListener.setup();
|
---|
1436 | }
|
---|
1437 | }
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | /**
|
---|
1441 | * Removes a window switch listener.
|
---|
1442 | *
|
---|
1443 | * @param listener the listener. Ignored if <code>null</code>.
|
---|
1444 | */
|
---|
1445 | public static void removeWindowSwitchListener(WindowSwitchListener listener) {
|
---|
1446 | if (listener == null) return;
|
---|
1447 | synchronized (Main.class){
|
---|
1448 | Iterator<WeakReference<WindowSwitchListener>> it = windowSwitchListeners.iterator();
|
---|
1449 | while (it.hasNext()){
|
---|
1450 | WeakReference<WindowSwitchListener> wr = it.next();
|
---|
1451 | // remove the listener - and any other listener which got garbage
|
---|
1452 | // collected in the meantime
|
---|
1453 | if (wr.get() == null || wr.get() == listener) {
|
---|
1454 | it.remove();
|
---|
1455 | }
|
---|
1456 | }
|
---|
1457 | if (windowSwitchListeners.isEmpty()) {
|
---|
1458 | MasterWindowListener.teardown();
|
---|
1459 | }
|
---|
1460 | }
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | /**
|
---|
1464 | * WindowListener, that is registered on all Windows of the application.
|
---|
1465 | *
|
---|
1466 | * Its purpose is to notify WindowSwitchListeners, that the user switches to
|
---|
1467 | * another application, e.g. a browser, or back to JOSM.
|
---|
1468 | *
|
---|
1469 | * When changing from JOSM to another application and back (e.g. two times
|
---|
1470 | * alt+tab), the active Window within JOSM may be different.
|
---|
1471 | * Therefore, we need to register listeners to <strong>all</strong> (visible)
|
---|
1472 | * Windows in JOSM, and it does not suffice to monitor the one that was
|
---|
1473 | * deactivated last.
|
---|
1474 | *
|
---|
1475 | * This class is only "active" on demand, i.e. when there is at least one
|
---|
1476 | * WindowSwitchListener registered.
|
---|
1477 | */
|
---|
1478 | protected static class MasterWindowListener extends WindowAdapter {
|
---|
1479 |
|
---|
1480 | private static MasterWindowListener INSTANCE;
|
---|
1481 |
|
---|
1482 | public static synchronized MasterWindowListener getInstance() {
|
---|
1483 | if (INSTANCE == null) {
|
---|
1484 | INSTANCE = new MasterWindowListener();
|
---|
1485 | }
|
---|
1486 | return INSTANCE;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | /**
|
---|
1490 | * Register listeners to all non-hidden windows.
|
---|
1491 | *
|
---|
1492 | * Windows that are created later, will be cared for in {@link #windowDeactivated(WindowEvent)}.
|
---|
1493 | */
|
---|
1494 | public static void setup() {
|
---|
1495 | if (!windowSwitchListeners.isEmpty()) {
|
---|
1496 | for (Window w : Window.getWindows()) {
|
---|
1497 | if (w.isShowing()) {
|
---|
1498 | if (!Arrays.asList(w.getWindowListeners()).contains(getInstance())) {
|
---|
1499 | w.addWindowListener(getInstance());
|
---|
1500 | }
|
---|
1501 | }
|
---|
1502 | }
|
---|
1503 | }
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | /**
|
---|
1507 | * Unregister all listeners.
|
---|
1508 | */
|
---|
1509 | public static void teardown() {
|
---|
1510 | for (Window w : Window.getWindows()) {
|
---|
1511 | w.removeWindowListener(getInstance());
|
---|
1512 | }
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 | @Override
|
---|
1516 | public void windowActivated(WindowEvent e) {
|
---|
1517 | if (e.getOppositeWindow() == null) { // we come from a window of a different application
|
---|
1518 | // fire WindowSwitchListeners
|
---|
1519 | synchronized (Main.class) {
|
---|
1520 | Iterator<WeakReference<WindowSwitchListener>> it = windowSwitchListeners.iterator();
|
---|
1521 | while (it.hasNext()){
|
---|
1522 | WeakReference<WindowSwitchListener> wr = it.next();
|
---|
1523 | WindowSwitchListener listener = wr.get();
|
---|
1524 | if (listener == null) {
|
---|
1525 | it.remove();
|
---|
1526 | continue;
|
---|
1527 | }
|
---|
1528 | listener.fromOtherApplication();
|
---|
1529 | }
|
---|
1530 | }
|
---|
1531 | }
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 | @Override
|
---|
1535 | public void windowDeactivated(WindowEvent e) {
|
---|
1536 | // set up windows that have been created in the meantime
|
---|
1537 | for (Window w : Window.getWindows()) {
|
---|
1538 | if (!w.isShowing()) {
|
---|
1539 | w.removeWindowListener(getInstance());
|
---|
1540 | } else {
|
---|
1541 | if (!Arrays.asList(w.getWindowListeners()).contains(getInstance())) {
|
---|
1542 | w.addWindowListener(getInstance());
|
---|
1543 | }
|
---|
1544 | }
|
---|
1545 | }
|
---|
1546 | if (e.getOppositeWindow() == null) { // we go to a window of a different application
|
---|
1547 | // fire WindowSwitchListeners
|
---|
1548 | synchronized (Main.class) {
|
---|
1549 | Iterator<WeakReference<WindowSwitchListener>> it = windowSwitchListeners.iterator();
|
---|
1550 | while (it.hasNext()){
|
---|
1551 | WeakReference<WindowSwitchListener> wr = it.next();
|
---|
1552 | WindowSwitchListener listener = wr.get();
|
---|
1553 | if (listener == null) {
|
---|
1554 | it.remove();
|
---|
1555 | continue;
|
---|
1556 | }
|
---|
1557 | listener.toOtherApplication();
|
---|
1558 | }
|
---|
1559 | }
|
---|
1560 | }
|
---|
1561 | }
|
---|
1562 | }
|
---|
1563 |
|
---|
1564 | /**
|
---|
1565 | * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes
|
---|
1566 | * @param listener The MapFrameListener
|
---|
1567 | * @param fireWhenMapViewPresent If true, will fire an initial mapFrameInitialized event
|
---|
1568 | * when the MapFrame is present. Otherwise will only fire when the MapFrame is created
|
---|
1569 | * or destroyed.
|
---|
1570 | * @return {@code true} if the listeners collection changed as a result of the call
|
---|
1571 | */
|
---|
1572 | public static boolean addMapFrameListener(MapFrameListener listener, boolean fireWhenMapViewPresent) {
|
---|
1573 | boolean changed = listener != null ? mapFrameListeners.add(listener) : false;
|
---|
1574 | if (fireWhenMapViewPresent && changed && map != null) {
|
---|
1575 | listener.mapFrameInitialized(null, map);
|
---|
1576 | }
|
---|
1577 | return changed;
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | /**
|
---|
1581 | * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes
|
---|
1582 | * @param listener The MapFrameListener
|
---|
1583 | * @return {@code true} if the listeners collection changed as a result of the call
|
---|
1584 | * @since 5957
|
---|
1585 | */
|
---|
1586 | public static boolean addMapFrameListener(MapFrameListener listener) {
|
---|
1587 | return addMapFrameListener(listener, false);
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | /**
|
---|
1591 | * Unregisters the given {@code MapFrameListener} from MapFrame changes
|
---|
1592 | * @param listener The MapFrameListener
|
---|
1593 | * @return {@code true} if the listeners collection changed as a result of the call
|
---|
1594 | * @since 5957
|
---|
1595 | */
|
---|
1596 | public static boolean removeMapFrameListener(MapFrameListener listener) {
|
---|
1597 | return listener != null ? mapFrameListeners.remove(listener) : false;
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | /**
|
---|
1601 | * Adds a new network error that occur to give a hint about broken Internet connection.
|
---|
1602 | * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
|
---|
1603 | *
|
---|
1604 | * @param url The accessed URL that caused the error
|
---|
1605 | * @param t The network error
|
---|
1606 | * @return The previous error associated to the given resource, if any. Can be {@code null}
|
---|
1607 | * @since 6642
|
---|
1608 | */
|
---|
1609 | public static Throwable addNetworkError(URL url, Throwable t) {
|
---|
1610 | if (url != null && t != null) {
|
---|
1611 | Throwable old = addNetworkError(url.toExternalForm(), t);
|
---|
1612 | if (old != null) {
|
---|
1613 | Main.warn("Already here "+old);
|
---|
1614 | }
|
---|
1615 | return old;
|
---|
1616 | }
|
---|
1617 | return null;
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | /**
|
---|
1621 | * Adds a new network error that occur to give a hint about broken Internet connection.
|
---|
1622 | * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
|
---|
1623 | *
|
---|
1624 | * @param url The accessed URL that caused the error
|
---|
1625 | * @param t The network error
|
---|
1626 | * @return The previous error associated to the given resource, if any. Can be {@code null}
|
---|
1627 | * @since 6642
|
---|
1628 | */
|
---|
1629 | public static Throwable addNetworkError(String url, Throwable t) {
|
---|
1630 | if (url != null && t != null) {
|
---|
1631 | return NETWORK_ERRORS.put(url, t);
|
---|
1632 | }
|
---|
1633 | return null;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | /**
|
---|
1637 | * Returns the network errors that occured until now.
|
---|
1638 | * @return the network errors that occured until now, indexed by URL
|
---|
1639 | * @since 6639
|
---|
1640 | */
|
---|
1641 | public static Map<String, Throwable> getNetworkErrors() {
|
---|
1642 | return new HashMap<>(NETWORK_ERRORS);
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | /**
|
---|
1646 | * Returns the command-line arguments used to run the application.
|
---|
1647 | * @return the command-line arguments used to run the application
|
---|
1648 | * @since 8356
|
---|
1649 | */
|
---|
1650 | public static List<String> getCommandLineArgs() {
|
---|
1651 | return Collections.unmodifiableList(COMMAND_LINE_ARGS);
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | /**
|
---|
1655 | * Returns the JOSM website URL.
|
---|
1656 | * @return the josm website URL
|
---|
1657 | * @since 6897
|
---|
1658 | */
|
---|
1659 | public static String getJOSMWebsite() {
|
---|
1660 | if (Main.pref != null)
|
---|
1661 | return Main.pref.get("josm.url", JOSM_WEBSITE);
|
---|
1662 | return JOSM_WEBSITE;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | /**
|
---|
1666 | * Returns the JOSM XML URL.
|
---|
1667 | * @return the josm XML URL
|
---|
1668 | * @since 6897
|
---|
1669 | */
|
---|
1670 | public static String getXMLBase() {
|
---|
1671 | // Always return HTTP (issues reported with HTTPS)
|
---|
1672 | return "http://josm.openstreetmap.de";
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | /**
|
---|
1676 | * Returns the OSM website URL.
|
---|
1677 | * @return the OSM website URL
|
---|
1678 | * @since 6897
|
---|
1679 | */
|
---|
1680 | public static String getOSMWebsite() {
|
---|
1681 | if (Main.pref != null)
|
---|
1682 | return Main.pref.get("osm.url", OSM_WEBSITE);
|
---|
1683 | return OSM_WEBSITE;
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | /**
|
---|
1687 | * Replies the base URL for browsing information about a primitive.
|
---|
1688 | * @return the base URL, i.e. https://www.openstreetmap.org
|
---|
1689 | * @since 7678
|
---|
1690 | */
|
---|
1691 | public static String getBaseBrowseUrl() {
|
---|
1692 | if (Main.pref != null)
|
---|
1693 | return Main.pref.get("osm-browse.url", getOSMWebsite());
|
---|
1694 | return getOSMWebsite();
|
---|
1695 | }
|
---|
1696 |
|
---|
1697 | /**
|
---|
1698 | * Replies the base URL for browsing information about a user.
|
---|
1699 | * @return the base URL, i.e. https://www.openstreetmap.org/user
|
---|
1700 | * @since 7678
|
---|
1701 | */
|
---|
1702 | public static String getBaseUserUrl() {
|
---|
1703 | if (Main.pref != null)
|
---|
1704 | return Main.pref.get("osm-user.url", getOSMWebsite() + "/user");
|
---|
1705 | return getOSMWebsite() + "/user";
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | /**
|
---|
1709 | * Determines if we are currently running on OSX.
|
---|
1710 | * @return {@code true} if we are currently running on OSX
|
---|
1711 | * @since 6957
|
---|
1712 | */
|
---|
1713 | public static boolean isPlatformOsx() {
|
---|
1714 | return Main.platform instanceof PlatformHookOsx;
|
---|
1715 | }
|
---|
1716 |
|
---|
1717 | /**
|
---|
1718 | * Determines if we are currently running on Windows.
|
---|
1719 | * @return {@code true} if we are currently running on Windows
|
---|
1720 | * @since 7335
|
---|
1721 | */
|
---|
1722 | public static boolean isPlatformWindows() {
|
---|
1723 | return Main.platform instanceof PlatformHookWindows;
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 | /**
|
---|
1727 | * Determines if the given online resource is currently offline.
|
---|
1728 | * @param r the online resource
|
---|
1729 | * @return {@code true} if {@code r} is offline and should not be accessed
|
---|
1730 | * @since 7434
|
---|
1731 | */
|
---|
1732 | public static boolean isOffline(OnlineResource r) {
|
---|
1733 | return OFFLINE_RESOURCES.contains(r) || OFFLINE_RESOURCES.contains(OnlineResource.ALL);
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 | /**
|
---|
1737 | * Sets the given online resource to offline state.
|
---|
1738 | * @param r the online resource
|
---|
1739 | * @return {@code true} if {@code r} was not already offline
|
---|
1740 | * @since 7434
|
---|
1741 | */
|
---|
1742 | public static boolean setOffline(OnlineResource r) {
|
---|
1743 | return OFFLINE_RESOURCES.add(r);
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | /**
|
---|
1747 | * Replies the set of online resources currently offline.
|
---|
1748 | * @return the set of online resources currently offline
|
---|
1749 | * @since 7434
|
---|
1750 | */
|
---|
1751 | public static Set<OnlineResource> getOfflineResources() {
|
---|
1752 | return new HashSet<>(OFFLINE_RESOURCES);
|
---|
1753 | }
|
---|
1754 | }
|
---|