1 33 34 package edu.rice.cs.drjava.model.repl; 35 36 import java.io.Serializable ; 37 38 import java.util.List ; 39 import edu.rice.cs.util.UnexpectedException; 40 import edu.rice.cs.util.text.EditDocumentException; 41 42 47 public class InteractionsScriptModel implements Serializable { 48 49 private InteractionsModel _model; 50 51 private InteractionsDocument _doc; 52 53 private List <String > _interactions; 54 55 private int _currentInteraction; 56 63 private boolean _passedCurrent; 64 65 70 public InteractionsScriptModel(InteractionsModel model, List <String > interactions) { 71 _model = model; 72 _doc = model.getDocument(); 73 _interactions = interactions; 74 _currentInteraction = -1; 75 _passedCurrent = false; 76 } 77 78 81 public void nextInteraction() { 82 if (!hasNextInteraction()) { 83 throw new IllegalStateException ("There is no next interaction!"); 84 } 85 _currentInteraction++; 86 _showCurrentInteraction(); 87 _passedCurrent = false; 88 } 89 90 106 107 110 public void prevInteraction() { 111 if (!hasPrevInteraction()) { 112 throw new IllegalStateException ("There is no previous interaction!"); 113 } 114 if (!_passedCurrent) { 116 _currentInteraction--; 117 } 118 _showCurrentInteraction(); 119 _passedCurrent = false; 120 } 121 122 126 protected void _showCurrentInteraction() { 127 try { 128 _doc.clearCurrentInteraction(); 129 String text = _interactions.get(_currentInteraction); 130 _doc.insertText(_doc.getLength(), text, _doc.DEFAULT_STYLE); 131 } 132 catch (EditDocumentException dae) { 133 throw new UnexpectedException(dae); 134 } 135 } 136 137 141 public void executeInteraction() { 142 _model.interpretCurrentInteraction(); 143 _passedCurrent = true; 144 } 145 146 150 public void closeScript() { 151 _currentInteraction = -1; 153 _passedCurrent = false; 154 } 155 156 159 public boolean hasNextInteraction() { 160 return _currentInteraction < _interactions.size() - 1; 161 } 162 163 169 170 173 public boolean hasPrevInteraction() { 174 int index = _currentInteraction; 175 if (_passedCurrent) { 176 index++; 178 } 179 return index > 0; 180 } 181 } | Popular Tags |