KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
37
38 import edu.rice.cs.drjava.DrJava;
39 import edu.rice.cs.drjava.config.OptionConstants;
40 import edu.rice.cs.drjava.config.OptionListener;
41 import edu.rice.cs.drjava.config.OptionEvent;
42 import edu.rice.cs.drjava.model.DefaultGlobalModel;
43 import edu.rice.cs.drjava.model.repl.newjvm.MainJVM;
44 import edu.rice.cs.util.StringOps;
45 import edu.rice.cs.util.text.ConsoleDocument;
46 import edu.rice.cs.util.text.*;
47 import edu.rice.cs.util.swing.Utilities;
48
49 /** Interactions model which can notify GlobalModelListeners on events.
50  * TODO: remove invokeLater wrappers here and enforce the policy that all of the listener methods must use them
51  * @version $Id: DefaultInteractionsModel.java 3925 2006-07-30 16:50:58Z rcartwright $
52  */

53 public class DefaultInteractionsModel extends RMIInteractionsModel {
54   /** Message to signal that input is required from the console. */
55 // public static final String INPUT_REQUIRED_MESSAGE =
56
// "Please enter input in the Console tab." + _newLine;
57

58   /** Model that contains the interpreter to use. (Can this be eliminated?) */
59   protected final DefaultGlobalModel _model;
60
61   /** Creates a new InteractionsModel.
62    * @param model DefaultGlobalModel to do the interpretation
63    * @param control RMI interface to the Interpreter JVM
64    * @param adapter InteractionsDJDocument to use for the document
65    */

66   public DefaultInteractionsModel(DefaultGlobalModel model, MainJVM control, EditDocumentInterface adapter, File JavaDoc wd) {
67     super(control, adapter, wd, DrJava.getConfig().getSetting(OptionConstants.HISTORY_MAX_SIZE).intValue(),
68           DefaultGlobalModel.WRITE_DELAY);
69     _model = model;
70     // Set whether to allow "assert" statements to be run in the remote JVM.
71
Boolean JavaDoc allow = DrJava.getConfig().getSetting(OptionConstants.RUN_WITH_ASSERT);
72     _jvm.setAllowAssertions(allow.booleanValue());
73     
74     // Add option listeners // WHEN ARE THESE EVER REMOVED?
75
DrJava.getConfig().addOptionListener(OptionConstants.HISTORY_MAX_SIZE, _document.getHistoryOptionListener());
76     DrJava.getConfig().addOptionListener(OptionConstants.RUN_WITH_ASSERT,
77                                          new OptionListener<Boolean JavaDoc>() {
78       public void optionChanged(OptionEvent<Boolean JavaDoc> oce) {
79         _jvm.setAllowAssertions(oce.value.booleanValue());
80       }
81     });
82   }
83
84   /** Called when the repl prints to System.out.
85    * @param s String to print
86    */

87   public void replSystemOutPrint(String JavaDoc s) {
88     super.replSystemOutPrint(s);
89     _model.systemOutPrint(s);
90   }
91
92   /** Called when the repl prints to System.err.
93    * @param s String to print
94    */

95   public void replSystemErrPrint(String JavaDoc s) {
96     super.replSystemErrPrint(s);
97     _model.systemErrPrint(s);
98   }
99
100   /** Returns a line of text entered by the user at the equivalent of System.in. */
101   public String JavaDoc getConsoleInput() {
102     String JavaDoc s = super.getConsoleInput();
103     _model.systemInEcho(s);
104     return s;
105   }
106
107   
108   /** Any extra action to perform (beyond notifying listeners) when the interpreter fails to reset.
109    * @param t The Throwable thrown by System.exit
110    */

111   protected void _interpreterResetFailed(Throwable JavaDoc t) {
112     _document.insertBeforeLastPrompt("Reset Failed! See the console tab for details." + _newLine,
113                                      InteractionsDocument.ERROR_STYLE);
114     // Print the exception to the console
115
_model.systemErrPrint(StringOps.getStackTrace(t));
116   }
117
118   /** Called when the Java interpreter is ready to use. This method body adds actions that involve the global model. */
119   public void interpreterReady(File JavaDoc wd) {
120     _model.resetInteractionsClassPath(); // Done here rather than in the superclass because _model is available here.
121
super.interpreterReady(wd);
122   }
123
124   /** Notifies listeners that an interaction has started. */
125   protected void _notifyInteractionStarted() {
126     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interactionStarted(); } });
127   }
128   
129   /** Notifies listeners that an interaction has ended. */
130   protected void _notifyInteractionEnded() {
131     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interactionEnded(); } });
132   }
133
134   /** Notifies listeners that an error was present in the interaction. */
135   protected void _notifySyntaxErrorOccurred(final int offset, final int length) {
136     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interactionErrorOccurred(offset,length); } });
137   }
138
139   /** Notifies listeners that the interpreter has changed.
140    * @param inProgress Whether the new interpreter is currently in progress.
141    */

142   protected void _notifyInterpreterChanged(final boolean inProgress) {
143     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interpreterChanged(inProgress); } });
144   }
145
146   /** Notifies listeners that the interpreter is resetting. */
147   protected void _notifyInterpreterResetting() {
148     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interpreterResetting(); } });
149   }
150
151   /** Notifies listeners that the interpreter is ready. */
152   public void _notifyInterpreterReady(final File JavaDoc wd) {
153 // System.out.println("Asynchronously notifying interpreterReady event listeners"); // DEBUG
154
Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interpreterReady(wd); } });
155   }
156
157   /** Notifies listeners that slave JVM has been used. */
158   protected void _notifySlaveJVMUsed(final File JavaDoc wd) {
159     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.slaveJVMUsed(); } });
160   }
161   
162   /** Notifies listeners that the interpreter has exited unexpectedly.
163    * @param status Status code of the dead process
164    */

165   protected void _notifyInterpreterExited(final int status) {
166     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interpreterExited(status); } });
167   }
168
169   /** Notifies listeners that the interpreter reset failed.
170    * @param t Throwable causing the failure
171    */

172   protected void _notifyInterpreterResetFailed(final Throwable JavaDoc t) {
173     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interpreterResetFailed(t); } });
174   }
175
176   /** Notifies the view that the current interaction is incomplete. */
177   protected void _notifyInteractionIncomplete() {
178     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.interactionIncomplete(); } });
179   }
180   
181    /** Notifies listeners that the slave JVM has been used. */
182   protected void _notifySlaveJVMUsed() {
183     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _notifier.slaveJVMUsed(); } });
184   }
185   
186   public ConsoleDocument getConsoleDocument() { return _model.getConsoleDocument(); }
187 }
188
Popular Tags