1 6 package com.nightlabs.ipanema.person.edit.blockbased; 7 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import java.util.LinkedList ; 11 import java.util.List ; 12 13 import org.apache.log4j.Logger; 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.widgets.Composite; 16 import org.eclipse.ui.forms.widgets.ScrolledForm; 17 18 import com.nightlabs.ipanema.person.PersonDataBlockGroup; 19 import com.nightlabs.ipanema.person.PersonDataBlockNotFoundException; 20 import com.nightlabs.ipanema.person.edit.PersonDataFieldEditor; 21 import com.nightlabs.rcp.composite.OrdinaryWrapperComposite; 22 import com.nightlabs.rcp.composite.TightWrapperComposite; 23 import com.nightlabs.rcp.extensionpoint.EPProcessorException; 24 25 28 public class PersonDataBlockGroupEditor 29 extends OrdinaryWrapperComposite 30 implements PersonDataBlockEditorChangedListener 31 { 32 33 private static final Logger LOGGER = Logger.getLogger(PersonDataBlockGroupEditor.class); 34 35 private PersonDataBlockGroup blockGroup; 36 37 41 public PersonDataBlockGroupEditor( 42 PersonDataBlockGroup blockGroup, 43 Composite parent 44 ) { 45 super(parent, SWT.NONE, true); 46 this.blockGroup = blockGroup; 47 48 wrapperComposite = new TightWrapperComposite(this,SWT.NONE,true); 49 50 createDataBlockEditors(wrapperComposite); 51 } 52 53 TightWrapperComposite wrapperComposite; 54 55 private List personDataBlockEditors = new LinkedList (); 56 57 public void refresh(PersonDataBlockGroup blockGroup) { 58 this.blockGroup = blockGroup; 59 createDataBlockEditors(wrapperComposite); 60 for (int i=0; i<personDataBlockEditors.size(); i++){ 61 PersonDataBlockEditor dataBlockEditor = (PersonDataBlockEditor)personDataBlockEditors.get(i); 62 try { 63 dataBlockEditor.refresh(blockGroup.getPersonDataBlock(i)); 64 } catch (PersonDataBlockNotFoundException e) { 65 IllegalStateException ill = new IllegalStateException ("No no datablock found on pos "+i); 66 ill.initCause(e); 67 throw ill; 68 } 69 } 70 } 71 72 73 protected void createDataBlockEditors(Composite wrapperComp) { 74 if (personDataBlockEditors.size() != blockGroup.getPersonDataBlocks().size()) { 75 int i = 0;; 76 int j = 0; 77 for (i=0; i<blockGroup.getPersonDataBlocks().size(); i++){ 78 if (personDataBlockEditors.size() <= i) { 79 try { 80 PersonDataBlockEditor blockEditor = PersonDataBlockEditorFactoryRegistry.getSharedInstace().getPersonDataBlockEditor( 81 blockGroup.getPersonDataBlock(0), 83 wrapperComp, 84 SWT.NONE, 85 2 86 ); 87 blockEditor.addPersonDataBlockEditorChangedListener(this); 88 personDataBlockEditors.add(blockEditor); 89 } catch (PersonDataBlockNotFoundException e) { 90 LOGGER.error("Could not find PersonDataBlock (idx = 0) for "+blockGroup.getStructBlockKey()); 91 } catch (EPProcessorException e1) { 92 LOGGER.error("Caught EPProcessorException when trying to findnot find PersonDataBlock (idx = 0) for "+blockGroup.getStructBlockKey()); 93 } 94 } 95 j = i; 96 } 97 for (int k=personDataBlockEditors.size()-1; k>j; k--) { 98 PersonDataBlockEditor dataBlockEditor = (PersonDataBlockEditor)personDataBlockEditors.get(k); 99 dataBlockEditor.dispose(); 100 personDataBlockEditors.remove(k); 101 } 102 } 103 104 } 105 106 107 private ScrolledForm owner = null; 108 109 public void setOwner(ScrolledForm owner) { 110 this.owner = owner; 111 } 112 113 private Collection changeListener = new LinkedList (); 114 public synchronized void addPersonDataBlockEditorChangedListener(PersonDataBlockEditorChangedListener listener) { 115 changeListener.add(listener); 116 } 117 public synchronized void removePersonDataBlockEditorChangedListener(PersonDataBlockEditorChangedListener listener) { 118 changeListener.add(listener); 119 } 120 protected synchronized void notifyChangeListeners(PersonDataBlockEditor dataBlockEditor, PersonDataFieldEditor dataFieldEditor) { 121 for (Iterator it = changeListener.iterator(); it.hasNext(); ) { 122 PersonDataBlockEditorChangedListener listener = (PersonDataBlockEditorChangedListener)it.next(); 123 listener.personDataBlockEditorChanged(dataBlockEditor,dataFieldEditor); 124 } 125 } 126 129 public void personDataBlockEditorChanged(PersonDataBlockEditor dataBlockEditor, PersonDataFieldEditor dataFieldEditor) { 130 notifyChangeListeners(dataBlockEditor,dataFieldEditor); 131 } 132 133 public void updatePerson() { 134 for (Iterator it = personDataBlockEditors.iterator(); it.hasNext(); ) { 135 PersonDataBlockEditor blockEditor = (PersonDataBlockEditor)it.next(); 136 blockEditor.updatePerson(); 137 } 138 } 139 140 } 141 | Popular Tags |