PageRenderTime 57ms CodeModel.GetById 43ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/search/HyperSearchResult.java

#
Java | 199 lines | 127 code | 26 blank | 46 comment | 18 complexity | 1d4e0ae34fe6d3036d8c92b567e8da30 MD5 | raw file
  1/*
  2 * HyperSearchResult.java - HyperSearch result
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1998, 2003 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.text.Position;
 27import org.gjt.sp.jedit.io.VFSManager;
 28import org.gjt.sp.jedit.textarea.*;
 29import org.gjt.sp.jedit.*;
 30//}}}
 31
 32/**
 33 * A set of occurrences of the search string on a given line in a buffer.
 34 */
 35public class HyperSearchResult
 36{
 37	public String path;
 38	public Buffer buffer;
 39	public int line;
 40	public String str; // cached for speed
 41	public Occur occur;
 42	public int occurCount;
 43
 44	//{{{ getBuffer() method
 45	public Buffer getBuffer()
 46	{
 47		if(buffer == null)
 48			buffer = jEdit.openFile(null,path);
 49		return buffer;
 50	} //}}}
 51
 52	//{{{ getSelection() method
 53	/**
 54	 * Returns an array of selection objects pointing to the occurrences
 55	 * of the search term on the current line. The buffer must be opened
 56	 * first.
 57	 * @since jEdit 4.2pre5
 58	 */
 59	public Selection[] getSelection()
 60	{
 61		if(buffer == null)
 62			return null;
 63
 64		Selection[] returnValue = new Selection[occurCount];
 65		Occur o = occur;
 66		int i = 0;
 67		while(o != null)
 68		{
 69			Selection.Range s = new Selection.Range(
 70				o.startPos.getOffset(),
 71				o.endPos.getOffset()
 72			);
 73			returnValue[i++] = s;
 74			o = o.next;
 75		}
 76		return returnValue;
 77	} //}}}
 78
 79	//{{{ goTo() method
 80	public void goTo(final View view)
 81	{
 82		if(buffer == null)
 83			buffer = jEdit.openFile(null,path);
 84
 85		if(buffer == null)
 86			return;
 87
 88		VFSManager.runInAWTThread(new Runnable()
 89		{
 90			public void run()
 91			{
 92				Selection[] s = getSelection();
 93				if(s == null)
 94					return;
 95
 96				EditPane pane = view.goToBuffer(buffer);
 97				JEditTextArea textArea = pane.getTextArea();
 98				if(textArea.isMultipleSelectionEnabled())
 99					textArea.addToSelection(s);
100				else
101					textArea.setSelection(s);
102                
103				textArea.moveCaretPosition(occur.endPos.getOffset());
104			}
105		});
106	} //}}}
107
108	//{{{ toString() method
109	public String toString()
110	{
111		return str;
112	} //}}}
113
114	//{{{ Package-private members
115
116	//{{{ HyperSearchResult constructor
117	HyperSearchResult(Buffer buffer, int line)
118	{
119		path = buffer.getPath();
120
121		if(!buffer.isTemporary())
122			bufferOpened(buffer);
123
124		this.line = line;
125
126		str = (line + 1) + ": " + buffer.getLineText(line)
127			.replace('\t',' ').trim();
128	} //}}}
129
130	//{{{ bufferOpened() method
131	void bufferOpened(Buffer buffer)
132	{
133		this.buffer = buffer;
134		Occur o = occur;
135		while(o != null)
136		{
137			o.bufferOpened();
138			o = o.next;
139		}
140	} //}}}
141
142	//{{{ bufferClosed() method
143	void bufferClosed()
144	{
145		buffer = null;
146		Occur o = occur;
147		while(o != null)
148		{
149			o.bufferClosed();
150			o = o.next;
151		}
152	} //}}}
153
154	//{{{ addOccur() method
155	void addOccur(int start, int end)
156	{
157		Occur o = new Occur(start,end);
158		o.next = occur;
159		occur = o;
160		occurCount++;
161	} //}}}
162
163	//}}}
164
165	//{{{ Occur class
166	public class Occur
167	{
168		public int start, end;
169		public Position startPos, endPos;
170		public Occur next;
171
172		//{{{ Occur constructor
173		Occur(int start, int end)
174		{
175			this.start = start;
176			this.end = end;
177
178			if(buffer != null && !buffer.isTemporary())
179				bufferOpened();
180		} //}}}
181
182		//{{{ bufferOpened() method
183		void bufferOpened()
184		{
185			startPos = buffer.createPosition(Math.min(
186				buffer.getLength(),start));
187			endPos = buffer.createPosition(Math.min(
188				buffer.getLength(),end));
189		} //}}}
190
191		//{{{ bufferClosed() method
192		void bufferClosed()
193		{
194			start = startPos.getOffset();
195			end = endPos.getOffset();
196			startPos = endPos = null;
197		} //}}}
198	} //}}}
199}