KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > ConsoleController


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.ui;
35
36 import javax.swing.*;
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.awt.event.KeyEvent JavaDoc;
39 import java.awt.Toolkit JavaDoc;
40 import java.awt.Event JavaDoc;
41 import javax.swing.text.DefaultEditorKit JavaDoc;
42 import java.io.Serializable JavaDoc;
43
44 import edu.rice.cs.drjava.DrJava;
45 import edu.rice.cs.drjava.config.*;
46 import edu.rice.cs.drjava.model.repl.*;
47 import edu.rice.cs.util.text.ConsoleDocument;
48 import edu.rice.cs.util.swing.Utilities;
49
50 /** @version $Id: ConsoleController.java 4031 2006-11-15 22:09:06Z rcartwright $ */
51 public class ConsoleController extends AbstractConsoleController implements Serializable JavaDoc {
52   protected ConsoleDocument _doc;
53
54   /** Object to wait on for input from System.in. */
55   private Object JavaDoc _inputWaitObject = new Object JavaDoc();
56
57   /** State so that the Enter action will only take place if the console is actually waiting for input. */
58   private boolean _blockedForConsoleInput;
59
60   public ConsoleController(final ConsoleDocument doc, InteractionsDJDocument adapter) {
61     super(adapter, new InteractionsPane("CONSOLE_KEYMAP", adapter) {
62       public int getPromptPos() { return doc.getPromptPos(); }
63     });
64     _doc = doc;
65     _blockedForConsoleInput = false;
66     _pane.setEditable(false);
67 // _pane.getCaret().setVisible(false);
68

69     _init();
70   }
71
72   /** Gets the ConsoleDocument. */
73   public ConsoleDocument getConsoleDoc() { return _doc; }
74
75   /** Allows the main controller to install the input listener into the model. */
76   public InputListener getInputListener() { return _inputListener; }
77
78   protected void _setupModel() {
79     _adapter.addDocumentListener(new CaretUpdateListener());
80     _doc.setBeep(_pane.getBeep());
81   }
82
83   /** Listens for input from System.in. */
84   protected InputListener _inputListener = new InputListener() {
85     public String JavaDoc getConsoleInput() {
86 // return JOptionPane.showInputDialog(MainFrame.this, "Please enter System.in:",
87
// "System.in", JOptionPane.QUESTION_MESSAGE);
88
Utilities.invokeAndWait(new Runnable JavaDoc() {
89         public void run() { _pane.setEditable(true); }
90       });
91       //_pane.getCaret().setVisible(true);
92
_waitForInput();
93       String JavaDoc s = _doc.getCurrentInput();
94       _doc.disablePrompt();
95       return s;
96     }
97   };
98
99   /** @return the Object that the console waits on. */
100   Object JavaDoc getInputWaitObject() { return _inputWaitObject; }
101
102   /** Waits for _inputWaitObject to be notified. */
103   protected void _waitForInput() {
104     synchronized(_inputWaitObject) {
105       try {
106         _blockedForConsoleInput = true;
107         while (_blockedForConsoleInput) _inputWaitObject.wait();
108       }
109       catch (InterruptedException JavaDoc ie) {
110         /* do nothing */
111       }
112     }
113   }
114
115   /** Adds actions to the view. */
116   protected void _setupView() {
117     super._setupView();
118
119     // Get proper cross-platform mask.
120
int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
121
122     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enterAction);
123     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, Event.SHIFT_MASK), newLineAction);
124
125     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_B, mask), clearCurrentAction);
126
127     // Left needs to be prevented from rolling cursor back before the prompt.
128
// Both left and right should lock when caret is before the prompt.
129
// Caret is allowed before the prompt for the purposes of mouse-based copy-
130
// and-paste.
131
_pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0), moveLeftAction);
132     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), moveLeftAction);
133
134     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0), moveRightAction);
135     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), moveRightAction);
136
137     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_KP_UP, 0), moveUpDownAction);
138     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), moveUpDownAction);
139     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_KP_DOWN, 0), moveUpDownAction);
140     _pane.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), moveUpDownAction);
141     _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_PASTE_FROM_HISTORY), pasteAction);
142   }
143       
144   /** No-op paste action. */
145   Action pasteAction = new DefaultEditorKit.PasteAction JavaDoc() {
146     public void actionPerformed(ActionEvent JavaDoc e) { }
147   };
148
149   AbstractAction enterAction = new EnterAction();
150   private class EnterAction extends AbstractAction implements Serializable JavaDoc {
151     public void actionPerformed(ActionEvent JavaDoc e) {
152       synchronized(_inputWaitObject) {
153         if (_blockedForConsoleInput) {
154           _pane.setEditable(false);
155           _pane.getCaret().setVisible(false);
156           _doc.insertNewLine(_doc.getLength());
157           _blockedForConsoleInput = false;
158           _inputWaitObject.notify(); // notify waiting thread that input is available
159
}
160       }
161     }
162   }
163
164   /** Moves the caret left or beeps at the edge. */
165   AbstractAction moveLeftAction = new LeftAction();
166   private class LeftAction extends AbstractAction implements Serializable JavaDoc {
167     public void actionPerformed(ActionEvent JavaDoc e) {
168       int position = _pane.getCaretPosition();
169       if (position < _doc.getPromptPos()) moveToPrompt();
170       else if (position == _doc.getPromptPos())_pane.getBeep().run();
171       else // position > _doc.getPromptPos()
172
_pane.setCaretPosition(position - 1);
173     }
174   }
175
176   /** Moves the caret right or beeps at the edge. */
177   AbstractAction moveRightAction = new RightAction();
178   
179   private class RightAction extends AbstractAction implements Serializable JavaDoc {
180     public void actionPerformed(ActionEvent JavaDoc e) {
181       int position = _pane.getCaretPosition();
182       if (position < _doc.getPromptPos()) moveToEnd();
183       else if (position >= _doc.getLength()) _pane.getBeep().run();
184       else // position between prompt and end
185
_pane.setCaretPosition(position + 1);
186     }
187   }
188
189
190   /** Cannot move up or down at console. Just move to the prompt if not in editable area, or beep if already after
191     * the prompt.
192     */

193   AbstractAction moveUpDownAction = new UpDownAction();
194   private class UpDownAction extends AbstractAction implements Serializable JavaDoc {
195     public void actionPerformed(ActionEvent JavaDoc e) {
196       int position = _pane.getCaretPosition();
197       if (position < _doc.getPromptPos()) moveToPrompt();
198       else _pane.getBeep().run();
199     }
200   }
201 }
202
203
Popular Tags