KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > person > edit > blockbased > special > CommentDataBlockEditor


1 /*
2  * Created on Jan 23, 2005
3  * by alex
4  *
5  */

6 package com.nightlabs.ipanema.person.edit.blockbased.special;
7
8 import java.util.Iterator JavaDoc;
9 import java.util.LinkedList JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
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.Label;
21 import org.eclipse.swt.widgets.Text;
22
23 import com.nightlabs.ipanema.person.AbstractPersonDataField;
24 import com.nightlabs.ipanema.person.PersonDataBlock;
25 import com.nightlabs.ipanema.person.PersonDataFieldNotFoundException;
26 import com.nightlabs.ipanema.person.PersonStruct;
27 import com.nightlabs.ipanema.person.TextPersonDataField;
28 import com.nightlabs.ipanema.person.edit.DataFieldEditorChangeListener;
29 import com.nightlabs.ipanema.person.edit.PersonDataFieldEditor;
30 import com.nightlabs.ipanema.person.edit.PersonDataFieldEditorFactory;
31 import com.nightlabs.ipanema.person.edit.blockbased.ExpandableBlocksPersonEditor;
32 import com.nightlabs.ipanema.person.edit.blockbased.PersonDataBlockEditor;
33 import com.nightlabs.ipanema.person.edit.blockbased.PersonDataBlockEditorFactory;
34 import com.nightlabs.ipanema.person.id.PersonStructBlockID;
35
36 /**
37  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
38  *
39  */

40 public class CommentDataBlockEditor extends PersonDataBlockEditor
41     implements
42         ModifyListener,
43         PersonDataFieldEditor
44 {
45     
46     public static final Logger LOGGER = Logger.getLogger(CommentDataBlockEditor.class);
47
48     private Text textComment;
49     private Label labelTitle;
50     private Composite wrapper;
51     
52     /**
53      * @param dataBlock
54      * @param parent
55      * @param style
56      */

57     public CommentDataBlockEditor(PersonDataBlock dataBlock, Composite parent, int style) {
58         super(dataBlock, parent, style);
59         setLayoutData(new GridData(GridData.FILL_BOTH));
60         GridLayout thisLayout = new GridLayout();
61         thisLayout.horizontalSpacing = 2;
62         thisLayout.verticalSpacing = 2;
63         this.setLayout(thisLayout);
64         refresh(dataBlock);
65     }
66
67     /**
68      * @see com.nightlabs.ipanema.person.edit.blockbased.PersonDataBlockEditor#refresh(com.nightlabs.ipanema.person.PersonDataBlock)
69      */

70     public void refresh(PersonDataBlock block) {
71         this.dataBlock = block;
72         try {
73             commentData = (TextPersonDataField)dataBlock.getPersonDataField(PersonStruct.COMMENT_COMMENT);
74             refresh();
75         } catch (PersonDataFieldNotFoundException e) {
76             LOGGER.error("PersonDataField not found. ",e);
77             commentData = null;
78         }
79     }
80
81     
82     private boolean refreshing = false;
83     /**
84      * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
85      */

86     public void modifyText(ModifyEvent evt) {
87         setChanged(true);
88     }
89
90     /**
91      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#getTargetPersonDataType()
92      */

93     public Class JavaDoc getTargetPersonDataType() {
94         return TextPersonDataField.class;
95     }
96
97     /**
98      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#getEditorType()
99      */

100     public String JavaDoc getEditorType() {
101         return ExpandableBlocksPersonEditor.EDITORTYPE_BLOCK_BASED_EXPANDABLE;
102     }
103
104     /**
105      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#createControl(org.eclipse.swt.widgets.Composite)
106      */

107     public Control createControl(Composite parent) {
108         if (textComment == null) {
109             labelTitle = new Label(parent,SWT.NONE);
110             labelTitle.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111             
112             textComment = new Text(parent,SWT.MULTI);
113             GridData commentLData = new GridData();
114             commentLData.grabExcessHorizontalSpace = true;
115             commentLData.grabExcessVerticalSpace = true;
116             commentLData.horizontalAlignment = GridData.FILL;
117             commentLData.verticalAlignment = GridData.FILL;
118             textComment.setLayoutData(commentLData);
119         }
120         return this;
121     }
122
123     /**
124      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#getNewEditorInstance(com.nightlabs.ipanema.person.AbstractPersonDataField)
125      */

126     public PersonDataFieldEditor getNewEditorInstance(AbstractPersonDataField data) {
127         return new CommentDataBlockEditor(dataBlock,getParent(),getStyle());
128     }
129
130     private TextPersonDataField commentData;
131     /**
132      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#setData(com.nightlabs.ipanema.person.AbstractPersonDataField)
133      */

134     public void setData(AbstractPersonDataField data) {
135         commentData = (TextPersonDataField)data;
136         refresh();
137     }
138
139     /**
140      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#refresh()
141      */

142     public void refresh() {
143         refreshing = true;
144         try {
145             createControl(this);
146             if (commentData != null) {
147                 if (commentData.getText() == null)
148                     textComment.setText("");
149                 else
150                     textComment.setText(commentData.getText());
151             }
152         } finally {
153             refreshing = false;
154         }
155     }
156
157     private List JavaDoc fieldEditorChangeListener = new LinkedList JavaDoc();
158     
159     /**
160      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#addDataFieldEditorChangedListener(com.nightlabs.ipanema.person.edit.DataFieldEditorChangeListener)
161      */

162     public void addDataFieldEditorChangedListener(DataFieldEditorChangeListener listener) {
163         fieldEditorChangeListener.add(listener);
164     }
165
166     /**
167      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#removeDataFieldEditorChangedListener(com.nightlabs.ipanema.person.edit.DataFieldEditorChangeListener)
168      */

169     public void removeDataFieldEditorChangedListener(DataFieldEditorChangeListener listener) {
170         fieldEditorChangeListener.remove(listener);
171     }
172
173     
174     private boolean changed = false;
175     /**
176      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#setChanged(boolean)
177      */

178     public void setChanged(boolean changed) {
179         this.changed = changed;
180         if (!refreshing)
181             notifyChangeListeners(this);
182     }
183     
184     protected synchronized void notifyChangeListeners(PersonDataFieldEditor dataFieldEditor) {
185         if (refreshing)
186             return;
187         // first notify fieldEditorChangeListener
188
for (Iterator JavaDoc iter = fieldEditorChangeListener.iterator(); iter.hasNext();) {
189             DataFieldEditorChangeListener listener = (DataFieldEditorChangeListener) iter.next();
190             listener.dataFieldEditorChanged(this);
191         }
192         // then blockEditorChangeListener
193
super.notifyChangeListeners(dataFieldEditor);
194     }
195
196     /**
197      * @see com.nightlabs.ipanema.person.edit.PersonDataFieldEditor#isChanged()
198      */

199     public boolean isChanged() {
200         return changed;
201     }
202
203     
204     public static class Factory implements PersonDataBlockEditorFactory {
205         /**
206          * @see com.nightlabs.ipanema.person.edit.blockbased.PersonDataBlockEditorFactory#getProviderStructBlockID()
207          */

208         public PersonStructBlockID getProviderStructBlockID() {
209             return PersonStruct.COMMENT;
210         }
211         
212         /**
213          * @see com.nightlabs.ipanema.person.edit.blockbased.PersonDataBlockEditorFactory#createPersonDataBlockEditor(com.nightlabs.ipanema.person.PersonDataBlock, org.eclipse.swt.widgets.Composite, int)
214          */

215         public PersonDataBlockEditor createPersonDataBlockEditor(PersonDataBlock dataBlock, Composite parent, int style) {
216             return new CommentDataBlockEditor(dataBlock,parent,style);
217         }
218     }
219
220
221     public Control getControl() {
222         return this;
223     }
224
225     protected PersonDataFieldEditorFactory factory;
226     
227     public void setPersonDataFieldEditorFactory(PersonDataFieldEditorFactory factory) {
228         this.factory = factory;
229     }
230
231     public PersonDataFieldEditorFactory getPersonDataFieldEditorFactory() {
232         return factory;
233     }
234
235 }
236
Popular Tags