1 33 34 package edu.rice.cs.drjava.model.repl; 35 36 import java.util.Vector ; 37 import java.util.HashMap ; 38 import edu.rice.cs.util.FileOps; 39 import edu.rice.cs.drjava.config.*; 40 import edu.rice.cs.drjava.DrJava; 41 import edu.rice.cs.drjava.model.*; 42 import edu.rice.cs.util.OperationCanceledException; 43 44 import java.io.Serializable ; 45 import java.io.IOException ; 46 import java.io.File ; 47 import java.io.OutputStreamWriter ; 48 import java.io.OutputStream ; 49 import java.io.BufferedWriter ; 50 51 55 public class History implements OptionConstants, Serializable { 56 57 public static final String INTERACTION_SEPARATOR = "//End of Interaction//"; 58 59 private int _maxSize; 61 62 65 public static final String HISTORY_FORMAT_VERSION_2 = 66 "// DrJava saved history v2" + System.getProperty("line.separator"); 67 68 private Vector <String > _vector = new Vector <String >(); 69 private int _cursor = -1; 70 71 72 private HashMap <Integer , String > _editedEntries = new HashMap <Integer , String >(); 73 74 75 private String _currentSearchString = ""; 76 77 78 public History() { 79 this(DrJava.getConfig().getSetting(HISTORY_MAX_SIZE).intValue()); 80 DrJava.getConfig().addOptionListener(HISTORY_MAX_SIZE, historyOptionListener); 81 } 82 83 86 public History(int maxSize) { 87 _maxSize = maxSize; 88 if (_maxSize < 0) _maxSize = 0; 90 } 91 92 99 public void add(String item) { 100 if (item.trim().length() > 0) { 102 _vector.add(item); 103 if (_vector.size() > _maxSize) { 106 _vector.remove(0); 107 } 108 moveEnd(); 109 _editedEntries.clear(); 110 } 111 } 112 113 114 public void moveEnd() { _cursor = _vector.size(); } 115 116 119 public void movePrevious(String entry) { 120 if (! hasPrevious()) throw new ArrayIndexOutOfBoundsException (); 121 setEditedEntry(entry); 122 _cursor--; 123 } 124 125 126 public String lastEntry() { return _vector.get(_cursor - 1); } 127 128 131 public void moveNext(String entry) { 132 if (! hasNext()) throw new ArrayIndexOutOfBoundsException (); 133 setEditedEntry(entry); 134 _cursor++; 135 } 136 137 138 public boolean hasNext() { return _cursor < (_vector.size()); } 139 140 141 public boolean hasPrevious() { return _cursor > 0; } 142 143 144 public String getCurrent() { 145 Integer cursor = new Integer (_cursor); 146 if (_editedEntries.containsKey(cursor)) return _editedEntries.get(cursor); 147 148 if (hasNext()) return _vector.get(_cursor); 149 return ""; 150 } 151 152 153 public int size() { return _vector.size(); } 154 155 156 public void clear() { _vector.clear(); } 157 158 161 public String getHistoryAsStringWithSemicolons() { 162 final StringBuilder s = new StringBuilder (); 163 final String delimiter = INTERACTION_SEPARATOR + System.getProperty("line.separator"); 164 for (int i = 0; i < _vector.size(); i++) { 165 String nextLine = _vector.get(i); 166 s.append(nextLine); 172 s.append(delimiter); 173 } 174 return s.toString(); 175 } 176 177 180 public String getHistoryAsString() { 181 final StringBuilder sb = new StringBuilder (); 182 final String delimiter = System.getProperty("line.separator"); 183 for (String s: _vector) sb.append(s).append(delimiter); 184 return sb.toString(); 185 } 186 187 190 public void writeToFile(FileSaveSelector selector) throws IOException { 191 writeToFile(selector, getHistoryAsStringWithSemicolons()); 192 } 193 194 199 public static void writeToFile(FileSaveSelector selector, final String editedVersion) throws IOException { 200 File c; 201 202 try { c = selector.getFile(); } 203 catch (OperationCanceledException oce) { return; } 204 205 if (c != null) { 207 if (! c.exists() || selector.verifyOverwrite()) { 208 FileOps.DefaultFileSaver saver = new FileOps.DefaultFileSaver(c) { 209 public void saveTo(OutputStream os) throws IOException { 210 211 OutputStreamWriter osw = new OutputStreamWriter (os); 212 BufferedWriter bw = new BufferedWriter (osw); 213 String file = HISTORY_FORMAT_VERSION_2 + editedVersion; 214 bw.write(file, 0, file.length()); 226 bw.close(); 227 } 228 }; 229 FileOps.saveFile(saver); 230 } 231 } 232 } 233 234 237 public void setMaxSize(int newSize) { 238 if (newSize < 0) newSize = 0; 240 241 if (size() > newSize) { 243 244 int numToDelete = size() - newSize; 245 246 for (int i = 0; i< numToDelete; i++) { _vector.remove(0); } 247 248 moveEnd(); 249 } 250 _maxSize = newSize; 251 } 252 253 254 public final OptionListener<Integer > historyOptionListener = new OptionListener<Integer >() { 255 public void optionChanged (OptionEvent<Integer > oce) { 256 int newSize = oce.value.intValue(); 257 setMaxSize(newSize); 258 } 259 }; 260 261 262 public OptionListener<Integer > getHistoryOptionListener() { return historyOptionListener; } 263 264 267 public void setEditedEntry(String entry) { 268 if (!entry.equals(getCurrent())) _editedEntries.put(new Integer (_cursor), entry); 269 } 270 271 274 public void reverseSearch(String currentInteraction) { 275 if (_currentSearchString.equals("") || ! currentInteraction.startsWith(_currentSearchString)) 276 _currentSearchString = currentInteraction; 277 278 setEditedEntry(currentInteraction); 279 while (hasPrevious()) { 280 movePrevious(getCurrent()); 281 if (getCurrent().startsWith(_currentSearchString, 0)) break; 282 } 283 284 if (! getCurrent().startsWith(_currentSearchString, 0)) moveEnd(); 285 } 286 287 290 public void forwardSearch(String currentInteraction) { 291 if (_currentSearchString.equals("") || ! currentInteraction.startsWith(_currentSearchString)) 292 _currentSearchString = currentInteraction; 293 294 setEditedEntry(currentInteraction); 295 while (hasNext()) { 296 moveNext(getCurrent()); 297 if (getCurrent().startsWith(_currentSearchString, 0)) break; 298 } 299 300 if (! getCurrent().startsWith(_currentSearchString, 0)) moveEnd(); 301 } 302 } 303 | Popular Tags |