Changeset 13021 in josm for trunk/src/org
- Timestamp:
- 2017-10-18T17:25:29+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r12990 r13021 6 6 7 7 import java.awt.Color; 8 import java.awt.GraphicsEnvironment;9 import java.awt.Toolkit;10 8 import java.io.File; 11 9 import java.io.IOException; … … 15 13 import java.lang.annotation.Retention; 16 14 import java.lang.annotation.RetentionPolicy; 17 import java.lang.reflect.Field;18 15 import java.nio.charset.StandardCharsets; 19 16 import java.util.ArrayList; … … 28 25 import java.util.Map.Entry; 29 26 import java.util.Optional; 30 import java.util.ResourceBundle;31 27 import java.util.Set; 32 28 import java.util.SortedMap; … … 46 42 import org.openstreetmap.josm.data.preferences.IntegerProperty; 47 43 import org.openstreetmap.josm.data.preferences.ColorInfo; 44 import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; 48 45 import org.openstreetmap.josm.data.preferences.LongProperty; 49 46 import org.openstreetmap.josm.data.preferences.NamedColorProperty; … … 60 57 import org.openstreetmap.josm.io.OfflineAccessException; 61 58 import org.openstreetmap.josm.io.OnlineResource; 59 import org.openstreetmap.josm.spi.preferences.Config; 62 60 import org.openstreetmap.josm.tools.CheckParameterUtil; 63 61 import org.openstreetmap.josm.tools.ColorHelper; … … 90 88 * @since 74 91 89 */ 92 public class Preferences extends AbstractPreferences implements IBaseDirectories{90 public class Preferences extends AbstractPreferences { 93 91 94 92 private static final String COLOR_PREFIX = "color."; … … 103 101 private static final long MAX_AGE_DEFAULT_PREFERENCES = TimeUnit.DAYS.toSeconds(50); 104 102 105 /** 106 * Internal storage for the preference directory. 107 * Do not access this variable directly! 108 * @see #getPreferencesDirectory() 109 */ 110 private File preferencesDir; 111 112 /** 113 * Internal storage for the cache directory. 114 */ 115 private File cacheDir; 116 117 /** 118 * Internal storage for the user data directory. 119 */ 120 private File userdataDir; 103 private final IBaseDirectories dirs; 121 104 122 105 /** … … 246 229 */ 247 230 public Preferences() { 248 // Default constructor 231 this.dirs = Config.getDirs(); 232 } 233 234 /** 235 * Constructs a new {@code Preferences}. 236 * 237 * @param dirs the directories to use for saving the preferences 238 */ 239 public Preferences(IBaseDirectories dirs) { 240 this.dirs = dirs; 249 241 } 250 242 … … 256 248 @SuppressWarnings("deprecation") 257 249 public Preferences(Preferences pref) { 250 this(pref.dirs); 258 251 settingsMap.putAll(pref.settingsMap); 259 252 defaultsMap.putAll(pref.defaultsMap); … … 420 413 421 414 /** 415 * Get the base directories associated with this preference instance. 416 * @return the base directories 417 */ 418 public IBaseDirectories getDirs() { 419 return dirs; 420 } 421 422 /** 422 423 * Returns the user defined preferences directory, containing the preferences.xml file 423 424 * @return The user defined preferences directory, containing the preferences.xml file … … 430 431 } 431 432 432 @Override 433 /** 434 * @deprecated use {@link #getDirs()} or (more generally) {@link Config#getDirs()} 435 */ 436 @Deprecated 433 437 public File getPreferencesDirectory(boolean createIfMissing) { 434 if (preferencesDir == null) { 435 String path; 436 path = System.getProperty("josm.pref"); 437 if (path != null) { 438 preferencesDir = new File(path).getAbsoluteFile(); 439 } else { 440 path = System.getProperty("josm.home"); 441 if (path != null) { 442 preferencesDir = new File(path).getAbsoluteFile(); 443 } else { 444 preferencesDir = Main.platform.getDefaultPrefDirectory(); 445 } 446 } 447 } 448 if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) { 449 Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile())); 450 JOptionPane.showMessageDialog( 451 Main.parent, 452 tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()), 453 tr("Error"), 454 JOptionPane.ERROR_MESSAGE 455 ); 456 } 457 return preferencesDir; 438 return dirs.getPreferencesDirectory(createIfMissing); 458 439 } 459 440 … … 470 451 } 471 452 472 @Override 453 /** 454 * @deprecated use {@link #getDirs()} or (more generally) {@link Config#getDirs()} 455 */ 456 @Deprecated 473 457 public File getUserDataDirectory(boolean createIfMissing) { 474 if (userdataDir == null) { 475 String path; 476 path = System.getProperty("josm.userdata"); 477 if (path != null) { 478 userdataDir = new File(path).getAbsoluteFile(); 479 } else { 480 path = System.getProperty("josm.home"); 481 if (path != null) { 482 userdataDir = new File(path).getAbsoluteFile(); 483 } else { 484 userdataDir = Main.platform.getDefaultUserDataDirectory(); 485 } 486 } 487 } 488 if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) { 489 Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile())); 490 JOptionPane.showMessageDialog( 491 Main.parent, 492 tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()), 493 tr("Error"), 494 JOptionPane.ERROR_MESSAGE 495 ); 496 } 497 return userdataDir; 458 return dirs.getUserDataDirectory(createIfMissing); 498 459 } 499 460 … … 535 496 } 536 497 537 @Override 498 /** 499 * @deprecated use {@link #getDirs()} or (more generally) {@link Config#getDirs()} 500 */ 501 @Deprecated 538 502 public File getCacheDirectory(boolean createIfMissing) { 539 if (cacheDir == null) { 540 String path = System.getProperty("josm.cache"); 541 if (path != null) { 542 cacheDir = new File(path).getAbsoluteFile(); 543 } else { 544 path = System.getProperty("josm.home"); 545 if (path != null) { 546 cacheDir = new File(path, "cache"); 547 } else { 548 path = get("cache.folder", null); 549 if (path != null) { 550 cacheDir = new File(path).getAbsoluteFile(); 551 } else { 552 cacheDir = Main.platform.getDefaultCacheDirectory(); 553 } 554 } 555 } 556 } 557 if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) { 558 Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile())); 559 JOptionPane.showMessageDialog( 560 Main.parent, 561 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()), 562 tr("Error"), 563 JOptionPane.ERROR_MESSAGE 564 ); 565 } 566 return cacheDir; 503 return dirs.getCacheDirectory(createIfMissing); 567 504 } 568 505 … … 782 719 /* currently unused, but may help to fix configuration issues in future */ 783 720 putInt("josm.version", Version.getInstance().getVersion()); 784 785 updateSystemProperties();786 721 } 787 722 … … 837 772 settingsMap.clear(); 838 773 settingsMap.putAll(reader.getSettings()); 839 updateSystemProperties();840 774 removeObsolete(reader.getVersion()); 841 775 } … … 984 918 public void resetToInitialState() { 985 919 resetToDefault(); 986 preferencesDir = null;987 cacheDir = null;988 userdataDir = null;989 920 saveOnPut = true; 990 921 initSuccessful = false; … … 1505 1436 1506 1437 /** 1507 * Updates system properties with the current values in the preferences.1508 */1509 public void updateSystemProperties() {1510 if ("true".equals(get("prefer.ipv6", "auto")) && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) {1511 // never set this to false, only true!1512 Logging.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup)."));1513 }1514 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());1515 Utils.updateSystemProperty("user.language", get("language"));1516 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-62927391517 // Force AWT toolkit to update its internal preferences (fix #6345).1518 // Does not work anymore with Java 9, to remove with Java 9 migration1519 if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) {1520 try {1521 Field field = Toolkit.class.getDeclaredField("resources");1522 Utils.setObjectsAccessible(field);1523 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));1524 } catch (ReflectiveOperationException | RuntimeException e) { // NOPMD1525 // Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 91526 Logging.warn(e);1527 }1528 }1529 // Possibility to disable SNI (not by default) in case of misconfigured https servers1530 // See #9875 + http://stackoverflow.com/a/14884941/22571721531 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details1532 if (getBoolean("jdk.tls.disableSNIExtension", false)) {1533 Utils.updateSystemProperty("jsse.enableSNIExtension", "false");1534 }1535 }1536 1537 /**1538 1438 * Replies the collection of plugin site URLs from where plugin lists can be downloaded. 1539 1439 * @return the collection of plugin site URLs -
trunk/src/org/openstreetmap/josm/gui/MainApplication.java
r12928 r13021 11 11 import java.awt.GraphicsEnvironment; 12 12 import java.awt.GridBagLayout; 13 import java.awt.Toolkit; 13 14 import java.awt.event.KeyEvent; 14 15 import java.io.File; 15 16 import java.io.IOException; 16 17 import java.io.InputStream; 18 import java.lang.reflect.Field; 17 19 import java.net.Authenticator; 18 20 import java.net.Inet6Address; … … 38 40 import java.util.Objects; 39 41 import java.util.Optional; 42 import java.util.ResourceBundle; 40 43 import java.util.Set; 41 44 import java.util.TreeSet; … … 90 93 import org.openstreetmap.josm.data.osm.UserInfo; 91 94 import org.openstreetmap.josm.data.osm.search.SearchMode; 95 import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; 92 96 import org.openstreetmap.josm.data.preferences.sources.SourceType; 93 97 import org.openstreetmap.josm.data.projection.ProjectionCLI; … … 148 152 import org.openstreetmap.josm.plugins.PluginInformation; 149 153 import org.openstreetmap.josm.spi.preferences.Config; 154 import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent; 155 import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener; 150 156 import org.openstreetmap.josm.tools.FontsManager; 151 157 import org.openstreetmap.josm.tools.GBC; … … 926 932 Main.pref.init(args.hasOption(Option.RESET_PREFERENCES)); 927 933 Config.setPreferencesInstance(Main.pref); 928 Config.setBaseDirectoriesProvider( Main.pref);934 Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance()); 929 935 930 936 args.getPreferencesToSet().forEach(Main.pref::put); … … 933 939 I18n.set(Config.getPref().get("language", null)); 934 940 } 935 Main.pref.updateSystemProperties(); 941 updateSystemProperties(); 942 Main.pref.addPreferenceChangeListener(new PreferenceChangedListener() { 943 @Override 944 public void preferenceChanged(PreferenceChangeEvent e) { 945 updateSystemProperties(); 946 } 947 }); 936 948 937 949 checkIPv6(); … … 1069 1081 Logging.info("Enabled EDT checker, wrongful access to gui from non EDT thread will be printed to console"); 1070 1082 RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager()); 1083 } 1084 } 1085 1086 /** 1087 * Updates system properties with the current values in the preferences. 1088 */ 1089 private static void updateSystemProperties() { 1090 if ("true".equals(Config.getPref().get("prefer.ipv6", "auto")) 1091 && !"true".equals(Utils.updateSystemProperty("java.net.preferIPv6Addresses", "true"))) { 1092 // never set this to false, only true! 1093 Logging.info(tr("Try enabling IPv6 network, prefering IPv6 over IPv4 (only works on early startup).")); 1094 } 1095 Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString()); 1096 Utils.updateSystemProperty("user.language", Config.getPref().get("language")); 1097 // Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739 1098 // Force AWT toolkit to update its internal preferences (fix #6345). 1099 // Does not work anymore with Java 9, to remove with Java 9 migration 1100 if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) { 1101 try { 1102 Field field = Toolkit.class.getDeclaredField("resources"); 1103 Utils.setObjectsAccessible(field); 1104 field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt")); 1105 } catch (ReflectiveOperationException | RuntimeException e) { // NOPMD 1106 // Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 9 1107 Logging.warn(e); 1108 } 1109 } 1110 // Possibility to disable SNI (not by default) in case of misconfigured https servers 1111 // See #9875 + http://stackoverflow.com/a/14884941/2257172 1112 // then https://josm.openstreetmap.de/ticket/12152#comment:5 for details 1113 if (Config.getPref().getBoolean("jdk.tls.disableSNIExtension", false)) { 1114 Utils.updateSystemProperty("jsse.enableSNIExtension", "false"); 1071 1115 } 1072 1116 } -
trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java
r12966 r13021 24 24 import org.openstreetmap.josm.Main; 25 25 import org.openstreetmap.josm.data.Bounds; 26 import org.openstreetmap.josm.data.Preferences;27 26 import org.openstreetmap.josm.data.ProjectionBounds; 28 27 import org.openstreetmap.josm.data.coor.EastNorth; … … 30 29 import org.openstreetmap.josm.data.coor.conversion.LatLonParser; 31 30 import org.openstreetmap.josm.data.osm.DataSet; 31 import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; 32 32 import org.openstreetmap.josm.data.projection.Projection; 33 33 import org.openstreetmap.josm.data.projection.Projections; … … 419 419 Logging.setLogLevel(getLogLevel()); 420 420 421 Config.setBaseDirectoriesProvider( new Preferences()); // for right-left-hand traffic cache file421 Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance()); // for right-left-hand traffic cache file 422 422 Config.setPreferencesInstance(new MemoryPreferences()); 423 423 Config.getPref().putBoolean("mappaint.auto_reload_local_styles", false); // unnecessary to listen for external changes
Note:
See TracChangeset
for help on using the changeset viewer.