1 package net.sf.jftp.tools; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.Font ; 6 import java.awt.event.KeyAdapter ; 7 import java.awt.event.KeyEvent ; 8 import java.io.BufferedOutputStream ; 9 import java.io.BufferedReader ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.InputStreamReader ; 13 import java.io.OutputStream ; 14 import java.io.StreamTokenizer ; 15 import java.util.Vector ; 16 17 import javax.swing.JFrame ; 18 import javax.swing.JScrollBar ; 19 import javax.swing.JScrollPane ; 20 import javax.swing.JTextArea ; 21 import javax.swing.text.DefaultCaret ; 22 23 import net.sf.jftp.gui.framework.HFrame; 24 import net.sf.jftp.system.logging.Log; 25 26 public class Shell extends HFrame implements Runnable 27 { 28 BufferedOutputStream out; 29 BufferedReader in; 31 BufferedOutputStream err; 32 JTextArea text = new JTextArea (25, 101); 33 34 long off; 36 Thread runner; 37 JScrollPane textP; 38 String input = ""; 39 Vector commands = new Vector (); 40 int currCmd = 0; 41 42 public Shell(InputStream in, OutputStream out) 43 { 44 try 45 { 46 this.in = new BufferedReader (new InputStreamReader (in)); 47 this.out = new BufferedOutputStream (out); 48 init(); 52 } 53 catch(Exception e) 54 { 55 e.printStackTrace(); 56 Log.debug("ERROR: " + e.getMessage()); 57 } 58 } 59 60 61 public Shell(BufferedReader in, OutputStream out) 62 { 63 try 64 { 65 this.in = in; 66 this.out = new BufferedOutputStream (out); 67 init(); 71 } 72 catch(Exception e) 73 { 74 e.printStackTrace(); 75 Log.debug("ERROR: " + e.getMessage()); 76 } 77 } 78 79 80 public void init() throws Exception 81 { 82 setTitle("Shell"); 83 84 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 85 86 HFrame.fixLocation(this); 88 89 textP = new JScrollPane (text); 90 text.setFont(new Font ("Monospaced", Font.TRUETYPE_FONT, 10)); 91 92 getContentPane().setLayout(new BorderLayout (5, 5)); 93 getContentPane().add("Center", textP); 94 95 text.setEditable(false); 97 setBackground(text.getBackground()); 98 99 DefaultCaret c = new DefaultCaret (); 100 c.setBlinkRate(1000); 101 102 text.setCaret(c); 103 text.setCaretColor(Color.BLACK); 104 c.setVisible(true); 105 106 text.setLineWrap(true); 107 108 text.addKeyListener(new KeyAdapter () 109 { 110 public void keyPressed(KeyEvent e) 111 { 112 if((e.getKeyCode() == KeyEvent.VK_BACK_SPACE) && 113 (input.length() > 0)) 114 { 115 input = input.substring(0, input.length() - 1); 116 117 String t = text.getText(); 118 t = t.substring(0, t.length() - 1); 119 text.setText(t); 120 } 121 else if(e.getKeyCode() == KeyEvent.VK_UP) 122 { 123 String t = text.getText(); 124 t = t.substring(0, t.length() - input.length()); 125 126 if((currCmd <= commands.size()) && (currCmd > 0)) 127 { 128 currCmd--; 129 130 String cmd = (String ) commands.get(currCmd); 131 input = cmd.substring(0, cmd.length() - 1); 132 text.setText(t + input); 133 } 134 } 135 else if(e.getKeyCode() == KeyEvent.VK_DOWN) 136 { 137 String t = text.getText(); 138 t = t.substring(0, t.length() - input.length()); 139 140 if(((currCmd + 1) < commands.size()) && (currCmd >= 0)) 141 { 142 currCmd++; 143 144 String cmd = (String ) commands.get(currCmd); 145 input = cmd.substring(0, cmd.length() - 1); 146 text.setText(t + input); 147 } 148 } 149 else if(e.getKeyCode() != KeyEvent.VK_SHIFT) 150 { 151 if(!e.isActionKey()) 153 { 154 input += e.getKeyChar(); 155 text.append("" + e.getKeyChar()); 156 } 157 } 158 159 if(e.getKeyCode() == KeyEvent.VK_ENTER) 160 { 161 send(); 162 } 163 } 164 }); 165 166 pack(); 167 HFrame.fixLocation(this); 168 setVisible(true); 169 170 runner = new Thread (this); 171 runner.start(); 172 173 toFront(); 174 text.requestFocus(); 175 } 176 177 public void run() 178 { 179 try 180 { 181 char[] b = new char[4096]; 182 int i; 183 184 while((i = in.read(b,0,b.length)) != StreamTokenizer.TT_EOF) 185 { 186 text.append(new String (b, 0, i)); 187 188 195 while(text.getRows() > 500) 196 { 197 String t = text.getText(); 198 t = t.substring(250); 199 200 text.setText(t); 201 } 203 204 try 205 { 206 Thread.sleep(100); 207 } 208 catch(Exception ex) 209 { 210 ex.printStackTrace(); 211 } 212 213 JScrollBar bar = textP.getVerticalScrollBar(); 214 bar.setValue(bar.getMaximum()); 215 216 text.setCaretPosition(text.getText().length()); 218 } 220 221 text.setEnabled(false); 222 } 223 catch(Exception ex) 224 { 225 ex.printStackTrace(); 226 Log.debug("ERROR: " + ex.getMessage()); 227 this.dispose(); 228 } 229 } 230 231 private void send() 232 { 233 try 234 { 235 String msg = input; 236 input = ""; 237 238 out.write(msg.getBytes()); 239 out.flush(); 240 241 commands.add(msg); 242 currCmd = commands.size(); 243 244 } 246 catch(IOException ex) 247 { 248 ex.printStackTrace(); 249 Log.debug("ERROR: " + ex.getMessage()); 250 this.dispose(); 251 } 252 } 253 254 public static void main(String [] argv) 255 { 256 try { 257 Process p = Runtime.getRuntime().exec(argv.length > 0 ? argv[0] : "/bin/bash"); 258 new Shell(p.getInputStream(), p.getOutputStream()); 259 } 260 catch(Exception ex) { 261 ex.printStackTrace(); 262 } 263 } 264 } 265 | Popular Tags |