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.ui;
038:
039: import javax.swing.*;
040: import java.awt.Font;
041: import java.awt.event.*;
042: import java.io.File;
043:
044: import edu.rice.cs.drjava.config.FileOption;
045: import edu.rice.cs.drjava.model.repl.*;
046:
047: /** A standalone Interactions Window that provides the functionality of DrJava's Interactions Pane in a single JVM.
048: * Useful for quickly testing small pieces of code if DrJava is not running.
049: * @version $Id: SimpleInteractionsWindow.java 4255 2007-08-28 19:17:37Z mgricken $
050: */
051: public class SimpleInteractionsWindow extends JFrame {
052: //private final SimpleRMIInteractionsModel _rmiModel;
053: private final SimpleInteractionsModel _model;
054: private final InteractionsDJDocument _adapter;
055: private final InteractionsPane _pane;
056: private final InteractionsController _controller;
057:
058: public SimpleInteractionsWindow() {
059: this ("Interactions Window");
060: }
061:
062: public SimpleInteractionsWindow(String title) {
063: super (title);
064: setSize(600, 400);
065:
066: _adapter = new InteractionsDJDocument();
067: //_rmiModel = new SimpleRMIInteractionsModel(_adapter);
068: _model = new SimpleInteractionsModel(_adapter);
069: _pane = new InteractionsPane(_adapter) {
070: public int getPromptPos() {
071: return _model.getDocument().getPromptPos();
072: }
073: };
074: _controller = new InteractionsController(_model, _adapter,
075: _pane);
076:
077: _pane.setFont(Font.decode("monospaced"));
078:
079: _model.addListener(new InteractionsListener() {
080: public void interactionStarted() {
081: _pane.setEditable(false);
082: }
083:
084: public void interactionEnded() {
085: _controller.moveToPrompt();
086: _pane.setEditable(true);
087: }
088:
089: public void interpreterResetting() {
090: _pane.setEditable(false);
091: }
092:
093: public void interactionErrorOccurred(int offset, int length) {
094: _pane.highlightError(offset, length);
095: }
096:
097: public void interpreterReady(File wd) {
098: _controller.moveToPrompt();
099: _pane.setEditable(true);
100: }
101:
102: public void interpreterExited(int status) {
103: }
104:
105: public void interpreterChanged(boolean inProgress) {
106: _pane.setEditable(inProgress);
107: }
108:
109: public void interpreterResetFailed(Throwable t) {
110: interpreterReady(FileOption.NULL_FILE);
111: }
112:
113: public void interactionIncomplete() {
114: int caretPos = _pane.getCaretPosition();
115: _controller.getConsoleDoc().insertNewLine(caretPos);
116: }
117:
118: public void slaveJVMUsed() {
119: }
120: });
121:
122: JScrollPane scroll = new JScrollPane(_pane);
123: getContentPane().add(scroll);
124:
125: // Add listener to quit if window is closed
126: this .addWindowListener(new WindowAdapter() {
127: public void windowClosing(WindowEvent ev) {
128: close();
129: }
130: });
131: }
132:
133: /** Terminates this process. This is overridden in DrJava so that is disposes of itself instead of calling
134: * System.exit(0).
135: */
136: protected void close() {
137: System.exit(0);
138: }
139:
140: /** Accessor for the controller. */
141: public InteractionsController getController() {
142: return _controller;
143: }
144:
145: /** Defines a variable in this window to the given value. */
146: public void defineVariable(String name, Object value) {
147: _model.defineVariable(name, value);
148: }
149:
150: /** Defines a final variable in this window to the given value. */
151: public void defineConstant(String name, Object value) {
152: _model.defineConstant(name, value);
153: }
154:
155: /** Sets whether protected and private variables and methods can be accessed from within the interpreter. */
156: public void setInterpreterPrivateAccessible(boolean accessible) {
157: _model.setInterpreterPrivateAccessible(accessible);
158: }
159:
160: /** Main method to create a SimpleInteractionsWindow from the console. Doesn't take any command line arguments. */
161: public static void main(String[] args) {
162: SimpleInteractionsWindow w = new SimpleInteractionsWindow();
163: if (args.length > 0 && args[0].equals("-debug")) {
164: w.defineVariable("FRAME", w);
165: w.defineVariable("CONTROLLER", w.getController());
166: w.setInterpreterPrivateAccessible(true);
167: }
168: w.setVisible(true);
169: }
170: }
|