001package jmri.implementation;
002
003import java.io.FileNotFoundException;
004import java.util.HashSet;
005import java.util.Locale;
006import java.util.Set;
007import java.util.prefs.Preferences;
008import javax.annotation.Nonnull;
009import jmri.profile.Profile;
010import jmri.profile.ProfileUtils;
011import jmri.spi.PreferencesManager;
012import jmri.util.FileUtil;
013import jmri.util.prefs.AbstractPreferencesManager;
014import jmri.util.prefs.InitializationException;
015import org.openide.util.lookup.ServiceProvider;
016
017/**
018 *
019 * @author Randall Wood (C) 2015
020 */
021@ServiceProvider(service = PreferencesManager.class)
022public class FileLocationsPreferences extends AbstractPreferencesManager {
023
024    public static final String USER_FILES = "user-files"; // NOI18N
025    public static final String SCRIPTS = "scripts"; // NOI18N
026
027    @Override
028    public void initialize(Profile profile) throws InitializationException {
029        if (!this.isInitialized(profile)) {
030            Preferences shared = ProfileUtils.getPreferences(profile, this.getClass(), true);
031            Preferences perNode = ProfileUtils.getPreferences(profile, this.getClass(), false);
032            String userFiles = shared.get(USER_FILES, FileUtil.PROFILE);
033            if (!userFiles.startsWith(FileUtil.PROFILE)) {
034                userFiles = perNode.get(USER_FILES, userFiles);
035            }
036            FileUtil.setUserFilesPath(profile, FileUtil.getAbsoluteFilename(profile, userFiles));
037            String scripts = shared.get(SCRIPTS, FileUtil.PROFILE);
038            if (!scripts.startsWith(FileUtil.PROFILE) && !scripts.startsWith(FileUtil.PROGRAM)) {
039                scripts = perNode.get(SCRIPTS, scripts);
040            }
041            FileUtil.setScriptsPath(profile, FileUtil.getAbsoluteFilename(profile, scripts));
042            this.setInitialized(profile, true);
043            try {
044                if (!FileUtil.getFile(profile, userFiles).isDirectory()) {
045                    String message = "UserFilesIsNotDir"; // NOI18N
046                    userFiles = FileUtil.getAbsoluteFilename(profile, userFiles);
047                    throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, message, userFiles), Bundle.getMessage(message, userFiles));
048                }
049            } catch (FileNotFoundException ex) {
050                String message = "UserFilesDoesNotExist"; // NOI18N
051                userFiles = FileUtil.getAbsoluteFilename(profile, userFiles);
052                throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, message, userFiles), Bundle.getMessage(message, userFiles));
053            }
054            try {
055                if (!FileUtil.getFile(profile, scripts).isDirectory()) {
056                    String message = "ScriptsIsNotDir"; // NOI18N
057                    scripts = FileUtil.getAbsoluteFilename(profile, scripts);
058                    throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, message, scripts), Bundle.getMessage(message, scripts));
059                }
060            } catch (FileNotFoundException ex) {
061                String message = "ScriptsDoesNotExist"; // NOI18N
062                scripts = FileUtil.getAbsoluteFilename(profile, scripts);
063                throw new InitializationException(Bundle.getMessage(Locale.ENGLISH, message, scripts), Bundle.getMessage(message, scripts));
064            }
065        }
066    }
067
068    @Override
069    @Nonnull
070    public Set<Class<? extends PreferencesManager>> getRequires() {
071        return new HashSet<>();
072    }
073
074    @Override
075    public void savePreferences(Profile profile) {
076        Preferences shared = ProfileUtils.getPreferences(profile, this.getClass(), true);
077        Preferences perNode = ProfileUtils.getPreferences(profile, this.getClass(), false);
078        shared.put(USER_FILES, FileUtil.getPortableFilename(profile, FileUtil.getUserFilesPath(profile), true, false));
079        shared.put(SCRIPTS, FileUtil.getPortableFilename(profile, FileUtil.getScriptsPath(profile)));
080        perNode.put(USER_FILES, FileUtil.getPortableFilename(profile, FileUtil.getUserFilesPath(profile), true, false));
081        perNode.put(SCRIPTS, FileUtil.getPortableFilename(profile, FileUtil.getScriptsPath(profile)));
082    }
083
084}