KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > search > CalendarSearchProvider


1 package org.columba.calendar.search;
2
3 import java.text.MessageFormat JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.ResourceBundle JavaDoc;
7 import java.util.Vector JavaDoc;
8
9 import javax.swing.ImageIcon JavaDoc;
10
11 import org.columba.api.gui.frame.IFrameMediator;
12 import org.columba.calendar.model.api.IComponent;
13 import org.columba.calendar.model.api.IComponentInfo;
14 import org.columba.calendar.model.api.IEventInfo;
15 import org.columba.calendar.model.api.ITodoInfo;
16 import org.columba.calendar.store.CalendarStoreFactory;
17 import org.columba.calendar.store.api.ICalendarStore;
18 import org.columba.calendar.ui.search.BasicResultPanel;
19 import org.columba.core.gui.search.StringCriteriaRenderer;
20 import org.columba.core.gui.search.api.ICriteriaRenderer;
21 import org.columba.core.gui.search.api.IResultPanel;
22 import org.columba.core.resourceloader.IconKeys;
23 import org.columba.core.resourceloader.ImageLoader;
24 import org.columba.core.search.SearchCriteria;
25 import org.columba.core.search.api.ISearchCriteria;
26 import org.columba.core.search.api.ISearchProvider;
27 import org.columba.core.search.api.ISearchRequest;
28 import org.columba.core.search.api.ISearchResult;
29
30 public class CalendarSearchProvider implements ISearchProvider {
31
32     public static final String JavaDoc CRITERIA_SUMMARY_CONTAINS = "summary_contains";
33
34     private ResourceBundle JavaDoc bundle;
35
36     private int totalResultCount = 0;
37
38     public CalendarSearchProvider() {
39         bundle = ResourceBundle.getBundle("org.columba.calendar.i18n.search");
40     }
41
42     public String JavaDoc getTechnicalName() {
43         return "CalendarSearchProvider";
44     }
45
46     public String JavaDoc getName() {
47         return bundle.getString("provider_title");
48     }
49
50     public String JavaDoc getDescription() {
51         return bundle.getString("provider_description");
52     }
53
54     public ImageIcon JavaDoc getIcon() {
55         return ImageLoader.getSmallIcon(IconKeys.CALENDAR);
56     }
57
58     public List JavaDoc<ISearchCriteria> getAllCriteria(String JavaDoc searchTerm) {
59         List JavaDoc<ISearchCriteria> list = new Vector JavaDoc<ISearchCriteria>();
60
61         list.add(getCriteria(CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS,
62                 searchTerm));
63
64         return list;
65     }
66
67     public IResultPanel getComplexResultPanel() {
68         return new ComplexResultPanel(getTechnicalName());
69     }
70
71     public ISearchCriteria getCriteria(String JavaDoc technicalName, String JavaDoc searchTerm) {
72         String JavaDoc title = MessageFormat.format(bundle.getString(technicalName
73                 + "_title"), new Object JavaDoc[] { searchTerm });
74         String JavaDoc name = bundle.getString(technicalName + "_name");
75         String JavaDoc description = MessageFormat.format(bundle
76                 .getString(technicalName + "_description"),
77                 new Object JavaDoc[] { searchTerm });
78
79         return new SearchCriteria(technicalName, name, title, description);
80     }
81
82     public ICriteriaRenderer getCriteriaRenderer(String JavaDoc criteriaTechnicalName) {
83         ICriteriaRenderer r = null;
84         if (criteriaTechnicalName
85                 .equals(CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS)) {
86             r = new StringCriteriaRenderer(getCriteria(
87                     CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS, ""), this);
88         }
89
90         return r;
91     }
92
93     public ISearchCriteria getDefaultCriteria(String JavaDoc searchTerm) {
94         return getCriteria(CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS,
95                 searchTerm);
96     }
97
98     public IResultPanel getResultPanel(String JavaDoc searchCriteriaTechnicalName) {
99         return new BasicResultPanel(getTechnicalName(),
100                 searchCriteriaTechnicalName);
101     }
102
103     public int getTotalResultCount() {
104         return totalResultCount;
105     }
106
107     public boolean hasSingleCriteriaSearchResult() {
108         throw new IllegalArgumentException JavaDoc("not implemented yet");
109     }
110
111     public List JavaDoc<ISearchResult> query(String JavaDoc searchTerm,
112             String JavaDoc searchCriteriaTechnicalName, boolean searchInside,
113             int startIndex, int resultCount) {
114
115         if (searchTerm == null)
116             throw new IllegalArgumentException JavaDoc("searchTerm == null");
117         if (searchCriteriaTechnicalName == null)
118             throw new IllegalArgumentException JavaDoc(
119                     "searchCriteriaTechnicalName == null");
120
121         List JavaDoc<ISearchResult> result = new Vector JavaDoc<ISearchResult>();
122
123         ICalendarStore store = CalendarStoreFactory.getInstance()
124                 .getLocaleStore();
125
126         if (searchCriteriaTechnicalName
127                 .equals(CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS)) {
128
129             Iterator JavaDoc<String JavaDoc> it = store.findBySummary(searchTerm);
130             while (it.hasNext()) {
131                 String JavaDoc id = it.next();
132                 IComponentInfo c = store.get(id);
133
134                 if (c.getType().equals(IComponent.TYPE.EVENT)) {
135                     IEventInfo event = (IEventInfo) c;
136
137                     result.add(new CalendarSearchResult(event.getEvent().getSummary(),
138                             event.getEvent().getLocation(), SearchResultBuilder.createURI(
139                                     event.getCalendar(), event.getId()), c.getComponent()));
140                 } else if (c.getType().equals(IComponent.TYPE.TODO)) {
141                     ITodoInfo todo = (ITodoInfo) c;
142
143                     result.add(new CalendarSearchResult(todo.getTodo().getSummary(), todo
144                             .getTodo().getDescription(), SearchResultBuilder.createURI(
145                             todo.getCalendar(), todo.getId()), c.getComponent()));
146                 } else
147                     throw new IllegalArgumentException JavaDoc(
148                             "unsupported component type " + c.getType());
149
150             }
151         }
152
153         return result;
154     }
155
156     public List JavaDoc<ISearchResult> query(List JavaDoc<ISearchRequest> list,
157             boolean matchAll, boolean searchInside, int startIndex,
158             int resultCount) {
159         Vector JavaDoc<ISearchResult> v = new Vector JavaDoc<ISearchResult>();
160         Iterator JavaDoc<ISearchRequest> it = list.listIterator();
161         while (it.hasNext()) {
162             ISearchRequest request = it.next();
163
164             List JavaDoc<ISearchResult> result = query(request.getSearchTerm(), request
165                     .getCriteria(), searchInside, startIndex, resultCount);
166
167             v.addAll(result);
168         }
169
170         return v;
171
172     }
173
174     public void showAllResults(IFrameMediator mediator, String JavaDoc searchTerm,
175             String JavaDoc searchCriteriaTechnicalName) {
176         throw new IllegalArgumentException JavaDoc("not implemented yet");
177     }
178
179 }
180
Popular Tags