KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > InteractionsScriptModel


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.repl;
35
36 import java.io.Serializable JavaDoc;
37
38 import java.util.List JavaDoc;
39 import edu.rice.cs.util.UnexpectedException;
40 import edu.rice.cs.util.text.EditDocumentException;
41
42 /**
43  * Manages the execution of a Interactions History as a script of
44  * individual commands. Useful for presentations.
45  * @version $Id: InteractionsScriptModel.java 3574 2006-03-06 04:21:44Z rcartwright $
46  */

47 public class InteractionsScriptModel implements Serializable JavaDoc {
48   /** The interactions model associated with the script. */
49   private InteractionsModel _model;
50   /** The interactions document. */
51   private InteractionsDocument _doc;
52   /** The interactions to perform. */
53   private List JavaDoc<String JavaDoc> _interactions;
54   /** The index into the list of the current interaction. */
55   private int _currentInteraction;
56   /**
57    * Indicates whether the iterator has "passed" the current interaction,
58    * which is the case after an execution.
59    * In this state, "next" will show the interaction after our index,
60    * and "prev" will show the interaction at our index (which was most
61    * recently executed).
62    */

63   private boolean _passedCurrent;
64
65   /**
66    * Constructs a new interactions script using the given model and interactions.
67    * @param model the interactions model
68    * @param interactions the interactions that make up the script.
69    */

70   public InteractionsScriptModel(InteractionsModel model, List JavaDoc<String JavaDoc> interactions) {
71     _model = model;
72     _doc = model.getDocument();
73     _interactions = interactions;
74     _currentInteraction = -1;
75     _passedCurrent = false;
76   }
77
78   /**
79    * Enters the next interaction into the interactions pane.
80    */

81   public void nextInteraction() {
82     if (!hasNextInteraction()) {
83       throw new IllegalStateException JavaDoc("There is no next interaction!");
84     }
85     _currentInteraction++;
86     _showCurrentInteraction();
87     _passedCurrent = false;
88   }
89
90   /**
91    * Enters the current interaction into the interactions pane.
92    *
93   public void currentInteraction() {
94     if (!hasCurrentInteraction()) {
95       throw new IllegalStateException("There is no current interaction!");
96     }
97     try {
98       _doc.clearCurrentInteraction();
99       String text = _interactions.get(_currentInteraction);
100       _doc.insertText(_doc.getLength(), text, _doc.DEFAULT_STYLE);
101     }
102     catch (EditDocumentException dae) {
103       throw new UnexpectedException(dae);
104     }
105   }*/

106
107   /**
108    * Enters the previous interaction into the interactions pane.
109    */

110   public void prevInteraction() {
111     if (!hasPrevInteraction()) {
112       throw new IllegalStateException JavaDoc("There is no previous interaction!");
113     }
114     // Only move back if we haven't passed the current interaction
115
if (!_passedCurrent) {
116       _currentInteraction--;
117     }
118     _showCurrentInteraction();
119     _passedCurrent = false;
120   }
121
122   /**
123    * Clears the current text at the prompt and shows the current
124    * interaction from the script.
125    */

126   protected void _showCurrentInteraction() {
127     try {
128       _doc.clearCurrentInteraction();
129       String JavaDoc 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   /**
138    * Executes the current interaction.
139    * After this call, we have passed the current interaction.
140    */

141   public void executeInteraction() {
142     _model.interpretCurrentInteraction();
143     _passedCurrent = true;
144   }
145
146   /**
147    * Ends the script.
148    * TODO: Is this method necessary at all?
149    */

150   public void closeScript() {
151     //_interactions = null; // Why do this? It can only cause problems...
152
_currentInteraction = -1;
153     _passedCurrent = false;
154   }
155
156   /**
157    * @return true iff this script has another interaction to perform.
158    */

159   public boolean hasNextInteraction() {
160     return _currentInteraction < _interactions.size() - 1;
161   }
162
163   /**
164    * @return true iff this script has a current interaction to perform.
165    *
166   public boolean hasCurrentInteraction() {
167     return _currentInteraction >= 0;
168   }*/

169
170   /**
171    * @return true iff this script has a previous interaction to perform.
172    */

173   public boolean hasPrevInteraction() {
174     int index = _currentInteraction;
175     if (_passedCurrent) {
176       // We're passed the current, so the previous interaction is the current.
177
index++;
178     }
179     return index > 0;
180   }
181 }
Popular Tags