1 package com.explosion.expfmodules.search.dialogs;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.MouseListener;
32 import java.io.File;
33 import java.util.Vector;
34
35 import javax.swing.JButton;
36 import javax.swing.JCheckBox;
37 import javax.swing.JInternalFrame;
38 import javax.swing.JLabel;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollPane;
41 import javax.swing.JTextField;
42 import javax.swing.JTree;
43 import javax.swing.tree.TreePath;
44
45 import com.explosion.datastream.exql.gui.ExqlTreeNode;
46 import com.explosion.expf.Application;
47 import com.explosion.expf.Closeable;
48 import com.explosion.expf.ExpComponent;
49 import com.explosion.expf.ExpFrame;
50 import com.explosion.expfmodules.search.ClassSearchProcess;
51 import com.explosion.expfmodules.search.FileSearchResultCollection;
52 import com.explosion.expfmodules.search.SearchConstants;
53 import com.explosion.expfmodules.search.SearchModuleManager;
54 import com.explosion.expfmodules.search.SearchResult;
55 import com.explosion.expfmodules.texteditor.TextEditor;
56 import com.explosion.utilities.FileSystemUtils;
57 import com.explosion.utilities.exception.ExceptionManagerFactory;
58 import com.explosion.utilities.preferences.Preference;
59 import com.explosion.utilities.process.ProcessMonitoringStatusBar;
60 import com.explosion.utilities.process.threads.Finishable;
61
62
63 /***
64 * @author Stephen Cowx
65 * @version
66 */
67
68 public class ClassSearchJPanel extends JPanel implements ExpComponent, Finishable, Closeable
69 {
70 private static int stateofit = 0;
71 private JPanel controlsPanel = new JPanel();
72
73 private JTextField searchStringTextField = new JTextField();
74 private JTextField searchInWhichFilesTextField = new JTextField();
75 private JCheckBox includeClassesOfSameTypeCheckBox = new JCheckBox();
76 private JLabel searchForLabel = new JLabel();
77 private JLabel searchInWhichFilesLabel = new JLabel();
78 private JLabel includeClassesOfSameTypeLabel = new JLabel();
79
80 private JButton goButton = new JButton();
81 private JButton clearResultsButton = new JButton();
82 private JButton closeButton = new JButton();
83 private JButton selectDirButton = new JButton();
84
85 private String lastSearch= "";
86 private String lastFilenamePatternSearch= "";
87 private File lastDir = null;
88
89 private ProcessMonitoringStatusBar processMonitoringStatusBar;
90 private ClassSearchProcess process;
91
92 private boolean caseSensitive = false;
93
94 private JScrollPane pane;
95 private JTree tree;
96
97 public ClassSearchJPanel()
98 {
99 try
100 {
101 init();
102 }
103 catch (Exception e)
104 {
105 e.printStackTrace();
106 }
107 }
108
109 private void init() throws Exception
110 {
111 processMonitoringStatusBar = new ProcessMonitoringStatusBar(Application.getApplicationFrame());
112 lastSearch = (String) SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_SEARCH_IN_FILES).getValue();
113 lastDir = (File) SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_DIRECTORY_SEARCHED).getValue();
114
115 searchForLabel.setText("Name of class to search for:");
116 searchInWhichFilesLabel.setText("In directory:");
117 includeClassesOfSameTypeLabel.setText("Include subclasses");
118
119 searchStringTextField.setText(lastSearch);
120 searchInWhichFilesTextField.setText(lastDir.getAbsolutePath());
121
122 goButton.setText("Search");
123 clearResultsButton.setText("Clear results");
124 closeButton.setText("Close");
125 selectDirButton.setText("...");
126
127 goButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) {goButton_actionPerformed(e); }});
128 closeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) {closeButton_actionPerformed(e); }});
129 clearResultsButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) {clearResultsButton_actionPerformed(e); }});
130
131 selectDirButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) {selectDirButton_actionPerformed(e); }});
132
133 JPanel buttonPanel = new JPanel();
134 buttonPanel.add(clearResultsButton);
135 buttonPanel.add(goButton);
136 buttonPanel.add(closeButton);
137
138 clearResultsButton.setEnabled(false);
139
140 controlsPanel.setLayout(new GridBagLayout());
141
142 controlsPanel.add(searchForLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 0), 0, 0));
143 controlsPanel.add(searchStringTextField, new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 0, 2, 5), 0, 0));
144 controlsPanel.add(searchInWhichFilesLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 2), 0, 0));
145 controlsPanel.add(searchInWhichFilesTextField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 0, 2, 2), 0, 0));
146 controlsPanel.add(selectDirButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 0, 2, 5), 0, 0));
147 controlsPanel.add(includeClassesOfSameTypeLabel, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 0, 2, 2), 0, 0));
148 controlsPanel.add(includeClassesOfSameTypeCheckBox, new GridBagConstraints(1, 2, 2, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 5, 2, 2), 0, 0));
149 controlsPanel.add(buttonPanel, new GridBagConstraints(0, 3, 3, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
150
151 this.setPreferredSize(new Dimension(400,125));
152 this.setLayout(new BorderLayout());
153 this.add(controlsPanel,BorderLayout.NORTH);
154
155 searchStringTextField.requestFocus();
156
157 }
158
159 void closeButton_actionPerformed(ActionEvent e)
160 {
161 try
162 {
163 ((ExpFrame) Application.getApplicationFrame()).closeFrameWithComponent(this, ExpFrame.PALETTE_LAYER);
164 }
165 catch (Exception ex)
166 {
167 ExceptionManagerFactory.getExceptionManager().manageException(ex,null);
168 }
169 }
170
171 public String getCloseCommand()
172 {
173 return SearchConstants.MENU_CLOSE_SEARCH;
174 }
175
176 void clearResultsButton_actionPerformed(ActionEvent e)
177 {
178 try
179 {
180 this.remove(pane);
181 this.setPreferredSize(new Dimension(400,125));
182 clearResultsButton.setEnabled(false);
183 tree = null;
184 pane = null;
185 ((ExpFrame) Application.getApplicationFrame()).getFrameWithComponent(this, ExpFrame.PALETTE_LAYER.intValue()).pack();
186 }
187 catch (Exception ex)
188 {
189 ExceptionManagerFactory.getExceptionManager().manageException(ex,null);
190 }
191 }
192
193 void goButton_actionPerformed(ActionEvent e)
194 {
195 try
196 {
197
198 if (!updateText())
199 return;
200
201 File directory = (File) SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_DIRECTORY_SEARCHED).getValue();
202 lastSearch = (String) SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_SEARCH_IN_FILES).getValue();
203
204 JInternalFrame frame = ((ExpFrame) Application.getApplicationFrame()).getFrameWithComponent(this, ExpFrame.PALETTE_LAYER.intValue());
205 frame.getContentPane().add(processMonitoringStatusBar,BorderLayout.SOUTH);
206 frame.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
207 frame.pack();
208
209 process = new ClassSearchProcess(this,lastSearch,caseSensitive,directory.getAbsolutePath(),"//.jar",includeClassesOfSameTypeCheckBox.isSelected(),true);
210
211 stateofit++;
212 processMonitoringStatusBar.setProcessToMonitor(process);
213 processMonitoringStatusBar.setProgressBarEnabled(false);
214 processMonitoringStatusBar.startProcess();
215
216 setAllEnabled(false);
217 }
218 catch (Exception ex)
219 {
220 ExceptionManagerFactory.getExceptionManager().manageException(ex,null);
221 setAllEnabled(true);
222 }
223 }
224
225 private void setAllEnabled(boolean truth)
226 {
227 searchStringTextField.setEnabled(truth);
228 searchInWhichFilesTextField.setEnabled(truth);
229 selectDirButton.setEnabled(truth);
230 goButton.setEnabled(truth);
231 closeButton.setEnabled(truth);
232 clearResultsButton.setEnabled(truth);
233 }
234
235 public void finish()
236 {
237 try
238 {
239 if (!process.getProcessControl().wasKilled())
240 {
241 Vector searchresults = process.getFileSearchResults();
242 ExqlTreeNode resultsNode = resultsNode = new ExqlTreeNode(lastSearch);
243
244 int matchingLines = 0;
245 if (searchresults.size() < 1)
246 resultsNode.add(new ExqlTreeNode("No matches."));
247
248 for (int i=0;i<searchresults.size();i++)
249 {
250 FileSearchResultCollection resultsCollection = (FileSearchResultCollection) searchresults.elementAt(i);
251 ShortCutFile file = new ShortCutFile(resultsCollection.getFileName());
252 ExqlTreeNode fileNode = new ExqlTreeNode(file);
253 fileNode.setUserObject(file);
254 resultsNode.add(fileNode);
255
256 Vector fileResults = resultsCollection.getSearchResults();
257 for (int j=0;j<fileResults.size();j++)
258 {
259 SearchResult result = (SearchResult) fileResults.elementAt(j);
260 ExqlTreeNode lineNode = new ExqlTreeNode(result.getLineNumber() + ": " + result.getMatchedLine());
261 fileNode.add(lineNode);
262 matchingLines++;
263 }
264 }
265
266 if (pane == null)
267 {
268 pane = new JScrollPane();
269 this.setPreferredSize(new Dimension(400,350));
270 this.add(pane,BorderLayout.CENTER);
271 }
272
273 if (tree == null)
274 {
275 ExqlTreeNode rootNode = new ExqlTreeNode("Results");
276 rootNode.add(resultsNode);
277 tree = new JTree(rootNode);
278 pane.getViewport().add(tree);
279
280 MouseListener ml = new MouseAdapter()
281 {
282 public void mousePressed(MouseEvent e)
283 {
284 int selRow = tree.getRowForLocation(e.getX(), e.getY());
285 TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
286 if(selRow != -1)
287 {
288 if(e.getClickCount() == 2)
289 {
290 Object parent = selPath.getParentPath().getLastPathComponent();
291 if (parent != null )
292 {
293
294 try
295 {
296 if (((ExqlTreeNode)parent).getUserObject() != null && ((ExqlTreeNode)parent).getUserObject() instanceof File)
297 {
298 TextEditor editor = new TextEditor((File)((ExqlTreeNode)parent).getUserObject());
299 ((ExpFrame) Application.getApplicationFrame()).createDocumentFrame(editor, new Dimension(500,500), editor.getDocument().getDocumentName(),true);
300 }
301 }
302 catch (Exception ex)
303 {
304 ExceptionManagerFactory.getExceptionManager().manageException(ex,"Exception caught while opening search document.");
305 }
306 }
307 }
308 }
309 }
310 };
311 tree.addMouseListener(ml);
312 }
313 else
314 {
315 ExqlTreeNode rootNode = (ExqlTreeNode)tree.getModel().getRoot();
316 rootNode.add(resultsNode);
317
318 tree.updateUI();
319 }
320 clearResultsButton.setEnabled(true);
321 }
322
323 setAllEnabled(true);
324
325 JInternalFrame frame = ((ExpFrame) Application.getApplicationFrame()).getFrameWithComponent(this, ExpFrame.PALETTE_LAYER.intValue());
326 frame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
327 frame.getContentPane().remove(processMonitoringStatusBar);
328 frame.pack();
329 }
330 catch (Exception ex)
331 {
332 ExceptionManagerFactory.getExceptionManager().manageException(ex,null);
333 setAllEnabled(true);
334 }
335 }
336
337
338 void selectDirButton_actionPerformed(ActionEvent e)
339 {
340 try
341 {
342 Preference preference = SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_DIRECTORY_SEARCHED);
343 File[] file = FileSystemUtils.chooseFiles(Application.getApplicationFrame(), FileSystemUtils.OPENTYPE, false, (File) preference.getValue(), FileSystemUtils.DIRECTORIES_ONLY,"Select the directory containing the files you want to search.");
344
345 if (file != null && file[0] != null)
346 {
347 preference.setValue(file[0]);
348 preference.save();
349 this.searchInWhichFilesTextField.setText(file[0].getAbsolutePath());
350 }
351
352 }
353 catch (Exception ex)
354 {
355 ExceptionManagerFactory.getExceptionManager().manageException(ex,null);
356 }
357
358 }
359
360 public boolean updateText()
361 {
362 try
363 {
364 SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_SEARCH_IN_FILES).setValue(searchStringTextField.getText());
365 SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_SEARCH_IN_FILES).save();
366 SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_DIRECTORY_SEARCHED).setValue(new File(searchInWhichFilesTextField.getText()));
367 SearchModuleManager.instance().getPreference(SearchConstants.LAST_CLASS_DIRECTORY_SEARCHED).save();
368 }
369 catch (Exception ex)
370 {
371 ExceptionManagerFactory.getExceptionManager().manageException(ex,null);
372 return false;
373 }
374 return true;
375 }
376
377 public void applyPreferences()
378 {
379 }
380
381 public ActionListener getGlobalListener()
382 {
383 return null;
384 }
385
386 }