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  package com.mindtree.techworks.insight.gui.search;
25  
26  import java.awt.Component;
27  import java.awt.Dimension;
28  import java.awt.GridBagConstraints;
29  import java.awt.GridBagLayout;
30  import java.awt.Insets;
31  import java.awt.event.ActionEvent;
32  import java.awt.event.ActionListener;
33  
34  import javax.swing.JButton;
35  import javax.swing.JCheckBox;
36  import javax.swing.JDialog;
37  import javax.swing.JLabel;
38  import javax.swing.JOptionPane;
39  import javax.swing.JTextField;
40  
41  import com.mindtree.techworks.insight.InsightConstants;
42  import com.mindtree.techworks.insight.eventsearch.EventSearchProvider;
43  import com.mindtree.techworks.insight.eventsearch.SearchCriteria;
44  import com.mindtree.techworks.insight.gui.EventDetailsPresentation;
45  import com.mindtree.techworks.insight.gui.Insight;
46  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
47  
48  /**
49  *
50  * The <code>SearchEventsFrame</code> class is the GUI for specifying the search
51  * text. The search is performed on the specified attributes of the LogEvent
52  * across the entire active page set.
53  *
54  * @author  Regunath B
55  * @version 1.0, 05/03/04
56  */
57  
58  public class SearchEventsFrame extends JDialog {
59  	
60  	/**
61  	 * Used for object serialization
62  	 */
63  	private static final long serialVersionUID = -2983953457363093486L;
64  	
65  	/**
66  	 * Useful constants for the preferred size of the search text field 
67  	 */
68  	private static final int WIDTH = 300;
69  	private static final int HEIGHT = 20;
70  	
71  	/**
72  	 * Useful identifiers for log event attributes
73  	 */
74  	private static final int FIRST_ATTRIBUTE = 0;
75  //	private static final int THREAD = 0;
76  //	private static final int CATEGORY = 1;
77  //	private static final int MESSAGE = 2;
78  //	private static final int EXCEPTION_NAME = 3;
79  //	private static final int THROWABLE = 4;
80  	private static final int LAST_ATTRIBUTE = 4;	
81  	
82  	/**
83  	 * Useful array of log event attribute check box names. 
84  	 * Follows the same order as the attribute identifier constants
85  	 */
86  	private static final String[] ATTRIBUTE_NAMES = {
87  		InsightConstants.getLiteral("THREAD_NAME_LABEL"),	
88  		InsightConstants.getLiteral("LOGGER_NAME"),
89  		InsightConstants.getLiteral("MESSAGE_LABEL"),
90  		InsightConstants.getLiteral("EXCEPTION_LABEL"),
91  		InsightConstants.getLiteral("THROWABLE"),
92  	};
93  	
94  	/**
95  	 * Constants for the search type for the various attributes.
96  	 */
97  	protected static final int[] ATTRIBUTE_TYPE_IDS = {
98  		SearchCriteria.THREAD_SEARCH,
99  		SearchCriteria.CATEGORY_SEARCH,
100 		SearchCriteria.MESSAGE_SEARCH,
101 		SearchCriteria.EXCEPTION_CLASS_NAME_SEARCH,
102 		SearchCriteria.THROWABLE_SEARCH,
103 	};
104 	
105 	/**
106 	 * Layout definition.
107 	 */
108 	private GridBagLayout gl;
109 	private GridBagConstraints gc;
110 	
111 	/**
112 	 * Text field for specifying the search text
113 	 */
114 	protected JTextField searchInputField;
115 	
116 	/**
117 	 * Checkboxes to inidcate the various log event attributes to search on
118 	 */
119 	protected JCheckBox[] logEventAttributeCheckBoxes = new JCheckBox[LAST_ATTRIBUTE + 1];
120 	
121 	/**
122 	 * Buttons in the display
123 	 */
124 	protected JButton searchButton;
125 	protected JButton closeButton;
126 	
127 	/**
128 	 * The Insight instance that created this frame
129 	 */
130 	protected Insight insight;
131 	
132 	/*
133 	 * The EventDetailsPresentation whose contents will be searched
134 	 */
135 	private EventDetailsPresentation eventDetailsPresentation;	
136 	
137 	/**
138 	 * Constructor for this class
139 	 * @param insight the Insight instance for this class
140 	 */
141 	public SearchEventsFrame(Insight insight, EventDetailsPresentation eventDetailsPresentation) {
142 		super(JOptionPane.getFrameForComponent(insight),InsightConstants.getLiteral("SEARCH_TITLE"),true);
143 		this.insight = insight;
144 		this.eventDetailsPresentation = eventDetailsPresentation;
145 		gl = new GridBagLayout();
146 		gc = new GridBagConstraints();
147 		getContentPane().setLayout(gl);
148 				
149 		addComponent(new JLabel(InsightConstants.getLiteral("SEARCH_WHAT")),0,0,0,0,1,1);
150 		searchInputField = new JTextField();
151 		addComponent(searchInputField,1,0,1,0,4,1);
152 		
153 		addComponent(new JLabel(InsightConstants.getLiteral("SEARCH_ON")),1,1,0,0,1,1);
154 		
155 		int xStartIndex = 2;
156 		int yStartIndex = 1;
157 		int countPerRowColumn = 2;
158 		
159 		for (int i = FIRST_ATTRIBUTE; i <= LAST_ATTRIBUTE; i++ ) {
160 			logEventAttributeCheckBoxes[i] = new JCheckBox(ATTRIBUTE_NAMES[i]);
161 			addComponent(logEventAttributeCheckBoxes[i],
162 					xStartIndex + (i % countPerRowColumn), 
163 					yStartIndex + (i / countPerRowColumn),1,0,1,1);
164 		}	
165 		
166 		ButtonActionProcessor actionProcessor = new ButtonActionProcessor();
167 		searchButton = new JButton(InsightConstants.getLiteral("SEARCH"));
168 		closeButton = new JButton(InsightConstants.getLiteral("CANCEL"));
169 		searchButton.addActionListener(actionProcessor);
170 		closeButton.addActionListener(actionProcessor);
171 		searchInputField.addActionListener(actionProcessor);
172 		addComponent(searchButton,0,LAST_ATTRIBUTE,0,0,1,1);
173 		addComponent(closeButton,1,LAST_ATTRIBUTE,0,0,1,1);
174 		
175         this.searchInputField.setPreferredSize(new Dimension(WIDTH, HEIGHT));        
176 	}
177 	
178 	/**
179 	 * Displays this frame
180 	 */
181 	public void showFrame() {
182 		this.pack();
183 		// Center this window in Insight
184 		int insightX = insight.getX();
185 		int insightY = insight.getY();
186 		int thisX = insightX + ((this.insight.getWidth() - this.getWidth())/ 2);
187 		int thisY = insightY + ((this.insight.getHeight() - this.getHeight())/ 2);
188 		this.setLocation(thisX, thisY);
189 		this.show();
190 	}
191 	
192 	/**
193 	 * Hides this frame
194 	 */
195 	public void hideFrame() {
196 		this.hide();
197 	}
198 
199 	/**
200 	 * Adds the specified component to this panel using the specified constraints.
201 	 * @param c The component to add
202 	 * @param gridx x coordinate for the GridBagConstraints
203 	 * @param gridy y coordinate for the GridBagConstraints
204 	 * @param weightx x weight for the GridBagConstraints
205 	 * @param weighty y weight for the GridBagConstraints
206 	 * @param width width for the GridBagConstraints
207 	 * @param height height for the GridBagConstraints
208 	 **/
209 	private final void addComponent(Component c, int gridx, int gridy, int weightx,
210 		int weighty, int width, int height) {
211 		gc.fill = GridBagConstraints.BOTH;
212 		gc.anchor = GridBagConstraints.NORTHWEST;
213 		gc.insets = new Insets(5,5,5,5);		
214 		gc.gridx = gridx;
215 		gc.gridy = gridy;
216 		gc.weightx = weightx;
217 		gc.weighty = weighty;
218 		gc.gridwidth = width;
219 		gc.gridheight = height;		
220 		gl.setConstraints( c, gc);
221 		getContentPane().add(c);
222 	}
223 	
224 	/**
225 	 * This class handles the events on button click for the search frame
226 	 * 
227 	 * @see ActionListener ActionListener
228 	 * 
229 	 * @author Regunath B
230 	 * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
231 	 * 
232 	 */
233 	private final class ButtonActionProcessor implements ActionListener {
234 
235 		/** 
236 		 * Interface method implementation
237 		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
238 		 */
239 		public void actionPerformed(ActionEvent e) {
240 			Object source = e.getSource();
241 			if (source == searchButton || source == searchInputField) {
242 				String searchText = searchInputField.getText().toUpperCase();
243 				if (searchText.trim().length() == 0 ) {
244 					StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("ERROR_NO_SEARCH_TEXT"),false);
245 					return;
246 				}
247 				int searchType = 0;
248 				for (int i = FIRST_ATTRIBUTE; i <= LAST_ATTRIBUTE; i++) {
249 					if (logEventAttributeCheckBoxes[i].isSelected()) {
250 						searchType = searchType | ATTRIBUTE_TYPE_IDS[i];
251 					}
252 				}
253 				if (searchType == 0) {
254 					StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("ERROR_NO_SEARCH_FIELDS"),false);
255 					return;
256 				}
257 				
258 				// Build the Search Criteria
259 				SearchCriteria searchCriteria = new SearchCriteria(searchType);
260 				searchCriteria.setSearchString(searchInputField.getText());
261 				
262 				// Call the search
263 				EventSearchProvider eventSearchProvider = new EventSearchProvider(insight, true, eventDetailsPresentation);
264 				eventSearchProvider.searchLogEvents(searchCriteria);
265 				hideFrame();
266 				
267 			} else if (source == closeButton) {
268 				hideFrame();
269 			}
270 		}		
271 	}
272 }