keldon85

TextConsole.java

Aug 24th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.Panel;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5. import java.util.HashMap;
  6. import java.util.StringTokenizer;
  7. import java.util.Vector;
  8.  
  9. import javax.swing.Box;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14. import javax.swing.JTextField;
  15.  
  16. /**
  17.  * Provides a text console coding interface based around the CmdHandler class.
  18.  * @author Keldon Alleyne
  19.  * @copyright QMUL Group A 2004-2005
  20.  */
  21. public class TextConsole extends Panel implements KeyListener {
  22.  
  23.     public JTextArea output;
  24.  
  25.     public JTextField input;
  26.    
  27.     public JLabel label;
  28.  
  29.     private HashMap commandList;
  30.     public Box box;
  31.  
  32.    
  33.     /**
  34.      * holds a set of Commands to be carried out by flushCommands
  35.      */
  36.     public Vector commandBuffer;
  37.  
  38.     public TextConsole() {
  39.         init();
  40.        
  41.         box =  Box.createVerticalBox();
  42.         Box bottom = Box.createHorizontalBox ();
  43.  
  44.         JScrollPane console = new JScrollPane(output);
  45.         console.setPreferredSize(new Dimension(550, 220));
  46.         label = new JLabel ();
  47.         bottom.add(label);
  48.         bottom.add(input);
  49.         add(console);
  50.         add(bottom);
  51.        
  52.         input.requestFocus();
  53.         input.selectAll();
  54.        
  55.         Dimension d=input.getPreferredSize();
  56.         input.setMinimumSize(d);
  57.         input.setMaximumSize(new Dimension (2147483647, d.height));
  58.        
  59.         box.add ( console );
  60.         box.add ( bottom );
  61.     }
  62.  
  63.     public void writeln(String line) {
  64.         output.append(line + "\n");
  65.         output.setCaretPosition(output.getText().length());
  66.     }
  67.  
  68.     public void echo(Command c) {
  69.         writeln("\n>" + c.toString());
  70.     }
  71.  
  72.     public void writeLine(String line) {
  73.         // TODO
  74.         writeln(line);
  75.     }
  76.  
  77.     private void init() {
  78.         commandList = new HashMap();
  79.         commandBuffer = new Vector();
  80.  
  81.         output = new JTextArea(8, 40);
  82.         output.setLineWrap(true);
  83.         output.setWrapStyleWord(true);
  84.         output.setEditable( false );
  85.  
  86.         input = new JTextField(40);
  87.         input.addKeyListener(this);
  88.  
  89.     }
  90.  
  91.     private void addCommands(HashMap map, CmdHandler handlers[]) {
  92.         for (int i = 0; i < handlers.length; i++) {
  93.             CmdHandler c = handlers[i];
  94.             map.put(c.cmdString().toLowerCase(), c);
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * sets current command set to CmdHandler's in newSet
  100.      * @param newSet
  101.      */
  102.     public void useCommandSet(CmdHandler[][] newSet) {
  103.         commandList = new HashMap();
  104.         for (int i = 0; i < newSet.length; i++) {
  105.             addCommands(commandList, newSet[i]);
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * will call handlers of given commands in commandBuffer
  111.      *
  112.      */
  113.     public void flushCommands() {
  114.        
  115.         while (commandBuffer.size() > 0 ) {
  116.             Command c = (Command) commandBuffer.remove(0);
  117.             String commandName = c.getCommand();
  118.  
  119.             CmdHandler hand = (CmdHandler) commandList.get(commandName);
  120.             if (hand == null) {
  121.                 writeln("unknown command (" + c.toString() + ")");
  122.             } else {
  123.                 if ( hand.echoOn ) echo(c);
  124.                 hand.action(c, this);
  125.             }
  126.            
  127.         }
  128.     }
  129.    
  130.     public static void main ( String argv [] ) {
  131.         TextConsole s =  new TextConsole ();
  132.         JFrame f = new JFrame ();
  133.         f.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
  134.         f.setSize ( 640, 480 );
  135.         f.setLocation ( 0,0 );
  136.         f.getContentPane().add( s );
  137.         f.setVisible(true);
  138.        
  139.     }
  140.  
  141.     public void keyPressed(KeyEvent e) {
  142.         if (e.getKeyChar() == KeyEvent.VK_ENTER) {
  143.             String line = input.getText();
  144.  
  145.             StringTokenizer st = new StringTokenizer(line, "|");
  146.             while (st.hasMoreTokens()) {
  147.                 String subLine = st.nextToken();
  148.  
  149.                 Command command = new Command(subLine);
  150.                 commandBuffer.add(command);
  151.             }
  152.  
  153.             input.setText("");
  154.             flushCommands ();
  155.         }
  156.     }
  157.  
  158.     public void keyReleased(KeyEvent e) {}
  159.  
  160.     public void keyTyped(KeyEvent e) {}
  161. }
Advertisement
Add Comment
Please, Sign In to add comment