001    // This file is part of the program FRYSK.
002    //
003    // Copyright 2005, Red Hat Inc.
004    //
005    // FRYSK is free software; you can redistribute it and/or modify it
006    // under the terms of the GNU General Public License as published by
007    // the Free Software Foundation; version 2 of the License.
008    //
009    // FRYSK is distributed in the hope that it will be useful, but
010    // WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012    // General Public License for more details.
013    // 
014    // You should have received a copy of the GNU General Public License
015    // along with FRYSK; if not, write to the Free Software Foundation,
016    // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
017    // 
018    // In addition, as a special exception, Red Hat, Inc. gives You the
019    // additional right to link the code of FRYSK with code not covered
020    // under the GNU General Public License ("Non-GPL Code") and to
021    // distribute linked combinations including the two, subject to the
022    // limitations in this paragraph. Non-GPL Code permitted under this
023    // exception must only link to the code of FRYSK through those well
024    // defined interfaces identified in the file named EXCEPTION found in
025    // the source code files (the "Approved Interfaces"). The files of
026    // Non-GPL Code may instantiate templates or use macros or inline
027    // functions from the Approved Interfaces without causing the
028    // resulting work to be covered by the GNU General Public
029    // License. Only Red Hat, Inc. may make changes or additions to the
030    // list of Approved Interfaces. You must obey the GNU General Public
031    // License in all respects for all of the FRYSK code and other code
032    // used in conjunction with FRYSK except the Non-GPL Code covered by
033    // this exception. If you modify this file, you may extend this
034    // exception to your version of the file, but you are not obligated to
035    // do so. If you do not wish to provide this exception without
036    // modification, you must delete this exception statement from your
037    // version and license this file solely under the GPL without
038    // exception.
039    
040    package frysk.gui.prefs;
041    
042    import java.util.Iterator;
043    import java.util.LinkedList;
044    import java.util.prefs.Preferences;
045    
046    /**
047     * BoleanPreference models a boolean-valued preference within Frysk
048     *
049     */
050    public class BooleanPreference extends FryskPreference {
051    
052            public interface BooleanPreferenceListener{
053                    void preferenceChanged(String prefName, boolean newValue);
054            }
055    
056        protected boolean value;
057            protected boolean fallback;
058            
059            protected LinkedList listeners;
060            
061            /**
062             * Creates a new BooleanPreference
063             * @param name The name of the preference
064             * @param fallback The default value of the preference
065             */
066            public BooleanPreference(String name, boolean fallback) {
067                    this.name = name;
068                    this.fallback = fallback;
069                    this.listeners = new LinkedList();
070            }
071    
072            /**
073             * 
074             * @return The current value of the preference
075             */
076            public boolean getCurrentValue() {
077                    return this.value;
078            }
079    
080            /**
081             * Sets the value of the preference. Note that this is not saved
082             * in the preferences model permanently until {@see #save(Preferences)}
083             * is called.
084             * @param val The new value
085             */
086            public void setCurrentValue(boolean val) {
087                    value = val;
088            }
089    
090            /**
091             * Saves the value into the model and notifies the attached
092             * listeners.
093             */
094            public void save(Preferences prefs) {
095                    this.model.putBoolean(name, value);
096                    
097                    Iterator it = this.listeners.iterator();
098                    while(it.hasNext())
099                            ((BooleanPreferenceListener) it.next()).preferenceChanged(this.name, this.value);
100            }
101    
102            /**
103             * Sets the preference to use the provided model and loads the value from it
104             */
105            public void load(Preferences prefs) {
106                    this.model = prefs;
107                    this.revert();
108            }
109            
110            /**
111             * Adds a listener that will be notified whenever the value
112             * of the preference is changed
113             * @param listener The object to notify
114             */
115            public void addListener(BooleanPreferenceListener listener){
116                    this.listeners.add(listener);
117                    listener.preferenceChanged(this.name, this.value);
118            }
119    
120            /**
121             * Restores the preference from the value in the model.
122             */
123            public void revert() {
124                    this.value = this.model.getBoolean(name, this.fallback);
125            }
126    }