1 6 package com.nightlabs.ipanema.person.edit.blockbased.special; 7 8 import java.util.Iterator ; 9 import java.util.LinkedList ; 10 import java.util.List ; 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 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 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 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 86 public void modifyText(ModifyEvent evt) { 87 setChanged(true); 88 } 89 90 93 public Class getTargetPersonDataType() { 94 return TextPersonDataField.class; 95 } 96 97 100 public String getEditorType() { 101 return ExpandableBlocksPersonEditor.EDITORTYPE_BLOCK_BASED_EXPANDABLE; 102 } 103 104 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 126 public PersonDataFieldEditor getNewEditorInstance(AbstractPersonDataField data) { 127 return new CommentDataBlockEditor(dataBlock,getParent(),getStyle()); 128 } 129 130 private TextPersonDataField commentData; 131 134 public void setData(AbstractPersonDataField data) { 135 commentData = (TextPersonDataField)data; 136 refresh(); 137 } 138 139 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 fieldEditorChangeListener = new LinkedList (); 158 159 162 public void addDataFieldEditorChangedListener(DataFieldEditorChangeListener listener) { 163 fieldEditorChangeListener.add(listener); 164 } 165 166 169 public void removeDataFieldEditorChangedListener(DataFieldEditorChangeListener listener) { 170 fieldEditorChangeListener.remove(listener); 171 } 172 173 174 private boolean changed = false; 175 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 for (Iterator iter = fieldEditorChangeListener.iterator(); iter.hasNext();) { 189 DataFieldEditorChangeListener listener = (DataFieldEditorChangeListener) iter.next(); 190 listener.dataFieldEditorChanged(this); 191 } 192 super.notifyChangeListeners(dataFieldEditor); 194 } 195 196 199 public boolean isChanged() { 200 return changed; 201 } 202 203 204 public static class Factory implements PersonDataBlockEditorFactory { 205 208 public PersonStructBlockID getProviderStructBlockID() { 209 return PersonStruct.COMMENT; 210 } 211 212 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 |