001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.swing;
023:
024: import java.awt.event.KeyAdapter;
025: import java.awt.event.KeyEvent;
026: import java.util.ArrayList;
027:
028: /**
029: * JCommandConsole - Extended version of JConsole which is targeted
030: * towards command line style use
031: * @version 1.0
032: * @author Wenzel Jakob
033: */
034: public class JCommandConsole extends JConsole {
035: private ArrayList history = null;
036: private ArrayList listeners = null;
037: private String command = null;
038: private String prompt = null;
039: private int cursorIndex = 0;
040: private int historyIndex = -1;
041:
042: private class ConsoleKeyListener extends KeyAdapter {
043: public void keyPressed(KeyEvent event) {
044: if (event.getKeyCode() == KeyEvent.VK_LEFT) {
045: if (cursorIndex > 0) {
046: cursorIndex--;
047: setCursorPosition(getCursorRow(),
048: getCursorColumn() - 1);
049: }
050: } else if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
051: if (cursorIndex < command.length()) {
052: setCursorPosition(getCursorRow(),
053: getCursorColumn() + 1);
054: cursorIndex++;
055: }
056: } else if (event.getKeyCode() == KeyEvent.VK_UP) {
057: if (historyIndex + 1 < history.size()) {
058: historyIndex++;
059: command = (String) history.get(historyIndex);
060: }
061: ConsoleRow row = getBuffer().getRow(
062: getBuffer().getRowCount() - 1);
063: row.setFragmentText(row.getFragmentCount() - 1, prompt
064: + command);
065: setCursorPosition(getBuffer().getRowCount() - 1,
066: command.length() + prompt.length());
067: cursorIndex = command.length();
068: } else if (event.getKeyCode() == KeyEvent.VK_DOWN) {
069: if (historyIndex > 0) {
070: historyIndex--;
071: command = (String) history.get(historyIndex);
072: } else if (historyIndex == 0) {
073: historyIndex--;
074: command = "";
075: }
076: ConsoleRow row = getBuffer().getRow(
077: getBuffer().getRowCount() - 1);
078: row.setFragmentText(row.getFragmentCount() - 1, prompt
079: + command);
080: setCursorPosition(getBuffer().getRowCount() - 1,
081: command.length() + prompt.length());
082: cursorIndex = command.length();
083: } else {
084: return;
085: }
086: refresh();
087: }
088:
089: public void keyTyped(KeyEvent event) {
090: if (event.getKeyChar() == '\b') {
091: if (cursorIndex > 0) {
092: command = command.substring(0, cursorIndex - 1)
093: + command.substring(cursorIndex);
094: ConsoleRow row = getBuffer().getRow(
095: getBuffer().getRowCount() - 1);
096: row.setFragmentText(row.getFragmentCount() - 1,
097: prompt + command);
098: setCursorPosition(getCursorRow(),
099: getCursorColumn() - 1);
100: cursorIndex--;
101: }
102: } else if (event.getKeyChar() == KeyEvent.VK_DELETE) {
103: if (cursorIndex < command.length()) {
104: command = command.substring(0, cursorIndex)
105: + command.substring(cursorIndex + 1);
106: ConsoleRow row = getBuffer().getRow(
107: getBuffer().getRowCount() - 1);
108: row.setFragmentText(row.getFragmentCount() - 1,
109: prompt + command);
110: }
111: } else if (event.getKeyChar() == '\n') {
112: appendText("\n");
113: fireCommand(command);
114: appendText(prompt);
115: setCursorPosition(getBuffer().getRowCount() - 1, prompt
116: .length());
117: command = "";
118: cursorIndex = 0;
119: scrollToBottom();
120: historyIndex = -1;
121: } else {
122: String keyString = String.valueOf(event.getKeyChar());
123: command = command.substring(0, cursorIndex)
124: + event.getKeyChar()
125: + command.substring(cursorIndex);
126: ConsoleRow row = getBuffer().getRow(
127: getBuffer().getRowCount() - 1);
128: row.setFragmentText(row.getFragmentCount() - 1, prompt
129: + command);
130: setCursorPosition(getCursorRow(), getCursorColumn() + 1);
131: cursorIndex++;
132: }
133: refresh();
134: }
135: }
136:
137: /**
138: * Create a new command console
139: */
140: public JCommandConsole() {
141: /* Install the key listener */
142: addKeyListener(new ConsoleKeyListener());
143:
144: /* Empty command at startup */
145: command = "";
146:
147: /* Default prompt */
148: prompt = "$ ";
149:
150: /* History */
151: history = new ArrayList();
152:
153: /* Listeners */
154: listeners = new ArrayList();
155:
156: /* Cursor setup */
157: setCursorPosition(0, prompt.length());
158: setDrawCursor(true);
159:
160: /* Add a prompt */
161: appendText(prompt);
162: }
163:
164: /**
165: * Add a command listener to the console
166: */
167: public void addCommandListener(CommandListener listener) {
168: listeners.add(listener);
169: }
170:
171: /**
172: * Remove a command listener from the console
173: */
174: public void removeCommandListener(CommandListener listener) {
175: listeners.remove(listener);
176: }
177:
178: /**
179: * Append text to the console
180: */
181: public void appendText(String text) {
182: getBuffer().append(text);
183: }
184:
185: /**
186: * Append text with an attribute to the console
187: */
188: public void appendText(String text, ConsoleAttribute attribute) {
189: getBuffer().append(text, attribute);
190: }
191:
192: /**
193: * Clear the console
194: */
195: public void clear() {
196: getBuffer().clear();
197: setCursorPosition(0, prompt.length());
198: appendText(prompt);
199: }
200:
201: /**
202: * Clear the history
203: */
204: public void clearHistory() {
205: history.clear();
206: }
207:
208: /**
209: * Get the prompt
210: */
211: public String getPrompt() {
212: return prompt;
213: }
214:
215: /**
216: * Set the prompt
217: */
218: public void setPrompt(String string) {
219: prompt = string;
220: }
221:
222: private void fireCommand(String command) {
223: history.add(0, command);
224:
225: for (int i = 0; i < listeners.size(); i++) {
226: ((CommandListener) listeners.get(i))
227: .onCommand(new CommandEvent(this, command));
228: }
229: }
230: }
|