001: package net.sf.jftp.tools;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Font;
006: import java.awt.event.KeyAdapter;
007: import java.awt.event.KeyEvent;
008: import java.io.BufferedOutputStream;
009: import java.io.BufferedReader;
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.io.InputStreamReader;
013: import java.io.OutputStream;
014: import java.io.StreamTokenizer;
015: import java.util.Vector;
016:
017: import javax.swing.JFrame;
018: import javax.swing.JScrollBar;
019: import javax.swing.JScrollPane;
020: import javax.swing.JTextArea;
021: import javax.swing.text.DefaultCaret;
022:
023: import net.sf.jftp.gui.framework.HFrame;
024: import net.sf.jftp.system.logging.Log;
025:
026: public class Shell extends HFrame implements Runnable {
027: BufferedOutputStream out;
028: //BufferedInputStream in;
029: BufferedReader in;
030: BufferedOutputStream err;
031: JTextArea text = new JTextArea(25, 101);
032:
033: //JTextField input = new JTextField();
034: long off;
035: Thread runner;
036: JScrollPane textP;
037: String input = "";
038: Vector commands = new Vector();
039: int currCmd = 0;
040:
041: public Shell(InputStream in, OutputStream out) {
042: try {
043: this .in = new BufferedReader(new InputStreamReader(in));
044: this .out = new BufferedOutputStream(out);
045: //in = new BufferedInputStream(System.in);
046: //out = new BufferedOutputStream(System.out);
047: //err = new BufferedOutputStream(System.err);
048: init();
049: } catch (Exception e) {
050: e.printStackTrace();
051: Log.debug("ERROR: " + e.getMessage());
052: }
053: }
054:
055: public Shell(BufferedReader in, OutputStream out) {
056: try {
057: this .in = in;
058: this .out = new BufferedOutputStream(out);
059: //in = new BufferedInputStream(System.in);
060: //out = new BufferedOutputStream(System.out);
061: //err = new BufferedOutputStream(System.err);
062: init();
063: } catch (Exception e) {
064: e.printStackTrace();
065: Log.debug("ERROR: " + e.getMessage());
066: }
067: }
068:
069: public void init() throws Exception {
070: setTitle("Shell");
071:
072: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
073:
074: //setLocation(150, 150);
075: HFrame.fixLocation(this );
076:
077: textP = new JScrollPane(text);
078: text.setFont(new Font("Monospaced", Font.TRUETYPE_FONT, 10));
079:
080: getContentPane().setLayout(new BorderLayout(5, 5));
081: getContentPane().add("Center", textP);
082:
083: //getContentPane().add("South", input);
084: text.setEditable(false);
085: setBackground(text.getBackground());
086:
087: DefaultCaret c = new DefaultCaret();
088: c.setBlinkRate(1000);
089:
090: text.setCaret(c);
091: text.setCaretColor(Color.BLACK);
092: c.setVisible(true);
093:
094: text.setLineWrap(true);
095:
096: text.addKeyListener(new KeyAdapter() {
097: public void keyPressed(KeyEvent e) {
098: if ((e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
099: && (input.length() > 0)) {
100: input = input.substring(0, input.length() - 1);
101:
102: String t = text.getText();
103: t = t.substring(0, t.length() - 1);
104: text.setText(t);
105: } else if (e.getKeyCode() == KeyEvent.VK_UP) {
106: String t = text.getText();
107: t = t.substring(0, t.length() - input.length());
108:
109: if ((currCmd <= commands.size()) && (currCmd > 0)) {
110: currCmd--;
111:
112: String cmd = (String) commands.get(currCmd);
113: input = cmd.substring(0, cmd.length() - 1);
114: text.setText(t + input);
115: }
116: } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
117: String t = text.getText();
118: t = t.substring(0, t.length() - input.length());
119:
120: if (((currCmd + 1) < commands.size())
121: && (currCmd >= 0)) {
122: currCmd++;
123:
124: String cmd = (String) commands.get(currCmd);
125: input = cmd.substring(0, cmd.length() - 1);
126: text.setText(t + input);
127: }
128: } else if (e.getKeyCode() != KeyEvent.VK_SHIFT) {
129: //Char c = new Char(e.getKeyChar());
130: if (!e.isActionKey()) {
131: input += e.getKeyChar();
132: text.append("" + e.getKeyChar());
133: }
134: }
135:
136: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
137: send();
138: }
139: }
140: });
141:
142: pack();
143: HFrame.fixLocation(this );
144: setVisible(true);
145:
146: runner = new Thread(this );
147: runner.start();
148:
149: toFront();
150: text.requestFocus();
151: }
152:
153: public void run() {
154: try {
155: char[] b = new char[4096];
156: int i;
157:
158: while ((i = in.read(b, 0, b.length)) != StreamTokenizer.TT_EOF) {
159: text.append(new String(b, 0, i));
160:
161: //Log.out("recv: "+i+" -> "+new String(b));
162: //while(err.available() > 0)
163: //{
164: // err.read(b);
165: // text.append(new String(b, 0, i));
166: //}
167:
168: while (text.getRows() > 500) {
169: String t = text.getText();
170: t = t.substring(250);
171:
172: text.setText(t);
173: // text.setCaretPosition(-250);
174: }
175:
176: try {
177: Thread.sleep(100);
178: } catch (Exception ex) {
179: ex.printStackTrace();
180: }
181:
182: JScrollBar bar = textP.getVerticalScrollBar();
183: bar.setValue(bar.getMaximum());
184:
185: // text.setCaretPosition(i);
186: text.setCaretPosition(text.getText().length());
187: //text.getCaret().setDot(1);
188: }
189:
190: text.setEnabled(false);
191: } catch (Exception ex) {
192: ex.printStackTrace();
193: Log.debug("ERROR: " + ex.getMessage());
194: this .dispose();
195: }
196: }
197:
198: private void send() {
199: try {
200: String msg = input;
201: input = "";
202:
203: out.write(msg.getBytes());
204: out.flush();
205:
206: commands.add(msg);
207: currCmd = commands.size();
208:
209: //Log.out("send: "+msg);
210: } catch (IOException ex) {
211: ex.printStackTrace();
212: Log.debug("ERROR: " + ex.getMessage());
213: this .dispose();
214: }
215: }
216:
217: public static void main(String[] argv) {
218: try {
219: Process p = Runtime.getRuntime().exec(
220: argv.length > 0 ? argv[0] : "/bin/bash");
221: new Shell(p.getInputStream(), p.getOutputStream());
222: } catch (Exception ex) {
223: ex.printStackTrace();
224: }
225: }
226: }
|