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.File;
040:
041: import edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM;
042: import edu.rice.cs.drjava.model.repl.newjvm.ClassPathManager;
043:
044: import edu.rice.cs.util.StringOps;
045: import edu.rice.cs.util.swing.Utilities;
046:
047: import edu.rice.cs.util.text.ConsoleDocument;
048:
049: /** A simple implementation of InteractionsModel, which uses a DynamicJavaAdapter directly (in the same JVM) to
050: * interpret code. It can be used in a standalone interface, such as edu.rice.cs.drjava.ui.SimpleInteractionsWindow.
051: * @version $Id: SimpleInteractionsModel.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public class SimpleInteractionsModel extends InteractionsModel {
054:
055: /** Milliseconds to wait after each println */
056: protected static final int WRITE_DELAY = 5;
057:
058: /** An interpreter to evaluate interactions. */
059: protected JavaInterpreter _interpreter;
060:
061: /** Creates a new InteractionsModel using a InteractionsDJDocument. */
062: public SimpleInteractionsModel() {
063: this (new InteractionsDJDocument());
064: }
065:
066: /** Creates a new InteractionsModel with the given document adapter.
067: * @param document Toolkit-independent document adapter
068: */
069: public SimpleInteractionsModel(InteractionsDJDocument document) {
070: super (document, new File(System.getProperty("user.dir")), 1000,
071: WRITE_DELAY);
072: _interpreter = new DynamicJavaAdapter(new ClassPathManager());
073:
074: _interpreter.defineVariable("INTERPRETER", _interpreter);
075: }
076:
077: /**
078: * Interprets the given command.
079: * @param toEval command to be evaluated
080: */
081: protected void _interpret(String toEval) {
082: try {
083: Object result = _interpreter.interpret(toEval);
084: if (result != Interpreter.NO_RESULT) {
085: append(
086: String.valueOf(result) + "\n" /* formerly StringOps.EOL*/,
087: InteractionsDocument.OBJECT_RETURN_STYLE);
088: }
089: } catch (ExceptionReturnedException e) {
090: Throwable t = e.getContainedException();
091: // getStackTrace should be a utility method somewhere...
092: _document.appendExceptionResult(t.getClass().getName(), t
093: .getMessage(), InterpreterJVM.getStackTrace(t),
094: InteractionsDocument.DEFAULT_STYLE);
095: } finally {
096: _interactionIsOver();
097: }
098: }
099:
100: /**
101: * Gets the string representation of the value of a variable in the current interpreter.
102: * @param var the name of the variable
103: */
104: public String getVariableToString(String var) {
105: Object value = _interpreter.getVariable(var);
106: return value.toString();
107: }
108:
109: /**
110: * Gets the class name of a variable in the current interpreter.
111: * @param var the name of the variable
112: */
113: public String getVariableClassName(String var) {
114: Class c = _interpreter.getVariableClass(var);
115: return c.getName();
116: }
117:
118: /** Adds the given path to the interpreter's classpath.
119: * @param path Path to add
120: */
121: public void addProjectClassPath(File path) {
122: _interpreter.addProjectClassPath(path);
123: }
124:
125: /** Adds the given path to the interpreter's classpath.
126: * @param path Path to add
127: */
128: public void addBuildDirectoryClassPath(File path) {
129: _interpreter.addBuildDirectoryClassPath(path);
130: }
131:
132: /** Adds the given path to the interpreter's classpath.
133: * @param path Path to add
134: */
135: public void addProjectFilesClassPath(File path) {
136: _interpreter.addProjectFilesClassPath(path);
137: }
138:
139: /** Adds the given path to the interpreter's classpath.
140: * @param path Path to add
141: */
142: public void addExternalFilesClassPath(File path) {
143: _interpreter.addExternalFilesClassPath(path);
144: }
145:
146: /** Adds the given path to the interpreter's classpath.
147: * @param path Path to add
148: */
149: public void addExtraClassPath(File path) {
150: _interpreter.addExtraClassPath(path);
151: }
152:
153: /** Defines a variable in the interpreter to the given value. */
154: public void defineVariable(String name, Object value) {
155: _interpreter.defineVariable(name, value);
156: }
157:
158: /** Defines a final variable in the interpreter to the given value. */
159: public void defineConstant(String name, Object value) {
160: _interpreter.defineConstant(name, value);
161: }
162:
163: /** Sets whether protected and private variables and methods can be accessed from within the interpreter. */
164: public void setInterpreterPrivateAccessible(boolean accessible) {
165: _interpreter.setPrivateAccessible(accessible);
166: }
167:
168: /** Any extra action to perform (beyond notifying listeners) when the interpreter fails to reset.
169: * @param t The Throwable thrown by System.exit
170: */
171: protected void _interpreterResetFailed(Throwable t) {
172: _document.insertBeforeLastPrompt("Reset Failed!" + _newLine,
173: InteractionsDocument.ERROR_STYLE);
174: }
175:
176: /** Resets the Java interpreter. */
177: protected void _resetInterpreter(File wd) {
178: interpreterResetting();
179: _interpreter = new DynamicJavaAdapter(new ClassPathManager());
180: interpreterReady(wd);
181: }
182:
183: /** Notifies listeners that an interaction has started. */
184: protected void _notifyInteractionStarted() {
185: Utilities.invokeLater(new Runnable() {
186: public void run() {
187: _notifier.interactionStarted();
188: }
189: });
190: }
191:
192: /** Notifies listeners that an interaction has ended. */
193: protected void _notifyInteractionEnded() {
194: Utilities.invokeLater(new Runnable() {
195: public void run() {
196: _notifier.interactionEnded();
197: }
198: });
199: }
200:
201: /** Notifies listeners that an interaction contained a syntax error. */
202: protected void _notifySyntaxErrorOccurred(final int offset,
203: final int length) {
204: Utilities.invokeLater(new Runnable() {
205: public void run() {
206: _notifier.interactionErrorOccurred(offset, length);
207: }
208: });
209: }
210:
211: /** Notifies listeners that the interpreter is resetting. */
212: protected void _notifyInterpreterResetting() { /* do nothing */
213: }
214:
215: /** Notifies listeners that the interpreter is ready. */
216: public void _notifyInterpreterReady(File wd) {
217: // Ok, we don't need to do anything special
218: }
219:
220: /** Notifies listeners that the interpreter has exited unexpectedly.
221: * @param status Status code of the dead process
222: */
223: protected void _notifyInterpreterExited(final int status) {
224: // Won't happen in a single JVM
225: }
226:
227: /** Notifies listeners that the interpreter reset failed. */
228: protected void _notifyInterpreterResetFailed(Throwable t) {
229: // Won't happen in a single JVM
230: }
231:
232: /** Notifies listeners that the interperaction was incomplete. */
233: protected void _notifyInteractionIncomplete() {
234: // Oh well. Nothing to do.
235: }
236:
237: /** Notifies listeners that the slave JVM has been used. */
238: protected void _notifySlaveJVMUsed() { /* do nothing; no slave JVM */
239: }
240:
241: /** Returns null because console tab document is not supported in this model */
242: public ConsoleDocument getConsoleDocument() {
243: return null;
244: }
245: }
|