001/**
002 * AppPreferences -- A collection of preferences for this program.
003 *
004 * Copyright (C) 2009-2024, by Joseph A. Huwaldt. All rights reserved.
005 *
006 * This library is free software; you can redistribute it and/or modify it under the terms
007 * of the GNU Lesser General Public License as published by the Free Software Foundation;
008 * either version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
012 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License along with
015 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place -
016 * Suite 330, Boston, MA 02111-1307, USA. Or visit: http://www.gnu.org/licenses/lgpl.html
017 */
018package geomss.app;
019
020import java.beans.PropertyChangeListener;
021import java.beans.PropertyChangeSupport;
022import static java.util.Objects.isNull;
023import static java.util.Objects.nonNull;
024import java.util.prefs.Preferences;
025
026/**
027 * This class serves as a collection of preferences for this program.
028 * 
029 * <p> Modified by: Joseph A. Huwaldt </p>
030 * 
031 * @author Joseph A. Huwaldt, Date: May 2, 2009
032 * @version January 1, 2024
033 */
034public class AppPreferences implements jahuwaldt.swing.Preferences {
035
036    //  The preferences for this application are stored as a Preferences object.
037    private final Preferences prefs = Preferences.userNodeForPackage(this.getClass());
038
039    //  Use a change-listener system to keep the preferences dialog updated
040    private final PropertyChangeSupport propertyChange = new PropertyChangeSupport(this);
041    
042    //  Preference keys
043    private static final String LASTPATH = "lastPath";
044    
045    
046        /**
047        *  Returns the file path to the parent of the last referenced file.
048        *  Returns null if no last path could be found.
049    * 
050     * @return The file path to the parent of the last referenced file.
051        */
052    @Override
053        public String getLastPath() {
054                return prefs.get(LASTPATH, null);
055        }
056        
057        /**
058        *  Set the last file path referenced by the user.  This is the path
059        *  to the last parent of the last referenced file.
060    * 
061     * @param path The last file path referenced by the user.
062        */
063    @Override
064        public void setLastPath(String path) {
065                String oldPath = getLastPath();
066                if ( (nonNull(path) && !path.equals(oldPath)) || (isNull(path) && nonNull(oldPath)) ) {
067                        prefs.put(LASTPATH, path);
068            propertyChange.firePropertyChange(LASTPATH, oldPath, path);
069                }
070        }
071
072    /**
073     * Return the preference with the specified key String.
074     *
075     * @param key The key String identifying the preference to be retrieved. May not be
076     *            null.
077     * @return The preference with the specified key String.
078     */
079    @Override
080    public String get(String key) {
081                return prefs.get(key, null);
082    }
083
084    /**
085     * Set the preference with the specified key String.
086     *
087     * @param key   The key String identifying the preference to be set. May not be null.
088     * @param value THe String value to store as the preference.  May not be null.
089     */
090    @Override
091    public void set(String key, String value) {
092        String oldValue = get(key);
093                prefs.put(key, value);
094        propertyChange.firePropertyChange(key, oldValue, value);
095    }
096
097    public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
098        propertyChange.addPropertyChangeListener(l);
099    }
100
101    public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener l) {
102        propertyChange.addPropertyChangeListener(propertyName, l);
103    }
104
105    public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
106        propertyChange.removePropertyChangeListener(l);
107    }
108
109    public synchronized void removePropertyChangeListener(String propertyName, PropertyChangeListener l) {
110        propertyChange.removePropertyChangeListener(propertyName, l);
111    }
112
113    /**
114     * Method that displays a dialog that allows the user to change the application
115     * preferences. This implementation does nothing.
116     */
117    @Override
118    public void showPreferenceDialog() {
119        
120    }
121
122}