| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 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 |
550 |
private List<T> history = null; |
| 28 |
550 |
private boolean enabled = true; |
| 29 |
550 |
private int index = -1; |
| 30 |
|
|
| 31 |
550 |
public HistoryList() { |
| 32 |
550 |
history = new ArrayList<T>(); |
| 33 |
550 |
reset(); |
| 34 |
550 |
} |
| 35 |
|
|
| 36 |
|
public void setEnabled(boolean enabled) { |
| 37 |
8468 |
this.enabled = enabled; |
| 38 |
8468 |
} |
| 39 |
|
|
| 40 |
|
public boolean isEnabled() { |
| 41 |
0 |
return enabled; |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
public void put(T item) { |
| 45 |
6742 |
if (enabled) { |
| 46 |
176 |
for (int i = history.size() - 1; i > index; i--) { |
| 47 |
66 |
history.remove(i); |
| 48 |
|
} |
| 49 |
|
|
| 50 |
110 |
history.add(item); |
| 51 |
110 |
index++; |
| 52 |
|
} |
| 53 |
6742 |
} |
| 54 |
|
|
| 55 |
|
public boolean hasNext() { |
| 56 |
7380 |
boolean next = history.size()-1 > index; |
| 57 |
7380 |
return next; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
public T next() { |
| 61 |
330 |
if (hasNext()) { |
| 62 |
132 |
index++; |
| 63 |
132 |
return history.get(index); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
198 |
return null; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
public T current() { |
| 70 |
418 |
if (index >= 0 && index < history.size()) { |
| 71 |
396 |
return history.get(index); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
22 |
return null; |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
public boolean hasPrevious() { |
| 78 |
7380 |
return index > 0; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
public T previous() { |
| 82 |
330 |
if (hasPrevious()) { |
| 83 |
198 |
index--; |
| 84 |
198 |
return history.get(index); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
132 |
return null; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
public T last() { |
| 91 |
22 |
if (history.size() > 0) { |
| 92 |
22 |
index = history.size() - 1; |
| 93 |
22 |
return history.get(index); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
0 |
return null; |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
public T first() { |
| 100 |
0 |
if (history.size() > 0) { |
| 101 |
0 |
index = 0; |
| 102 |
0 |
return history.get(0); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
0 |
return null; |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
public void reset() { |
| 109 |
670 |
history.clear(); |
| 110 |
670 |
index = -1; |
| 111 |
670 |
enabled = true; |
| 112 |
670 |
} |
| 113 |
|
} |