View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  
25  package com.mindtree.techworks.insight.eventsearch;
26  
27  import java.io.Serializable;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import com.mindtree.techworks.insight.Controller;
34  import com.mindtree.techworks.insight.InsightConstants;
35  import com.mindtree.techworks.insight.gui.EventDetailsPresentation;
36  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
37  
38  
39  /**
40   * This object holds the results of an event search operation.
41   * 
42   * @see com.mindtree.techworks.insight.eventsearch.EventSearchProvider EventSearchProvider
43   * @author Bindul Bhowmik
44   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
45   */
46  public class SearchResults implements Serializable {
47  
48  	/**
49  	 * Used for object serialization
50  	 */
51  	private static final long serialVersionUID = 2164352117429657476L;
52  	
53  	//
54  	// Member variables
55  	//
56  
57  	/**
58  	 * The log event model of which it is a part of
59  	 */
60  	private Controller controller;
61  
62  	/**
63  	 * The EventDetailsPresentation whose contents will be searched
64  	 */
65  	private EventDetailsPresentation eventDetailsPresentation;
66  
67  	/**
68  	 * The search criteria which created this search result
69  	 */
70  	private SearchCriteria searchCriteria;
71  
72  	/**
73  	 * List of matching log events.
74  	 */
75  	private List matchingLogEvents;
76  
77  	/**
78  	 * Stores the list of matching events in a page.
79  	 */
80  	private Map pagedMatchingLogEvents;
81  
82  	/**
83  	 * Index of the current accessed matching log event.
84  	 */
85  	private int currentMatchingLogEventIndex = -1;
86  
87  	//
88  	// Constructors
89  	//
90  
91  	/**
92  	 * Public constructor.
93  	 * 
94  	 * @param controller The controller instance
95  	 * @param searchCriteria The search criteria which created this search
96  	 *            result
97  	 * @param eventDetailsPresentation The event details presentation used for
98  	 *            highlighting
99  	 */
100 	public SearchResults (Controller controller, SearchCriteria searchCriteria,
101 			EventDetailsPresentation eventDetailsPresentation) {
102 
103 		this.controller = controller;
104 		this.searchCriteria = searchCriteria;
105 		this.eventDetailsPresentation = eventDetailsPresentation;
106 		this.matchingLogEvents = new ArrayList();
107 		this.pagedMatchingLogEvents = new HashMap();
108 	}
109 
110 	//
111 	// Accessors
112 	//
113 
114 	/**
115 	 * Get the search criteria which created this search result
116 	 * 
117 	 * @return <code>SearchCriteria</code> instance for this class.
118 	 */
119 	public SearchCriteria getSearchCriteria () {
120 
121 		return this.searchCriteria;
122 	}
123 
124 	/**
125 	 * The current matching index displayed
126 	 * 
127 	 * @return The current matching index displayed
128 	 */
129 	public int getCurrentMatchingLogEventIndex () {
130 
131 		return currentMatchingLogEventIndex;
132 	}
133 
134 	/**
135 	 * Returns the number of matching log events.
136 	 * 
137 	 * @return The number of matching log events.
138 	 */
139 	public int getCountOfMatchingLogEvents () {
140 
141 		return matchingLogEvents.size();
142 	}
143 
144 	/**
145 	 * Add a Matching log event to the collection
146 	 * 
147 	 * @param matchingLogEvent MatchingLogEvent instance.
148 	 */
149 	public void addMatcingLogEvent (MatchingLogEvent matchingLogEvent) {
150 
151 		this.matchingLogEvents.add(matchingLogEvent);
152 
153 		// Put in the per page cache.
154 		Long pageNum = new Long(matchingLogEvent.getPageNumber());
155 		List pageList = null;
156 		if (this.pagedMatchingLogEvents.containsKey(pageNum)) {
157 			pageList = (List) this.pagedMatchingLogEvents.get(pageNum);
158 		} else {
159 			pageList = new ArrayList();
160 			this.pagedMatchingLogEvents.put(pageNum, pageList);
161 		}
162 		pageList.add(matchingLogEvent);
163 	}
164 
165 	/**
166 	 * Retuns the next matching log event
167 	 * 
168 	 * @return The next matching log event
169 	 */
170 	public MatchingLogEvent getNextMatchingLogEvent () {
171 
172 		if ((currentMatchingLogEventIndex + 1) < matchingLogEvents.size()) {
173 			return (MatchingLogEvent) matchingLogEvents
174 					.get(++currentMatchingLogEventIndex);
175 		} else {
176 			return null;
177 		}
178 	}
179 
180 	/**
181 	 * Returns the previous matching log event
182 	 * 
183 	 * @return The previous matching log event
184 	 */
185 	public MatchingLogEvent getPrevMatchingLogEvent () {
186 
187 		if ((currentMatchingLogEventIndex - 1) >= 0) {
188 			return (MatchingLogEvent) matchingLogEvents
189 					.get(--currentMatchingLogEventIndex);
190 		} else {
191 			return null;
192 		}
193 	}
194 
195 	/**
196 	 * Returns a list of all matching events for a page.
197 	 * 
198 	 * @param pageNumber Page number for which events are required.
199 	 * @return a <code>List</code> of all matching events for a page.
200 	 */
201 	public List getMatchedEventsForPage (int pageNumber) {
202 
203 		return (List) this.pagedMatchingLogEvents.get(new Long(pageNumber));
204 	}
205 
206 	//
207 	// Navigation
208 	//
209 
210 	/**
211 	 * Moves the focus to the matching search event
212 	 * 
213 	 * @param direction The direction to move to.
214 	 */
215 	public void navigate (String direction) {
216 
217 		MatchingLogEvent matchingLogEvent = null;
218 		if (direction.equals(InsightConstants.getLiteral("FIRST_SEARCH_MATCH"))) {
219 			if (matchingLogEvents.size() > 0) {
220 				matchingLogEvent = (MatchingLogEvent) matchingLogEvents.get(0);
221 				currentMatchingLogEventIndex = 0;
222 			}
223 		} else if (direction.equals(InsightConstants
224 				.getLiteral("PREV_SEARCH_MATCH"))) {
225 			matchingLogEvent = getPrevMatchingLogEvent();
226 		} else if (direction.equals(InsightConstants
227 				.getLiteral("NEXT_SEARCH_MATCH"))) {
228 			matchingLogEvent = getNextMatchingLogEvent();
229 		}
230 
231 		if (null != matchingLogEvent) {
232 			controller.setSelectedEvent(matchingLogEvent.getPageNumber(),
233 					matchingLogEvent.getEventSequenceNumber());
234 			this.eventDetailsPresentation.highlightText(this.searchCriteria
235 					.getSearchString(), this.searchCriteria.getSearchType());
236 		} else {
237 			StatusBar.getInstance().setDisplayText(0,
238 					InsightConstants.getLiteral("NO_NAVIGATE"), false);
239 		}
240 	}
241 
242 }