KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > History


1 /*
2  * History.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: History.java,v 1.2 2002/10/13 16:57:36 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class History
25 {
26     private final int limit;
27
28     private final String JavaDoc name;
29     private final String JavaDoc[] strings;
30     private int count;
31     private int index; // Index for next/prev retrieval.
32

33     public History(String JavaDoc name)
34     {
35         this(name, 10);
36     }
37
38     public History(String JavaDoc name, int limit)
39     {
40         this.name = name;
41         this.limit = limit;
42         strings = new String JavaDoc[limit];
43         if (name != null) {
44             SessionProperties sessionProperties = Editor.getSessionProperties();
45             int i;
46             for (i = 0; i < limit; i++) {
47                 String JavaDoc key = "history." + name + "." + String.valueOf(i);
48                 String JavaDoc value = sessionProperties.getStringProperty(key, null);
49                 if (value == null)
50                     break;
51                 strings[i] = value;
52             }
53             count = i;
54         }
55         reset();
56     }
57
58     public int size()
59     {
60         return count;
61     }
62
63     public void save()
64     {
65         if (name != null) {
66             SessionProperties sessionProperties = Editor.getSessionProperties();
67             for (int i = 0; i < count; i++) {
68                 if (strings[i] == null)
69                     break;
70                 String JavaDoc key = "history." + name + "." + String.valueOf(i);
71                 sessionProperties.setStringProperty(key, strings[i]);
72             }
73         }
74     }
75
76     public void append(String JavaDoc s)
77     {
78         if (s.length() == 0)
79             return;
80         for (int i = 0; i < count; i++) {
81             if (s.equals(strings[i])) {
82                 for (int j = i+1; j < count; j++)
83                     strings[j-1] = strings[j];
84                 --count;
85                 break;
86             }
87         }
88         if (count == limit) {
89             for (int i = 0; i < limit - 1; i++)
90                 strings[i] = strings[i+1];
91             --count;
92         }
93         strings[count++] = s;
94         reset();
95     }
96
97     public String JavaDoc get(int i)
98     {
99         if (i < 0)
100             return null;
101         if (i > count - 1)
102             return null;
103         return strings[i];
104     }
105
106     public String JavaDoc getPrevious()
107     {
108         if (index > 0)
109             return get(--index);
110         return null;
111     }
112
113     public String JavaDoc getNext()
114     {
115         if (index < count - 1)
116             return get(++index);
117         return null;
118     }
119
120     public final void reset()
121     {
122         index = count;
123     }
124 }
125
Popular Tags