|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.model.repl; |
|
38 |
| |
|
39 |
| import java.util.ArrayList; |
|
40 |
| import java.util.HashMap; |
|
41 |
| import edu.rice.cs.util.FileOps; |
|
42 |
| import edu.rice.cs.util.StringOps; |
|
43 |
| import edu.rice.cs.drjava.config.*; |
|
44 |
| import edu.rice.cs.drjava.DrJava; |
|
45 |
| import edu.rice.cs.drjava.model.*; |
|
46 |
| import edu.rice.cs.util.OperationCanceledException; |
|
47 |
| |
|
48 |
| import java.io.Serializable; |
|
49 |
| import java.io.IOException; |
|
50 |
| import java.io.File; |
|
51 |
| import java.io.OutputStreamWriter; |
|
52 |
| import java.io.OutputStream; |
|
53 |
| import java.io.BufferedWriter; |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| public class History implements OptionConstants, Serializable { |
|
60 |
| |
|
61 |
| public static final String INTERACTION_SEPARATOR = "//End of Interaction//"; |
|
62 |
| |
|
63 |
| |
|
64 |
| private volatile int _maxSize; |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| public static final String HISTORY_FORMAT_VERSION_2 = "// DrJava saved history v2" + StringOps.EOL; |
|
70 |
| |
|
71 |
| private final ArrayList<String> _vector = new ArrayList<String>(); |
|
72 |
| private volatile int _cursor = -1; |
|
73 |
| |
|
74 |
| |
|
75 |
| private final HashMap<Integer, String> _editedEntries = new HashMap<Integer, String>(); |
|
76 |
| |
|
77 |
| |
|
78 |
| private volatile String _currentSearchString = ""; |
|
79 |
| |
|
80 |
| |
|
81 |
| public final OptionListener<Integer> historyOptionListener = new OptionListener<Integer>() { |
|
82 |
5
| public void optionChanged (OptionEvent<Integer> oce) {
|
|
83 |
5
| int newSize = oce.value;
|
|
84 |
| |
|
85 |
5
| setMaxSize(newSize);
|
|
86 |
| } |
|
87 |
1
| public String toString() { return "HISTORY_MAX_SIZE OptionListener #" + hashCode(); }
|
|
88 |
| }; |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
21
| public History() {
|
|
93 |
21
| this(DrJava.getConfig().getSetting(HISTORY_MAX_SIZE));
|
|
94 |
| |
|
95 |
21
| DrJava.getConfig().addOptionListener(HISTORY_MAX_SIZE, historyOptionListener);
|
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
214
| public History(int maxSize) {
|
|
102 |
1
| if (maxSize < 0) maxSize = 0;
|
|
103 |
214
| _maxSize = maxSize;
|
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
157
| public OptionListener<Integer> getHistoryOptionListener() { return historyOptionListener; }
|
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
529
| public void setEditedEntry(String entry) {
|
|
113 |
9
| if (! entry.equals(getCurrent())) _editedEntries.put(Integer.valueOf(_cursor), entry);
|
|
114 |
| } |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
691
| public void add(String item) {
|
|
124 |
| |
|
125 |
691
| if (item.trim().length() > 0) {
|
|
126 |
690
| _vector.add(item);
|
|
127 |
| |
|
128 |
101
| if (_vector.size() > _maxSize) _vector.remove(0);
|
|
129 |
| |
|
130 |
690
| moveEnd();
|
|
131 |
690
| _editedEntries.clear();
|
|
132 |
| } |
|
133 |
| } |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
3
| public String removeLast() {
|
|
139 |
1
| if (_vector.size() == 0) { return null; }
|
|
140 |
2
| String last = _vector.remove(_vector.size()-1);
|
|
141 |
2
| if (_cursor > _vector.size()) { _cursor = _vector.size()-1; }
|
|
142 |
2
| return last;
|
|
143 |
| } |
|
144 |
| |
|
145 |
| |
|
146 |
931
| public void moveEnd() { _cursor = _vector.size(); }
|
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
518
| public void movePrevious(String entry) {
|
|
152 |
1
| if (! hasPrevious()) throw new ArrayIndexOutOfBoundsException();
|
|
153 |
517
| setEditedEntry(entry);
|
|
154 |
517
| _cursor--;
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
1
| public String lastEntry() { return _vector.get(_cursor - 1); }
|
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
7
| public void moveNext(String entry) {
|
|
164 |
0
| if (! hasNext()) throw new ArrayIndexOutOfBoundsException();
|
|
165 |
7
| setEditedEntry(entry);
|
|
166 |
6
| _cursor++;
|
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
561
| public boolean hasNext() { return _cursor < (_vector.size()); }
|
|
171 |
| |
|
172 |
| |
|
173 |
1028
| public boolean hasPrevious() { return _cursor > 0; }
|
|
174 |
| |
|
175 |
| |
|
176 |
564
| public String getCurrent() {
|
|
177 |
564
| Integer cursor = Integer.valueOf(_cursor);
|
|
178 |
13
| if (_editedEntries.containsKey(cursor)) return _editedEntries.get(cursor);
|
|
179 |
| |
|
180 |
537
| if (hasNext()) return _vector.get(_cursor);
|
|
181 |
14
| return "";
|
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
19
| public int size() { return _vector.size(); }
|
|
186 |
| |
|
187 |
| |
|
188 |
1
| public void clear() { _vector.clear(); }
|
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
4
| public String getHistoryAsStringWithSemicolons() {
|
|
194 |
4
| final StringBuilder s = new StringBuilder();
|
|
195 |
4
| final String delimiter = INTERACTION_SEPARATOR + StringOps.EOL;
|
|
196 |
4
| for (int i = 0; i < _vector.size(); i++) {
|
|
197 |
9
| String nextLine = _vector.get(i);
|
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
9
| s.append(nextLine);
|
|
204 |
9
| s.append(delimiter);
|
|
205 |
| } |
|
206 |
4
| return s.toString();
|
|
207 |
| } |
|
208 |
| |
|
209 |
| |
|
210 |
5
| public String getHistoryAsString() {
|
|
211 |
5
| final StringBuilder sb = new StringBuilder();
|
|
212 |
5
| final String delimiter = StringOps.EOL;
|
|
213 |
7
| for (String s: _vector) sb.append(s).append(delimiter);
|
|
214 |
5
| return sb.toString();
|
|
215 |
| } |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
3
| public void writeToFile(FileSaveSelector selector) throws IOException {
|
|
221 |
3
| writeToFile(selector, getHistoryAsStringWithSemicolons());
|
|
222 |
| } |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
3
| public static void writeToFile(FileSaveSelector selector, final String editedVersion) throws IOException {
|
|
230 |
3
| File c;
|
|
231 |
| |
|
232 |
3
| try { c = selector.getFile(); }
|
|
233 |
0
| catch (OperationCanceledException oce) { return; }
|
|
234 |
| |
|
235 |
| |
|
236 |
3
| if (c != null) {
|
|
237 |
2
| if (! c.exists() || selector.verifyOverwrite(c)) {
|
|
238 |
2
| FileOps.DefaultFileSaver saver = new FileOps.DefaultFileSaver(c) {
|
|
239 |
2
| public void saveTo(OutputStream os) throws IOException {
|
|
240 |
| |
|
241 |
2
| OutputStreamWriter osw = new OutputStreamWriter(os);
|
|
242 |
2
| BufferedWriter bw = new BufferedWriter(osw);
|
|
243 |
2
| String file = HISTORY_FORMAT_VERSION_2 + editedVersion;
|
|
244 |
2
| bw.write(file, 0, file.length());
|
|
245 |
2
| bw.close();
|
|
246 |
| } |
|
247 |
| }; |
|
248 |
2
| FileOps.saveFile(saver);
|
|
249 |
| } |
|
250 |
| } |
|
251 |
| } |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
7
| public void setMaxSize(int newSize) {
|
|
257 |
1
| if (newSize < 0) newSize = 0;
|
|
258 |
| |
|
259 |
| |
|
260 |
7
| if (size() > newSize) {
|
|
261 |
| |
|
262 |
2
| int numToDelete = size() - newSize;
|
|
263 |
20
| for (int i = 0; i < numToDelete; i++) { _vector.remove(0); }
|
|
264 |
| |
|
265 |
2
| moveEnd();
|
|
266 |
| } |
|
267 |
7
| _maxSize = newSize;
|
|
268 |
| } |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
4
| public void reverseSearch(String currentInteraction) {
|
|
274 |
4
| if (_currentSearchString.equals("") || ! currentInteraction.startsWith(_currentSearchString))
|
|
275 |
3
| _currentSearchString = currentInteraction;
|
|
276 |
| |
|
277 |
4
| setEditedEntry(currentInteraction);
|
|
278 |
4
| while (hasPrevious()) {
|
|
279 |
6
| movePrevious(getCurrent());
|
|
280 |
3
| if (getCurrent().startsWith(_currentSearchString, 0)) break;
|
|
281 |
| } |
|
282 |
| |
|
283 |
1
| if (! getCurrent().startsWith(_currentSearchString, 0)) moveEnd();
|
|
284 |
| } |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
1
| public void forwardSearch(String currentInteraction) {
|
|
290 |
1
| if (_currentSearchString.equals("") || ! currentInteraction.startsWith(_currentSearchString))
|
|
291 |
1
| _currentSearchString = currentInteraction;
|
|
292 |
| |
|
293 |
1
| setEditedEntry(currentInteraction);
|
|
294 |
1
| while (hasNext()) {
|
|
295 |
1
| moveNext(getCurrent());
|
|
296 |
1
| if (getCurrent().startsWith(_currentSearchString, 0)) break;
|
|
297 |
| } |
|
298 |
| |
|
299 |
0
| if (! getCurrent().startsWith(_currentSearchString, 0)) moveEnd();
|
|
300 |
| } |
|
301 |
| } |