PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/help/HelpSearchPanel.java

#
Java | 300 lines | 225 code | 42 blank | 33 comment | 22 complexity | 6d5e664814a77763f68f142aa0631e9a 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. * HelpSearchPanel.java - Help search GUI
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2002 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.help;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.awt.event.*;
  26. import java.awt.*;
  27. import java.util.*;
  28. import org.gjt.sp.jedit.gui.*;
  29. import org.gjt.sp.jedit.io.VFSManager;
  30. import org.gjt.sp.jedit.*;
  31. import org.gjt.sp.util.Log;
  32. //}}}
  33. class HelpSearchPanel extends JPanel
  34. {
  35. //{{{ HelpSearchPanel constructor
  36. public HelpSearchPanel(HelpViewer helpViewer)
  37. {
  38. super(new BorderLayout(6,6));
  39. this.helpViewer = helpViewer;
  40. Box box = new Box(BoxLayout.X_AXIS);
  41. box.add(new JLabel(jEdit.getProperty("helpviewer.search.caption")));
  42. box.add(Box.createHorizontalStrut(6));
  43. box.add(searchField = new HistoryTextField("helpviewer.search"));
  44. searchField.addActionListener(new ActionHandler());
  45. add(BorderLayout.NORTH,box);
  46. results = new JList();
  47. results.addMouseListener(new MouseHandler());
  48. results.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  49. results.setCellRenderer(new ResultRenderer());
  50. add(BorderLayout.CENTER,new JScrollPane(results));
  51. } //}}}
  52. //{{{ Private members
  53. private HelpViewer helpViewer;
  54. private HistoryTextField searchField;
  55. private JList results;
  56. private HelpIndex index;
  57. private HelpIndex getHelpIndex()
  58. {
  59. if(index == null)
  60. {
  61. index = new HelpIndex();
  62. try
  63. {
  64. index.indexEditorHelp();
  65. }
  66. catch(Exception e)
  67. {
  68. index = null;
  69. Log.log(Log.ERROR,this,e);
  70. GUIUtilities.error(helpViewer,"helpviewer.search.error",
  71. new String[] { e.toString() });
  72. }
  73. }
  74. return index;
  75. } //}}}
  76. //{{{ ResultIcon class
  77. static class ResultIcon implements Icon
  78. {
  79. private static RenderingHints renderingHints;
  80. static
  81. {
  82. HashMap hints = new HashMap();
  83. hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  84. hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  85. renderingHints = new RenderingHints(hints);
  86. }
  87. private int rank;
  88. ResultIcon(int rank)
  89. {
  90. this.rank = rank;
  91. }
  92. public int getIconWidth()
  93. {
  94. return 40;
  95. }
  96. public int getIconHeight()
  97. {
  98. return 9;
  99. }
  100. public void paintIcon(Component c, Graphics g, int x, int y)
  101. {
  102. Graphics2D g2d = (Graphics2D)g.create();
  103. g2d.setRenderingHints(renderingHints);
  104. for(int i = 0; i < 4; i++)
  105. {
  106. if(rank > i)
  107. g2d.setColor(UIManager.getColor("Label.foreground"));
  108. else
  109. g2d.setColor(UIManager.getColor("Label.disabledForeground"));
  110. g2d.fillOval(x+i*10,y,9,9);
  111. }
  112. }
  113. } //}}}
  114. //{{{ ResultRenderer class
  115. class ResultRenderer extends DefaultListCellRenderer
  116. {
  117. public Component getListCellRendererComponent(
  118. JList list,
  119. Object value,
  120. int index,
  121. boolean isSelected,
  122. boolean cellHasFocus)
  123. {
  124. super.getListCellRendererComponent(list,null,index,
  125. isSelected,cellHasFocus);
  126. if(value instanceof String)
  127. {
  128. setIcon(null);
  129. setText((String)value);
  130. }
  131. else
  132. {
  133. Result result = (Result)value;
  134. setIcon(new ResultIcon(result.rank));
  135. setText(result.title);
  136. }
  137. return this;
  138. }
  139. } //}}}
  140. //{{{ Result class
  141. static class Result
  142. {
  143. String file;
  144. String title;
  145. int rank;
  146. Result(HelpIndex.HelpFile file, int count)
  147. {
  148. this.file = file.file;
  149. this.title = file.title;
  150. rank = count;
  151. }
  152. } //}}}
  153. //{{{ ResultCompare class
  154. static class ResultCompare implements Comparator
  155. {
  156. public int compare(Object o1, Object o2)
  157. {
  158. Result r1 = (Result)o1;
  159. Result r2 = (Result)o2;
  160. if(r1.rank == r2.rank)
  161. return r1.title.compareTo(r2.title);
  162. else
  163. return r2.rank - r1.rank;
  164. }
  165. } //}}}
  166. //{{{ ActionHandler class
  167. class ActionHandler implements ActionListener
  168. {
  169. public void actionPerformed(ActionEvent evt)
  170. {
  171. final HelpIndex index = getHelpIndex();
  172. if(index == null)
  173. return;
  174. results.setListData(new String[] { jEdit.getProperty(
  175. "helpviewer.searching") });
  176. final String text = searchField.getText();
  177. final Vector resultModel = new Vector();
  178. VFSManager.runInWorkThread(new Runnable()
  179. {
  180. public void run()
  181. {
  182. StringTokenizer st = new StringTokenizer(text,",.;:-? ");
  183. // we later use this to compute a relative ranking
  184. int maxRank = 0;
  185. while(st.hasMoreTokens())
  186. {
  187. String word = st.nextToken().toLowerCase();
  188. HelpIndex.Word lookup = index.lookupWord(word);
  189. if(lookup == null)
  190. continue;
  191. for(int i = 0; i < lookup.occurCount; i++)
  192. {
  193. HelpIndex.Word.Occurrence occur = lookup.occurrences[i];
  194. boolean ok = false;
  195. HelpIndex.HelpFile file = index.getFile(occur.file);
  196. for(int j = 0; j < resultModel.size(); j++)
  197. {
  198. Result result = (Result)resultModel.elementAt(j);
  199. if(result.file.equals(file.file))
  200. {
  201. result.rank += occur.count;
  202. result.rank += 20; // multiple files w/ word bonus
  203. maxRank = Math.max(result.rank,maxRank);
  204. ok = true;
  205. break;
  206. }
  207. }
  208. if(!ok)
  209. {
  210. maxRank = Math.max(occur.count,maxRank);
  211. resultModel.addElement(new Result(file,occur.count));
  212. }
  213. }
  214. }
  215. if(maxRank != 0)
  216. {
  217. // turn the rankings into relative rankings, from 1 to 4
  218. for(int i = 0; i < resultModel.size(); i++)
  219. {
  220. Result result = (Result)resultModel.elementAt(i);
  221. result.rank = (int)Math.ceil((double)result.rank * 4 / maxRank);
  222. }
  223. Collections.sort(resultModel,new ResultCompare());
  224. }
  225. }
  226. });
  227. VFSManager.runInAWTThread(new Runnable()
  228. {
  229. public void run()
  230. {
  231. if(resultModel.size() == 0)
  232. {
  233. results.setListData(new String[] {
  234. jEdit.getProperty(
  235. "helpviewer.no-results") });
  236. getToolkit().beep();
  237. }
  238. else
  239. results.setListData(resultModel);
  240. }
  241. });
  242. }
  243. } //}}}
  244. //{{{ MouseHandler class
  245. public class MouseHandler extends MouseAdapter
  246. {
  247. public void mouseReleased(MouseEvent evt)
  248. {
  249. int row = results.locationToIndex(evt.getPoint());
  250. if(row != -1)
  251. {
  252. Result result = (Result)results.getModel()
  253. .getElementAt(row);
  254. helpViewer.gotoURL(result.file,true);
  255. }
  256. }
  257. } //}}}
  258. }