|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| InteractionsScriptModel.java | 100% | 96.6% | 100% | 97.8% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model.repl; | |
| 38 | ||
| 39 | import java.awt.EventQueue; | |
| 40 | ||
| 41 | import java.util.List; | |
| 42 | import edu.rice.cs.util.UnexpectedException; | |
| 43 | import edu.rice.cs.util.text.EditDocumentException; | |
| 44 | ||
| 45 | /** Manages the execution of a Interactions History as a script of individual commands. Useful for presentations. | |
| 46 | * @version $Id: InteractionsScriptModel.java 5175 2010-01-20 08:46:32Z mgricken $ | |
| 47 | */ | |
| 48 | public class InteractionsScriptModel /* implements Serializable */ { | |
| 49 | /** The interactions model associated with the script. */ | |
| 50 | private volatile InteractionsModel _model; | |
| 51 | /** The interactions document. */ | |
| 52 | private volatile InteractionsDocument _doc; | |
| 53 | /** The interactions to perform. */ | |
| 54 | private volatile List<String> _interactions; | |
| 55 | /** The index into the list of the current interaction. */ | |
| 56 | private volatile int _currentInteraction; | |
| 57 | /** Indicates whether the iterator has "passed" the current interaction, which is the case after an execution. In | |
| 58 | * this state, "next" will show the interaction after our index, and "prev" will show the interaction at our index | |
| 59 | * (which was mostrecently executed). | |
| 60 | */ | |
| 61 | private volatile boolean _passedCurrent; | |
| 62 | ||
| 63 | /** Constructs a new interactions script using the given model and interactions. | |
| 64 | * @param model the interactions model | |
| 65 | * @param interactions the interactions that make up the script. | |
| 66 | */ | |
| 67 | 1 | public InteractionsScriptModel(InteractionsModel model, List<String> interactions) { |
| 68 | 1 | _model = model; |
| 69 | 1 | _doc = model.getDocument(); |
| 70 | 1 | _interactions = interactions; |
| 71 | 1 | _currentInteraction = -1; |
| 72 | 1 | _passedCurrent = false; |
| 73 | } | |
| 74 | ||
| 75 | /** Enters the next interaction into the interactions pane. Should only run in the event thread. */ | |
| 76 | 4 | public void nextInteraction() { |
| 77 | 1 | if (! hasNextInteraction()) { throw new IllegalStateException("There is no next interaction!"); } |
| 78 | 3 | _currentInteraction++; |
| 79 | 3 | _showCurrentInteraction(); |
| 80 | 3 | _passedCurrent = false; |
| 81 | } | |
| 82 | ||
| 83 | // /** Enters the current interaction into the interactions pane. */ | |
| 84 | // public void currentInteraction() { | |
| 85 | // if (!hasCurrentInteraction()) { | |
| 86 | // throw new IllegalStateException("There is no current interaction!"); | |
| 87 | // } | |
| 88 | // try { | |
| 89 | // _doc.clearCurrentInteraction(); | |
| 90 | // String text = _interactions.get(_currentInteraction); | |
| 91 | // _doc.insertText(_doc.getLength(), text, _doc.DEFAULT_STYLE); | |
| 92 | // } | |
| 93 | // catch (EditDocumentException dae) { | |
| 94 | // throw new UnexpectedException(dae); | |
| 95 | // } | |
| 96 | // } | |
| 97 | ||
| 98 | /** Enters the previous interaction into the interactions pane. Should only run in the event thread. */ | |
| 99 | 7 | public void prevInteraction() { |
| 100 | 3 | if (! hasPrevInteraction()) throw new IllegalStateException("There is no previous interaction!"); |
| 101 | ||
| 102 | // Only move back if we haven't passed the current interaction | |
| 103 | 2 | if (! _passedCurrent) _currentInteraction--; |
| 104 | 4 | _showCurrentInteraction(); |
| 105 | 4 | _passedCurrent = false; |
| 106 | } | |
| 107 | ||
| 108 | /** Clears the current text at the prompt and shows the current interaction from the script. Should only run in the | |
| 109 | * event thread. Assumes that write lock is already held. | |
| 110 | */ | |
| 111 | 7 | private void _showCurrentInteraction() { |
| 112 | 7 | try { |
| 113 | 7 | _doc.clearCurrentInteraction(); |
| 114 | 7 | String text = _interactions.get(_currentInteraction); |
| 115 | 7 | _doc.insertText(_doc.getLength(), text, InteractionsDocument.DEFAULT_STYLE); |
| 116 | } | |
| 117 | catch (EditDocumentException dae) { | |
| 118 | 0 | throw new UnexpectedException(dae); |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | /** Executes the current interaction. Should only run in the event thread. After this call, we have passed the | |
| 123 | * current interaction. | |
| 124 | */ | |
| 125 | 2 | public void executeInteraction() { |
| 126 | 2 | _passedCurrent = true; |
| 127 | /* The following must use EventQueue rather than Utilities because this task must be placed at the end of the | |
| 128 | * event queue, running the interpretCurrentInteraction call apart from this write locked section. In | |
| 129 | * SimpleInteractionModel, the interpret method is called SYNCHRONOUSLY. There is a faint chance of a race with | |
| 130 | * regard to the sequenceing of operations in the event queue. There could already be operations that affect | |
| 131 | * the determination of the current interaction on the event queue. If we forced the interpret method to run | |
| 132 | * asynchronously in SimpleInteractionsModel, then we could determine the current interaction within this write | |
| 133 | * locked section avoiding the race. */ | |
| 134 | 2 | EventQueue.invokeLater(new Runnable() { public void run() { _model.interpretCurrentInteraction(); } }); |
| 135 | } | |
| 136 | ||
| 137 | // /** Ends the script. Not currently used. */ | |
| 138 | // public synchronized void closeScript() { | |
| 139 | // _currentInteraction = -1; | |
| 140 | // _passedCurrent = false; | |
| 141 | // } | |
| 142 | ||
| 143 | /** @return true iff this script has another interaction to perform. */ | |
| 144 | 7 | public boolean hasNextInteraction() { |
| 145 | 7 | return _currentInteraction < _interactions.size() - 1; |
| 146 | } | |
| 147 | ||
| 148 | // /** @return true iff this script has a current interaction to perform. Not currently used. No sync required because | |
| 149 | // * it only reads a single volatile field. | |
| 150 | // */ | |
| 151 | // public boolean hasCurrentInteraction() { return _currentInteraction >= 0; } | |
| 152 | ||
| 153 | /** @return true iff this script has a previous interaction to perform. */ | |
| 154 | 15 | public boolean hasPrevInteraction() { |
| 155 | 15 | int index = _currentInteraction; |
| 156 | 4 | if (_passedCurrent) index++; // We're passed the current, so the previous interaction is the current. |
| 157 | 15 | return index > 0; |
| 158 | } | |
| 159 | } |
|
||||||||||