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.JDialog;
36  import javax.swing.JLabel;
37  import javax.swing.JOptionPane;
38  import javax.swing.JTextField;
39  
40  import com.mindtree.techworks.insight.InsightConstants;
41  import com.mindtree.techworks.insight.eventsearch.SearchCriteria;
42  import com.mindtree.techworks.insight.gui.EventDetailsPresentation;
43  import com.mindtree.techworks.insight.gui.Insight;
44  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
45  
46  /**
47  *
48  * The <code>SearchTextFrame</code> class is the GUI for specifying the search
49  * text. The search is performed on the text displayed in the Event details pane.
50  *
51  * @author  Regunath B
52  * @version 1.0, 05/02/04
53  */
54  
55  public class SearchTextFrame extends JDialog {
56  	
57  	/**
58  	 * Used for object serialization
59  	 */
60  	private static final long serialVersionUID = -32898128179387599L;
61  	
62  	/*
63  	 * Useful constants for the preferred size of the search text field 
64  	 */
65  	private static final int WIDTH = 300;
66  	private static final int HEIGHT = 20;
67  	
68  	/*
69  	 * Layout definition.
70  	 */
71  	private GridBagLayout gl;
72  	private GridBagConstraints gc;
73  	
74  	/*
75  	 * Text field for specifying the search text
76  	 */
77  	private JTextField searchInputField;
78  	
79  	/*
80  	 * Buttons in the display
81  	 */
82  	private JButton findButton;
83  	private JButton closeButton;
84  	
85  	/*
86  	 * The Insight instance that created this frame
87  	 */
88  	private Insight insight;
89  	
90  	/*
91  	 * The EventDetailsPresentation whose contents will be searched
92  	 */
93  	private EventDetailsPresentation eventDetailsPresentation;
94  	
95  	/**
96  	 * Constructor for this class
97  	 * @param insight the Insight instance for this class
98  	 */
99  	public SearchTextFrame(Insight insight, EventDetailsPresentation eventDetailsPresentation) {
100 		super(JOptionPane.getFrameForComponent(insight),InsightConstants.getLiteral("FIND_TITLE"),true);
101 		this.insight = insight;
102 		this.eventDetailsPresentation = eventDetailsPresentation;
103 		gl = new GridBagLayout();
104 		gc = new GridBagConstraints();
105 		getContentPane().setLayout(gl);
106 		
107 		addComponent(new JLabel(InsightConstants.getLiteral("FIND_WHAT")),0,0,0,0,1,1);
108 		searchInputField = new JTextField();
109 		addComponent(searchInputField,1,0,1,0, 3, 1);
110 		
111 		ButtionActionProcessor actionProcessor = new ButtionActionProcessor();
112 		findButton = new JButton(InsightConstants.getLiteral("FIND"));
113 		closeButton = new JButton(InsightConstants.getLiteral("CANCEL"));
114 		findButton.addActionListener(actionProcessor);
115 		closeButton.addActionListener(actionProcessor);
116 		searchInputField.addActionListener(actionProcessor);
117 		addComponent(findButton,0,1,0,0,1,1);
118 		addComponent(closeButton,1,1,0,0,1,1);
119 		
120         this.searchInputField.setPreferredSize(new Dimension(WIDTH, HEIGHT));        
121 	}
122 	
123 	/**
124 	 * Displays this frame
125 	 */
126 	public void showFrame() {
127 		this.pack();
128 		// Center this window in Insight
129 		int insightX = insight.getX();
130 		int insightY = insight.getY();
131 		int thisX = insightX + ((this.insight.getWidth() - this.getWidth())/ 2);
132 		int thisY = insightY + ((this.insight.getHeight() - this.getHeight())/ 2);
133 		this.setLocation(thisX, thisY);
134 		this.show();
135 	}
136 	
137 	/**
138 	 * Hides this frame
139 	 */
140 	public void hideFrame() {
141 		this.hide();
142 	}
143 
144 	/**
145 	 * Adds the specified component to this panel using the specified constraints.
146 	 **/
147 	private final void addComponent(Component c, int gridx, int gridy, int weightx,
148 		int weighty, int width, int height) {
149 		gc.fill = GridBagConstraints.BOTH;
150 		gc.anchor = GridBagConstraints.NORTHWEST;
151 		gc.insets = new Insets(5,5,5,5);		
152 		gc.gridx = gridx;
153 		gc.gridy = gridy;
154 		gc.weightx = weightx;
155 		gc.weighty = weighty;
156 		gc.gridwidth = width;
157 		gc.gridheight = height;		
158 		gl.setConstraints( c, gc);
159 		getContentPane().add(c);
160 	}
161 	
162 	private final class ButtionActionProcessor implements ActionListener {
163 
164 		/** 
165 		 * Interface method implementation
166 		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
167 		 */
168 		public void actionPerformed(ActionEvent e) {
169 			Object source = e.getSource();
170 			if (source == findButton || source == searchInputField) {
171 				String searchText = searchInputField.getText();
172 				if (searchText.trim().length() == 0) {
173 					StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("ERROR_NO_SEARCH_TEXT"),false);
174 					return;
175 				}
176 				// hide the frame if match(es) have been found for the search text. An error is displayed by the 
177 				// EventDetailsPresentation if no match is found. This window continues to show if no match is found
178 				// Search is performed on all log event text fields
179 				if (eventDetailsPresentation.highlightText(searchText, SearchCriteria.ALL_FIELDS_SEARCH)) {
180 					hideFrame();
181 				}
182 			} else if (source == closeButton) {
183 				hideFrame();
184 			}
185 		}		
186 	}
187 }