KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > person > edit > blockbased > ExpandableBlocksPersonEditor


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

6 package com.nightlabs.ipanema.person.edit.blockbased;
7
8 import java.util.Collections JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.LinkedList JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.ui.forms.widgets.FormToolkit;
22 import org.eclipse.ui.forms.widgets.ScrolledForm;
23
24 import com.nightlabs.ipanema.person.Person;
25 import com.nightlabs.ipanema.person.PersonDataBlockGroup;
26 import com.nightlabs.ipanema.person.PersonStruct;
27 import com.nightlabs.ipanema.person.PersonStructCache;
28 import com.nightlabs.ipanema.person.edit.PersonEditor;
29 import com.nightlabs.ipanema.person.id.PersonStructBlockID;
30 import com.nightlabs.ipanema.person.preferences.PersonStructOrderConfigModule;
31
32 /**
33  * A PersonEditor based onon PersonStructBlocks/PersonDataBlocks.
34  * This will present one expandable Composite with all PersonDataFields
35  * for all PersonStructBlock/PersonDataBlock it gets assosiated with.<br/>
36  * Control wich blocks a editor displays by associating a list of blocks
37  * to a editor-domain.
38  *
39  * @see com.nightlabs.ipanema.person.edit.blockbased.PersonDataBlockEditor
40  * @see com.nightlabs.ipanema.person.edit.blockbased.PersonEditorStructBlockRegistry
41  * @see com.nightlabs.ipanema.person.edit.PersonEditor
42  *
43  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
44  */

45 public class ExpandableBlocksPersonEditor implements PersonEditor { // extends ScrolledComposite {
46
private static Logger LOGGER = Logger.getLogger(ExpandableBlocksPersonEditor.class);
47     public static final String JavaDoc EDITORTYPE_BLOCK_BASED_EXPANDABLE = "block-based-expandable";
48     
49     public ExpandableBlocksPersonEditor() {
50         this (null);
51     }
52     
53     public ExpandableBlocksPersonEditor(Person person) {
54         this.person = person;
55     }
56
57     
58     private FormToolkit toolkit = null;
59     
60     private Person person;
61     /**
62      * Sets the current person of thiss editor.
63      * If refresh is true {@link #refreshForm(PersonDataBlockEditorChangedListener)}
64      * is called.
65      * @param person
66      * @param refresh
67      */

68     public void setPerson(Person person, boolean refresh) {
69         this.person = person;
70         if (refresh)
71             refreshControl();
72     }
73     /**
74      * Will only set the person, no changes to the UI will be made.
75      * @param person
76      */

77     public void setPerson(Person person) {
78         setPerson(person,false);
79     }
80     /**
81      * Returns the person.
82      * @return
83      */

84     public Person getPerson() {
85         return person;
86     }
87
88     /**
89      * Returns a cached version of the {@link PersonStruct}.
90      * @return
91      */

92     protected PersonStruct getPersonStructure() {
93         return PersonStructCache.getPersonStructure();
94     }
95     
96     
97     private ScrolledForm form = null;
98     /**
99      * Returns the {@link ScrolledForm}.
100      * With {@link ScrolledForm#getBody()} you can access the Composite.
101      * @return
102      */

103     public ScrolledForm getForm() {
104         return form;
105     }
106     
107     /**
108      * Holds the GroupEditors.
109      */

110     private Map JavaDoc groupEditors = new HashMap JavaDoc();
111     
112     
113     public Map JavaDoc getGroupEditors() {
114         return groupEditors;
115     }
116     
117 // public void refreshControl() {
118
// refreshControl(null);
119
// }
120

121     /**
122      * Refreshes the UI-Representation of the given Person.
123      *
124      * @param changeListener
125      */

126     public void refreshControl() {
127         Display.getDefault().asyncExec(
128             new Runnable JavaDoc() {
129                 public void run() {
130                     getPersonStructure().explodePerson(person);
131                     
132                     // get the ordered dataBlocks
133
for (Iterator JavaDoc it = ExpandableBlocksPersonEditor.this.getOrderedDataBlockGroupsIterator(); it.hasNext(); ) {
134                         PersonDataBlockGroup blockGroup = (PersonDataBlockGroup)it.next();
135                         if (shouldDisplayStructBlock(blockGroup)) {
136                             if (!groupEditors.containsKey(blockGroup.getStructBlockKey())) {
137                                 ExpandablePersonDataBlockGroupEditor groupEditor = new ExpandablePersonDataBlockGroupEditor(blockGroup, form.getBody());
138                                 groupEditor.setOwner(form);
139                                 if (ExpandableBlocksPersonEditor.this.changeListener != null)
140                                     groupEditor.addPersonDataBlockEditorChangedListener(ExpandableBlocksPersonEditor.this.changeListener);
141                                 groupEditors.put(blockGroup.getStructBlockKey(),groupEditor);
142                             }
143                             else {
144                                 ExpandablePersonDataBlockGroupEditor groupEditor = (ExpandablePersonDataBlockGroupEditor)groupEditors.get(blockGroup.getStructBlockKey());
145                                 groupEditor.refresh(blockGroup);
146                             }
147                         } // if (shouldDisplayStructBlock(blockGroup)) {
148
}
149                     form.reflow(true);
150                 }
151             }
152         );
153     }
154     
155     private PersonDataBlockEditorChangedListener changeListener;
156     
157     public ScrolledForm createForm(Composite parent, PersonDataBlockEditorChangedListener changeListener, boolean refresh) {
158         return (ScrolledForm)createControl(parent,changeListener,refresh);
159     }
160     /**
161      * Creates the Form.
162      * If refresh is true {@link #refreshForm(PersonDataBlockEditorChangedListener)}
163      * will be called.
164      *
165      * @param parent
166      * @param changeListener
167      * @param refresh
168      * @return
169      */

170     public Control createControl(Composite parent, PersonDataBlockEditorChangedListener changeListener, boolean refresh) {
171         if (form != null)
172             return form;
173         if (toolkit == null)
174             toolkit = new FormToolkit(parent.getDisplay());
175         this.changeListener = changeListener;
176         
177         form = toolkit.createScrolledForm(parent);
178         form.setBackground(parent.getBackground());
179         form.setForeground(parent.getForeground());
180         GridLayout layout = new GridLayout();
181         layout.horizontalSpacing = 0;
182         layout.verticalSpacing = 0;
183         layout.marginHeight = 0;
184         layout.marginWidth = 0;
185         form.getBody().setLayout(layout);
186         GridData gd = new GridData(GridData.FILL_BOTH);
187         form.getBody().setLayoutData(gd);
188         
189         if (refresh)
190             refreshControl();
191         return form;
192     }
193     
194     /**
195      *
196      * @see com.nightlabs.ipanema.person.edit.PersonEditor#disposeControl()
197      */

198     public void disposeControl() {
199         if (form != null && !form.isDisposed()) {
200             form.dispose();
201             form = null;
202         }
203     }
204
205     /**
206      * Will only create the Form. No person data will be displayed
207      *
208      * @param parent
209      * @param changeListener
210      * @return
211      */

212     public ScrolledForm createForm(Composite parent, PersonDataBlockEditorChangedListener changeListener) {
213         return createForm(parent,changeListener,false);
214     }
215     
216     /**
217      * Will create the form. No change listener will be set and
218      * no person data will be displayed.
219      *
220      * @param parent
221      * @return
222      */

223     public ScrolledForm createForm(Composite parent) {
224         return createForm(parent, null);
225     }
226     
227     public ScrolledForm createForm(Composite parent, boolean refresh) {
228         return createForm(parent, null, refresh);
229     }
230
231     public Control createControl(Composite parent, boolean refresh) {
232         return createForm(parent, refresh);
233     }
234
235     private String JavaDoc editorScope;
236     private String JavaDoc editorName;
237     
238     /**
239      * Set the scope and the name of the editor.
240      * This can be used by to limit the PersonStructBlocks
241      * a editor shows by registering it in the {@link PersonEditorStructBlockRegistry}
242      * and calling this function with the appropriate values.<br/>
243      * Default will be all PersonStructBlocks.
244      *
245      * @param editorScope
246      * @param editorName
247      */

248     public void setEditorDomain(String JavaDoc editorScope, String JavaDoc editorName) {
249         this.editorScope = editorScope;
250         this.editorName = editorName;
251     }
252     
253     /**
254      * Sets the editor domain for this editor and additionally
255      * registeres structBlocks to display in {@link PersonE}
256      * @param editorScope
257      * @param editorName
258      * @param personStructBlockKeys
259      */

260     public void setEditorDomain(String JavaDoc editorScope, String JavaDoc editorName, PersonStructBlockID[] personStructBlockKeys) {
261         setEditorDomain(editorScope,editorName);
262         PersonEditorStructBlockRegistry.getSharedInstance().addEditorStructBlocks(editorScope,editorName,personStructBlockKeys);
263     }
264     
265     private List JavaDoc domainPersonStructBlocks;
266     
267     protected boolean shouldDisplayStructBlock(PersonDataBlockGroup blockGroup) {
268         // default is all PersonStructBlocks
269
if (domainPersonStructBlocks == null)
270             return true;
271         else
272             return domainPersonStructBlocks.contains(PersonStructBlockID.create(blockGroup.getPersonStructBlockOrganisationID(),blockGroup.getPersonStructBlockID()));
273     }
274     
275     protected void buildDomainDataBlockGroups() {
276         if (domainPersonStructBlocks == null) {
277             if ((editorScope != null ) && (editorName != null)) {
278                 List JavaDoc structBlockList = PersonEditorStructBlockRegistry.getSharedInstance().getEditorStructBlocks(editorScope,editorName);
279                 if (!structBlockList.isEmpty())
280                     domainPersonStructBlocks = structBlockList;
281             }
282         }
283     }
284     
285     /**
286      * Shortcut of setting the list of PersonStructBlocks
287      * this editor should display.
288      * After this was set to a non null value this editor
289      * will not care about registrations in {@link PersonEditorStructBlockRegistry}.
290      *
291      * @param structBlockList
292      */

293     public void setEditorPersonStructBlockList(List JavaDoc structBlockList) {
294         if (structBlockList != null) {
295             if (structBlockList.size() > 0)
296                 domainPersonStructBlocks = structBlockList;
297             else
298                 domainPersonStructBlocks = null;
299         } else {
300             domainPersonStructBlocks = null;
301         }
302     }
303     
304     
305     protected Iterator JavaDoc getDataBlockGroupsIterator() {
306         buildDomainDataBlockGroups();
307         return person.getPersonDataBlockGroups().iterator();
308     }
309     
310     public Map JavaDoc getStructBlockDisplayOrder() {
311         return PersonStructOrderConfigModule.getSharedInstance().structBlockDisplayOrder();
312     }
313     
314     protected Iterator JavaDoc getOrderedDataBlockGroupsIterator() {
315         buildDomainDataBlockGroups();
316     
317         int allStructBlockCount = getPersonStructure().getPersonStructBlocks().size();
318         List JavaDoc result = new LinkedList JavaDoc();
319         Map JavaDoc structBlockOrder = getStructBlockDisplayOrder();
320         
321         int maxIndex = 0;
322         int unmentionedCount = 0;
323         // all datablocks of this person
324
for (Iterator JavaDoc it = person.getPersonDataBlockGroups().iterator(); it.hasNext(); ) {
325             PersonDataBlockGroup blockGroup = (PersonDataBlockGroup)it.next();
326             boolean orderedAdd = false;
327             if (structBlockOrder.containsKey(blockGroup.getStructBlockKey())) {
328                 // block mentioned in structBlockOrder
329
Integer JavaDoc index = (Integer JavaDoc)structBlockOrder.get(blockGroup.getStructBlockKey());
330                 blockGroup.setPriority(index.intValue());
331             }
332             else {
333                 blockGroup.setPriority(allStructBlockCount + (unmentionedCount++));
334             }
335             result.add(blockGroup);
336         }
337         Collections.sort(result);
338         return result.iterator();
339     }
340
341     public void updatePerson() {
342         for (Iterator JavaDoc it = groupEditors.values().iterator(); it.hasNext(); ) {
343             ExpandablePersonDataBlockGroupEditor groupEditor = (ExpandablePersonDataBlockGroupEditor)it.next();
344             groupEditor.updatePerson();
345         }
346     }
347     
348 }
349
Popular Tags