add the Java Client from M0AZM
[spider.git] / SpiderConsole / src / Console.java
1 /**
2  * Console - modular Amateur Radio console for clusters and converse.
3  * @author Ian Norton
4  * @version 1.00 - 20010418.
5  *
6  * Copyright (C) 2001 Ian Norton.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public Licence as published by
10  * the Free Software Foundation; either version 2 of the Licence, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public Licence for more details.
17  *
18  * You should have received a copy of the GNU General Public Licence
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * Contacting the author :
23  * Ian Norton
24  * i.norton@lancaster.ac.uk
25  * http://www.lancs.ac.uk/~norton/
26  **/
27
28 import javax.swing.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import java.util.*;
32 import java.io.*;
33
34 public class Console
35     {
36     private JFrame frame ;
37     private JPanel buttonpanel ;
38     private JTabbedPane tabbedpane ;
39
40     // Static variables.
41     public static final int WIDTH  = 620 ;
42     public static final int HEIGHT = 350 ;
43     public static final String VERSION = "1.0" ;
44     public static final String INFO = "RadioConsole Version " + VERSION +
45                                       "\nWritten By Ian Norton (M0AZM)\n" +
46                                       "i.norton@lancaster.ac.uk" ;
47
48     // IO Multiplexors
49     private PipedInputMUX inmux ;
50     private PipedOutputMUX outmux ;
51     
52     // Vector to store plugins
53     private Vector plugins ;
54
55     private int plugnumber ;
56
57     // Connection object.
58     private Connection connection ;
59
60     // Host that we connected to (May include port number).
61     private String host ;
62
63     /**
64      * main
65      **/
66     public static void main(String[] args)
67         {
68         // Start the console.
69         Console c = new Console("DX Cluster Console " + VERSION) ;
70         }
71
72     /**
73      * Console init method.
74      * @param String title
75      **/
76     public Console(String s)
77         {
78         // Default host and port to connect to.
79         host = "127.0.0.1:27754" ;
80
81         // Initialise the frame for the whole thing.
82         frame = new JFrame(s) ;
83
84         // Build connection here.
85         PipedInputStream pincon = new PipedInputStream() ;
86         PipedOutputStream poscon = new PipedOutputStream() ;
87         connection = new Connection(pincon, poscon, this) ;
88
89         // Build protocol here.
90         /**
91         PipedInputStream pinprot ;
92         PipedOutputStream posprot ;
93         try
94             {
95             pinprot = new PipedInputStream(poscon) ;
96             posprot = new PipedOutputStream(pincon) ;
97             Protocol protocol = new Protocol(pinprot, posprot, this) ;
98             }
99         catch(IOException ex)
100             {
101             System.out.println("Console: IOException creating protocol.") ;
102             System.exit(1) ;
103             }
104         **/
105         
106         // Build input/output MUX's here.
107         PipedInputStream pinmux ;
108         PipedOutputStream posmux ;
109
110         try
111             {
112             // Initialise pipes.
113             pinmux = new PipedInputStream(poscon) ;
114             posmux = new PipedOutputStream(pincon) ;
115
116             // Initialise the MUX's
117             inmux = new PipedInputMUX(posmux) ;
118             outmux = new PipedOutputMUX(pinmux) ;
119             }
120         catch(IOException ex)
121             {
122             System.out.println("Console: IOException creating MUXes.") ;
123             System.out.println(ex) ;
124             System.exit(1) ;
125             }
126
127         // Initialise the plugin stuff.
128         plugins = new Vector() ;
129         plugnumber = 0 ;
130         
131         // Build tabbed panes from the plugins.
132         buildTabs() ;
133
134         // Build menu bars.
135         buildMenus() ;
136
137         // Build the button bar.
138         // buildToolbar() ;
139
140         // Add action listener to close the window.
141         frame.addWindowListener(new WindowAdapter() {
142             public void windowClosing(WindowEvent e) {
143             System.exit(0) ; }
144             });
145
146         // Set initial size.
147         frame.setSize(WIDTH, HEIGHT) ;
148                                                                                 
149         frame.getContentPane().add(tabbedpane, BorderLayout.CENTER);
150         frame.show();
151
152         // Pop a connection dialog or use saved hostname or something here.
153         connection.connect(host) ;
154         }
155
156     /**
157      * buildTabs - build the tabbed panes with the plugins.
158      **/
159     public void buildTabs()
160         {
161         tabbedpane = new JTabbedPane() ;
162         tabbedpane.setTabPlacement(JTabbedPane.BOTTOM);
163
164         // The first plugin should always be the cluster plugin.
165         // addPlugin("Cluster") ;
166         addPlugin("SpiderCluster") ;
167         
168         // Call insert plugins method here. **AZM**
169
170         }
171
172     /**
173      * buildMenus - build the Menus with the plugins.
174      **/
175     public void buildMenus()
176         {
177         // Create a menu bar and add it to the frame.
178         JMenuBar mbar = new JMenuBar() ;
179         frame.setJMenuBar(mbar) ;
180  
181         // Create the file menu stuff.
182         JMenu filemenu = new JMenu("File") ;
183
184         JMenuItem item ;
185         filemenu.add(item = new JMenuItem("Connect")) ;
186         item.setMnemonic(KeyEvent.VK_C) ;
187         item.setAccelerator(KeyStroke.getKeyStroke(
188                 KeyEvent.VK_C, ActionEvent.ALT_MASK));
189         item.addActionListener(new ActionListener() { 
190         public void actionPerformed(ActionEvent e) { 
191                 connection.connect(host);
192         }});
193
194         filemenu.add(item = new JMenuItem("Connect To")) ;
195         // item.setMnemonic(KeyEvent.VK_C) ;
196         // item.setAccelerator(KeyStroke.getKeyStroke(
197         //           KeyEvent.VK_C, ActionEvent.ALT_MASK));
198         item.addActionListener(new ActionListener() { 
199         public void actionPerformed(ActionEvent e) { 
200             // Connection dialog.
201             String ho = JOptionPane.showInputDialog("Enter the host to connect to") ;
202             if(ho == null || ho.indexOf(" ") > -1)
203                 return ;
204
205             if(ho != null && ho.length() > 0)
206                 {
207                 // connection.disconnect() ;
208                 connection.connect(ho);
209                 }
210         }});
211
212         filemenu.add(item = new JMenuItem("Disconnect")) ;
213         item.addActionListener(new ActionListener() {
214         public void actionPerformed(ActionEvent e) 
215             { connection.disconnect() ; }});
216
217         filemenu.add(item = new JMenuItem("About")) ;
218         item.addActionListener(new ActionListener() {
219         public void actionPerformed(ActionEvent e) 
220             { JOptionPane.showMessageDialog(frame, INFO) ; }});
221
222         filemenu.addSeparator() ;
223
224         filemenu.add(item = new JMenuItem("Quit")) ;
225         item.addActionListener(new ActionListener() { // Quit.
226         public void actionPerformed(ActionEvent e) { System.exit(0) ; }});
227
228         // Add the menus onto the menu bar.
229         mbar.add(filemenu) ;
230         }
231
232     /**
233      * buildToolbar - build the Toolbar with the plugins.
234      **/
235     public void buildToolbar()
236         {
237         
238         }
239
240     /**
241      * addPlugin
242      * @param String - name of the plugin to insert.
243      **/
244     private void addPlugin(String p)
245         {
246         Plugin pl = null ;
247         
248         try
249             {
250             Class c = Class.forName(p) ;
251             pl = (Plugin)c.newInstance();
252             }
253         catch(ClassNotFoundException ex)
254             {
255             System.out.println("Exceptional!\n"+ex) ;
256             }
257         catch(InstantiationException ex)
258             {
259             System.out.println("Exceptional!\n"+ex) ;
260             }
261         catch(IllegalAccessException ex)
262             {
263             System.out.println("Exceptional!\n"+ex) ;
264             }
265
266         PipedOutputStream plugoutstr = new PipedOutputStream() ;
267         PipedInputStream pluginstr = new PipedInputStream() ;
268
269         // Insert the object into the vector.
270         plugins.addElement(pl) ;
271
272         // Add the plug in to the tabbedpane.
273         tabbedpane.addTab(pl.getTabName(), null, pl, pl.getTabTip()) ;
274
275         PipedInputStream pinmux = null ;
276         PipedOutputStream posmux = null ;
277
278         try
279             {
280             pinmux = new PipedInputStream(plugoutstr) ;
281             posmux = new PipedOutputStream(pluginstr) ;
282             }
283         catch(IOException ex)
284             {
285             System.out.println("Console: IOException creating plugin pipes.") ;
286             System.out.println(ex) ;
287             }
288
289         // Add the streams to the multiplexors.
290         inmux.addInputStream(pinmux) ;
291         outmux.addOutputStream(posmux) ;
292
293         // Initialise the plugin.
294         pl.init(pluginstr, plugoutstr) ;
295
296         plugnumber++ ;
297
298         // Menus?
299
300         // Toolbars?
301         }
302     }
303