PageRenderTime 81ms CodeModel.GetById 66ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 347 lines | 256 code | 42 blank | 49 comment | 29 complexity | 67805dd3abab93e1f6da0ab1cbd2c330 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.tree.*;
 30import java.awt.*;
 31import java.awt.event.*;
 32import org.gjt.sp.jedit.gui.*;
 33import org.gjt.sp.jedit.io.VFSManager;
 34import org.gjt.sp.jedit.msg.*;
 35import org.gjt.sp.jedit.textarea.*;
 36import org.gjt.sp.jedit.*;
 37import org.gjt.sp.util.Log;
 38//}}}
 39
 40/**
 41 * HyperSearch results window.
 42 * @author Slava Pestov
 43 * @version $Id: HyperSearchResults.java 4338 2002-09-11 16:41:47Z spestov $
 44 */
 45public class HyperSearchResults extends JPanel implements EBComponent
 46{
 47	public static final String NAME = "hypersearch-results";
 48
 49	//{{{ HyperSearchResults constructor
 50	public HyperSearchResults(View view)
 51	{
 52		super(new BorderLayout());
 53
 54		this.view = view;
 55
 56		caption = new JLabel();
 57		updateCaption(0,0);
 58		add(BorderLayout.NORTH, caption);
 59
 60		resultTreeRoot = new DefaultMutableTreeNode();
 61		resultTreeModel = new DefaultTreeModel(resultTreeRoot);
 62		resultTree = new JTree(resultTreeModel);
 63		resultTree.setCellRenderer(new ResultCellRenderer());
 64		resultTree.setVisibleRowCount(16);
 65		resultTree.setRootVisible(false);
 66		resultTree.setShowsRootHandles(true);
 67
 68		// looks bad with the OS X L&F, apparently...
 69		if(!OperatingSystem.isMacOSLF())
 70			resultTree.putClientProperty("JTree.lineStyle", "Angled");
 71
 72		resultTree.setEditable(false);
 73
 74		resultTree.addTreeSelectionListener(new TreeSelectionHandler());
 75		resultTree.addKeyListener(new KeyHandler());
 76		resultTree.addMouseListener(new MouseHandler());
 77
 78		JScrollPane scrollPane = new JScrollPane(resultTree);
 79		Dimension dim = scrollPane.getPreferredSize();
 80		dim.width = 400;
 81		scrollPane.setPreferredSize(dim);
 82		add(BorderLayout.CENTER, scrollPane);
 83	} //}}}
 84
 85	//{{{ requestDefaultFocus() method
 86	public boolean requestDefaultFocus()
 87	{
 88		resultTree.grabFocus();
 89		return true;
 90	} //}}}
 91
 92	//{{{ addNotify() method
 93	public void addNotify()
 94	{
 95		super.addNotify();
 96		EditBus.addToBus(this);
 97	} //}}}
 98
 99	//{{{ removeNotify() method
100	public void removeNotify()
101	{
102		super.removeNotify();
103		EditBus.removeFromBus(this);
104	} //}}}
105
106	//{{{ handleMessage() method
107	public void handleMessage(EBMessage msg)
108	{
109		if(msg instanceof BufferUpdate)
110		{
111			BufferUpdate bmsg = (BufferUpdate)msg;
112			Buffer buffer = bmsg.getBuffer();
113			if(bmsg.getWhat() == BufferUpdate.LOADED)
114			{
115				for(int i = resultTreeRoot.getChildCount() - 1; i >= 0; i--)
116				{
117					DefaultMutableTreeNode bufferNode = (DefaultMutableTreeNode)
118						resultTreeRoot.getChildAt(i);
119
120					for(int j = bufferNode.getChildCount() - 1;
121						j >= 0; j--)
122					{
123						HyperSearchResult result = (HyperSearchResult)
124							((DefaultMutableTreeNode)bufferNode
125							.getChildAt(j)).getUserObject();
126						if(buffer.getPath().equals(result.path))
127							result.bufferOpened(buffer);
128					}
129				}
130			}
131			else if(bmsg.getWhat() == BufferUpdate.CLOSED)
132			{
133				for(int i = resultTreeRoot.getChildCount() - 1; i >= 0; i--)
134				{
135					DefaultMutableTreeNode bufferNode = (DefaultMutableTreeNode)
136						resultTreeRoot.getChildAt(i);
137
138					for(int j = bufferNode.getChildCount() - 1;
139						j >= 0; j--)
140					{
141						HyperSearchResult result = (HyperSearchResult)
142							((DefaultMutableTreeNode)bufferNode
143							.getChildAt(j)).getUserObject();
144						if(buffer.getPath().equals(result.path))
145							result.bufferClosed();
146					}
147				}
148			}
149		}
150	} //}}}
151
152	//{{{ getTreeModel() method
153	public DefaultTreeModel getTreeModel()
154	{
155		return resultTreeModel;
156	} //}}}
157
158	//{{{ searchStarted() method
159	public void searchStarted()
160	{
161		caption.setText(jEdit.getProperty("hypersearch-status"));
162		resultTreeRoot.removeAllChildren();
163		resultTreeModel.reload(resultTreeRoot);
164	} //}}}
165
166	//{{{ searchDone() method
167	public void searchDone(int resultCount, int bufferCount)
168	{
169		updateCaption(resultCount,bufferCount);
170
171		SwingUtilities.invokeLater(new Runnable()
172		{
173			public void run()
174			{
175				for(int i = 0; i < resultTreeRoot.getChildCount(); i++)
176				{
177					resultTree.expandPath(new TreePath(
178						((DefaultMutableTreeNode)
179						resultTreeRoot.getChildAt(i))
180						.getPath()));
181				}
182			}
183		});
184	} //}}}
185
186	//{{{ Private members
187	private View view;
188
189	private JLabel caption;
190	private JTree resultTree;
191	private DefaultMutableTreeNode resultTreeRoot;
192	private DefaultTreeModel resultTreeModel;
193
194	//{{{ goToSelectedNode() method
195	private void goToSelectedNode()
196	{
197		TreePath path = resultTree.getSelectionPath();
198		if(path == null)
199			return;
200
201		Object value = ((DefaultMutableTreeNode)path
202			.getLastPathComponent()).getUserObject();
203
204		if(value instanceof String)
205		{
206			Buffer buffer = jEdit.openFile(view,(String)value);
207			if(buffer == null)
208				return;
209
210			view.setBuffer(buffer);
211
212			// fuck me dead
213			SwingUtilities.invokeLater(new Runnable()
214			{
215				public void run()
216				{
217					resultTree.requestFocus();
218				}
219			});
220		}
221		else
222		{
223			final HyperSearchResult result = (HyperSearchResult)value;
224			final Buffer buffer = result.getBuffer();
225
226			if(buffer == null)
227				return;
228
229			VFSManager.runInAWTThread(new Runnable()
230			{
231				public void run()
232				{
233					int start = result.startPos.getOffset();
234					int end = result.endPos.getOffset();
235					Selection s = new Selection.Range(start,end);
236					view.setBuffer(buffer);
237					JEditTextArea textArea = view.getTextArea();
238					if(textArea.isMultipleSelectionEnabled())
239						textArea.addToSelection(s);
240					else
241						textArea.setSelection(s);
242
243					textArea.moveCaretPosition(end);
244				}
245			});
246		}
247	} //}}}
248
249	//{{{ updateCaption() method
250	private void updateCaption(int resultCount, int bufferCount)
251	{
252		Object[] pp = { new Integer(resultCount), new Integer(bufferCount) };
253		caption.setText(jEdit.getProperty("hypersearch-results.caption",pp));
254	} //}}}
255
256	//}}}
257
258	//{{{ KeyHandler class
259	class KeyHandler extends KeyAdapter
260	{
261		public void keyPressed(KeyEvent evt)
262		{
263			if(evt.getKeyCode() == KeyEvent.VK_ENTER)
264				goToSelectedNode();
265		}
266	} //}}}
267
268	//{{{ MouseHandler class
269	class MouseHandler extends MouseAdapter
270	{
271		public void mouseClicked(MouseEvent evt)
272		{
273			TreePath path1 = resultTree.getPathForLocation(
274				evt.getX(),evt.getY());
275			if(path1 == null)
276				return;
277
278			resultTree.setSelectionPath(path1);
279			goToSelectedNode();
280
281			view.toFront();
282			view.requestFocus();
283			view.getTextArea().requestFocus();
284		}
285	} //}}}
286
287	//{{{ TreeSelectionHandler class
288	class TreeSelectionHandler implements TreeSelectionListener
289	{
290		public void valueChanged(TreeSelectionEvent evt)
291		{
292			goToSelectedNode();
293		}
294	} //}}}
295
296	//{{{ ResultCellRenderer class
297	class ResultCellRenderer extends DefaultTreeCellRenderer
298	{
299		Font plainFont, boldFont;
300
301		//{{{ ResultCellRenderer constructor
302		ResultCellRenderer()
303		{
304			plainFont = UIManager.getFont("Tree.font");
305			boldFont = new Font(plainFont.getName(),Font.BOLD,
306				plainFont.getSize());
307		} //}}}
308
309		//{{{ getTreeCellRendererComponent() method
310		public Component getTreeCellRendererComponent(JTree tree,
311			Object value, boolean sel, boolean expanded,
312			boolean leaf, int row, boolean hasFocus)
313		{
314			Component comp = super.getTreeCellRendererComponent(tree,value,sel,
315				expanded,leaf,row,hasFocus);
316			setIcon(null);
317			DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
318			if(node.getUserObject() instanceof String)
319			{
320				// file name
321				ResultCellRenderer.this.setFont(boldFont);
322				int count = node.getChildCount();
323				if(count == 1)
324				{
325					setText(jEdit.getProperty("hypersearch-results"
326						+ ".file-caption1",new Object[] {
327						node.getUserObject()
328						}));
329				}
330				else
331				{
332					setText(jEdit.getProperty("hypersearch-results"
333						+ ".file-caption",new Object[] {
334						node.getUserObject(),
335						new Integer(count)
336						}));
337				}
338			}
339			else
340			{
341				ResultCellRenderer.this.setFont(plainFont);
342			}
343
344			return this;
345		} //}}}
346	} //}}}
347}