PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/help/HelpSearchPanel.java

#
Java | 196 lines | 138 code | 26 blank | 32 comment | 14 complexity | f3f92c4eaa89fe4154bdf8fa237ababb 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.StringTokenizer;
  28. import org.gjt.sp.jedit.gui.*;
  29. import org.gjt.sp.jedit.*;
  30. import org.gjt.sp.util.Log;
  31. //}}}
  32. public class HelpSearchPanel extends JPanel
  33. {
  34. //{{{ HelpSearchPanel constructor
  35. public HelpSearchPanel(HelpViewer helpViewer)
  36. {
  37. super(new BorderLayout(6,6));
  38. this.helpViewer = helpViewer;
  39. Box box = new Box(BoxLayout.X_AXIS);
  40. box.add(new JLabel(jEdit.getProperty("helpviewer.search.caption")));
  41. box.add(Box.createHorizontalStrut(6));
  42. box.add(searchField = new HistoryTextField("helpviewer.search"));
  43. searchField.addActionListener(new ActionHandler());
  44. add(BorderLayout.NORTH,box);
  45. results = new JList();
  46. results.addMouseListener(new MouseHandler());
  47. results.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  48. results.setCellRenderer(new ResultRenderer());
  49. add(BorderLayout.CENTER,new JScrollPane(results));
  50. } //}}}
  51. //{{{ Private members
  52. private HelpViewer helpViewer;
  53. private HistoryTextField searchField;
  54. private JList results;
  55. private HelpIndex index;
  56. private HelpIndex getHelpIndex()
  57. {
  58. if(index == null)
  59. {
  60. index = new HelpIndex();
  61. try
  62. {
  63. index.indexEditorHelp();
  64. }
  65. catch(Exception e)
  66. {
  67. index = null;
  68. Log.log(Log.ERROR,this,e);
  69. GUIUtilities.error(helpViewer,"helpviewer.search.error",
  70. new String[] { e.toString() });
  71. }
  72. }
  73. return index;
  74. } //}}}
  75. //}}}
  76. //{{{ Result class
  77. class Result
  78. {
  79. int rank;
  80. String title;
  81. String file;
  82. Result(String title, String file)
  83. {
  84. this.title = title;
  85. this.file = file;
  86. }
  87. public String toString()
  88. {
  89. return rank + ":" + title + ":" + file;
  90. }
  91. public boolean equals(Object o)
  92. {
  93. if(o instanceof Result)
  94. return ((Result)o).file.equals(file);
  95. else
  96. return false;
  97. }
  98. } //}}}
  99. //{{{ ResultRenderer class
  100. class ResultRenderer extends DefaultListCellRenderer
  101. {
  102. public Component getListCellRendererComponent(
  103. JList list,
  104. Object value,
  105. int index,
  106. boolean isSelected,
  107. boolean cellHasFocus)
  108. {
  109. super.getListCellRendererComponent(list,null,index,
  110. isSelected,cellHasFocus);
  111. Result result = (Result)value;
  112. setText(result.title);
  113. return this;
  114. }
  115. } //}}}
  116. //{{{ ActionHandler class
  117. class ActionHandler implements ActionListener
  118. {
  119. public void actionPerformed(ActionEvent evt)
  120. {
  121. HelpIndex index = getHelpIndex();
  122. if(index == null)
  123. return;
  124. StringTokenizer st = new StringTokenizer(
  125. searchField.getText(),",.;:- ");
  126. DefaultListModel resultModel = new DefaultListModel();
  127. while(st.hasMoreTokens())
  128. {
  129. String word = st.nextToken().toLowerCase();
  130. HelpIndex.Word lookup = index.getWord(word);
  131. if(lookup != null)
  132. {
  133. for(int i = 0; i < lookup.fileCount; i++)
  134. {
  135. Result result = new Result(
  136. MiscUtilities.getFileName(lookup.files[i]),
  137. lookup.files[i]);
  138. int idx = resultModel.indexOf(result);
  139. // if not in list, add; otherwise increment
  140. // rank
  141. if(idx == -1)
  142. resultModel.addElement(result);
  143. else
  144. {
  145. ((Result)resultModel.getElementAt(idx))
  146. .rank += 1;
  147. }
  148. }
  149. }
  150. }
  151. results.setModel(resultModel);
  152. if(resultModel.getSize() == 0)
  153. getToolkit().beep();
  154. }
  155. } //}}}
  156. //{{{ MouseHandler class
  157. public class MouseHandler extends MouseAdapter
  158. {
  159. public void mouseReleased(MouseEvent evt)
  160. {
  161. int row = results.locationToIndex(evt.getPoint());
  162. if(row != -1)
  163. {
  164. Result result = (Result)results.getModel()
  165. .getElementAt(row);
  166. helpViewer.gotoURL(result.file,true);
  167. }
  168. }
  169. } //}}}
  170. }