View Javadoc

1   /**
2    *	jline - Java console input library
3    *	Copyright (c) 2002, 2003, 2004, 2005, Marc Prud'hommeaux <mwp1@cornell.edu>
4    *	All rights reserved.
5    *
6    *	Redistribution and use in source and binary forms, with or
7    *	without modification, are permitted provided that the following
8    *	conditions are met:
9    *
10   *	Redistributions of source code must retain the above copyright
11   *	notice, this list of conditions and the following disclaimer.
12   *
13   *	Redistributions in binary form must reproduce the above copyright
14   *	notice, this list of conditions and the following disclaimer
15   *	in the documentation and/or other materials provided with
16   *	the distribution.
17   *
18   *	Neither the name of JLine nor the names of its contributors
19   *	may be used to endorse or promote products derived from this
20   *	software without specific prior written permission.
21   *
22   *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   *	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24   *	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25   *	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26   *	EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27   *	FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28   *	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29   *	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30   *	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31   *	AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32   *	LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33   *	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34   *	OF THE POSSIBILITY OF SUCH DAMAGE.
35   */
36  package jline;
37  
38  /**
39   *  A CursorBuffer is a holder for a {@link StringBuffer} that
40   *  also contains the current cursor position.
41   *
42   *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
43   */
44  public class CursorBuffer
45  {
46  	public int cursor = 0;
47  	public final StringBuffer buffer = new StringBuffer ();
48  
49  
50  	public int length ()
51  	{
52  		return buffer.length ();
53  	}
54  
55  
56  	public char current ()
57  	{
58  		if (cursor <= 0)
59  			return 0;
60  
61  		return buffer.charAt (cursor - 1);
62  	}
63  
64  
65  	/**
66  	 *  Insert the specific character into the buffer, setting the
67  	 *  cursor position ahead one.
68  	 *
69  	 *  @param  c  the character to insert
70  	 */
71  	public void insert (final char c)
72  	{
73  		buffer.insert (cursor++, c);
74  	}
75  
76  
77  	/**
78  	 *  Insert the specified {@link String} into the buffer, setting
79  	 *  the cursor to the end of the insertion point.
80  	 *
81  	 *  @param  str  the String to insert. Must not be null.
82  	 */
83  	public void insert (final String str)
84  	{
85  		if (buffer.length () == 0)
86  			buffer.append (str);
87  		else
88  			buffer.insert (cursor, str);
89  
90  		cursor += str.length ();
91  	}
92  
93  
94  	public String toString ()
95  	{
96  		return buffer.toString ();
97  	}
98  }