KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > History


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002,2003 Marc Prud'hommeaux mwp1@cornell.edu
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jline;
20
21 import java.io.*;
22 import java.util.*;
23
24
25 /**
26  * A command history buffer.
27  *
28  * @author <a HREF="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
29  */

30 public class History
31 {
32     private List history = new ArrayList ();
33     private PrintWriter output = null;
34     private int maxSize = 500;
35     private int currentIndex = 0;
36
37
38     /**
39      * Construstor: initialize a blank history.
40      */

41     public History ()
42     {
43     }
44
45
46     /**
47      * Construstor: initialize History object the the specified
48      * {@link File} for storage.
49      */

50     public History (File historyFile)
51         throws IOException
52     {
53         setHistoryFile (historyFile);
54     }
55
56
57     public void setHistoryFile (File historyFile)
58         throws IOException
59     {
60         if (historyFile.isFile ())
61             load (new FileInputStream (historyFile));
62         setOutput (new PrintWriter (new FileWriter (historyFile, false), true));
63         flushBuffer ();
64     }
65
66
67     /**
68      * Load the history buffer from the specified InputStream.
69      */

70     public void load (InputStream in)
71         throws IOException
72     {
73         load (new InputStreamReader (in));
74     }
75
76
77     /**
78      * Load the history buffer from the specified Reader.
79      */

80     public void load (Reader reader)
81         throws IOException
82     {
83         BufferedReader breader = new BufferedReader (reader);
84         List lines = new ArrayList ();
85         String JavaDoc line;
86         while ((line = breader.readLine ()) != null)
87         {
88             lines.add (line);
89         }
90
91         for (Iterator i = lines.iterator (); i.hasNext (); )
92             addToHistory ((String JavaDoc)i.next ());
93     }
94
95
96     public int size ()
97     {
98         return history.size ();
99     }
100
101
102     /**
103      * Clear the history buffer
104      */

105     public void clear ()
106     {
107         history.clear ();
108         currentIndex = 0;
109     }
110
111
112     /**
113      * Add the specified buffer to the end of the history. The pointer is
114      * set to the end of the history buffer.
115      */

116     public void addToHistory (String JavaDoc buffer)
117     {
118         // don't append duplicates to the end of the buffer
119
if (history.size () != 0 && buffer.equals (
120             history.get (history.size () - 1)))
121             return;
122
123         history.add (buffer);
124         while (history.size () > getMaxSize ())
125             history.remove (0);
126
127         currentIndex = history.size ();
128
129         if (getOutput () != null)
130         {
131             getOutput ().println (buffer);
132             getOutput ().flush ();
133         }
134     }
135
136
137     /**
138      * Flush the entire history buffer to the output PrintWriter.
139      */

140     public void flushBuffer ()
141         throws IOException
142     {
143         if (getOutput () != null)
144         {
145             for (Iterator i = history.iterator (); i.hasNext ();
146                 getOutput ().println ((String JavaDoc)i.next ()));
147
148             getOutput ().flush ();
149         }
150     }
151
152
153     /**
154      * Move to the end of the history buffer.
155      */

156     public void moveToEnd ()
157     {
158         currentIndex = history.size ();
159     }
160
161
162     /**
163      * Set the maximum size that the history buffer will store.
164      */

165     public void setMaxSize (int maxSize)
166     {
167         this.maxSize = maxSize;
168     }
169
170
171     /**
172      * Get the maximum size that the history buffer will store.
173      */

174     public int getMaxSize ()
175     {
176         return this.maxSize;
177     }
178
179
180     /**
181      * The output to which all history elements will be written (or null
182      * of history is not saved to a buffer).
183      */

184     public void setOutput (PrintWriter output)
185     {
186         this.output = output;
187     }
188
189
190     /**
191      * Returns the PrintWriter that is used to store history elements.
192      */

193     public PrintWriter getOutput ()
194     {
195         return this.output;
196     }
197
198
199     /**
200      * Returns the current history index.
201      */

202     public int getCurrentIndex ()
203     {
204         return this.currentIndex;
205     }
206
207
208     /**
209      * Return the content of the current buffer.
210      */

211     public String JavaDoc current ()
212     {
213         if (currentIndex >= history.size ())
214             return "";
215
216         return (String JavaDoc)history.get (currentIndex);
217     }
218
219
220     /**
221      * Move the pointer to the previous element in the buffer.
222      *
223      * @return true if we successfully went to the previous element
224      */

225     public boolean previous ()
226     {
227         if (currentIndex <= 0)
228             return false;
229
230         currentIndex--;
231         return true;
232     }
233
234
235     /**
236      * Move the pointer to the next element in the buffer.
237      *
238      * @return true if we successfully went to the next element
239      */

240     public boolean next ()
241     {
242         if (currentIndex >= history.size ())
243             return false;
244
245         currentIndex++;
246         return true;
247     }
248
249
250     /**
251      * Returns an immutable list of the history buffer.
252      */

253     public List getHistoryList ()
254     {
255         return Collections.unmodifiableList (history);
256     }
257
258
259     /**
260      * Returns the standard {@link AbstractCollection#toString} representation
261      * of the history list.
262      */

263     public String JavaDoc toString ()
264     {
265         return history.toString ();
266     }
267 }
268
269
Popular Tags