001: package org.columba.contact.search;
002:
003: import java.text.MessageFormat;
004: import java.util.Iterator;
005: import java.util.List;
006: import java.util.ResourceBundle;
007: import java.util.Vector;
008:
009: import javax.swing.ImageIcon;
010:
011: import org.columba.addressbook.folder.AddressbookFolder;
012: import org.columba.addressbook.folder.virtual.VirtualFolder;
013: import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
014: import org.columba.addressbook.gui.search.BasicResultPanel;
015: import org.columba.addressbook.gui.search.ComplexResultPanel;
016: import org.columba.addressbook.model.IContactModel;
017: import org.columba.api.gui.frame.IFrameMediator;
018: import org.columba.api.plugin.PluginLoadingFailedException;
019: import org.columba.core.gui.frame.FrameManager;
020: import org.columba.core.gui.search.StringCriteriaRenderer;
021: import org.columba.core.gui.search.api.ICriteriaRenderer;
022: import org.columba.core.gui.search.api.IResultPanel;
023: import org.columba.core.resourceloader.IconKeys;
024: import org.columba.core.resourceloader.ImageLoader;
025: import org.columba.core.search.SearchCriteria;
026: import org.columba.core.search.api.ISearchCriteria;
027: import org.columba.core.search.api.ISearchProvider;
028: import org.columba.core.search.api.ISearchRequest;
029: import org.columba.core.search.api.ISearchResult;
030:
031: public class ContactSearchProvider implements ISearchProvider {
032: public static final String CRITERIA_DISPLAYNAME_CONTAINS = "displayname_contains";
033:
034: public static final String CRITERIA_EMAIL_CONTAINS = "email_contains";
035:
036: private ResourceBundle bundle;
037:
038: private int totalResultCount = 0;
039:
040: private VirtualFolder virtualFolder;
041:
042: public ContactSearchProvider() {
043: bundle = ResourceBundle
044: .getBundle("org.columba.addressbook.i18n.search");
045: }
046:
047: public String getTechnicalName() {
048: return "ContactSearchProvider";
049: }
050:
051: public String getName() {
052: return bundle.getString("provider_title");
053: }
054:
055: public String getDescription() {
056: return bundle.getString("provider_description");
057: }
058:
059: public ImageIcon getIcon() {
060: return ImageLoader.getSmallIcon(IconKeys.ADDRESSBOOK);
061: }
062:
063: public List<ISearchCriteria> getAllCriteria(String searchTerm) {
064:
065: List<ISearchCriteria> list = new Vector<ISearchCriteria>();
066:
067: list.add(getCriteria(
068: ContactSearchProvider.CRITERIA_EMAIL_CONTAINS,
069: searchTerm));
070: list.add(getCriteria(
071: ContactSearchProvider.CRITERIA_DISPLAYNAME_CONTAINS,
072: searchTerm));
073: return list;
074: }
075:
076: public IResultPanel getResultPanel(
077: String searchCriteriaTechnicalName) {
078: return new BasicResultPanel(getTechnicalName(),
079: searchCriteriaTechnicalName);
080: }
081:
082: public IResultPanel getComplexResultPanel() {
083: return new ComplexResultPanel(getTechnicalName());
084: }
085:
086: public ISearchCriteria getCriteria(String technicalName,
087: String searchTerm) {
088: String title = MessageFormat.format(bundle
089: .getString(technicalName + "_title"),
090: new Object[] { searchTerm });
091: String name = bundle.getString(technicalName + "_name");
092: String description = MessageFormat.format(bundle
093: .getString(technicalName + "_description"),
094: new Object[] { searchTerm });
095:
096: return new SearchCriteria(technicalName, name, title,
097: description);
098: }
099:
100: public List<ISearchResult> query(String searchTerm,
101: String criteriaTechnicalName, boolean searchInside,
102: int startIndex, int resultCount) {
103: if (searchTerm == null)
104: throw new IllegalArgumentException("searchTerm == null");
105: if (criteriaTechnicalName == null)
106: throw new IllegalArgumentException(
107: "criteriaTechnicalName == null");
108:
109: List<ISearchResult> result = new Vector<ISearchResult>();
110:
111: // create list of contact folders
112: List<AddressbookFolder> v = SearchUtility
113: .createContactFolderList();
114:
115: virtualFolder = new VirtualFolder();
116: Iterator<AddressbookFolder> it = v.iterator();
117: while (it.hasNext()) {
118: AddressbookFolder f = it.next();
119: String id = null;
120:
121: if (criteriaTechnicalName
122: .equals(ContactSearchProvider.CRITERIA_DISPLAYNAME_CONTAINS)) {
123: id = f.findByName(searchTerm);
124: } else if (criteriaTechnicalName
125: .equals(ContactSearchProvider.CRITERIA_EMAIL_CONTAINS)) {
126: id = f.findByEmailAddress(searchTerm);
127: }
128:
129: if (id != null) {
130: IContactModel model = f.get(id);
131:
132: result.add(new ContactSearchResult(model
133: .getSortString(), model.getPreferredEmail(),
134: SearchResultBuilder.createURI(f.getId(), id),
135: model));
136:
137: // remember search result
138: virtualFolder.add(f, id);
139: }
140:
141: }
142:
143: totalResultCount = result.size();
144:
145: return result;
146: }
147:
148: public List<ISearchResult> query(List<ISearchRequest> list,
149: boolean matchAll, boolean searchInside, int startIndex,
150: int resultCount) {
151: Vector<ISearchResult> v = new Vector<ISearchResult>();
152: Iterator<ISearchRequest> it = list.listIterator();
153: while (it.hasNext()) {
154: ISearchRequest request = it.next();
155:
156: List<ISearchResult> result = query(request.getSearchTerm(),
157: request.getCriteria(), searchInside, startIndex,
158: resultCount);
159:
160: v.addAll(result);
161: }
162:
163: return v;
164: }
165:
166: public int getTotalResultCount() {
167: return totalResultCount;
168: }
169:
170: public void showAllResults(IFrameMediator mediator,
171: String searchTerm, String searchCriteriaTechnicalName) {
172:
173: // ensure that we are currently in the contact component
174: IFrameMediator newMediator = null;
175: try {
176: newMediator = FrameManager.getInstance().switchView(
177: mediator.getContainer(), "Addressbook");
178: } catch (PluginLoadingFailedException e) {
179: e.printStackTrace();
180: }
181:
182: // update folder selection
183: ((AddressbookFrameMediator) newMediator).getTree()
184: .setSelectedFolder(virtualFolder);
185: }
186:
187: public ICriteriaRenderer getCriteriaRenderer(
188: String criteriaTechnicalName) {
189: ICriteriaRenderer r = null;
190: if (criteriaTechnicalName
191: .equals(ContactSearchProvider.CRITERIA_DISPLAYNAME_CONTAINS)) {
192: r = new StringCriteriaRenderer(
193: getCriteria(
194: ContactSearchProvider.CRITERIA_DISPLAYNAME_CONTAINS,
195: ""), this );
196: } else if (criteriaTechnicalName
197: .equals(ContactSearchProvider.CRITERIA_EMAIL_CONTAINS)) {
198: r = new StringCriteriaRenderer(getCriteria(
199: ContactSearchProvider.CRITERIA_EMAIL_CONTAINS, ""),
200: this );
201: }
202:
203: return r;
204: }
205:
206: public ISearchCriteria getDefaultCriteria(String searchTerm) {
207: return getCriteria(
208: ContactSearchProvider.CRITERIA_EMAIL_CONTAINS,
209: searchTerm);
210: }
211:
212: public boolean hasSingleCriteriaSearchResult() {
213: // doesn't support search inside yet
214: return false;
215: }
216:
217: }
|