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.DrJava;
042: import edu.rice.cs.drjava.config.OptionConstants;
043: import edu.rice.cs.drjava.config.OptionListener;
044: import edu.rice.cs.drjava.config.OptionEvent;
045: import edu.rice.cs.drjava.model.DefaultGlobalModel;
046: import edu.rice.cs.drjava.model.repl.newjvm.MainJVM;
047: import edu.rice.cs.drjava.ui.InteractionsController;
048: import edu.rice.cs.util.StringOps;
049: import edu.rice.cs.util.text.ConsoleDocument;
050: import edu.rice.cs.util.text.ConsoleDocumentInterface; //import edu.rice.cs.util.text.*;
051: import edu.rice.cs.util.swing.Utilities;
052:
053: /** Interactions model which can notify GlobalModelListeners on events.
054: * TODO: remove invokeLater wrappers here and enforce the policy that all of the listener methods must use them
055: * @version $Id: DefaultInteractionsModel.java 4255 2007-08-28 19:17:37Z mgricken $
056: */
057: public class DefaultInteractionsModel extends RMIInteractionsModel {
058: /** Message to signal that input is required from the console. */
059: // public static final String INPUT_REQUIRED_MESSAGE =
060: // "Please enter input in the Console tab." + _newLine;
061: /** Model that contains the interpreter to use. (Can this be eliminated?) */
062: protected final DefaultGlobalModel _model;
063:
064: /** Creates a new InteractionsModel.
065: * @param model DefaultGlobalModel to do the interpretation
066: * @param control RMI interface to the Interpreter JVM
067: * @param adapter InteractionsDJDocument to use for the document
068: */
069: public DefaultInteractionsModel(DefaultGlobalModel model,
070: MainJVM jvm, ConsoleDocumentInterface adapter, File wd) {
071: super (jvm, adapter, wd, DrJava.getConfig().getSetting(
072: OptionConstants.HISTORY_MAX_SIZE).intValue(),
073: DefaultGlobalModel.WRITE_DELAY);
074: _model = model;
075: // Set whether to allow "assert" statements to be run in the remote JVM.
076: Boolean allow = DrJava.getConfig().getSetting(
077: OptionConstants.RUN_WITH_ASSERT);
078: _jvm.setAllowAssertions(allow.booleanValue());
079:
080: // Add option listeners // WHEN ARE THESE EVER REMOVED?
081: DrJava.getConfig().addOptionListener(
082: OptionConstants.HISTORY_MAX_SIZE,
083: _document.getHistoryOptionListener());
084: DrJava.getConfig().addOptionListener(
085: OptionConstants.RUN_WITH_ASSERT,
086: new OptionListener<Boolean>() {
087: public void optionChanged(OptionEvent<Boolean> oce) {
088: _jvm.setAllowAssertions(oce.value
089: .booleanValue());
090: }
091: });
092: }
093:
094: /** Called when the repl prints to System.out.
095: * @param s String to print
096: */
097: public void replSystemOutPrint(String s) {
098: super .replSystemOutPrint(s); // Print s to interactions pane
099: _model.systemOutPrint(s); // Print s to console
100: }
101:
102: /** Called when the repl prints to System.err.
103: * @param s String to print
104: */
105: public void replSystemErrPrint(String s) {
106: super .replSystemErrPrint(s);
107: _model.systemErrPrint(s);
108: }
109:
110: /** Returns a line of text entered by the user at the equivalent of System.in. */
111: public String getConsoleInput() {
112: String s = super .getConsoleInput();
113: // System.err.println("Returning '" + s + "' as console input");
114: _model.systemInEcho(s);
115: return s;
116: }
117:
118: /** Any extra action to perform (beyond notifying listeners) when the interpreter fails to reset.
119: * @param t The Throwable thrown by System.exit
120: */
121: protected void _interpreterResetFailed(Throwable t) {
122: _document.insertBeforeLastPrompt(
123: "Reset Failed! See the console tab for details."
124: + _newLine, InteractionsDocument.ERROR_STYLE);
125: // Print the exception to the console
126: _model.systemErrPrint(StringOps.getStackTrace(t));
127: }
128:
129: /** Called when the Java interpreter is ready to use. This method body adds actions that involve the global model. */
130: public void interpreterReady(File wd) {
131: _model.resetInteractionsClassPath(); // Done here rather than in the superclass because _model is available here.
132: super .interpreterReady(wd);
133: }
134:
135: /** Notifies listeners that an interaction has started. */
136: protected void _notifyInteractionStarted() {
137: Utilities.invokeLater(new Runnable() {
138: public void run() {
139: _notifier.interactionStarted();
140: }
141: });
142: }
143:
144: /** Notifies listeners that an interaction has ended. */
145: protected void _notifyInteractionEnded() {
146: Utilities.invokeLater(new Runnable() {
147: public void run() {
148: _notifier.interactionEnded();
149: }
150: });
151: }
152:
153: /** Notifies listeners that an error was present in the interaction. */
154: protected void _notifySyntaxErrorOccurred(final int offset,
155: final int length) {
156: Utilities.invokeLater(new Runnable() {
157: public void run() {
158: _notifier.interactionErrorOccurred(offset, length);
159: }
160: });
161: }
162:
163: /** Notifies listeners that the interpreter has changed.
164: * @param inProgress Whether the new interpreter is currently in progress.
165: */
166: protected void _notifyInterpreterChanged(final boolean inProgress) {
167: Utilities.invokeLater(new Runnable() {
168: public void run() {
169: _notifier.interpreterChanged(inProgress);
170: }
171: });
172: }
173:
174: /** Notifies listeners that the interpreter is resetting. */
175: protected void _notifyInterpreterResetting() {
176: Utilities.invokeLater(new Runnable() {
177: public void run() {
178: _notifier.interpreterResetting();
179: }
180: });
181: }
182:
183: /** Notifies listeners that the interpreter is ready. */
184: public void _notifyInterpreterReady(final File wd) {
185: // System.out.println("Asynchronously notifying interpreterReady event listeners"); // DEBUG
186: Utilities.invokeLater(new Runnable() {
187: public void run() {
188: _notifier.interpreterReady(wd);
189: }
190: });
191: }
192:
193: /** Notifies listeners that slave JVM has been used. */
194: protected void _notifySlaveJVMUsed(final File wd) {
195: Utilities.invokeLater(new Runnable() {
196: public void run() {
197: _notifier.slaveJVMUsed();
198: }
199: });
200: }
201:
202: /** Notifies listeners that the interpreter has exited unexpectedly.
203: * @param status Status code of the dead process
204: */
205: protected void _notifyInterpreterExited(final int status) {
206: Utilities.invokeLater(new Runnable() {
207: public void run() {
208: _notifier.interpreterExited(status);
209: }
210: });
211: }
212:
213: /** Notifies listeners that the interpreter reset failed.
214: * @param t Throwable causing the failure
215: */
216: protected void _notifyInterpreterResetFailed(final Throwable t) {
217: Utilities.invokeLater(new Runnable() {
218: public void run() {
219: _notifier.interpreterResetFailed(t);
220: }
221: });
222: }
223:
224: /** Notifies the view that the current interaction is incomplete. */
225: protected void _notifyInteractionIncomplete() {
226: Utilities.invokeLater(new Runnable() {
227: public void run() {
228: _notifier.interactionIncomplete();
229: }
230: });
231: }
232:
233: /** Notifies listeners that the slave JVM has been used. */
234: protected void _notifySlaveJVMUsed() {
235: Utilities.invokeLater(new Runnable() {
236: public void run() {
237: _notifier.slaveJVMUsed();
238: }
239: });
240: }
241:
242: public ConsoleDocument getConsoleDocument() {
243: return _model.getConsoleDocument();
244: }
245: }
|