KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TelnetApp


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is Terminal Emulator.
16  * The Initial Developer of the Original Software is Sun Microsystems, Inc..
17  * Portions created by Sun Microsystems, Inc. are Copyright (C) 2001.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Ivan Soleimanipour.
21  */

22
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26
27 import de.mud.telnet.*;
28
29 import org.netbeans.lib.terminalemulator.*;
30
31 public class TelnetApp extends JFrame {
32     TermInputListener input_listener;
33     Term term;
34     TelnetWrapper tio;
35
36     /*
37      * Override de.mud.telnet.TelnetWrapper so we can specialize
38      * the terminal type.
39      */

40     static class MyTelnetWrapper extends TelnetWrapper {
41
42     private String JavaDoc terminalType;
43
44     MyTelnetWrapper(String JavaDoc terminalType) {
45         this.terminalType = terminalType;
46     }
47
48     public String JavaDoc getTerminalType() {
49         return terminalType;
50     }
51     }
52
53     TelnetApp(Font font) {
54     super("TelnetApp");
55
56     input_listener = new TermInputListener() {
57         public void sendChar(char c) {
58         // System.out.println(c);
59
final char tmp[]= new char[1];
60         tmp[0] = c;
61         String JavaDoc s = new String JavaDoc(tmp);
62         try {
63             tio.write(s.getBytes());
64         } catch (Exception JavaDoc x) {
65             x.printStackTrace();
66         }
67         }
68         public void sendChars(char[] c, int offset, int count) {
69         // System.out.println(c);
70
String JavaDoc s = new String JavaDoc(c, offset, count);
71         try {
72             tio.write(s.getBytes());
73         } catch (Exception JavaDoc x) {
74             x.printStackTrace();
75         }
76         }
77     };
78
79     term = new Term();
80     term.addInputListener(input_listener);
81
82     term.setEmulation("ansi");
83     term.setBackground(Color.white);
84     term.setClickToType(false);
85     term.setHighlightColor(Color.orange);
86     term.setWordDelineator(new WordDelineator() {
87         public int charClass(char c) {
88         if (Character.isJavaIdentifierPart(c))
89             return 1;
90         else
91             return 0;
92         }
93     } );
94     }
95
96     public String JavaDoc terminalType() {
97     return term.getEmulation();
98     }
99
100     private void setup_gui() {
101     addWindowListener(new WindowAdapter() {
102         public void windowClosing(WindowEvent e) {
103         System.exit(0);
104         }
105     });
106     setContentPane(term);
107     pack();
108     }
109
110     private void setup_telnet(String JavaDoc host, String JavaDoc terminalType) {
111     tio = new MyTelnetWrapper(terminalType);
112     try {
113         System.out.println("connecting to '" + host + "' ...");
114         tio.connect(host, 23);
115     } catch (Exception JavaDoc x) {
116         x.printStackTrace();
117     }
118     }
119
120     private void run() {
121     byte[] buf = new byte[512];
122     int count;
123     while (true) {
124         try {
125         count = tio.read(buf);
126         } catch (Exception JavaDoc x) {
127         x.printStackTrace();
128         break;
129         }
130         if (count == -1)
131         break;
132         String JavaDoc s = new String JavaDoc(buf, 0, count);
133         char[] tmp = new char[s.length()];
134         s.getChars(0, s.length(), tmp, 0);
135         term.putChars(tmp, 0, s.length());
136     }
137     System.exit(0);
138     }
139
140     private static void usage(String JavaDoc msg) {
141     System.out.println(msg);
142     System.out.println("usage: TelnetApp [ <hostname> ]");
143     System.exit(1);
144     }
145
146     public static void main(String JavaDoc[] args) {
147
148     String JavaDoc host = "localhost";
149
150     // process cmdline args
151
for (int cx = 0; cx < args.length; cx++) {
152         if (args[cx].startsWith("-")) {
153         usage("Unrecognized option: " + args[cx]);
154         } else {
155         host = args[cx];
156         }
157     }
158
159     Font term_font = new Font("Helvetica", Font.PLAIN, 10);
160     TelnetApp app = new TelnetApp(term_font);
161     app.setup_gui();
162     app.setVisible(true);
163     app.setup_telnet(host, app.terminalType());
164     app.run();
165     }
166 }
167
Popular Tags