Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.Panel;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.HashMap;
- import java.util.StringTokenizer;
- import java.util.Vector;
- import javax.swing.Box;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- /**
- * Provides a text console coding interface based around the CmdHandler class.
- * @author Keldon Alleyne
- * @copyright QMUL Group A 2004-2005
- */
- public class TextConsole extends Panel implements KeyListener {
- public JTextArea output;
- public JTextField input;
- public JLabel label;
- private HashMap commandList;
- public Box box;
- /**
- * holds a set of Commands to be carried out by flushCommands
- */
- public Vector commandBuffer;
- public TextConsole() {
- init();
- box = Box.createVerticalBox();
- Box bottom = Box.createHorizontalBox ();
- JScrollPane console = new JScrollPane(output);
- console.setPreferredSize(new Dimension(550, 220));
- label = new JLabel ();
- bottom.add(label);
- bottom.add(input);
- add(console);
- add(bottom);
- input.requestFocus();
- input.selectAll();
- Dimension d=input.getPreferredSize();
- input.setMinimumSize(d);
- input.setMaximumSize(new Dimension (2147483647, d.height));
- box.add ( console );
- box.add ( bottom );
- }
- public void writeln(String line) {
- output.append(line + "\n");
- output.setCaretPosition(output.getText().length());
- }
- public void echo(Command c) {
- writeln("\n>" + c.toString());
- }
- public void writeLine(String line) {
- // TODO
- writeln(line);
- }
- private void init() {
- commandList = new HashMap();
- commandBuffer = new Vector();
- output = new JTextArea(8, 40);
- output.setLineWrap(true);
- output.setWrapStyleWord(true);
- output.setEditable( false );
- input = new JTextField(40);
- input.addKeyListener(this);
- }
- private void addCommands(HashMap map, CmdHandler handlers[]) {
- for (int i = 0; i < handlers.length; i++) {
- CmdHandler c = handlers[i];
- map.put(c.cmdString().toLowerCase(), c);
- }
- }
- /**
- * sets current command set to CmdHandler's in newSet
- * @param newSet
- */
- public void useCommandSet(CmdHandler[][] newSet) {
- commandList = new HashMap();
- for (int i = 0; i < newSet.length; i++) {
- addCommands(commandList, newSet[i]);
- }
- }
- /**
- * will call handlers of given commands in commandBuffer
- *
- */
- public void flushCommands() {
- while (commandBuffer.size() > 0 ) {
- Command c = (Command) commandBuffer.remove(0);
- String commandName = c.getCommand();
- CmdHandler hand = (CmdHandler) commandList.get(commandName);
- if (hand == null) {
- writeln("unknown command (" + c.toString() + ")");
- } else {
- if ( hand.echoOn ) echo(c);
- hand.action(c, this);
- }
- }
- }
- public static void main ( String argv [] ) {
- TextConsole s = new TextConsole ();
- JFrame f = new JFrame ();
- f.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
- f.setSize ( 640, 480 );
- f.setLocation ( 0,0 );
- f.getContentPane().add( s );
- f.setVisible(true);
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyChar() == KeyEvent.VK_ENTER) {
- String line = input.getText();
- StringTokenizer st = new StringTokenizer(line, "|");
- while (st.hasMoreTokens()) {
- String subLine = st.nextToken();
- Command command = new Command(subLine);
- commandBuffer.add(command);
- }
- input.setText("");
- flushCommands ();
- }
- }
- public void keyReleased(KeyEvent e) {}
- public void keyTyped(KeyEvent e) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment