VisClient/org/hfbk/util/ShellPanel.java

Go to the documentation of this file.
00001 package org.hfbk.util;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Panel;
00005 import java.awt.TextArea;
00006 import java.awt.TextField;
00007 import java.awt.event.ActionEvent;
00008 import java.awt.event.ActionListener;
00009 import java.awt.event.KeyAdapter;
00010 import java.awt.event.KeyEvent;
00011 
00020 public abstract class ShellPanel extends Panel {
00021         
00022         TextArea output;
00023         public ShellPanel() {
00024                 setLayout(new BorderLayout());
00025                 
00026                 output=new TextArea(20,50);
00027                 output.setEditable(false);
00028                 add(output, BorderLayout.CENTER);
00029                 
00030                 final TextField input=new TextField();
00031                 input.addActionListener(new ActionListener(){
00032                         public void actionPerformed(ActionEvent e) {
00033                                 String command=input.getText();
00034                                 input.setText("");
00035                                 output.append("\n> "+command);
00036                                 String result=action(command);
00037                                 output.append("\n"+result);
00038                         }
00039                 });
00040                 input.addKeyListener(new KeyAdapter(){
00041                         public void keyReleased(KeyEvent e) {
00042                                 String history="\n"+output.getText();
00043                                 String current=input.getText();
00044                                 int i=history.lastIndexOf("> "+current+"\n");
00045                                 if (i==-1) i=history.length()+1;
00046                         
00047                                 int j=-1;
00048                                 switch(e.getKeyCode()){
00049                                 case KeyEvent.VK_UP: 
00050                                         j=history.lastIndexOf("\n> ", i-3);                                             
00051                                 break;
00052                                 case KeyEvent.VK_DOWN:
00053                                         j=history.indexOf("\n> ", i);
00054                                 break;
00055                                 }
00056                                 if (j>-1){
00057                                         j+=3; //skip "\n> "
00058                                         int k=history.indexOf('\n',j);
00059                                         current=history.substring(j, k);
00060                                         input.setText(current);
00061                                         input.setCaretPosition(Integer.MAX_VALUE);
00062                                 }                       
00063                         }
00064                 });             
00065                 add(input, BorderLayout.SOUTH);
00066         }
00067         
00068         abstract public String action(String command);
00069         
00070         public void println(Object o){
00071                 String result="";
00072                 if (o instanceof Object[])
00073                         for (Object p :  ((Object[])o))
00074                                 result+=p+"\n";
00075                 else if (o!=null)
00076                         result=o.toString();
00077                 
00078                 output.append("\n"+result);
00079         }
00080         
00081 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1