001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.repl;
038:
039: import java.io.Serializable;
040:
041: import java.util.List;
042: import edu.rice.cs.util.UnexpectedException;
043: import edu.rice.cs.util.text.EditDocumentException;
044:
045: /**
046: * Manages the execution of a Interactions History as a script of
047: * individual commands. Useful for presentations.
048: * @version $Id: InteractionsScriptModel.java 4255 2007-08-28 19:17:37Z mgricken $
049: */
050: public class InteractionsScriptModel implements Serializable {
051: /** The interactions model associated with the script. */
052: private InteractionsModel _model;
053: /** The interactions document. */
054: private InteractionsDocument _doc;
055: /** The interactions to perform. */
056: private List<String> _interactions;
057: /** The index into the list of the current interaction. */
058: private int _currentInteraction;
059: /**
060: * Indicates whether the iterator has "passed" the current interaction,
061: * which is the case after an execution.
062: * In this state, "next" will show the interaction after our index,
063: * and "prev" will show the interaction at our index (which was most
064: * recently executed).
065: */
066: private boolean _passedCurrent;
067:
068: /**
069: * Constructs a new interactions script using the given model and interactions.
070: * @param model the interactions model
071: * @param interactions the interactions that make up the script.
072: */
073: public InteractionsScriptModel(InteractionsModel model,
074: List<String> interactions) {
075: _model = model;
076: _doc = model.getDocument();
077: _interactions = interactions;
078: _currentInteraction = -1;
079: _passedCurrent = false;
080: }
081:
082: /**
083: * Enters the next interaction into the interactions pane.
084: */
085: public void nextInteraction() {
086: if (!hasNextInteraction()) {
087: throw new IllegalStateException(
088: "There is no next interaction!");
089: }
090: _currentInteraction++;
091: _showCurrentInteraction();
092: _passedCurrent = false;
093: }
094:
095: /**
096: * Enters the current interaction into the interactions pane.
097: *
098: public void currentInteraction() {
099: if (!hasCurrentInteraction()) {
100: throw new IllegalStateException("There is no current interaction!");
101: }
102: try {
103: _doc.clearCurrentInteraction();
104: String text = _interactions.get(_currentInteraction);
105: _doc.insertText(_doc.getLength(), text, _doc.DEFAULT_STYLE);
106: }
107: catch (EditDocumentException dae) {
108: throw new UnexpectedException(dae);
109: }
110: }*/
111:
112: /**
113: * Enters the previous interaction into the interactions pane.
114: */
115: public void prevInteraction() {
116: if (!hasPrevInteraction()) {
117: throw new IllegalStateException(
118: "There is no previous interaction!");
119: }
120: // Only move back if we haven't passed the current interaction
121: if (!_passedCurrent) {
122: _currentInteraction--;
123: }
124: _showCurrentInteraction();
125: _passedCurrent = false;
126: }
127:
128: /**
129: * Clears the current text at the prompt and shows the current
130: * interaction from the script.
131: */
132: protected void _showCurrentInteraction() {
133: try {
134: _doc.clearCurrentInteraction();
135: String text = _interactions.get(_currentInteraction);
136: _doc.insertText(_doc.getLength(), text, _doc.DEFAULT_STYLE);
137: } catch (EditDocumentException dae) {
138: throw new UnexpectedException(dae);
139: }
140: }
141:
142: /**
143: * Executes the current interaction.
144: * After this call, we have passed the current interaction.
145: */
146: public void executeInteraction() {
147: _model.interpretCurrentInteraction();
148: _passedCurrent = true;
149: }
150:
151: /**
152: * Ends the script.
153: * TODO: Is this method necessary at all?
154: */
155: public void closeScript() {
156: //_interactions = null; // Why do this? It can only cause problems...
157: _currentInteraction = -1;
158: _passedCurrent = false;
159: }
160:
161: /**
162: * @return true iff this script has another interaction to perform.
163: */
164: public boolean hasNextInteraction() {
165: return _currentInteraction < _interactions.size() - 1;
166: }
167:
168: /**
169: * @return true iff this script has a current interaction to perform.
170: *
171: public boolean hasCurrentInteraction() {
172: return _currentInteraction >= 0;
173: }*/
174:
175: /**
176: * @return true iff this script has a previous interaction to perform.
177: */
178: public boolean hasPrevInteraction() {
179: int index = _currentInteraction;
180: if (_passedCurrent) {
181: // We're passed the current, so the previous interaction is the current.
182: index++;
183: }
184: return index > 0;
185: }
186: }
|