PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit.search;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import javax.swing.border.*;
  26. import javax.swing.event.*;
  27. import javax.swing.text.*;
  28. import javax.swing.tree.*;
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import org.gjt.sp.jedit.gui.*;
  32. import org.gjt.sp.jedit.io.VFSManager;
  33. import org.gjt.sp.jedit.msg.*;
  34. import org.gjt.sp.jedit.textarea.*;
  35. import org.gjt.sp.jedit.jEdit;
  36. import org.gjt.sp.jedit.Buffer;
  37. import org.gjt.sp.jedit.EBComponent;
  38. import org.gjt.sp.jedit.EBMessage;
  39. import org.gjt.sp.jedit.EditBus;
  40. import org.gjt.sp.jedit.View;
  41. import org.gjt.sp.util.Log;
  42. //}}}
  43. /**
  44. * HyperSearch results window.
  45. * @author Slava Pestov
  46. * @version $Id: HyperSearchResults.java 3832 2001-10-10 10:07:05Z spestov $
  47. */
  48. public class HyperSearchResults extends JPanel implements EBComponent
  49. {
  50. public static final String NAME = "hypersearch-results";
  51. //{{{ HyperSearchResults constructor
  52. public HyperSearchResults(View view)
  53. {
  54. super(new BorderLayout());
  55. this.view = view;
  56. caption = new JLabel();
  57. updateCaption(0,0);
  58. add(BorderLayout.NORTH, caption);
  59. resultTreeRoot = new DefaultMutableTreeNode();
  60. resultTreeModel = new DefaultTreeModel(resultTreeRoot);
  61. resultTree = new JTree(resultTreeModel);
  62. resultTree.setCellRenderer(new ResultCellRenderer());
  63. resultTree.setVisibleRowCount(16);
  64. resultTree.setRootVisible(false);
  65. resultTree.setShowsRootHandles(true);
  66. resultTree.putClientProperty("JTree.lineStyle", "Angled");
  67. resultTree.setEditable(false);
  68. resultTree.addTreeSelectionListener(new TreeSelectionHandler());
  69. resultTree.addMouseListener(new MouseHandler());
  70. JScrollPane scrollPane = new JScrollPane(resultTree);
  71. Dimension dim = scrollPane.getPreferredSize();
  72. dim.width = 400;
  73. scrollPane.setPreferredSize(dim);
  74. add(BorderLayout.CENTER, scrollPane);
  75. } //}}}
  76. //{{{ requestDefaultFocus() method
  77. public boolean requestDefaultFocus()
  78. {
  79. resultTree.grabFocus();
  80. return true;
  81. } //}}}
  82. //{{{ addNotify() method
  83. public void addNotify()
  84. {
  85. super.addNotify();
  86. EditBus.addToBus(this);
  87. } //}}}
  88. //{{{ removeNotify() method
  89. public void removeNotify()
  90. {
  91. super.removeNotify();
  92. EditBus.removeFromBus(this);
  93. } //}}}
  94. //{{{ handleMessage() method
  95. public void handleMessage(EBMessage msg)
  96. {
  97. if(msg instanceof BufferUpdate)
  98. {
  99. BufferUpdate bmsg = (BufferUpdate)msg;
  100. Buffer buffer = bmsg.getBuffer();
  101. if(bmsg.getWhat() == BufferUpdate.LOADED)
  102. {
  103. for(int i = resultTreeRoot.getChildCount() - 1; i >= 0; i--)
  104. {
  105. DefaultMutableTreeNode bufferNode = (DefaultMutableTreeNode)
  106. resultTreeRoot.getChildAt(i);
  107. for(int j = bufferNode.getChildCount() - 1;
  108. j >= 0; j--)
  109. {
  110. HyperSearchResult result = (HyperSearchResult)
  111. ((DefaultMutableTreeNode)bufferNode
  112. .getChildAt(j)).getUserObject();
  113. if(buffer.getPath().equals(result.path))
  114. result.bufferOpened(buffer);
  115. }
  116. }
  117. }
  118. else if(bmsg.getWhat() == BufferUpdate.CLOSED)
  119. {
  120. for(int i = resultTreeRoot.getChildCount() - 1; i >= 0; i--)
  121. {
  122. DefaultMutableTreeNode bufferNode = (DefaultMutableTreeNode)
  123. resultTreeRoot.getChildAt(i);
  124. for(int j = bufferNode.getChildCount() - 1;
  125. j >= 0; j--)
  126. {
  127. HyperSearchResult result = (HyperSearchResult)
  128. ((DefaultMutableTreeNode)bufferNode
  129. .getChildAt(j)).getUserObject();
  130. if(buffer.getPath().equals(result.path))
  131. result.bufferClosed();
  132. }
  133. }
  134. }
  135. }
  136. } //}}}
  137. //{{{ getTreeModel() method
  138. public DefaultTreeModel getTreeModel()
  139. {
  140. return resultTreeModel;
  141. } //}}}
  142. //{{{ searchStarted() method
  143. public void searchStarted()
  144. {
  145. caption.setText(jEdit.getProperty("hypersearch-results.searching"));
  146. resultTreeRoot.removeAllChildren();
  147. resultTreeModel.reload(resultTreeRoot);
  148. } //}}}
  149. //{{{ searchDone() method
  150. public void searchDone(int resultCount, int bufferCount)
  151. {
  152. updateCaption(resultCount,bufferCount);
  153. SwingUtilities.invokeLater(new Runnable()
  154. {
  155. public void run()
  156. {
  157. for(int i = 0; i < resultTreeRoot.getChildCount(); i++)
  158. {
  159. resultTree.expandPath(new TreePath(
  160. ((DefaultMutableTreeNode)
  161. resultTreeRoot.getChildAt(i))
  162. .getPath()));
  163. }
  164. }
  165. });
  166. } //}}}
  167. //{{{ Private members
  168. private View view;
  169. private JLabel caption;
  170. private JTree resultTree;
  171. private DefaultMutableTreeNode resultTreeRoot;
  172. private DefaultTreeModel resultTreeModel;
  173. //{{{ goToSelectedNode() method
  174. private void goToSelectedNode()
  175. {
  176. TreePath path = resultTree.getSelectionPath();
  177. if(path == null)
  178. return;
  179. Object value = ((DefaultMutableTreeNode)path
  180. .getLastPathComponent()).getUserObject();
  181. if(value instanceof String)
  182. {
  183. Buffer buffer = jEdit.openFile(view,(String)value);
  184. if(buffer == null)
  185. return;
  186. view.setBuffer(buffer);
  187. view.toFront();
  188. view.requestFocus();
  189. }
  190. else
  191. {
  192. final HyperSearchResult result = (HyperSearchResult)value;
  193. final Buffer buffer = result.getBuffer();
  194. if(buffer == null)
  195. return;
  196. VFSManager.runInAWTThread(new Runnable()
  197. {
  198. public void run()
  199. {
  200. int start = result.startPos.getOffset();
  201. int end = result.endPos.getOffset();
  202. Selection s = new Selection.Range(start,end);
  203. view.setBuffer(buffer);
  204. JEditTextArea textArea = view.getTextArea();
  205. if(textArea.isMultipleSelectionEnabled())
  206. textArea.addToSelection(s);
  207. else
  208. textArea.setSelection(s);
  209. textArea.moveCaretPosition(start);
  210. view.toFront();
  211. view.requestFocus();
  212. }
  213. });
  214. }
  215. } //}}}
  216. //{{{ updateCaption() method
  217. private void updateCaption(int resultCount, int bufferCount)
  218. {
  219. Object[] pp = { new Integer(resultCount), new Integer(bufferCount) };
  220. caption.setText(jEdit.getProperty("hypersearch-results.caption",pp));
  221. } //}}}
  222. //}}}
  223. //{{{ MouseHandler class
  224. class MouseHandler extends MouseAdapter
  225. {
  226. //{{{ mouseClicked() method
  227. public void mouseClicked(MouseEvent evt)
  228. {
  229. TreePath path1 = resultTree.getPathForLocation(
  230. evt.getX(),evt.getY());
  231. if(path1 == null)
  232. return;
  233. TreePath path2 = resultTree.getSelectionPath();
  234. // if same thing selected twice, another selection event
  235. // is not sent, so we handle it in the mouse event
  236. if(path1.equals(path2))
  237. {
  238. resultTree.setSelectionPath(path1);
  239. goToSelectedNode();
  240. }
  241. } //}}}
  242. } //}}}
  243. //{{{ TreeSelectionHandler class
  244. class TreeSelectionHandler implements TreeSelectionListener
  245. {
  246. //{{{ valueChanged() method
  247. public void valueChanged(TreeSelectionEvent evt)
  248. {
  249. goToSelectedNode();
  250. } //}}}
  251. } //}}}
  252. //{{{ ResultCellRenderer class
  253. class ResultCellRenderer extends DefaultTreeCellRenderer
  254. {
  255. Font plainFont, boldFont;
  256. //{{{ ResultCellRenderer constructor
  257. ResultCellRenderer()
  258. {
  259. plainFont = UIManager.getFont("Tree.font");
  260. boldFont = new Font(plainFont.getName(),Font.BOLD,
  261. plainFont.getSize());
  262. } //}}}
  263. //{{{ getTreeCellRendererComponent() method
  264. public Component getTreeCellRendererComponent(JTree tree,
  265. Object value, boolean sel, boolean expanded,
  266. boolean leaf, int row, boolean hasFocus)
  267. {
  268. Component comp = super.getTreeCellRendererComponent(tree,value,sel,
  269. expanded,leaf,row,hasFocus);
  270. setIcon(null);
  271. DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
  272. if(node.getUserObject() instanceof String)
  273. {
  274. // file name
  275. ResultCellRenderer.this.setFont(boldFont);
  276. int count = node.getChildCount();
  277. if(count == 1)
  278. {
  279. setText(jEdit.getProperty("hypersearch-results"
  280. + ".file-caption1",new Object[] {
  281. node.getUserObject()
  282. }));
  283. }
  284. else
  285. {
  286. setText(jEdit.getProperty("hypersearch-results"
  287. + ".file-caption",new Object[] {
  288. node.getUserObject(),
  289. new Integer(count)
  290. }));
  291. }
  292. }
  293. else
  294. {
  295. ResultCellRenderer.this.setFont(plainFont);
  296. }
  297. return this;
  298. } //}}}
  299. } //}}}
  300. }