1 /*
2 * $Id$
3 *
4 * The contents of this file are subject to the Mozilla Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the License.
12 *
13 * The Original Code is XML Hammer code. (org.xmlhammer.*)
14 *
15 * The Initial Developer of the Original Code is Edwin Dankert. Portions created
16 * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer.
17 * All Rights Reserved.
18 *
19 * Contributor(s): Edwin Dankert <edankert@gmail.com>
20 */
21 package org.xmlhammer.gui.util;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 public class HistoryList<T> {
27 private List<T> history = null;
28 private boolean enabled = true;
29 private int index = -1;
30
31 public HistoryList() {
32 history = new ArrayList<T>();
33 reset();
34 }
35
36 public void setEnabled(boolean enabled) {
37 this.enabled = enabled;
38 }
39
40 public boolean isEnabled() {
41 return enabled;
42 }
43
44 public void put(T item) {
45 if (enabled) {
46 for (int i = history.size() - 1; i > index; i--) {
47 history.remove(i);
48 }
49
50 history.add(item);
51 index++;
52 }
53 }
54
55 public boolean hasNext() {
56 boolean next = history.size()-1 > index;
57 return next;
58 }
59
60 public T next() {
61 if (hasNext()) {
62 index++;
63 return history.get(index);
64 }
65
66 return null;
67 }
68
69 public T current() {
70 if (index >= 0 && index < history.size()) {
71 return history.get(index);
72 }
73
74 return null;
75 }
76
77 public boolean hasPrevious() {
78 return index > 0;
79 }
80
81 public T previous() {
82 if (hasPrevious()) {
83 index--;
84 return history.get(index);
85 }
86
87 return null;
88 }
89
90 public T last() {
91 if (history.size() > 0) {
92 index = history.size() - 1;
93 return history.get(index);
94 }
95
96 return null;
97 }
98
99 public T first() {
100 if (history.size() > 0) {
101 index = 0;
102 return history.get(0);
103 }
104
105 return null;
106 }
107
108 public void reset() {
109 history.clear();
110 index = -1;
111 enabled = true;
112 }
113 }