KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > person > edit > AbstractPersonDataFieldEditor


1 /*
2  * Created on Nov 26, 2004
3  * by alex
4  *
5  */

6 package com.nightlabs.ipanema.person.edit;
7
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.LinkedList JavaDoc;
11
12 import org.eclipse.swt.events.ModifyEvent;
13 import org.eclipse.swt.events.ModifyListener;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16
17 import com.nightlabs.ipanema.person.AbstractPersonDataField;
18 import com.nightlabs.ipanema.person.AbstractPersonStructField;
19 import com.nightlabs.ipanema.person.PersonStructCache;
20
21 /**
22  * Abstract base class for all {@link PersonDataFieldEditor}s with implementations
23  * for the listener stuff and other common things for all field editors.<br/>
24  * This class as well already implements ModifyListener so it can be used as
25  * listener for Text Widgets.
26  *
27  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
28  */

29 public abstract class AbstractPersonDataFieldEditor implements PersonDataFieldEditor, ModifyListener {
30
31     /**
32      * Default constructor does nothing.
33      */

34     public AbstractPersonDataFieldEditor() { }
35     
36     /**
37      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#createControl(org.eclipse.swt.widgets.Composite)
38      */

39     public abstract Control createControl(Composite parent);
40
41     private AbstractPersonDataField _data;
42     
43     /**
44      * Not intendet to be overridden.<br/>
45      * Sublclasses should set their data in {@link #doSetData(AbstractPersonDataField)}.
46      *
47      * @see #doSetData(AbstractPersonDataField)
48      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#setData(com.nightlabs.ipanema.person.AbstractPersonDataField)
49      */

50     public void setData(AbstractPersonDataField data) {
51         refreshing = true;
52         try {
53             _data = data;
54             doSetData(data);
55         } finally {
56             refreshing = false;
57         }
58         
59     }
60     
61     
62     
63     /**
64      * Subclasses can do things when data changes here.
65      *
66      *@see PersonDataFieldEditor#setData(AbstractPersonDataField)
67      */

68     public abstract void doSetData(AbstractPersonDataField data);
69     
70     /**
71      * Subclasses should perfom refreshing <b>here<b> and not override
72      * {@link #refresh(AbstractPersonDataField)}
73      *
74      * @param data
75      */

76     public abstract void doRefresh();
77     
78     private boolean refreshing = false;
79     
80     /**
81      * Not intendeted to be overridden.
82      *
83      * @see #doRefresh(AbstractPersonDataField)
84      * @param data
85      */

86     public void refresh() {
87         refreshing = true;
88         try {
89             doRefresh();
90         } finally {
91             refreshing = false;
92         }
93     }
94     
95     private Collection JavaDoc changeListener = new LinkedList JavaDoc();
96     /**
97      *
98      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#addDataFieldEditorChangedListener(com.nightlabs.ipanema.person.edit.DataFieldEditorChangeListener)
99      */

100     public synchronized void addDataFieldEditorChangedListener(DataFieldEditorChangeListener listener) {
101         changeListener.add(listener);
102     }
103     /**
104      *
105      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#removeDataFieldEditorChangedListener(com.nightlabs.ipanema.person.edit.DataFieldEditorChangeListener)
106      */

107     public synchronized void removeDataFieldEditorChangedListener(DataFieldEditorChangeListener listener) {
108         changeListener.add(listener);
109     }
110     
111     protected synchronized void notifyChangeListeners() {
112         // TODO: Rewrite to noitfy listener asynchronously
113
for (Iterator JavaDoc it = changeListener.iterator(); it.hasNext(); ) {
114             DataFieldEditorChangeListener listener = (DataFieldEditorChangeListener)it.next();
115             listener.dataFieldEditorChanged(this);
116         }
117     }
118     
119     private boolean changed;
120     
121     /**
122      * Sets the changed state of this editor.
123      *
124      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#setChanged(boolean)
125      */

126     public void setChanged(boolean changed) {
127         this.changed = changed;
128         if (!refreshing) {
129             if (changed) {
130                 notifyChangeListeners();
131             }
132         }
133     }
134     
135     /**
136      * Checks if this editors value has changed.
137      *
138      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#isChanged()
139      */

140     public boolean isChanged() {
141         return changed;
142     }
143     
144     /**
145      * Returns the PersonStructField this editor is
146      * associated with.
147      *
148      * @return
149      */

150     public AbstractPersonStructField getPersonStructField() {
151         try {
152             return PersonStructCache.getPersonStructure().getPersonStructField(
153                 _data.getPersonStructBlockOrganisationID(),
154                 _data.getPersonStructBlockID(),
155                 _data.getPersonStructFieldOrganisationID(),
156                 _data.getPersonStructFieldID()
157             );
158         } catch (Exception JavaDoc e) {
159             IllegalStateException JavaDoc ill = new IllegalStateException JavaDoc("Caught exception while accessing PresonStructFactory.");
160             ill.initCause(e);
161             throw ill;
162         }
163     }
164     
165     public void modifyText(ModifyEvent arg0) {
166         setChanged(true);
167     }
168
169     protected PersonDataFieldEditorFactory factory;
170     
171     /**
172      *
173      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#getPersonDataFieldEditorFactory()
174      */

175     public PersonDataFieldEditorFactory getPersonDataFieldEditorFactory() {
176         return factory;
177     }
178
179     /**
180      *
181      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#setPersonDataFieldEditorFactory(com.nightlabs.ipanema.person.edit.PersonDataFieldEditorFactory)
182      */

183     public void setPersonDataFieldEditorFactory(PersonDataFieldEditorFactory factory) {
184         this.factory = factory;
185     }
186     
187     
188
189 }
190
Popular Tags