KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > tools > Shell


1 package net.sf.jftp.tools;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Font JavaDoc;
6 import java.awt.event.KeyAdapter JavaDoc;
7 import java.awt.event.KeyEvent JavaDoc;
8 import java.io.BufferedOutputStream JavaDoc;
9 import java.io.BufferedReader JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.InputStreamReader JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.io.StreamTokenizer JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import javax.swing.JFrame JavaDoc;
18 import javax.swing.JScrollBar JavaDoc;
19 import javax.swing.JScrollPane JavaDoc;
20 import javax.swing.JTextArea JavaDoc;
21 import javax.swing.text.DefaultCaret JavaDoc;
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 JavaDoc
27 {
28     BufferedOutputStream JavaDoc out;
29     //BufferedInputStream in;
30
BufferedReader JavaDoc in;
31     BufferedOutputStream JavaDoc err;
32     JTextArea JavaDoc text = new JTextArea JavaDoc(25, 101);
33
34     //JTextField input = new JTextField();
35
long off;
36     Thread JavaDoc runner;
37     JScrollPane JavaDoc textP;
38     String JavaDoc input = "";
39     Vector JavaDoc commands = new Vector JavaDoc();
40     int currCmd = 0;
41
42     public Shell(InputStream JavaDoc in, OutputStream JavaDoc out)
43     {
44         try
45         {
46             this.in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
47             this.out = new BufferedOutputStream JavaDoc(out);
48             //in = new BufferedInputStream(System.in);
49
//out = new BufferedOutputStream(System.out);
50
//err = new BufferedOutputStream(System.err);
51
init();
52         }
53         catch(Exception JavaDoc e)
54         {
55             e.printStackTrace();
56             Log.debug("ERROR: " + e.getMessage());
57         }
58     }
59
60     
61     public Shell(BufferedReader JavaDoc in, OutputStream JavaDoc out)
62     {
63         try
64         {
65             this.in = in;
66             this.out = new BufferedOutputStream JavaDoc(out);
67             //in = new BufferedInputStream(System.in);
68
//out = new BufferedOutputStream(System.out);
69
//err = new BufferedOutputStream(System.err);
70
init();
71         }
72         catch(Exception JavaDoc e)
73         {
74             e.printStackTrace();
75             Log.debug("ERROR: " + e.getMessage());
76         }
77     }
78
79     
80     public void init() throws Exception JavaDoc
81     {
82         setTitle("Shell");
83
84         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
85
86         //setLocation(150, 150);
87
HFrame.fixLocation(this);
88
89         textP = new JScrollPane JavaDoc(text);
90         text.setFont(new Font JavaDoc("Monospaced", Font.TRUETYPE_FONT, 10));
91
92         getContentPane().setLayout(new BorderLayout JavaDoc(5, 5));
93         getContentPane().add("Center", textP);
94
95         //getContentPane().add("South", input);
96
text.setEditable(false);
97         setBackground(text.getBackground());
98         
99         DefaultCaret JavaDoc c = new DefaultCaret JavaDoc();
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 JavaDoc()
109             {
110                 public void keyPressed(KeyEvent JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc cmd = (String JavaDoc) 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 JavaDoc 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 JavaDoc cmd = (String JavaDoc) 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                         //Char c = new Char(e.getKeyChar());
152
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 JavaDoc(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 JavaDoc(b, 0, i));
187
188                 //Log.out("recv: "+i+" -> "+new String(b));
189
//while(err.available() > 0)
190
//{
191
// err.read(b);
192
// text.append(new String(b, 0, i));
193
//}
194

195                 while(text.getRows() > 500)
196                 {
197                     String JavaDoc t = text.getText();
198                     t = t.substring(250);
199
200                     text.setText(t);
201                    // text.setCaretPosition(-250);
202
}
203
204                 try
205                 {
206                     Thread.sleep(100);
207                 }
208                 catch(Exception JavaDoc ex)
209                 {
210                     ex.printStackTrace();
211                 }
212
213                 JScrollBar JavaDoc bar = textP.getVerticalScrollBar();
214                 bar.setValue(bar.getMaximum());
215
216                // text.setCaretPosition(i);
217
text.setCaretPosition(text.getText().length());
218                 //text.getCaret().setDot(1);
219
}
220             
221             text.setEnabled(false);
222         }
223         catch(Exception JavaDoc 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 JavaDoc msg = input;
236             input = "";
237
238             out.write(msg.getBytes());
239             out.flush();
240
241             commands.add(msg);
242             currCmd = commands.size();
243
244             //Log.out("send: "+msg);
245
}
246         catch(IOException JavaDoc ex)
247         {
248             ex.printStackTrace();
249             Log.debug("ERROR: " + ex.getMessage());
250             this.dispose();
251         }
252     }
253
254     public static void main(String JavaDoc[] argv)
255     {
256         try {
257             Process JavaDoc p = Runtime.getRuntime().exec(argv.length > 0 ? argv[0] : "/bin/bash");
258             new Shell(p.getInputStream(), p.getOutputStream());
259         }
260         catch(Exception JavaDoc ex) {
261             ex.printStackTrace();
262         }
263     }
264 }
265
Popular Tags