001package jmri.jmrit.symbolicprog;
002
003import java.util.Set;
004import java.util.prefs.BackingStoreException;
005import java.util.prefs.Preferences;
006import javax.annotation.Nonnull;
007import jmri.implementation.FileLocationsPreferences;
008import jmri.jmrit.symbolicprog.tabbedframe.PaneProgFrame;
009import jmri.profile.Profile;
010import jmri.profile.ProfileUtils;
011import jmri.spi.PreferencesManager;
012import jmri.util.prefs.AbstractPreferencesManager;
013import jmri.util.prefs.InitializationException;
014import org.openide.util.lookup.ServiceProvider;
015import org.slf4j.Logger;
016import org.slf4j.LoggerFactory;
017
018/**
019 *
020 * @author Randall Wood (C) 2015
021 */
022@ServiceProvider(service = PreferencesManager.class)
023public class ProgrammerConfigManager extends AbstractPreferencesManager {
024
025    private final static Logger log = LoggerFactory.getLogger(ProgrammerConfigManager.class);
026    public final static String DEFAULT_FILE = "defaultFile";
027    public final static String SHOW_EMPTY_PANES = "showEmptyPanes";
028    public final static String SHOW_CV_NUMBERS = "showCvNumbers";
029    public final static String CAN_CACHE_DEFAULT = "canCacheDefault";
030    public final static String DO_CONFIRM_READ = "doConfirmRead";
031    private String defaultFile = null;
032    private boolean showEmptyPanes = true;
033    private boolean showCvNumbers = false;
034    private boolean canCacheDefault = false;
035    private boolean doConfirmRead = false;
036
037    @Override
038    public void initialize(Profile profile) throws InitializationException {
039        if (!this.isInitialized(profile)) {
040            Preferences preferences = ProfileUtils.getPreferences(profile, this.getClass(), true);
041            if (preferences.get(DEFAULT_FILE, this.getDefaultFile()) != null) {
042                this.setDefaultFile(preferences.get(DEFAULT_FILE, this.getDefaultFile()));
043                ProgDefault.setDefaultProgFile(this.getDefaultFile());
044            }
045            
046            this.setShowEmptyPanes(preferences.getBoolean(SHOW_EMPTY_PANES, this.isShowEmptyPanes()));
047            PaneProgFrame.setShowEmptyPanes(this.isShowEmptyPanes());
048            
049            this.setShowCvNumbers(preferences.getBoolean(SHOW_CV_NUMBERS, this.isShowCvNumbers()));
050            PaneProgFrame.setShowCvNumbers(this.isShowCvNumbers());
051            
052            this.setCanCacheDefault(preferences.getBoolean(CAN_CACHE_DEFAULT, this.isCanCacheDefault()));
053            PaneProgFrame.setCanCacheDefault(this.isCanCacheDefault());
054            
055            this.setDoConfirmRead(preferences.getBoolean(DO_CONFIRM_READ, this.isDoConfirmRead()));
056            PaneProgFrame.setDoConfirmRead(this.isDoConfirmRead());
057            
058            this.setInitialized(profile, true);
059        }
060    }
061
062    @Override
063    @Nonnull
064    public Set<Class<? extends PreferencesManager>> getRequires() {
065        Set<Class<? extends PreferencesManager>> requires = super.getRequires();
066        requires.add(FileLocationsPreferences.class);
067        return requires;
068    }
069
070    @Override
071    @Nonnull
072    public Set<Class<?>> getProvides() {
073        Set<Class<?>> provides = super.getProvides();
074        provides.stream().forEach((provide) -> {
075            log.debug("ProgammerConfigManager provides {}", provide);
076        });
077        return provides;
078    }
079
080    @Override
081    public void savePreferences(Profile profile) {
082        Preferences preferences = ProfileUtils.getPreferences(profile, this.getClass(), true);
083        if (this.defaultFile != null) {
084            preferences.put(DEFAULT_FILE, this.defaultFile);
085        } else {
086            preferences.remove(DEFAULT_FILE);
087        }
088        preferences.putBoolean(SHOW_EMPTY_PANES, this.showEmptyPanes);
089        preferences.putBoolean(SHOW_CV_NUMBERS, this.showCvNumbers);
090        preferences.putBoolean(CAN_CACHE_DEFAULT, this.canCacheDefault);
091        preferences.putBoolean(DO_CONFIRM_READ, this.doConfirmRead);
092        try {
093            preferences.sync();
094        } catch (BackingStoreException ex) {
095            log.error("Unable to save preferences.", ex);
096        }
097    }
098
099    /**
100     * @return the defaultFile
101     */
102    public String getDefaultFile() {
103        return defaultFile;
104    }
105
106    /**
107     * @param defaultFile the defaultFile to set
108     */
109    public void setDefaultFile(String defaultFile) {
110        java.lang.String oldDefaultFile = this.defaultFile;
111        this.defaultFile = defaultFile;
112        firePropertyChange(DEFAULT_FILE, oldDefaultFile, defaultFile);
113    }
114
115    /**
116     * @return the showEmptyPanes
117     */
118    public boolean isShowEmptyPanes() {
119        return showEmptyPanes;
120    }
121
122    /**
123     * @param showEmptyPanes the showEmptyPanes to set
124     */
125    public void setShowEmptyPanes(boolean showEmptyPanes) {
126        boolean oldShowEmptyPanes = this.showEmptyPanes;
127        this.showEmptyPanes = showEmptyPanes;
128        firePropertyChange(SHOW_EMPTY_PANES, oldShowEmptyPanes, showEmptyPanes);
129    }
130
131    /**
132     * @return the showCvNumbers
133     */
134    public boolean isShowCvNumbers() {
135        return showCvNumbers;
136    }
137
138    /**
139     * @param showCvNumbers the showCvNumbers to set
140     */
141    public void setShowCvNumbers(boolean showCvNumbers) {
142        boolean oldShowCvNumbers = this.showCvNumbers;
143        this.showCvNumbers = showCvNumbers;
144        firePropertyChange(SHOW_CV_NUMBERS, oldShowCvNumbers, showCvNumbers);
145    }
146
147    /**
148     * @return the canCacheDefault
149     */
150    public boolean isCanCacheDefault() {
151        return canCacheDefault;
152    }
153
154    /**
155     * @param canCacheDefault new value
156     */
157    public void setCanCacheDefault(boolean canCacheDefault) {
158        boolean oldCanCacheDefault = this.canCacheDefault;
159        this.canCacheDefault = canCacheDefault;
160        firePropertyChange(CAN_CACHE_DEFAULT, oldCanCacheDefault, canCacheDefault);
161    }
162
163    /**
164     * @return the doConfirmRead
165     */
166    public boolean isDoConfirmRead() {
167        return doConfirmRead;
168    }
169
170    /**
171     * @param doConfirmRead new value
172     */
173    public void setDoConfirmRead(boolean doConfirmRead) {
174        boolean oldDoConfirmRead = this.doConfirmRead;
175        this.doConfirmRead = doConfirmRead;
176        firePropertyChange(DO_CONFIRM_READ, oldDoConfirmRead, doConfirmRead);
177    }
178
179}