PageRenderTime 122ms CodeModel.GetById 94ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/search/HyperSearchResults.java

#
Java | 349 lines | 261 code | 40 blank | 48 comment | 28 complexity | 0e43dc6b1231319a3a0069826d2fb5ab MD5 | raw file
  1/*
  2 * HyperSearchResults.java - HyperSearch results
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1998, 1999, 2000, 2001 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.search;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import javax.swing.border.*;
 28import javax.swing.event.*;
 29import javax.swing.text.*;
 30import javax.swing.tree.*;
 31import java.awt.*;
 32import java.awt.event.*;
 33import org.gjt.sp.jedit.gui.*;
 34import org.gjt.sp.jedit.io.VFSManager;
 35import org.gjt.sp.jedit.msg.*;
 36import org.gjt.sp.jedit.textarea.*;
 37import org.gjt.sp.jedit.jEdit;
 38import org.gjt.sp.jedit.Buffer;
 39import org.gjt.sp.jedit.EBComponent;
 40import org.gjt.sp.jedit.EBMessage;
 41import org.gjt.sp.jedit.EditBus;
 42import org.gjt.sp.jedit.View;
 43import org.gjt.sp.util.Log;
 44//}}}
 45
 46/**
 47 * HyperSearch results window.
 48 * @author Slava Pestov
 49 * @version $Id: HyperSearchResults.java 4006 2002-02-03 06:37:56Z spestov $
 50 */
 51public class HyperSearchResults extends JPanel implements EBComponent
 52{
 53	public static final String NAME = "hypersearch-results";
 54
 55	//{{{ HyperSearchResults constructor
 56	public HyperSearchResults(View view)
 57	{
 58		super(new BorderLayout());
 59
 60		this.view = view;
 61
 62		caption = new JLabel();
 63		updateCaption(0,0);
 64		add(BorderLayout.NORTH, caption);
 65
 66		resultTreeRoot = new DefaultMutableTreeNode();
 67		resultTreeModel = new DefaultTreeModel(resultTreeRoot);
 68		resultTree = new JTree(resultTreeModel);
 69		resultTree.setCellRenderer(new ResultCellRenderer());
 70		resultTree.setVisibleRowCount(16);
 71		resultTree.setRootVisible(false);
 72		resultTree.setShowsRootHandles(true);
 73		resultTree.putClientProperty("JTree.lineStyle", "Angled");
 74		resultTree.setEditable(false);
 75
 76		resultTree.addTreeSelectionListener(new TreeSelectionHandler());
 77		resultTree.addKeyListener(new KeyHandler());
 78		resultTree.addMouseListener(new MouseHandler());
 79
 80		JScrollPane scrollPane = new JScrollPane(resultTree);
 81		Dimension dim = scrollPane.getPreferredSize();
 82		dim.width = 400;
 83		scrollPane.setPreferredSize(dim);
 84		add(BorderLayout.CENTER, scrollPane);
 85	} //}}}
 86
 87	//{{{ requestDefaultFocus() method
 88	public boolean requestDefaultFocus()
 89	{
 90		resultTree.grabFocus();
 91		return true;
 92	} //}}}
 93
 94	//{{{ addNotify() method
 95	public void addNotify()
 96	{
 97		super.addNotify();
 98		EditBus.addToBus(this);
 99	} //}}}
100
101	//{{{ removeNotify() method
102	public void removeNotify()
103	{
104		super.removeNotify();
105		EditBus.removeFromBus(this);
106	} //}}}
107
108	//{{{ handleMessage() method
109	public void handleMessage(EBMessage msg)
110	{
111		if(msg instanceof BufferUpdate)
112		{
113			BufferUpdate bmsg = (BufferUpdate)msg;
114			Buffer buffer = bmsg.getBuffer();
115			if(bmsg.getWhat() == BufferUpdate.LOADED)
116			{
117				for(int i = resultTreeRoot.getChildCount() - 1; i >= 0; i--)
118				{
119					DefaultMutableTreeNode bufferNode = (DefaultMutableTreeNode)
120						resultTreeRoot.getChildAt(i);
121
122					for(int j = bufferNode.getChildCount() - 1;
123						j >= 0; j--)
124					{
125						HyperSearchResult result = (HyperSearchResult)
126							((DefaultMutableTreeNode)bufferNode
127							.getChildAt(j)).getUserObject();
128						if(buffer.getPath().equals(result.path))
129							result.bufferOpened(buffer);
130					}
131				}
132			}
133			else if(bmsg.getWhat() == BufferUpdate.CLOSED)
134			{
135				for(int i = resultTreeRoot.getChildCount() - 1; i >= 0; i--)
136				{
137					DefaultMutableTreeNode bufferNode = (DefaultMutableTreeNode)
138						resultTreeRoot.getChildAt(i);
139
140					for(int j = bufferNode.getChildCount() - 1;
141						j >= 0; j--)
142					{
143						HyperSearchResult result = (HyperSearchResult)
144							((DefaultMutableTreeNode)bufferNode
145							.getChildAt(j)).getUserObject();
146						if(buffer.getPath().equals(result.path))
147							result.bufferClosed();
148					}
149				}
150			}
151		}
152	} //}}}
153
154	//{{{ getTreeModel() method
155	public DefaultTreeModel getTreeModel()
156	{
157		return resultTreeModel;
158	} //}}}
159
160	//{{{ searchStarted() method
161	public void searchStarted()
162	{
163		caption.setText(jEdit.getProperty("hypersearch-results.searching"));
164		resultTreeRoot.removeAllChildren();
165		resultTreeModel.reload(resultTreeRoot);
166	} //}}}
167
168	//{{{ searchDone() method
169	public void searchDone(int resultCount, int bufferCount)
170	{
171		updateCaption(resultCount,bufferCount);
172
173		SwingUtilities.invokeLater(new Runnable()
174		{
175			public void run()
176			{
177				for(int i = 0; i < resultTreeRoot.getChildCount(); i++)
178				{
179					resultTree.expandPath(new TreePath(
180						((DefaultMutableTreeNode)
181						resultTreeRoot.getChildAt(i))
182						.getPath()));
183				}
184			}
185		});
186	} //}}}
187
188	//{{{ Private members
189	private View view;
190
191	private JLabel caption;
192	private JTree resultTree;
193	private DefaultMutableTreeNode resultTreeRoot;
194	private DefaultTreeModel resultTreeModel;
195
196	//{{{ goToSelectedNode() method
197	private void goToSelectedNode()
198	{
199		TreePath path = resultTree.getSelectionPath();
200		if(path == null)
201			return;
202
203		Object value = ((DefaultMutableTreeNode)path
204			.getLastPathComponent()).getUserObject();
205
206		if(value instanceof String)
207		{
208			Buffer buffer = jEdit.openFile(view,(String)value);
209			if(buffer == null)
210				return;
211
212			view.setBuffer(buffer);
213
214			// fuck me dead
215			SwingUtilities.invokeLater(new Runnable()
216			{
217				public void run()
218				{
219					resultTree.requestFocus();
220				}
221			});
222		}
223		else
224		{
225			final HyperSearchResult result = (HyperSearchResult)value;
226			final Buffer buffer = result.getBuffer();
227
228			if(buffer == null)
229				return;
230
231			VFSManager.runInAWTThread(new Runnable()
232			{
233				public void run()
234				{
235					int start = result.startPos.getOffset();
236					int end = result.endPos.getOffset();
237					Selection s = new Selection.Range(start,end);
238					view.setBuffer(buffer);
239					JEditTextArea textArea = view.getTextArea();
240					if(textArea.isMultipleSelectionEnabled())
241						textArea.addToSelection(s);
242					else
243						textArea.setSelection(s);
244
245					textArea.moveCaretPosition(end);
246				}
247			});
248		}
249	} //}}}
250
251	//{{{ updateCaption() method
252	private void updateCaption(int resultCount, int bufferCount)
253	{
254		Object[] pp = { new Integer(resultCount), new Integer(bufferCount) };
255		caption.setText(jEdit.getProperty("hypersearch-results.caption",pp));
256	} //}}}
257
258	//}}}
259
260	//{{{ KeyHandler class
261	class KeyHandler extends KeyAdapter
262	{
263		public void keyPressed(KeyEvent evt)
264		{
265			if(evt.getKeyCode() == KeyEvent.VK_ENTER)
266				goToSelectedNode();
267		}
268	} //}}}
269
270	//{{{ MouseHandler class
271	class MouseHandler extends MouseAdapter
272	{
273		public void mouseClicked(MouseEvent evt)
274		{
275			TreePath path1 = resultTree.getPathForLocation(
276				evt.getX(),evt.getY());
277			if(path1 == null)
278				return;
279
280			resultTree.setSelectionPath(path1);
281			goToSelectedNode();
282
283			view.toFront();
284			view.requestFocus();
285			view.getTextArea().requestFocus();
286		}
287	} //}}}
288
289	//{{{ TreeSelectionHandler class
290	class TreeSelectionHandler implements TreeSelectionListener
291	{
292		public void valueChanged(TreeSelectionEvent evt)
293		{
294			goToSelectedNode();
295		}
296	} //}}}
297
298	//{{{ ResultCellRenderer class
299	class ResultCellRenderer extends DefaultTreeCellRenderer
300	{
301		Font plainFont, boldFont;
302
303		//{{{ ResultCellRenderer constructor
304		ResultCellRenderer()
305		{
306			plainFont = UIManager.getFont("Tree.font");
307			boldFont = new Font(plainFont.getName(),Font.BOLD,
308				plainFont.getSize());
309		} //}}}
310
311		//{{{ getTreeCellRendererComponent() method
312		public Component getTreeCellRendererComponent(JTree tree,
313			Object value, boolean sel, boolean expanded,
314			boolean leaf, int row, boolean hasFocus)
315		{
316			Component comp = super.getTreeCellRendererComponent(tree,value,sel,
317				expanded,leaf,row,hasFocus);
318			setIcon(null);
319			DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
320			if(node.getUserObject() instanceof String)
321			{
322				// file name
323				ResultCellRenderer.this.setFont(boldFont);
324				int count = node.getChildCount();
325				if(count == 1)
326				{
327					setText(jEdit.getProperty("hypersearch-results"
328						+ ".file-caption1",new Object[] {
329						node.getUserObject()
330						}));
331				}
332				else
333				{
334					setText(jEdit.getProperty("hypersearch-results"
335						+ ".file-caption",new Object[] {
336						node.getUserObject(),
337						new Integer(count)
338						}));
339				}
340			}
341			else
342			{
343				ResultCellRenderer.this.setFont(plainFont);
344			}
345
346			return this;
347		} //}}}
348	} //}}}
349}