KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
37 import java.io.File JavaDoc;
38
39
40 import edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM;
41 import edu.rice.cs.drjava.model.repl.newjvm.ClassPathManager;
42
43 import edu.rice.cs.util.swing.Utilities;
44
45 import edu.rice.cs.util.text.ConsoleDocument;
46
47 /** A simple implementation of InteractionsModel, which uses a DynamicJavaAdapter directly (in the same JVM) to
48  * interpret code. It can be used in a standalone interface, such as edu.rice.cs.drjava.ui.SimpleInteractionsWindow.
49  * @version $Id: SimpleInteractionsModel.java 3839 2006-05-14 20:28:51Z rcartwright $
50  */

51 public class SimpleInteractionsModel extends InteractionsModel {
52
53   /** Milliseconds to wait after each println */
54   protected static final int WRITE_DELAY = 5;
55
56   /** An interpreter to evaluate interactions. */
57   protected JavaInterpreter _interpreter;
58
59   /** Creates a new InteractionsModel using a InteractionsDJDocument. */
60   public SimpleInteractionsModel() { this(new InteractionsDJDocument()); }
61
62   /** Creates a new InteractionsModel with the given document adapter.
63    * @param document Toolkit-independent document adapter
64    */

65   public SimpleInteractionsModel(InteractionsDJDocument document) {
66     super(document, new File JavaDoc(System.getProperty("user.dir")), 1000, WRITE_DELAY);
67     _interpreter = new DynamicJavaAdapter(new ClassPathManager());
68
69     _interpreter.defineVariable("INTERPRETER", _interpreter);
70   }
71
72   /**
73    * Interprets the given command.
74    * @param toEval command to be evaluated
75    */

76   protected void _interpret(String JavaDoc toEval) {
77     try {
78       Object JavaDoc result = _interpreter.interpret(toEval);
79       if (result != Interpreter.NO_RESULT) {
80         append(String.valueOf(result) + System.getProperty("line.separator"),
81                    InteractionsDocument.OBJECT_RETURN_STYLE);
82       }
83     }
84     catch (ExceptionReturnedException e) {
85       Throwable JavaDoc t = e.getContainedException();
86       // getStackTrace should be a utility method somewhere...
87
_document.appendExceptionResult(t.getClass().getName(),
88                                       t.getMessage(),
89                                       InterpreterJVM.getStackTrace(t),
90                                       InteractionsDocument.DEFAULT_STYLE);
91     }
92     finally { _interactionIsOver(); }
93   }
94
95   /**
96    * Gets the string representation of the value of a variable in the current interpreter.
97    * @param var the name of the variable
98    */

99   public String JavaDoc getVariableToString(String JavaDoc var) {
100     Object JavaDoc value = _interpreter.getVariable(var);
101     return value.toString();
102   }
103
104   /**
105    * Gets the class name of a variable in the current interpreter.
106    * @param var the name of the variable
107    */

108   public String JavaDoc getVariableClassName(String JavaDoc var) {
109     Class JavaDoc c = _interpreter.getVariableClass(var);
110     return c.getName();
111   }
112
113   /** Adds the given path to the interpreter's classpath.
114    * @param path Path to add
115    */

116   public void addProjectClassPath(URL JavaDoc path) { _interpreter.addProjectClassPath(path); }
117
118   /** Adds the given path to the interpreter's classpath.
119    * @param path Path to add
120    */

121   public void addBuildDirectoryClassPath(URL JavaDoc path) { _interpreter.addBuildDirectoryClassPath(path); }
122
123   /** Adds the given path to the interpreter's classpath.
124    * @param path Path to add
125    */

126   public void addProjectFilesClassPath(URL JavaDoc path) { _interpreter.addProjectFilesClassPath(path); }
127
128   /** Adds the given path to the interpreter's classpath.
129    * @param path Path to add
130    */

131   public void addExternalFilesClassPath(URL JavaDoc path) { _interpreter.addExternalFilesClassPath(path); }
132
133   /** Adds the given path to the interpreter's classpath.
134    * @param path Path to add
135    */

136   public void addExtraClassPath(URL JavaDoc path) { _interpreter.addExtraClassPath(path); }
137
138
139   /** Defines a variable in the interpreter to the given value. */
140   public void defineVariable(String JavaDoc name, Object JavaDoc value) { _interpreter.defineVariable(name, value); }
141
142   /** Defines a final variable in the interpreter to the given value. */
143   public void defineConstant(String JavaDoc name, Object JavaDoc value) { _interpreter.defineConstant(name, value); }
144
145   /** Sets whether protected and private variables and methods can be accessed from within the interpreter. */
146   public void setInterpreterPrivateAccessible(boolean accessible) { _interpreter.setPrivateAccessible(accessible); }
147
148   /** Any extra action to perform (beyond notifying listeners) when the interpreter fails to reset.
149    * @param t The Throwable thrown by System.exit
150    */

151   protected void _interpreterResetFailed(Throwable JavaDoc t) {
152     _document.insertBeforeLastPrompt("Reset Failed!" + _newLine, InteractionsDocument.ERROR_STYLE);
153   }
154
155   /** Resets the Java interpreter. */
156   protected void _resetInterpreter(File JavaDoc wd) {
157     interpreterResetting();
158     _interpreter = new DynamicJavaAdapter(new ClassPathManager());
159     interpreterReady(wd);
160   }
161
162   /** Notifies listeners that an interaction has started. */
163   protected void _notifyInteractionStarted() {
164     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interactionStarted(); } });
165   }
166
167   /** Notifies listeners that an interaction has ended. */
168   protected void _notifyInteractionEnded() {
169     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interactionEnded(); } });
170   }
171
172   /** Notifies listeners that an interaction contained a syntax error. */
173   protected void _notifySyntaxErrorOccurred(final int offset, final int length) {
174     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interactionErrorOccurred(offset, length); } });
175   }
176
177   /** Notifies listeners that the interpreter is resetting. */
178   protected void _notifyInterpreterResetting() { /* do nothing */ }
179
180   /** Notifies listeners that the interpreter is ready. */
181   public void _notifyInterpreterReady(File JavaDoc wd) {
182     // Ok, we don't need to do anything special
183
}
184
185   /** Notifies listeners that the interpreter has exited unexpectedly.
186    * @param status Status code of the dead process
187    */

188   protected void _notifyInterpreterExited(final int status) {
189     // Won't happen in a single JVM
190
}
191
192   /** Notifies listeners that the interpreter reset failed. */
193   protected void _notifyInterpreterResetFailed(Throwable JavaDoc t) {
194     // Won't happen in a single JVM
195
}
196
197   /** Notifies listeners that the interperaction was incomplete. */
198   protected void _notifyInteractionIncomplete() {
199     // Oh well. Nothing to do.
200
}
201   
202   /** Notifies listeners that the slave JVM has been used. */
203   protected void _notifySlaveJVMUsed() { /* do nothing; no slave JVM */ }
204    
205   /** Returns null because console tab document is not supported in this model */
206   public ConsoleDocument getConsoleDocument() { return null; }
207 }
208
Popular Tags