001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.gui;
024:
025: import java.util.ArrayList;
026: import java.util.Enumeration;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.Vector;
030: import javax.swing.JComponent;
031: import javax.swing.event.ChangeEvent;
032: import javax.swing.event.ChangeListener;
033: import org.skunk.config.Configurator;
034: import org.skunk.trace.Debug;
035:
036: public abstract class PreferencesCustomizer {
037: private String title;
038: private Configurator configurator;
039: private volatile boolean dirty = false;
040: private HashSet configTargets;
041:
042: //public static final String DIRTY_PROPERTY="dirty";
043:
044: private ChangeEvent reusableChangeEvent = new ChangeEvent(this );
045: private Vector changeListeners = new Vector();
046:
047: public PreferencesCustomizer(String title, Configurator configurator) {
048: this .title = title;
049: this .configurator = configurator;
050: this .configTargets = new HashSet();
051: }
052:
053: public void addDirtListener(ChangeListener listener) {
054: changeListeners.add(listener);
055: }
056:
057: public void removeDirtListener(ChangeListener listener) {
058: changeListeners.remove(listener);
059: }
060:
061: public abstract JComponent getCustomizer();
062:
063: protected final boolean isDirty() {
064: return dirty;
065: }
066:
067: protected final void setDirty(boolean dirty) {
068: if (Debug.DEBUG)
069: Debug.trace(this , Debug.DP4, "in setDirty({0})",
070: new Boolean(dirty));
071: this .dirty = dirty;
072: fireChangeEvent();
073: }
074:
075: private void fireChangeEvent()
076: {
077: for (Enumeration enum=changeListeners.elements();enum.hasMoreElements();)
078: {
079: ((ChangeListener)enum.nextElement()).stateChanged(reusableChangeEvent);
080: }
081: }
082:
083: public final String getTitle() {
084: return this .title;
085: }
086:
087: public void ok() {
088: for (Iterator it = getConfigTargets(); it.hasNext();) {
089: Object target = it.next();
090: if (isDirty())
091: configurator.configure(target);
092: configurator.tryOff(target);
093: }
094: setDirty(false);
095: }
096:
097: public void tryOn() {
098: if (isDirty()) {
099: for (Iterator it = getConfigTargets(); it.hasNext();) {
100: Object o = it.next();
101: if (Debug.DEBUG)
102: Debug.trace(this , Debug.DP4,
103: "about to try on target {0}", o);
104: configurator.tryOn(o);
105: }
106: }
107: setDirty(false);
108: }
109:
110: public void revert() {
111: configurator.revert(configTargets.toArray());
112: setDirty(false);
113: }
114:
115: public void cancel() {
116: configurator.revert(configTargets.toArray());
117: setDirty(false);
118: }
119:
120: public void revertToDefault() {
121: for (Iterator it = getConfigTargets(); it.hasNext();) {
122: configurator.revertToDefault(it.next());
123: }
124: setDirty(false);
125: }
126:
127: protected final Iterator getConfigTargets() {
128: return configTargets.iterator();
129: }
130:
131: protected final void removeConfigTarget(Object configTarget) {
132: configTargets.remove(configTarget);
133: }
134:
135: protected final void removeConfigTargetsOfClass(Class c) {
136: synchronized (configTargets) {
137: ArrayList targetsToRemove = new ArrayList();
138: for (Iterator it = getConfigTargets(); it.hasNext();) {
139: Object o = it.next();
140: if (o == null || c.isAssignableFrom(o.getClass()))
141: targetsToRemove.add(o);
142: }
143: for (Iterator it = targetsToRemove.iterator(); it.hasNext();)
144: configTargets.remove(it.next());
145: }
146: }
147:
148: protected final void addConfigTarget(Object configTarget) {
149: configTargets.add(configTarget);
150: }
151:
152: protected final void setConfigValue(Class configClass,
153: String configProperty, Object configValue) {
154: configurator.getStore().setConfigValue(configClass,
155: configProperty, configValue);
156: }
157:
158: protected Configurator getConfigurator() {
159: return configurator;
160: }
161: }
162:
163: /* $Log: PreferencesCustomizer.java,v $
164: /* Revision 1.9 2001/07/26 18:35:33 smulloni
165: /* patched HTTPClient so jikes doesn't issue warnings about catching Exception;
166: /* replaced PropertyChangeEvents with ChangeEvents in PreferencesCustomizer and
167: /* related classes; incremented app version to 1.0.2.3.
168: /*
169: /* Revision 1.8 2001/02/16 18:15:10 smulloni
170: /* many fixes to TextEditorCustomizer. FileMode and SyntaxStyle now have a
171: /* configData property (they will probably be made into sibling classes).
172: /*
173: /* Revision 1.7 2001/02/06 00:11:18 smulloni
174: /* struggle, perhaps futile, with the TextEditorCustomizer and other implicated
175: /* classes
176: /*
177: /* Revision 1.6 2000/12/19 22:36:05 smulloni
178: /* adjustments to preamble.
179: /*
180: /* Revision 1.5 2000/12/03 23:53:26 smulloni
181: /* added license and copyright preamble to java files.
182: /*
183: /* Revision 1.4 2000/11/09 23:34:58 smullyan
184: /* log added to every Java file, with the help of python. Lock stealing
185: /* implemented, and treatment of locks made more robust.
186: /* */
|