2 * Cluster - Cluster console plugin.
4 * @verison 0.1 - 28/12/00.
9 * Copyright (C) 2001 Ian Norton.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public Licence as published by
13 * the Free Software Foundation; either version 2 of the Licence, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public Licence for more details.
21 * You should have received a copy of the GNU General Public Licence
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
24 * Contacting the author :
26 * i.norton@lancaster.ac.uk
27 * http://www.lancs.ac.uk/~norton/
31 import javax.swing.text.*;
32 import javax.swing.event.*;
35 import java.awt.event.*;
36 import java.util.Hashtable ;
37 import java.util.Enumeration ;
39 // public class Cluster extends Plugin implements Runnable
40 class Cluster extends Plugin implements Runnable
42 // Name and tip used when creating the tabbed pane.
43 public static final String NAME = "Cluster" ;
44 public static final String TIP = "DX Cluster Console" ;
46 // Number of commands to buffer.
47 public static final int CMDBUFFERLINES = 30 ;
49 // Number of lines of scrollback to buffer.
50 public static final int SCROLLBUFFERLINES = 100 ;
52 public static final boolean DEBUG = false ;
54 // Input and output streams for the plugin.
55 // private PipedInputStream pin ;
56 private BufferedReader bir ;
57 private PipedOutputStream pos ;
60 private JTextField tf ;
62 private JTextPane jtp ;
64 private SimpleAttributeSet attr ;
65 private LimitedStyledDocument doc ;
67 // Input line scrollback buffer.
68 private CommandBuffer cbuf ;
70 private static final String encoding = "latin1"; // "ISO8859_1";
82 * @param PipedInputStream i - Stream to read data from
83 * @param PipedOutputStream o - Stream to write data to
85 public void init(PipedInputStream i, PipedOutputStream o)
87 // Initialise the plugin IO.
88 bir = new BufferedReader(new InputStreamReader(i)) ;
91 // Initialise the ScrollingTextArea.
92 // ScrollingTextArea sta = new ScrollingTextArea(pin, SCROLLBUFFERLINES, doc) ;
93 // sta.setFont(new Font("Courier", Font.PLAIN, 10)) ;
94 // sta.setFont(new Font("Monospaced", Font.PLAIN, 10)) ;
95 // System.out.println(sta.getFont()) ;
97 doc = new LimitedStyledDocument(SCROLLBUFFERLINES) ;
98 jtp = new JTextPane(doc) ;
99 jtp.setEditable(false) ;
100 attr = new SimpleAttributeSet() ;
101 StyleConstants.setFontFamily(attr, "Monospaced") ;
102 StyleConstants.setFontSize(attr, 10) ;
103 jtp.setBackground(Color.black) ;
105 doc.addDocumentListener(new DocumentListener() {
106 public void insertUpdate(DocumentEvent e) {
107 jtp.setCaretPosition(doc.getLength()) ;
108 // tf.requestFocus() ;
110 public void removeUpdate(DocumentEvent e) {
112 public void changedUpdate(DocumentEvent e) {
116 // Initialise the TextField for user input.
117 tf = new JTextField() ;
118 tf.setFont(new Font("Courier", Font.PLAIN, 10)) ;
119 Insets inset = tf.getMargin() ;
120 inset.top = inset.top + 1 ;
121 inset.bottom = inset.bottom + 1 ;
122 tf.setMargin(inset) ;
123 tf.setForeground(Color.white) ;
124 tf.setBackground(Color.black) ;
126 // Set the layout manager.
127 this.setLayout(new BorderLayout()) ;
129 // Scrollbars for scrolling text area.
130 // JScrollPane scrollpane = new JScrollPane(sta);
131 JScrollPane scrollpane = new JScrollPane(jtp);
133 // Add the bits to the panel.
134 this.add(scrollpane, BorderLayout.CENTER);
135 this.add(tf, BorderLayout.SOUTH);
137 // Initialise the command buffer.
138 cbuf = new CommandBuffer(CMDBUFFERLINES) ;
140 // Action listener stuff.
141 tf.addKeyListener(new KeyAdapter()
143 public void keyTyped(KeyEvent e)
146 if((e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == KeyEvent.VK_ENTER))
148 // System.out.println("Enter Event") ;
149 send(tf.getText() + '\n') ;
150 cbuf.addCommand(tf.getText()) ;
154 public void keyPressed(KeyEvent e)
157 if((e.getID() == KeyEvent.KEY_PRESSED) && (e.getKeyCode() == KeyEvent.VK_UP))
159 // System.out.println("UP Event") ;
160 tf.setText(cbuf.getPreviousCommand()) ;
161 tf.setCaretPosition(tf.getText().length()) ;
164 if((e.getID() == KeyEvent.KEY_PRESSED) && (e.getKeyCode() == KeyEvent.VK_DOWN))
166 // System.out.println("DOWN Event") ;
167 tf.setText(cbuf.getNextCommand()) ;
168 tf.setCaretPosition(tf.getText().length()) ;
171 if((e.getID() == KeyEvent.KEY_PRESSED) && (e.getKeyCode() == KeyEvent.VK_ESCAPE))
173 // System.out.println("ESCAPE Event") ;
178 // Add component listener to focus text field.
179 this.addComponentListener(new ComponentAdapter() {
180 public void componentShown(ComponentEvent e) {
181 tf.setVisible(true) ;
186 // Init the scrolling thread.
187 t = new Thread(this, "Scrolling thread") ;
192 * getTabName - Get the name that this component should show on it's tab
193 * @returns String s - Tab name
196 public String getTabName()
201 * getTabTip - Get the tip that this component should show on it's tab
202 * @returns String s - Tab tip
204 public String getTabTip()
210 * getMenu - get the menu to add to the main menu bar.
213 public JMenu getMenu()
218 * send - Helper function to send data out to the PipedOutputMUX
219 * @param String s - data to send.
221 private void send(String s)
223 // System.out.println("Cluster: send got : " + s) ;
227 // Write the data to the stream.
228 for(int i=0;i<s.length();i++)
230 pos.write(s.charAt(i)) ;
233 catch(IOException ex)
235 System.out.println("Cluster: IOException on destination stream.") ;
236 System.out.println(ex) ;
241 * Loop continually checking to see if anything has been written to the
242 * file that is being monitored.
246 String output = new String() ;
247 // Loop continually reading from the input stream
258 // output = new String(b, 0, n, encoding) ;
260 // // System.out.println("Read : " + output) ;
263 output = bir.readLine() ;
264 if(output != null) display(output) ;
266 if(DEBUG) System.out.println("After reading a line.") ;
268 catch(IOException ex)
270 System.out.println("ScrollingTextArea: IOException trying to read.") ;
272 } // End of infinate loop.
275 private void display(String s)
277 // System.out.println(s) ;
279 // s = s.replace('\r', ' ') ;
280 s = s.replace('
\a', ' ') ;
282 attr = getAttributes(s) ;
283 doc.append(s + "\n", attr) ;
286 private SimpleAttributeSet getAttributes(String s)
288 SimpleAttributeSet sas = attr ;
291 # 0 - $foreground, $background
292 # 1 - RED, $background
293 # 2 - BROWN, $background
294 # 3 - GREEN, $background
295 # 4 - CYAN, $background
296 # 5 - BLUE, $background
297 # 6 - MAGENTA, $background
300 [ '^DX de [\-A-Z0-9]+:\s+([57][01]\d\d\d\.|\d\d\d\d\d\d+.)', COLOR_PAIR(1) ],
302 [ '^G0VGS de GB7MBC', COLOR_PAIR(6) ],
304 [ '^G0VGS de', A_BOLD|COLOR_PAIR(2) ],
306 [ '^DX', COLOR_PAIR(5) ],
308 [ '^To', COLOR_PAIR(3) ],
310 [ '^WWV', COLOR_PAIR(4) ],
312 [ '^[-A-Z0-9]+ de [-A-Z0-9]+ \d\d-\w\w\w-\d\d\d\d \d\d\d\dZ', COLOR_PAIR(0) ],
313 DUNNO! - PROBABLY A TALK
314 [ '^[-A-Z0-9]+ de [-A-Z0-9]+ ', COLOR_PAIR(6) ],
316 [ '^WX', COLOR_PAIR(3) ],
318 [ '^New mail', A_BOLD|COLOR_PAIR(4) ],
320 [ '^User', COLOR_PAIR(2) ],
322 [ '^Node', COLOR_PAIR(2) ],
325 Hashtable h = new Hashtable() ;
326 h.put("DX de", Color.red) ;
327 h.put("M0AZM de GB7MBC", Color.magenta) ;
328 h.put("G0VGS de GB7MBC", Color.magenta) ;
329 h.put("G0VGS2 de GB7MBC", Color.magenta) ;
330 // h.put("DX", Color.blue) ;
331 h.put("To", Color.green) ;
332 h.put("WWV", Color.cyan) ;
333 h.put("WCY", Color.cyan) ;
334 // h.put("", Color.) ;
335 // h.put("", Color.) ;
336 h.put("WX", Color.green) ;
337 h.put("New mail", Color.cyan) ;
338 //h.put("User", Color.brown) ;
339 //h.put("Node", Color.brown) ;
340 h.put("User", Color.yellow) ;
341 h.put("Node", Color.orange) ;
343 Enumeration e = h.keys() ;
345 while(e.hasMoreElements())
347 String prefix = (String)e.nextElement() ;
348 if(s.startsWith(prefix))
350 StyleConstants.setForeground(sas, (Color)h.get(prefix)) ;
355 StyleConstants.setForeground(sas, Color.white) ;