PageRenderTime 115ms CodeModel.GetById 107ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/textarea/Selection.java

#
Java | 229 lines | 113 code | 27 blank | 89 comment | 8 complexity | c3802f969c0600eed0d127bceb8a5941 MD5 | raw file
  1/*
  2 * Selection.java - Selected text
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 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
 23package org.gjt.sp.jedit.textarea;
 24
 25import org.gjt.sp.jedit.Buffer;
 26
 27/**
 28 * An interface representing a portion of the current selection.
 29 * @author Slava Pestov
 30 * @version $Id: Selection.java 3853 2001-10-25 07:35:25Z spestov $
 31 * @since jEdit 3.2pre1
 32 */
 33public abstract class Selection
 34{
 35	//{{{ getStart() method
 36	/**
 37	 * Returns the start offset of this selection.
 38	 */
 39	public int getStart()
 40	{
 41		return start;
 42	} //}}}
 43
 44	//{{{ getEnd() method
 45	/**
 46	 * Returns the end offset of this selection.
 47	 */
 48	public int getEnd()
 49	{
 50		return end;
 51	} //}}}
 52
 53	//{{{ getStartLine() method
 54	/**
 55	 * Returns the starting line number of this selection.
 56	 */
 57	public int getStartLine()
 58	{
 59		return startLine;
 60	} //}}}
 61
 62	//{{{ getEndLine() method
 63	/**
 64	 * Returns the ending line number of this selection.
 65	 */
 66	public int getEndLine()
 67	{
 68		return endLine;
 69	} //}}}
 70
 71	//{{{ getStart() method
 72	/**
 73	 * Returns the start offset of this selection on the specified
 74	 * line.
 75	 * @param buffer The buffer
 76	 * @param line The line number
 77	 */
 78	public abstract int getStart(Buffer buffer, int line);
 79	//}}}
 80
 81	//{{{ getEnd() method
 82	/**
 83	 * Returns the end offset of this selection on the specified
 84	 * line.
 85	 * @param buffer The buffer
 86	 * @param line The line number
 87	 */
 88	public abstract int getEnd(Buffer buffer, int line);
 89	//}}}
 90
 91	//{{{ toString() method
 92	public String toString()
 93	{
 94		return getClass().getName() + "[start=" + start
 95			+ ",end=" + end + ",startLine=" + startLine
 96			+ ",endLine=" + endLine + "]";
 97	} //}}}
 98
 99	//{{{ Package-private members
100	int start, end, startLine, endLine;
101	//}}}
102
103	//{{{ Protected members
104
105	//{{{ Selection constructor
106	protected Selection()
107	{
108	} //}}}
109
110	//{{{ Selection constructor
111	protected Selection(Selection copy)
112	{
113		start = copy.start;
114		end = copy.end;
115	} //}}}
116
117	//{{{ Selection constructor
118	protected Selection(int start, int end)
119	{
120		this.start = start;
121		this.end = end;
122
123		// setting these is handled by textArea._addToSelection();
124		//this.startLine = startLine;
125		//this.endLine = endLine;
126	} //}}}
127
128	//{{{ Range class
129	/**
130	 * An ordinary range selection.
131	 * @since jEdit 3.2pre1
132	 */
133	public static class Range extends Selection
134	{
135		//{{{ Range constructor
136		public Range()
137		{
138			super();
139		} //}}}
140
141		//{{{ Range constructor
142		public Range(Selection sel)
143		{
144			super(sel);
145		} //}}}
146
147		//{{{ Range constructor
148		public Range(int start, int end)
149		{
150			super(start,end);
151		} //}}}
152
153		//{{{ getStart() method
154		public int getStart(Buffer buffer, int line)
155		{
156			if(line == startLine)
157				return start;
158			else
159				return buffer.getLineStartOffset(line);
160		} //}}}
161
162		//{{{ getEnd() method
163		public int getEnd(Buffer buffer, int line)
164		{
165			if(line == endLine)
166				return end;
167			else
168				return buffer.getLineEndOffset(line) - 1;
169		} //}}}
170	} //}}}
171
172	//{{{ Rect class
173	/**
174	 * A rectangular selection.
175	 * @since jEdit 3.2pre1
176	 */
177	public static class Rect extends Selection
178	{
179		//{{{ Rect constructor
180		public Rect()
181		{
182			super();
183		} //}}}
184
185		//{{{ Rect constructor
186		public Rect(Selection sel)
187		{
188			super(sel);
189		} //}}}
190
191		//{{{ Rect constructor
192		public Rect(int start, int end)
193		{
194			super(start,end);
195		} //}}}
196
197		//{{{ getStart() method
198		public int getStart(Buffer buffer, int line)
199		{
200			if(line == startLine)
201				return start;
202			else
203			{
204				int _start = start - buffer.getLineStartOffset(startLine);
205				int _end = end - buffer.getLineStartOffset(endLine);
206
207				return Math.min(buffer.getLineEndOffset(line) - 1,
208					buffer.getLineStartOffset(line)
209					+ Math.min(_start,_end));
210			}
211		} //}}}
212
213		//{{{ getEnd() method
214		public int getEnd(Buffer buffer, int line)
215		{
216			if(line == endLine)
217				return end;
218			else
219			{
220				int _start = start - buffer.getLineStartOffset(startLine);
221				int _end = end - buffer.getLineEndOffset(endLine);
222
223				return Math.min(buffer.getLineEndOffset(line) - 1,
224					buffer.getLineStartOffset(line)
225					+ Math.max(_start,_end));
226			}
227		} //}}}
228	} //}}}
229}