Changeset 7834 in josm for trunk/src/org


Ignore:
Timestamp:
2014-12-19T15:05:33+01:00 (9 years ago)
Author:
Don-vip
Message:

see #10026 - use recommended user data directory on OSX (distinct from preferences directory). Linux and Windows behaviour is unchanged.

Location:
trunk/src/org/openstreetmap/josm
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/AutosaveTask.java

    r7580 r7834  
    8585    private final Deque<File> deletedLayers = new LinkedList<>();
    8686
    87     private final File autosaveDir = new File(Main.pref.getPreferencesDir() + AUTOSAVE_DIR);
    88     private final File deletedLayersDir = new File(Main.pref.getPreferencesDir() + DELETED_LAYERS_DIR);
     87    private final File autosaveDir = new File(Main.pref.getUserDataDirectory(), AUTOSAVE_DIR);
     88    private final File deletedLayersDir = new File(Main.pref.getUserDataDirectory(), DELETED_LAYERS_DIR);
    8989
    9090    public void schedule() {
  • trunk/src/org/openstreetmap/josm/data/CustomConfigurator.java

    r7082 r7834  
    157157     */
    158158    public static void messageBox(String type, String text) {
    159         if (type==null || type.length()==0) type="plain";
     159        if (type==null || type.isEmpty()) type="plain";
    160160
    161161        switch (type.charAt(0)) {
     
    390390
    391391    private static String getDirectoryByAbbr(String base) {
    392             String dir;
    393             if ("prefs".equals(base) || base.length()==0) {
    394                 dir = Main.pref.getPreferencesDir();
    395             } else if ("cache".equals(base)) {
    396                 dir = Main.pref.getCacheDirectory().getAbsolutePath();
    397             } else if ("plugins".equals(base)) {
    398                 dir = Main.pref.getPluginsDirectory().getAbsolutePath();
    399             } else {
    400                 dir = null;
    401             }
    402             return dir;
     392        String dir;
     393        if ("prefs".equals(base) || base.isEmpty()) {
     394            dir = Main.pref.getPreferencesDirectory().getAbsolutePath();
     395        } else if ("cache".equals(base)) {
     396            dir = Main.pref.getCacheDirectory().getAbsolutePath();
     397        } else if ("plugins".equals(base)) {
     398            dir = Main.pref.getPluginsDirectory().getAbsolutePath();
     399        } else {
     400            dir = null;
     401        }
     402        return dir;
    403403    }
    404404
     
    459459                engine.eval("API={}; API.pref={}; API.fragments={};");
    460460
    461                 engine.eval("homeDir='"+normalizeDirName(Main.pref.getPreferencesDir()) +"';");
     461                engine.eval("homeDir='"+normalizeDirName(Main.pref.getPreferencesDirectory().getAbsolutePath()) +"';");
    462462                engine.eval("josmVersion="+Version.getInstance().getVersion()+";");
    463463                String className = CustomConfigurator.class.getName();
     
    629629            if (locText.length()>0) text=locText;
    630630            String var = elem.getAttribute("var");
    631             if (var.length()==0) var="result";
     631            if (var.isEmpty()) var="result";
    632632
    633633            String input = evalVars(elem.getAttribute("input"));
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r7831 r7834  
    2222import java.util.Collection;
    2323import java.util.Collections;
     24import java.util.HashSet;
    2425import java.util.Iterator;
    2526import java.util.LinkedHashMap;
     
    3031import java.util.Objects;
    3132import java.util.ResourceBundle;
     33import java.util.Set;
    3234import java.util.SortedMap;
    3335import java.util.TreeMap;
     
    8486     * Internal storage for the preference directory.
    8587     * Do not access this variable directly!
    86      * @see #getPreferencesDirFile()
    87      */
    88     private File preferencesDirFile = null;
     88     * @see #getPreferencesDirectory()
     89     */
     90    private File preferencesDir = null;
    8991
    9092    /**
    9193     * Internal storage for the cache directory.
    9294     */
    93     private File cacheDirFile = null;
     95    private File cacheDir = null;
    9496
    9597    /**
     
    530532     * Returns the location of the user defined preferences directory
    531533     * @return The location of the user defined preferences directory
    532      */
     534     * @deprecated use #getPreferencesDirectory() to access preferences directory
     535     * or #getUserDataDirectory to access user data directory
     536     */
     537    @Deprecated
    533538    public String getPreferencesDir() {
    534         final String path = getPreferencesDirFile().getPath();
     539        final String path = getPreferencesDirectory().getPath();
    535540        if (path.endsWith(File.separator))
    536541            return path;
     
    539544
    540545    /**
    541      * Returns the user defined preferences directory
    542      * @return The user defined preferences directory
    543      */
    544     public File getPreferencesDirFile() {
    545         if (preferencesDirFile != null)
    546             return preferencesDirFile;
     546     * Returns the user defined preferences directory, containing the preferences.xml file
     547     * @return The user defined preferences directory, containing the preferences.xml file
     548     * @since 7834
     549     */
     550    public File getPreferencesDirectory() {
     551        if (preferencesDir != null)
     552            return preferencesDir;
    547553        String path;
    548554        path = System.getProperty("josm.home");
    549555        if (path != null) {
    550             preferencesDirFile = new File(path).getAbsoluteFile();
     556            preferencesDir = new File(path).getAbsoluteFile();
    551557        } else {
    552             preferencesDirFile = Main.platform.getDefaultPrefDirectory();
    553         }
    554         return preferencesDirFile;
    555     }
    556 
    557     /**
    558      * Returns the user preferences file
    559      * @return The user preferences file
     558            preferencesDir = Main.platform.getDefaultPrefDirectory();
     559        }
     560        return preferencesDir;
     561    }
     562
     563    /**
     564     * Returns the user data directory, containing autosave, plugins, etc.
     565     * Depending on the OS it may be the same directory as preferences directory.
     566     * @return The user data directory, containing autosave, plugins, etc.
     567     * @since 7834
     568     */
     569    public File getUserDataDirectory() {
     570        return Main.platform.getDefaultUserDataDirectory();
     571    }
     572
     573    /**
     574     * Returns the user preferences file (preferences.xml)
     575     * @return The user preferences file (preferences.xml)
    560576     */
    561577    public File getPreferenceFile() {
    562         return new File(getPreferencesDirFile(), "preferences.xml");
     578        return new File(getPreferencesDirectory(), "preferences.xml");
    563579    }
    564580
     
    568584     */
    569585    public File getPluginsDirectory() {
    570         return new File(getPreferencesDirFile(), "plugins");
     586        return new File(getUserDataDirectory(), "plugins");
    571587    }
    572588
     
    580596     */
    581597    public File getCacheDirectory() {
    582         if (cacheDirFile != null)
    583             return cacheDirFile;
     598        if (cacheDir != null)
     599            return cacheDir;
    584600        String path = System.getProperty("josm.cache");
    585601        if (path != null) {
    586             cacheDirFile = new File(path).getAbsoluteFile();
     602            cacheDir = new File(path).getAbsoluteFile();
    587603        } else {
    588604            path = get("cache.folder", null);
    589605            if (path != null) {
    590                 cacheDirFile = new File(path);
     606                cacheDir = new File(path);
    591607            } else {
    592                 cacheDirFile = Main.platform.getDefaultCacheDirectory();
    593             }
    594         }
    595         if (!cacheDirFile.exists() && !cacheDirFile.mkdirs()) {
    596             Main.warn(tr("Failed to create missing cache directory: {0}", cacheDirFile.getAbsoluteFile()));
     608                cacheDir = Main.platform.getDefaultCacheDirectory();
     609            }
     610        }
     611        if (!cacheDir.exists() && !cacheDir.mkdirs()) {
     612            Main.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
    597613            JOptionPane.showMessageDialog(
    598614                    Main.parent,
    599                     tr("<html>Failed to create missing cache directory: {0}</html>", cacheDirFile.getAbsoluteFile()),
     615                    tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
    600616                    tr("Error"),
    601617                    JOptionPane.ERROR_MESSAGE
    602618            );
    603619        }
    604         return cacheDirFile;
    605     }
    606 
    607     /**
    608      * @return A list of all existing directories where resources could be stored.
     620        return cacheDir;
     621    }
     622
     623    private void addPossibleResourceDir(Set<String> locations, String s) {
     624        if (s != null) {
     625            if (!s.endsWith(File.separator)) {
     626                s += File.separator;
     627            }
     628            locations.add(s);
     629        }
     630    }
     631
     632    /**
     633     * Returns a set of all existing directories where resources could be stored.
     634     * @return A set of all existing directories where resources could be stored.
    609635     */
    610636    public Collection<String> getAllPossiblePreferenceDirs() {
    611         LinkedList<String> locations = new LinkedList<>();
    612         locations.add(getPreferencesDir());
    613         String s;
    614         if ((s = System.getenv("JOSM_RESOURCES")) != null) {
    615             if (!s.endsWith(File.separator)) {
    616                 s = s + File.separator;
    617             }
    618             locations.add(s);
    619         }
    620         if ((s = System.getProperty("josm.resources")) != null) {
    621             if (!s.endsWith(File.separator)) {
    622                 s = s + File.separator;
    623             }
    624             locations.add(s);
    625         }
    626         String appdata = System.getenv("APPDATA");
    627         if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
    628                 && appdata.lastIndexOf(File.separator) != -1) {
    629             appdata = appdata.substring(appdata.lastIndexOf(File.separator));
    630             locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
    631                     appdata), "JOSM").getPath());
    632         }
    633         locations.add("/usr/local/share/josm/");
    634         locations.add("/usr/local/lib/josm/");
    635         locations.add("/usr/share/josm/");
    636         locations.add("/usr/lib/josm/");
     637        Set<String> locations = new HashSet<>();
     638        addPossibleResourceDir(locations, getPreferencesDirectory().getPath());
     639        addPossibleResourceDir(locations, getUserDataDirectory().getPath());
     640        addPossibleResourceDir(locations, System.getenv("JOSM_RESOURCES"));
     641        addPossibleResourceDir(locations, System.getProperty("josm.resources"));
     642        if (Main.isPlatformWindows()) {
     643            String appdata = System.getenv("APPDATA");
     644            if (System.getenv("ALLUSERSPROFILE") != null && appdata != null
     645                    && appdata.lastIndexOf(File.separator) != -1) {
     646                appdata = appdata.substring(appdata.lastIndexOf(File.separator));
     647                locations.add(new File(new File(System.getenv("ALLUSERSPROFILE"),
     648                        appdata), "JOSM").getPath());
     649            }
     650        } else {
     651            locations.add("/usr/local/share/josm/");
     652            locations.add("/usr/local/lib/josm/");
     653            locations.add("/usr/share/josm/");
     654            locations.add("/usr/lib/josm/");
     655        }
    637656        return locations;
    638657    }
     
    811830    public void init(boolean reset) {
    812831        // get the preferences.
    813         File prefDir = getPreferencesDirFile();
     832        File prefDir = getPreferencesDirectory();
    814833        if (prefDir.exists()) {
    815834            if(!prefDir.isDirectory()) {
  • trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java

    r7574 r7834  
    154154     */
    155155    public static String getValidatorDir() {
    156         return Main.pref.getPreferencesDir() + "validator/";
     156        return new File(Main.pref.getUserDataDirectory(), "validator").getAbsolutePath();
    157157    }
    158158
     
    174174        ignoredErrors.clear();
    175175        if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
    176             Path path = Paths.get(getValidatorDir() + "ignorederrors");
     176            Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
    177177            if (Files.exists(path)) {
    178178                try {
     
    196196
    197197    public static void saveIgnoredErrors() {
    198         try (PrintWriter out = new PrintWriter(new OutputStreamWriter(
    199                 new FileOutputStream(getValidatorDir() + "ignorederrors"), StandardCharsets.UTF_8), false)) {
     198        try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
     199                new File(getValidatorDir(), "ignorederrors")), StandardCharsets.UTF_8), false)) {
    200200            for (String e : ignoredErrors) {
    201201                out.println(e);
     
    282282     * until most bugs were discovered while keeping the processing time reasonable)
    283283     */
    284     public final void initializeGridDetail() {
     284    public static final void initializeGridDetail() {
    285285        String code = Main.getProjection().toCode();
    286286        if (Arrays.asList(ProjectionPreference.wgs84.allCodes()).contains(code)) {
  • trunk/src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java

    r7509 r7834  
    22package org.openstreetmap.josm.gui.dialogs;
    33
    4 import org.openstreetmap.josm.Main;
    5 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    6 import org.openstreetmap.josm.data.osm.PrimitiveId;
    7 import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    8 import org.openstreetmap.josm.gui.ExtendedDialog;
    9 import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
    10 import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    11 import org.openstreetmap.josm.gui.widgets.JosmTextField;
    12 import org.openstreetmap.josm.gui.widgets.OsmIdTextField;
    13 import org.openstreetmap.josm.gui.widgets.OsmPrimitiveTypesComboBox;
    14 import org.openstreetmap.josm.tools.Utils;
    15 
    16 import javax.swing.BorderFactory;
    17 import javax.swing.GroupLayout;
    18 import javax.swing.JLabel;
    19 import javax.swing.JOptionPane;
    20 import javax.swing.JPanel;
    21 import javax.swing.KeyStroke;
    22 import javax.swing.border.EtchedBorder;
    23 import javax.swing.plaf.basic.BasicComboBoxEditor;
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5import static org.openstreetmap.josm.tools.I18n.trc;
     6
    247import java.awt.Component;
    258import java.awt.Dimension;
     
    3720import java.util.Set;
    3821
    39 import static org.openstreetmap.josm.tools.I18n.tr;
    40 import static org.openstreetmap.josm.tools.I18n.trc;
     22import javax.swing.BorderFactory;
     23import javax.swing.GroupLayout;
     24import javax.swing.JLabel;
     25import javax.swing.JOptionPane;
     26import javax.swing.JPanel;
     27import javax.swing.KeyStroke;
     28import javax.swing.border.EtchedBorder;
     29import javax.swing.plaf.basic.BasicComboBoxEditor;
     30
     31import org.openstreetmap.josm.Main;
     32import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     33import org.openstreetmap.josm.data.osm.PrimitiveId;
     34import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
     35import org.openstreetmap.josm.gui.ExtendedDialog;
     36import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
     37import org.openstreetmap.josm.gui.widgets.HtmlPanel;
     38import org.openstreetmap.josm.gui.widgets.JosmTextField;
     39import org.openstreetmap.josm.gui.widgets.OsmIdTextField;
     40import org.openstreetmap.josm.gui.widgets.OsmPrimitiveTypesComboBox;
     41import org.openstreetmap.josm.tools.Utils;
    4142
    4243/**
     
    197198    protected void tryToPasteFromClipboard(OsmIdTextField tfId, OsmPrimitiveTypesComboBox cbType) {
    198199        String buf = Utils.getClipboardContent();
    199         if (buf == null || buf.length()==0) return;
     200        if (buf == null || buf.isEmpty()) return;
    200201        if (buf.length() > Main.pref.getInteger("downloadprimitive.max-autopaste-length", 2000)) return;
    201202        final List<SimplePrimitiveId> ids = SimplePrimitiveId.fuzzyParse(buf);
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java

    r7668 r7834  
    367367                   }
    368368                }
    369                 for (File f: Main.pref.getPreferencesDirFile().listFiles()) {
     369                for (File f: Main.pref.getPreferencesDirectory().listFiles()) {
    370370                   String s = f.getName();
    371371                   int idx = s.indexOf('_');
  • trunk/src/org/openstreetmap/josm/gui/preferences/shortcut/PrefJPanel.java

    r7015 r7834  
    358358        public void filter() {
    359359            String expr = filterField.getText().trim();
    360             if (expr.length()==0) { expr=null; }
     360            if (expr.isEmpty()) { expr=null; }
    361361            try {
    362362                final TableRowSorter<? extends TableModel> sorter =
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r7434 r7834  
    7979     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
    8080     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
    81      *  <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li></ul>
     81     *  <li>{@code josmdir://SOME/FILE} file inside josm user data directory (since r7058)</li></ul>
     82     *  <li>{@code josmplugindir://SOME/FILE} file inside josm plugin directory (since r7832)</li></ul>
    8283     */
    8384    public CachedFile(String name) {
     
    9293     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
    9394     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
    94      *  <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li></ul>
     95     *  <li>{@code josmdir://SOME/FILE} file inside josm user data directory (since r7058)</li></ul>
     96     *  <li>{@code josmplugindir://SOME/FILE} file inside josm plugin directory (since r7832)</li></ul>
    9597     * @return this object
    9698     */
     
    206208                return null;
    207209            } else if (name.startsWith("josmdir://")) {
    208                 cacheFile = new File(Main.pref.getPreferencesDir(), name.substring("josmdir://".length()));
     210                cacheFile = new File(Main.pref.getUserDataDirectory(), name.substring("josmdir://".length()));
     211            } else if (name.startsWith("josmplugindir://")) {
     212                cacheFile = new File(Main.pref.getPluginsDirectory(), name.substring("josmplugindir://".length()));
    209213            } else {
    210214                cacheFile = new File(name);
  • trunk/src/org/openstreetmap/josm/io/GpxExporter.java

    r7518 r7834  
    200200
    201201        if (enable) {
    202             if (copyrightYear.getText().length()==0) {
     202            if (copyrightYear.getText().isEmpty()) {
    203203                String sCopyrightYear = data.getString(META_COPYRIGHT_YEAR);
    204204                if (sCopyrightYear == null) {
     
    207207                copyrightYear.setText(sCopyrightYear);
    208208            }
    209             if (copyright.getText().length()==0) {
     209            if (copyright.getText().isEmpty()) {
    210210                String sCopyright = data.getString(META_COPYRIGHT_LICENSE);
    211211                if (sCopyright == null) {
     
    308308                        break;
    309309                    }
    310                     license += license.length()==0 ? urls[i] : ", "+urls[i];
     310                    license += license.isEmpty() ? urls[i] : ", "+urls[i];
    311311                }
    312312                copyright.setText(license);
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControl.java

    r7830 r7834  
    22package org.openstreetmap.josm.io.remotecontrol;
    33
     4import java.io.File;
    45import java.net.InetAddress;
    56import java.net.UnknownHostException;
     
    2829     * @since 7335
    2930     */
    30     public static final BooleanProperty PROP_REMOTECONTROL_HTTPS_ENABLED = new BooleanProperty("remotecontrol.https.enabled", false);
     31    public static final BooleanProperty PROP_REMOTECONTROL_HTTPS_ENABLED = new BooleanProperty(
     32            "remotecontrol.https.enabled", false);
    3133
    3234    /**
     
    7274     */
    7375    public static String getRemoteControlDir() {
    74         return Main.pref.getPreferencesDir() + "remotecontrol/";
     76        return new File(Main.pref.getUserDataDirectory(), "remotecontrol").getAbsolutePath();
    7577    }
    7678
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r7805 r7834  
    880880            }
    881881        }
    882         // Try user-preference directory
    883         String dir = Main.pref.getPreferencesDir() + "images";
     882        // Try user-data directory
     883        String dir = new File(Main.pref.getUserDataDirectory(), "images").getAbsolutePath();
    884884        try {
    885885            u = getImageUrl(dir, imageName, additionalClassLoaders);
     
    952952            });
    953953
    954             CachedFile cf = new CachedFile(base + fn).setDestDir(new File(Main.pref.getPreferencesDir(), "images").toString());
     954            CachedFile cf = new CachedFile(base + fn).setDestDir(
     955                    new File(Main.pref.getUserDataDirectory(), "images").getPath());
    955956            try (InputStream is = cf.getInputStream()) {
    956957                parser.parse(new InputSource(is));
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r7831 r7834  
    134134     */
    135135    public File getDefaultPrefDirectory();
     136
     137    /**
     138     * Returns the platform-dependent default user data directory.
     139     * @return the platform-dependent default user data directory
     140     * @since 7834
     141     */
     142    public File getDefaultUserDataDirectory();
    136143}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r7831 r7834  
    333333        return new File(System.getProperty("user.home")+"/Library/Preferences", "JOSM");
    334334    }
     335
     336    @Override
     337    public File getDefaultUserDataDirectory() {
     338        return new File(System.getProperty("user.home")+"/Library/Application Support", "JOSM");
     339    }
    335340}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r7831 r7834  
    373373    @Override
    374374    public File getDefaultCacheDirectory() {
    375         return new File(Main.pref.getPreferencesDirFile(), "cache");
     375        return new File(Main.pref.getUserDataDirectory(), "cache");
    376376    }
    377377
     
    380380        return new File(System.getProperty("user.home"), ".josm");
    381381    }
     382
     383    @Override
     384    public File getDefaultUserDataDirectory() {
     385        // Use preferences directory by default
     386        return Main.pref.getPreferencesDirectory();
     387    }
    382388}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r7833 r7834  
    287287    public File getDefaultCacheDirectory() {
    288288        String p = System.getenv("LOCALAPPDATA");
    289         if (p == null || "".equals(p)) {
     289        if (p == null || p.isEmpty()) {
     290            // Fallback for Windows OS earlier than Windows Vista, where the variable is not defined
    290291            p = System.getenv("APPDATA");
    291292        }
Note: See TracChangeset for help on using the changeset viewer.