001: package org.columba.calendar.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.api.gui.frame.IFrameMediator;
012: import org.columba.calendar.model.api.IComponent;
013: import org.columba.calendar.model.api.IComponentInfo;
014: import org.columba.calendar.model.api.IEventInfo;
015: import org.columba.calendar.model.api.ITodoInfo;
016: import org.columba.calendar.store.CalendarStoreFactory;
017: import org.columba.calendar.store.api.ICalendarStore;
018: import org.columba.calendar.ui.search.BasicResultPanel;
019: import org.columba.core.gui.search.StringCriteriaRenderer;
020: import org.columba.core.gui.search.api.ICriteriaRenderer;
021: import org.columba.core.gui.search.api.IResultPanel;
022: import org.columba.core.resourceloader.IconKeys;
023: import org.columba.core.resourceloader.ImageLoader;
024: import org.columba.core.search.SearchCriteria;
025: import org.columba.core.search.api.ISearchCriteria;
026: import org.columba.core.search.api.ISearchProvider;
027: import org.columba.core.search.api.ISearchRequest;
028: import org.columba.core.search.api.ISearchResult;
029:
030: public class CalendarSearchProvider implements ISearchProvider {
031:
032: public static final String CRITERIA_SUMMARY_CONTAINS = "summary_contains";
033:
034: private ResourceBundle bundle;
035:
036: private int totalResultCount = 0;
037:
038: public CalendarSearchProvider() {
039: bundle = ResourceBundle
040: .getBundle("org.columba.calendar.i18n.search");
041: }
042:
043: public String getTechnicalName() {
044: return "CalendarSearchProvider";
045: }
046:
047: public String getName() {
048: return bundle.getString("provider_title");
049: }
050:
051: public String getDescription() {
052: return bundle.getString("provider_description");
053: }
054:
055: public ImageIcon getIcon() {
056: return ImageLoader.getSmallIcon(IconKeys.CALENDAR);
057: }
058:
059: public List<ISearchCriteria> getAllCriteria(String searchTerm) {
060: List<ISearchCriteria> list = new Vector<ISearchCriteria>();
061:
062: list.add(getCriteria(
063: CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS,
064: searchTerm));
065:
066: return list;
067: }
068:
069: public IResultPanel getComplexResultPanel() {
070: return new ComplexResultPanel(getTechnicalName());
071: }
072:
073: public ISearchCriteria getCriteria(String technicalName,
074: String searchTerm) {
075: String title = MessageFormat.format(bundle
076: .getString(technicalName + "_title"),
077: new Object[] { searchTerm });
078: String name = bundle.getString(technicalName + "_name");
079: String description = MessageFormat.format(bundle
080: .getString(technicalName + "_description"),
081: new Object[] { searchTerm });
082:
083: return new SearchCriteria(technicalName, name, title,
084: description);
085: }
086:
087: public ICriteriaRenderer getCriteriaRenderer(
088: String criteriaTechnicalName) {
089: ICriteriaRenderer r = null;
090: if (criteriaTechnicalName
091: .equals(CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS)) {
092: r = new StringCriteriaRenderer(getCriteria(
093: CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS,
094: ""), this );
095: }
096:
097: return r;
098: }
099:
100: public ISearchCriteria getDefaultCriteria(String searchTerm) {
101: return getCriteria(
102: CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS,
103: searchTerm);
104: }
105:
106: public IResultPanel getResultPanel(
107: String searchCriteriaTechnicalName) {
108: return new BasicResultPanel(getTechnicalName(),
109: searchCriteriaTechnicalName);
110: }
111:
112: public int getTotalResultCount() {
113: return totalResultCount;
114: }
115:
116: public boolean hasSingleCriteriaSearchResult() {
117: throw new IllegalArgumentException("not implemented yet");
118: }
119:
120: public List<ISearchResult> query(String searchTerm,
121: String searchCriteriaTechnicalName, boolean searchInside,
122: int startIndex, int resultCount) {
123:
124: if (searchTerm == null)
125: throw new IllegalArgumentException("searchTerm == null");
126: if (searchCriteriaTechnicalName == null)
127: throw new IllegalArgumentException(
128: "searchCriteriaTechnicalName == null");
129:
130: List<ISearchResult> result = new Vector<ISearchResult>();
131:
132: ICalendarStore store = CalendarStoreFactory.getInstance()
133: .getLocaleStore();
134:
135: if (searchCriteriaTechnicalName
136: .equals(CalendarSearchProvider.CRITERIA_SUMMARY_CONTAINS)) {
137:
138: Iterator<String> it = store.findBySummary(searchTerm);
139: while (it.hasNext()) {
140: String id = it.next();
141: IComponentInfo c = store.get(id);
142:
143: if (c.getType().equals(IComponent.TYPE.EVENT)) {
144: IEventInfo event = (IEventInfo) c;
145:
146: result.add(new CalendarSearchResult(event
147: .getEvent().getSummary(), event.getEvent()
148: .getLocation(), SearchResultBuilder
149: .createURI(event.getCalendar(), event
150: .getId()), c.getComponent()));
151: } else if (c.getType().equals(IComponent.TYPE.TODO)) {
152: ITodoInfo todo = (ITodoInfo) c;
153:
154: result.add(new CalendarSearchResult(todo.getTodo()
155: .getSummary(), todo.getTodo()
156: .getDescription(),
157: SearchResultBuilder.createURI(todo
158: .getCalendar(), todo.getId()), c
159: .getComponent()));
160: } else
161: throw new IllegalArgumentException(
162: "unsupported component type " + c.getType());
163:
164: }
165: }
166:
167: return result;
168: }
169:
170: public List<ISearchResult> query(List<ISearchRequest> list,
171: boolean matchAll, boolean searchInside, int startIndex,
172: int resultCount) {
173: Vector<ISearchResult> v = new Vector<ISearchResult>();
174: Iterator<ISearchRequest> it = list.listIterator();
175: while (it.hasNext()) {
176: ISearchRequest request = it.next();
177:
178: List<ISearchResult> result = query(request.getSearchTerm(),
179: request.getCriteria(), searchInside, startIndex,
180: resultCount);
181:
182: v.addAll(result);
183: }
184:
185: return v;
186:
187: }
188:
189: public void showAllResults(IFrameMediator mediator,
190: String searchTerm, String searchCriteriaTechnicalName) {
191: throw new IllegalArgumentException("not implemented yet");
192: }
193:
194: }
|