|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| RMIInteractionsModel.java | 50% | 58.1% | 52.6% | 55.9% |
|
||||||||||||||
| 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 edu.rice.cs.drjava.model.repl.newjvm.*; | |
| 40 | import edu.rice.cs.plt.iter.IterUtil; | |
| 41 | import edu.rice.cs.plt.tuple.Option; | |
| 42 | import edu.rice.cs.plt.tuple.Pair; | |
| 43 | import edu.rice.cs.util.text.ConsoleDocumentInterface; | |
| 44 | ||
| 45 | import java.io.File; | |
| 46 | import java.awt.EventQueue; | |
| 47 | ||
| 48 | import static edu.rice.cs.plt.debug.DebugUtil.debug; | |
| 49 | ||
| 50 | /** A Swing specific InteractionsModel which can serve as the glue between a local InteractionsDocument and a remote | |
| 51 | * JavaInterpreter in another JVM. | |
| 52 | * @version $Id: RMIInteractionsModel.java 5181 2010-02-24 16:53:27Z mgricken $ | |
| 53 | */ | |
| 54 | public abstract class RMIInteractionsModel extends InteractionsModel { | |
| 55 | ||
| 56 | /** RMI interface to the remote Java interpreter.*/ | |
| 57 | protected final MainJVM _jvm; | |
| 58 | ||
| 59 | /** Constructs an InteractionsModel which can communicate with another JVM. | |
| 60 | * @param jvm RMI interface to the slave JVM | |
| 61 | * @param cDoc document to use in the InteractionsDocument | |
| 62 | * @param historySize Number of lines to store in the history | |
| 63 | * @param writeDelay Number of milliseconds to wait after each println | |
| 64 | */ | |
| 65 | 158 | public RMIInteractionsModel(MainJVM jvm, ConsoleDocumentInterface cDoc, File wd, int historySize, int writeDelay) { |
| 66 | 158 | super(cDoc, wd, historySize, writeDelay); |
| 67 | 158 | _jvm = jvm; |
| 68 | } | |
| 69 | ||
| 70 | /** Interprets the given command. | |
| 71 | * @param toEval command to be evaluated | |
| 72 | */ | |
| 73 | 41 | protected void _interpret(String toEval) { |
| 74 | 41 | debug.logStart("Interpret " + toEval); |
| 75 | 41 | _jvm.interpret(toEval); |
| 76 | 41 | debug.logEnd(); |
| 77 | } | |
| 78 | ||
| 79 | /** Gets the string representation of the value of a variable in the current interpreter. | |
| 80 | * @param var the name of the variable | |
| 81 | */ | |
| 82 | 0 | public Pair<String,String> getVariableToString(String var) { |
| 83 | 0 | System.out.println("getVariableToString: "+var); |
| 84 | 0 | Option<Pair<String,String>> result = _jvm.getVariableToString(var); |
| 85 | 0 | System.out.println("\tresult.isNone? "+result.isNone()); |
| 86 | 0 | Pair<String,String> retval = result.unwrap(new Pair<String,String>("","")); |
| 87 | 0 | System.out.println("\tretval: "+retval); |
| 88 | 0 | return retval; |
| 89 | } | |
| 90 | ||
| 91 | /** Adds the given path to the interpreter's class path. | |
| 92 | * @param f the path to add | |
| 93 | */ | |
| 94 | 0 | public void addProjectClassPath(File f) { _jvm.addProjectClassPath(f); } |
| 95 | ||
| 96 | /** These add the given path to the build directory class paths used in the interpreter. | |
| 97 | * @param f the path to add | |
| 98 | */ | |
| 99 | 0 | public void addBuildDirectoryClassPath(File f) { _jvm.addBuildDirectoryClassPath(f); } |
| 100 | ||
| 101 | /** These add the given path to the project files class paths used in the interpreter. | |
| 102 | * @param f the path to add | |
| 103 | */ | |
| 104 | 183 | public void addProjectFilesClassPath(File f) { _jvm.addProjectFilesClassPath(f); } |
| 105 | ||
| 106 | /** These add the given path to the external files class paths used in the interpreter. | |
| 107 | * @param f the path to add | |
| 108 | */ | |
| 109 | 224 | public void addExternalFilesClassPath(File f) { _jvm.addExternalFilesClassPath(f); } |
| 110 | ||
| 111 | /** These add the given path to the extra class paths used in the interpreter. | |
| 112 | * @param f the path to add | |
| 113 | */ | |
| 114 | 2 | public void addExtraClassPath(File f) { _jvm.addExtraClassPath(f); } |
| 115 | ||
| 116 | /** Resets the Java interpreter. */ | |
| 117 | 25 | protected void _resetInterpreter(File wd, boolean force) { |
| 118 | 25 | setToDefaultInterpreter(); |
| 119 | 25 | _jvm.setWorkingDirectory(wd); |
| 120 | 25 | _jvm.restartInterpreterJVM(force); |
| 121 | } | |
| 122 | ||
| 123 | /** Adds a named interpreter to the list. | |
| 124 | * @param name the unique name for the interpreter | |
| 125 | * @throws IllegalArgumentException if the name is not unique | |
| 126 | */ | |
| 127 | 1 | public void addInterpreter(String name) { _jvm.addInterpreter(name); } |
| 128 | ||
| 129 | /** Removes the interpreter with the given name, if it exists. | |
| 130 | * @param name Name of the interpreter to remove | |
| 131 | */ | |
| 132 | 0 | public void removeInterpreter(String name) { _jvm.removeInterpreter(name); } |
| 133 | ||
| 134 | /** Sets the active interpreter. | |
| 135 | * @param name the (unique) name of the interpreter. | |
| 136 | * @param prompt the prompt the interpreter should have. | |
| 137 | */ | |
| 138 | 1 | public void setActiveInterpreter(String name, String prompt) { |
| 139 | 1 | Option<Pair<Boolean, Boolean>> result = _jvm.setActiveInterpreter(name); |
| 140 | 1 | debug.logValue("result", result); |
| 141 | 1 | if (result.isSome() && result.unwrap().first()) { // interpreter changed |
| 142 | 1 | boolean inProgress = result.unwrap().second(); |
| 143 | 1 | _updateDocument(prompt, inProgress); |
| 144 | 1 | _notifyInterpreterChanged(inProgress); |
| 145 | } | |
| 146 | } | |
| 147 | ||
| 148 | /** Sets the default interpreter to be the current one. */ | |
| 149 | 25 | public void setToDefaultInterpreter() { |
| 150 | 25 | Option<Pair<Boolean, Boolean>> result = _jvm.setToDefaultInterpreter(); |
| 151 | 25 | if (result.isSome() && result.unwrap().first()) { // interpreter changed |
| 152 | 0 | boolean inProgress = result.unwrap().second(); |
| 153 | 0 | _updateDocument(InteractionsDocument.DEFAULT_PROMPT, inProgress); |
| 154 | 0 | _notifyInterpreterChanged(inProgress); |
| 155 | } | |
| 156 | } | |
| 157 | ||
| 158 | /** Updates the prompt and status of the document after an interpreter change. | |
| 159 | * Must run in event thread. (TODO: is it okay that related RMI calls occur in the event thread?) | |
| 160 | * @param prompt New prompt to display | |
| 161 | * @param inProgress whether the interpreter is currently in progress | |
| 162 | */ | |
| 163 | 1 | private void _updateDocument(String prompt, boolean inProgress) { |
| 164 | 1 | assert EventQueue.isDispatchThread(); |
| 165 | 1 | _document.setPrompt(prompt); |
| 166 | 1 | _document.insertNewline(_document.getLength()); |
| 167 | 1 | _document.insertPrompt(); |
| 168 | // int len = _document.getPromptLength(); | |
| 169 | // advanceCaret(len); | |
| 170 | 1 | _document.setInProgress(inProgress); |
| 171 | 1 | scrollToCaret(); |
| 172 | } | |
| 173 | ||
| 174 | /** Notifies listeners that the interpreter has changed. (Subclasses must maintain listeners.) | |
| 175 | * @param inProgress Whether the new interpreter is currently in progress with an interaction, i.e., whether | |
| 176 | * an interactionEnded event will be fired) | |
| 177 | */ | |
| 178 | protected abstract void _notifyInterpreterChanged(boolean inProgress); | |
| 179 | ||
| 180 | /** Sets whether or not the interpreter should enforce access to all members. */ | |
| 181 | 0 | public void setEnforceAllAccess(boolean enforce) { _jvm.setEnforceAllAccess(enforce); } |
| 182 | ||
| 183 | /** Sets whether or not the interpreter should enforce access to private members. */ | |
| 184 | 0 | public void setEnforcePrivateAccess(boolean enforce) { _jvm.setEnforcePrivateAccess(enforce); } |
| 185 | ||
| 186 | /** Require a semicolon at the end of statements. */ | |
| 187 | 0 | public void setRequireSemicolon(boolean require) { _jvm.setRequireSemicolon(require); } |
| 188 | ||
| 189 | /** Require variable declarations to include an explicit type. */ | |
| 190 | 0 | public void setRequireVariableType(boolean require) { _jvm.setRequireVariableType(require); } |
| 191 | ||
| 192 | /** Gets the interpreter class path from the interpreter jvm. | |
| 193 | * @return a list of class path elements | |
| 194 | */ | |
| 195 | 0 | public Iterable<File> getClassPath() { |
| 196 | 0 | Option<Iterable<File>> result = _jvm.getClassPath(); |
| 197 | 0 | return result.unwrap(IterUtil.<File>empty()); |
| 198 | } | |
| 199 | ||
| 200 | } |
|
||||||||||