PageRenderTime 32ms CodeModel.GetById 8ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Java | 337 lines | 250 code | 37 blank | 50 comment | 27 complexity | d84158e22084ff46f39d0f556f6c654d 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 3832 2001-10-10 10:07:05Z 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.addMouseListener(new MouseHandler());
 78
 79		JScrollPane scrollPane = new JScrollPane(resultTree);
 80		Dimension dim = scrollPane.getPreferredSize();
 81		dim.width = 400;
 82		scrollPane.setPreferredSize(dim);
 83		add(BorderLayout.CENTER, scrollPane);
 84	} //}}}
 85
 86	//{{{ requestDefaultFocus() method
 87	public boolean requestDefaultFocus()
 88	{
 89		resultTree.grabFocus();
 90		return true;
 91	} //}}}
 92
 93	//{{{ addNotify() method
 94	public void addNotify()
 95	{
 96		super.addNotify();
 97		EditBus.addToBus(this);
 98	} //}}}
 99
100	//{{{ removeNotify() method
101	public void removeNotify()
102	{
103		super.removeNotify();
104		EditBus.removeFromBus(this);
105	} //}}}
106
107	//{{{ handleMessage() method
108	public void handleMessage(EBMessage msg)
109	{
110		if(msg instanceof BufferUpdate)
111		{
112			BufferUpdate bmsg = (BufferUpdate)msg;
113			Buffer buffer = bmsg.getBuffer();
114			if(bmsg.getWhat() == BufferUpdate.LOADED)
115			{
116				for(int i = resultTreeRoot.getChildCount() - 1; i >= 0; i--)
117				{
118					DefaultMutableTreeNode bufferNode = (DefaultMutableTreeNode)
119						resultTreeRoot.getChildAt(i);
120
121					for(int j = bufferNode.getChildCount() - 1;
122						j >= 0; j--)
123					{
124						HyperSearchResult result = (HyperSearchResult)
125							((DefaultMutableTreeNode)bufferNode
126							.getChildAt(j)).getUserObject();
127						if(buffer.getPath().equals(result.path))
128							result.bufferOpened(buffer);
129					}
130				}
131			}
132			else if(bmsg.getWhat() == BufferUpdate.CLOSED)
133			{
134				for(int i = resultTreeRoot.getChildCount() - 1; i >= 0; i--)
135				{
136					DefaultMutableTreeNode bufferNode = (DefaultMutableTreeNode)
137						resultTreeRoot.getChildAt(i);
138
139					for(int j = bufferNode.getChildCount() - 1;
140						j >= 0; j--)
141					{
142						HyperSearchResult result = (HyperSearchResult)
143							((DefaultMutableTreeNode)bufferNode
144							.getChildAt(j)).getUserObject();
145						if(buffer.getPath().equals(result.path))
146							result.bufferClosed();
147					}
148				}
149			}
150		}
151	} //}}}
152
153	//{{{ getTreeModel() method
154	public DefaultTreeModel getTreeModel()
155	{
156		return resultTreeModel;
157	} //}}}
158
159	//{{{ searchStarted() method
160	public void searchStarted()
161	{
162		caption.setText(jEdit.getProperty("hypersearch-results.searching"));
163		resultTreeRoot.removeAllChildren();
164		resultTreeModel.reload(resultTreeRoot);
165	} //}}}
166
167	//{{{ searchDone() method
168	public void searchDone(int resultCount, int bufferCount)
169	{
170		updateCaption(resultCount,bufferCount);
171
172		SwingUtilities.invokeLater(new Runnable()
173		{
174			public void run()
175			{
176				for(int i = 0; i < resultTreeRoot.getChildCount(); i++)
177				{
178					resultTree.expandPath(new TreePath(
179						((DefaultMutableTreeNode)
180						resultTreeRoot.getChildAt(i))
181						.getPath()));
182				}
183			}
184		});
185	} //}}}
186
187	//{{{ Private members
188	private View view;
189
190	private JLabel caption;
191	private JTree resultTree;
192	private DefaultMutableTreeNode resultTreeRoot;
193	private DefaultTreeModel resultTreeModel;
194
195	//{{{ goToSelectedNode() method
196	private void goToSelectedNode()
197	{
198		TreePath path = resultTree.getSelectionPath();
199		if(path == null)
200			return;
201
202		Object value = ((DefaultMutableTreeNode)path
203			.getLastPathComponent()).getUserObject();
204
205		if(value instanceof String)
206		{
207			Buffer buffer = jEdit.openFile(view,(String)value);
208			if(buffer == null)
209				return;
210
211			view.setBuffer(buffer);
212			view.toFront();
213			view.requestFocus();
214		}
215		else
216		{
217			final HyperSearchResult result = (HyperSearchResult)value;
218			final Buffer buffer = result.getBuffer();
219
220			if(buffer == null)
221				return;
222
223			VFSManager.runInAWTThread(new Runnable()
224			{
225				public void run()
226				{
227					int start = result.startPos.getOffset();
228					int end = result.endPos.getOffset();
229					Selection s = new Selection.Range(start,end);
230					view.setBuffer(buffer);
231					JEditTextArea textArea = view.getTextArea();
232					if(textArea.isMultipleSelectionEnabled())
233						textArea.addToSelection(s);
234					else
235						textArea.setSelection(s);
236
237					textArea.moveCaretPosition(start);
238					view.toFront();
239					view.requestFocus();
240				}
241			});
242		}
243	} //}}}
244
245	//{{{ updateCaption() method
246	private void updateCaption(int resultCount, int bufferCount)
247	{
248		Object[] pp = { new Integer(resultCount), new Integer(bufferCount) };
249		caption.setText(jEdit.getProperty("hypersearch-results.caption",pp));
250	} //}}}
251
252	//}}}
253
254	//{{{ MouseHandler class
255	class MouseHandler extends MouseAdapter
256	{
257		//{{{ mouseClicked() method
258		public void mouseClicked(MouseEvent evt)
259		{
260			TreePath path1 = resultTree.getPathForLocation(
261				evt.getX(),evt.getY());
262			if(path1 == null)
263				return;
264
265			TreePath path2 = resultTree.getSelectionPath();
266			// if same thing selected twice, another selection event
267			// is not sent, so we handle it in the mouse event
268			if(path1.equals(path2))
269			{
270				resultTree.setSelectionPath(path1);
271				goToSelectedNode();
272			}
273		} //}}}
274	} //}}}
275
276	//{{{ TreeSelectionHandler class
277	class TreeSelectionHandler implements TreeSelectionListener
278	{
279		//{{{ valueChanged() method
280		public void valueChanged(TreeSelectionEvent evt)
281		{
282			goToSelectedNode();
283		} //}}}
284	} //}}}
285
286	//{{{ ResultCellRenderer class
287	class ResultCellRenderer extends DefaultTreeCellRenderer
288	{
289		Font plainFont, boldFont;
290
291		//{{{ ResultCellRenderer constructor
292		ResultCellRenderer()
293		{
294			plainFont = UIManager.getFont("Tree.font");
295			boldFont = new Font(plainFont.getName(),Font.BOLD,
296				plainFont.getSize());
297		} //}}}
298
299		//{{{ getTreeCellRendererComponent() method
300		public Component getTreeCellRendererComponent(JTree tree,
301			Object value, boolean sel, boolean expanded,
302			boolean leaf, int row, boolean hasFocus)
303		{
304			Component comp = super.getTreeCellRendererComponent(tree,value,sel,
305				expanded,leaf,row,hasFocus);
306			setIcon(null);
307			DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
308			if(node.getUserObject() instanceof String)
309			{
310				// file name
311				ResultCellRenderer.this.setFont(boldFont);
312				int count = node.getChildCount();
313				if(count == 1)
314				{
315					setText(jEdit.getProperty("hypersearch-results"
316						+ ".file-caption1",new Object[] {
317						node.getUserObject()
318						}));
319				}
320				else
321				{
322					setText(jEdit.getProperty("hypersearch-results"
323						+ ".file-caption",new Object[] {
324						node.getUserObject(),
325						new Integer(count)
326						}));
327				}
328			}
329			else
330			{
331				ResultCellRenderer.this.setFont(plainFont);
332			}
333
334			return this;
335		} //}}}
336	} //}}}
337}