1 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 40 static class MyTelnetWrapper extends TelnetWrapper { 41 42 private String terminalType; 43 44 MyTelnetWrapper(String terminalType) { 45 this.terminalType = terminalType; 46 } 47 48 public String 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 final char tmp[]= new char[1]; 60 tmp[0] = c; 61 String s = new String (tmp); 62 try { 63 tio.write(s.getBytes()); 64 } catch (Exception x) { 65 x.printStackTrace(); 66 } 67 } 68 public void sendChars(char[] c, int offset, int count) { 69 String s = new String (c, offset, count); 71 try { 72 tio.write(s.getBytes()); 73 } catch (Exception 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 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 host, String terminalType) { 111 tio = new MyTelnetWrapper(terminalType); 112 try { 113 System.out.println("connecting to '" + host + "' ..."); 114 tio.connect(host, 23); 115 } catch (Exception 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 x) { 127 x.printStackTrace(); 128 break; 129 } 130 if (count == -1) 131 break; 132 String s = new String (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 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 [] args) { 147 148 String host = "localhost"; 149 150 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 |