001: /*
002: * History.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: History.java,v 1.2 2002/10/13 16:57:36 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: public final class History {
025: private final int limit;
026:
027: private final String name;
028: private final String[] strings;
029: private int count;
030: private int index; // Index for next/prev retrieval.
031:
032: public History(String name) {
033: this (name, 10);
034: }
035:
036: public History(String name, int limit) {
037: this .name = name;
038: this .limit = limit;
039: strings = new String[limit];
040: if (name != null) {
041: SessionProperties sessionProperties = Editor
042: .getSessionProperties();
043: int i;
044: for (i = 0; i < limit; i++) {
045: String key = "history." + name + "."
046: + String.valueOf(i);
047: String value = sessionProperties.getStringProperty(key,
048: null);
049: if (value == null)
050: break;
051: strings[i] = value;
052: }
053: count = i;
054: }
055: reset();
056: }
057:
058: public int size() {
059: return count;
060: }
061:
062: public void save() {
063: if (name != null) {
064: SessionProperties sessionProperties = Editor
065: .getSessionProperties();
066: for (int i = 0; i < count; i++) {
067: if (strings[i] == null)
068: break;
069: String key = "history." + name + "."
070: + String.valueOf(i);
071: sessionProperties.setStringProperty(key, strings[i]);
072: }
073: }
074: }
075:
076: public void append(String s) {
077: if (s.length() == 0)
078: return;
079: for (int i = 0; i < count; i++) {
080: if (s.equals(strings[i])) {
081: for (int j = i + 1; j < count; j++)
082: strings[j - 1] = strings[j];
083: --count;
084: break;
085: }
086: }
087: if (count == limit) {
088: for (int i = 0; i < limit - 1; i++)
089: strings[i] = strings[i + 1];
090: --count;
091: }
092: strings[count++] = s;
093: reset();
094: }
095:
096: public String get(int i) {
097: if (i < 0)
098: return null;
099: if (i > count - 1)
100: return null;
101: return strings[i];
102: }
103:
104: public String getPrevious() {
105: if (index > 0)
106: return get(--index);
107: return null;
108: }
109:
110: public String getNext() {
111: if (index < count - 1)
112: return get(++index);
113: return null;
114: }
115:
116: public final void reset() {
117: index = count;
118: }
119: }
|