1 6 package com.nightlabs.ipanema.person.search; 7 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 import org.apache.log4j.Logger; 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.SelectionEvent; 15 import org.eclipse.swt.events.SelectionListener; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.layout.GridLayout; 18 import org.eclipse.swt.widgets.Combo; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 22 import com.nightlabs.ipanema.person.AbstractPersonStructField; 23 import com.nightlabs.ipanema.person.PersonStructBlock; 24 import com.nightlabs.ipanema.person.PersonStructCache; 25 import com.nightlabs.ipanema.person.TextPersonStructField; 26 import com.nightlabs.jdo.search.SearchFilterItem; 27 import com.nightlabs.jdo.search.SearchFilterItemEditor; 28 import com.nightlabs.rcp.composite.TightWrapperComposite; 29 30 36 public class PersonSearchFilterItemEditor extends SearchFilterItemEditor implements SelectionListener{ 37 private static final Logger LOGGER = Logger.getLogger(PersonSearchFilterItemEditor.class); 38 39 private TightWrapperComposite wrapper; 40 private List searchFieldList; 41 private Combo comboSearchField; 42 43 46 public Control getControl(Composite parent) { 47 if (wrapper == null) { 48 wrapper = new TightWrapperComposite(parent,SWT.NONE,true); 49 GridLayout wrapperLayout = (GridLayout)wrapper.getLayout(); 50 wrapperLayout.numColumns = 2; 51 wrapperLayout.makeColumnsEqualWidth = false; 52 53 comboSearchField = new Combo(wrapper, SWT.DROP_DOWN | SWT.READ_ONLY); 54 GridData gdCombo = new GridData(); 55 gdCombo.grabExcessHorizontalSpace = false; 56 gdCombo.horizontalAlignment = GridData.FILL; 57 comboSearchField.setLayoutData(gdCombo); 58 59 if (searchFieldList == null) { 60 try { 61 searchFieldList = buildSearchFieldList(); 62 } 63 catch (Throwable t) { 64 searchFieldList = null; 65 wrapper.dispose(); 66 wrapper = null; 67 throw new RuntimeException (t); 68 } 69 } 70 71 for (int i = 0; i<searchFieldList.size()-1; i++) { 72 PersonSearchFilterItemEditorHelper helper = (PersonSearchFilterItemEditorHelper) searchFieldList.get(i); 73 comboSearchField.add(helper.getDisplayName()); 74 } 75 comboSearchField.addSelectionListener(this); 76 PersonSearchFilterItemEditorHelperRegistry.getSharedInstance().addItemEditor(TextPersonStructField.class,new TextStructFieldSearchItemEditorHelper()); 78 comboSearchField.select(0); 79 onComboChange(); 80 } 81 return wrapper; 82 } 83 84 91 protected List buildSearchFieldList() { 92 List helperList = new ArrayList (); 93 for (Iterator iter = PersonStructCache.getOrderedPersonStructBlocks(false).iterator(); iter.hasNext();) { 94 PersonStructBlock structBlock = (PersonStructBlock) iter.next(); 95 for (Iterator iterator = structBlock.getPersonStructFields().iterator(); iterator.hasNext();) { 96 AbstractPersonStructField structField = (AbstractPersonStructField) iterator.next(); 97 helperList.add(new PersonStructFieldSearchItemEditorManager(structField)); 98 } 99 } 100 return helperList; 101 } 102 103 108 public SearchFilterItem getSearchFilterItem() { 109 return getCurrentHelper().getSearchFilterItem(); 110 } 111 112 113 private PersonSearchFilterItemEditorHelper lastHelper; 114 private int lastIdx = -1; 115 116 private PersonSearchFilterItemEditorHelper getCurrentHelper() { 117 int idx = comboSearchField.getSelectionIndex(); 118 if ((idx < 0) || (idx >= searchFieldList.size())) 119 throw new ArrayIndexOutOfBoundsException ("Selection index of search field combo is out of range of searchFieldList.S"); 120 return (PersonSearchFilterItemEditorHelper) searchFieldList.get(idx); 121 } 122 123 private void onComboChange() { 124 int idx = comboSearchField.getSelectionIndex(); 125 if (idx == lastIdx) 126 return; 127 if (idx < 0) 128 return; 129 PersonSearchFilterItemEditorHelper helper = getCurrentHelper(); 130 if (lastHelper != null) { 131 lastHelper.close(); 132 try { 133 lastHelper.getControl(null).dispose(); 134 } catch (Throwable t) { 135 LOGGER.error("Error disposing helper control.",t); 136 } 137 } 138 helper.getControl(wrapper); 139 wrapper.layout(); 140 lastIdx = idx; 141 lastHelper = helper; 142 } 143 146 public void widgetSelected(SelectionEvent evt) { 147 if (evt.getSource().equals(comboSearchField)) { 148 onComboChange(); 149 } 150 } 151 152 155 public void widgetDefaultSelected(SelectionEvent arg0) { 156 } 157 158 161 public void close() { 162 comboSearchField.removeSelectionListener(this); 163 } 164 165 } 166 | Popular Tags |