PageRenderTime 431ms CodeModel.GetById 124ms app.highlight 101ms RepoModel.GetById 97ms app.codeStats 1ms

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