KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > person > search > PersonSearchFilterItemEditor


1 /*
2  * Created on Dec 16, 2004
3  * by alex
4  *
5  */

6 package com.nightlabs.ipanema.person.search;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator 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.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 /**
31  * Concrete SearchFilterItemEditor that represents a
32  * criteria for the search of persons.
33  *
34  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
35  */

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 JavaDoc searchFieldList;
41     private Combo comboSearchField;
42     
43     /**
44      * @see com.nightlabs.jdo.search.SearchFilterItemEditor#getControl(org.eclipse.swt.widgets.Composite, int)
45      */

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 JavaDoc t) {
64                     searchFieldList = null;
65                     wrapper.dispose();
66                     wrapper = null;
67                     throw new RuntimeException JavaDoc(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             // TODO: temporär -> ExtensionPoint
77
PersonSearchFilterItemEditorHelperRegistry.getSharedInstance().addItemEditor(TextPersonStructField.class,new TextStructFieldSearchItemEditorHelper());
78             comboSearchField.select(0);
79             onComboChange();
80         }
81         return wrapper;
82     }
83     
84     /**
85      * Builds a list of PersonSearchFilterItemEditorHelper
86      * that are used to build the contents of the search field combo
87      * and the right part of the editor.
88      *
89      * @return
90      */

91     protected List JavaDoc buildSearchFieldList() {
92         List JavaDoc helperList = new ArrayList JavaDoc();
93         for (Iterator JavaDoc iter = PersonStructCache.getOrderedPersonStructBlocks(false).iterator(); iter.hasNext();) {
94             PersonStructBlock structBlock = (PersonStructBlock) iter.next();
95             for (Iterator JavaDoc 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     /**
104      * Delegates to the current PersonSearchFilterItemEditorHelper.
105      *
106      * @see com.nightlabs.jdo.search.SearchFilterItemEditor#getSearchFilterItem()
107      */

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 JavaDoc("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 JavaDoc 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     /**
144      * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
145      */

146     public void widgetSelected(SelectionEvent evt) {
147         if (evt.getSource().equals(comboSearchField)) {
148             onComboChange();
149         }
150     }
151
152     /**
153      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
154      */

155     public void widgetDefaultSelected(SelectionEvent arg0) {
156     }
157
158     /**
159      * @see com.nightlabs.jdo.search.SearchFilterItemEditor#close()
160      */

161     public void close() {
162         comboSearchField.removeSelectionListener(this);
163     }
164
165 }
166
Popular Tags