1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.ac.ukc.iscream.conient; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import javax.swing.*; |
6 |
|
import javax.swing.border.*; |
6 |
– |
import java.awt.Color; |
7 |
– |
|
7 |
|
import java.awt.*; |
8 |
|
import java.awt.event.*; |
10 |
– |
import java.net.*; |
11 |
– |
import java.io.*; |
12 |
– |
import java.util.Date; |
13 |
– |
import java.text.DateFormat; |
14 |
– |
import java.util.Locale; |
15 |
– |
import java.util.HashMap; |
9 |
|
|
10 |
|
/** |
11 |
< |
* NASTY AND BASIC, PLEASE DON'T COMPLAIN |
11 |
> |
* This is the main class of the Conient client. |
12 |
|
* |
13 |
+ |
* This sets up the control panel, the data panel |
14 |
+ |
* and its own display. |
15 |
+ |
* |
16 |
|
* @author $Author$ |
17 |
|
* @version $Id$ |
18 |
|
*/ |
19 |
< |
public class SwingClient extends JFrame implements Runnable { |
19 |
> |
public class Conient extends JFrame { |
20 |
|
|
21 |
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
24 |
|
* The current CVS revision of this class |
25 |
|
*/ |
26 |
|
public final String REVISION = "$Revision$"; |
27 |
+ |
|
28 |
+ |
/** |
29 |
+ |
* The initial width of the window |
30 |
+ |
*/ |
31 |
+ |
private final int DEFAULT_WIDTH = 600; |
32 |
|
|
33 |
< |
private final int width = 400; |
34 |
< |
private final int height = 700; |
33 |
> |
/** |
34 |
> |
* The initial height of the window |
35 |
> |
*/ |
36 |
> |
private final int DEFAULT_HEIGHT = 600; |
37 |
|
|
38 |
+ |
/** |
39 |
+ |
* The default configuration fle |
40 |
+ |
* This can be specified on the command line |
41 |
+ |
*/ |
42 |
+ |
public static final String DEFAULT_CONFIG_FILE = "./etc/default.conf"; |
43 |
+ |
|
44 |
|
//---STATIC METHODS--- |
45 |
|
|
46 |
+ |
/** |
47 |
+ |
* The first method that is called. |
48 |
+ |
* Sets up the various panels |
49 |
+ |
* |
50 |
+ |
* @param args the command line arguments |
51 |
+ |
*/ |
52 |
|
public static void main(String[] args) { |
53 |
< |
try { |
54 |
< |
String host = null; |
55 |
< |
if (args.length != 1) { |
56 |
< |
Object response = null; |
57 |
< |
while(!(response instanceof String)) { |
58 |
< |
response = JOptionPane.showInputDialog(null, "Please enter the name of a server running the I-Scream client interface:", "I-Scream Server", JOptionPane.INFORMATION_MESSAGE); |
44 |
< |
} |
45 |
< |
host = (String) response; |
46 |
< |
} else { |
47 |
< |
host = args[0]; |
48 |
< |
} |
53 |
> |
// get the host from the command line if they gave it |
54 |
> |
String host = null; |
55 |
> |
String configFile = DEFAULT_CONFIG_FILE; |
56 |
> |
if (args.length == 1) { |
57 |
> |
configFile = args[0]; |
58 |
> |
} |
59 |
|
|
60 |
< |
int port = 4510; |
61 |
< |
Socket socket = new Socket(host, port); |
62 |
< |
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
63 |
< |
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
64 |
< |
DataReader data = new DataReader(inBound); |
60 |
> |
// start the configuration system |
61 |
> |
Configuration.initialise(configFile); |
62 |
> |
|
63 |
> |
// the data display panel |
64 |
> |
DataPanel data = new DataPanel(); |
65 |
|
|
66 |
< |
SwingClient client = new SwingClient(data); |
67 |
< |
Thread clientThread = new Thread(client); |
68 |
< |
clientThread.start(); |
69 |
< |
} catch (Exception e) { |
70 |
< |
System.err.println("ERROR START: " + e); |
71 |
< |
System.exit(1); |
72 |
< |
} |
66 |
> |
// the control panel |
67 |
> |
ControlPanel control = new ControlPanel(data); |
68 |
> |
|
69 |
> |
// the main frame (passed the two panels) |
70 |
> |
Conient client = new Conient(data, control); |
71 |
> |
Conient.addMessage("Conient {an i-scream Client} © 2001 University of Kent & Project i-scream"); |
72 |
> |
|
73 |
> |
Conient.addMessage("Conient ready."); |
74 |
|
} |
75 |
+ |
|
76 |
|
|
77 |
|
//---CONSTRUCTORS--- |
78 |
|
|
79 |
|
/** |
80 |
< |
* Creates a new Swing Client. |
80 |
> |
* Creates a new Swing Client Frame |
81 |
|
*/ |
82 |
< |
public SwingClient(DataReader data) { |
71 |
< |
_data = data; |
82 |
> |
private Conient(JPanel data, JPanel control) { |
83 |
|
// set up the Frame |
84 |
< |
setTitle("I-Scream Client"); |
85 |
< |
setSize(width, height); |
75 |
< |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
76 |
< |
Box box = Box.createVerticalBox(); |
84 |
> |
super("Conient {an i-scream Client}"); |
85 |
> |
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); |
86 |
|
|
87 |
< |
// create the i-scream logo at the top |
88 |
< |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
89 |
< |
JLabel comment = new JLabel(" I-Scream Client"); |
90 |
< |
comment.setForeground( new Color(0, 0, 102)); |
91 |
< |
Box header = Box.createHorizontalBox(); |
92 |
< |
header.add(comment); |
93 |
< |
header.add(Box.createHorizontalGlue()); |
85 |
< |
header.add(iscream); |
87 |
> |
// set what happens when the X in the corner is clicked |
88 |
> |
addWindowListener(new WindowAdapter() { |
89 |
> |
public void windowClosing(WindowEvent e) {System.exit(0);} |
90 |
> |
}); |
91 |
> |
|
92 |
> |
// add the control pane to the top border |
93 |
> |
getContentPane().add(control, "North"); |
94 |
|
|
95 |
< |
//_tabbedPane.setSelectedIndex(0); |
96 |
< |
|
97 |
< |
// build the frame |
90 |
< |
box.add(header); |
91 |
< |
box.add(_tabbedPane); |
92 |
< |
box.add(_status); |
93 |
< |
getContentPane().add(box); |
95 |
> |
// create a scrollable pane for the data in the centre |
96 |
> |
JScrollPane scrollPane = new JScrollPane(data); |
97 |
> |
getContentPane().add(scrollPane, "Center"); |
98 |
|
|
99 |
+ |
// setup the status panel |
100 |
+ |
JPanel bottom = new JPanel(); |
101 |
+ |
bottom.setLayout(new BorderLayout()); |
102 |
+ |
_messages.setEditable(false); |
103 |
+ |
_messages.setRows(3); |
104 |
+ |
JScrollPane messagesPane = new JScrollPane(_messages); |
105 |
+ |
messagesPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
106 |
+ |
bottom.add(messagesPane, "Center"); |
107 |
+ |
JPanel statusPanel = new JPanel(); |
108 |
+ |
JPanel linkPanel = new JPanel(); |
109 |
+ |
linkPanel.setLayout(new GridLayout(2,1)); |
110 |
+ |
linkPanel.add(_controlStatus); |
111 |
+ |
linkPanel.add(_dataStatus); |
112 |
+ |
statusPanel.setLayout(new GridLayout(1,3)); |
113 |
+ |
statusPanel.add(linkPanel); |
114 |
+ |
statusPanel.add(_queueStatus); |
115 |
+ |
bottom.add(statusPanel, "South"); |
116 |
+ |
|
117 |
+ |
// add the status panel to the bottom border |
118 |
+ |
getContentPane().add(bottom, "South"); |
119 |
+ |
|
120 |
|
// show the window |
121 |
|
show(); |
97 |
– |
_status.setText("Running..."); |
122 |
|
} |
123 |
|
|
124 |
|
//---PUBLIC METHODS--- |
125 |
|
|
102 |
– |
public void run() { |
103 |
– |
HashMap hostList = new HashMap(); |
104 |
– |
try { |
105 |
– |
_data.start(); |
106 |
– |
while(true) { |
107 |
– |
synchronized (_data) { |
108 |
– |
_data.wait(); |
109 |
– |
} |
110 |
– |
String xml = _data.getXML(); |
111 |
– |
if (xml == null) { |
112 |
– |
_status.setText("No XML to update..."); |
113 |
– |
} else { |
114 |
– |
|
115 |
– |
// Get a string without any null characters in it. |
116 |
– |
// -- maybe String.trim() would be better here ? |
117 |
– |
if (xml.indexOf(0) != -1) { |
118 |
– |
xml = xml.substring(0, xml.indexOf(0)); |
119 |
– |
} |
120 |
– |
else { |
121 |
– |
xml = xml.substring(0, xml.length()); |
122 |
– |
} |
123 |
– |
|
124 |
– |
// Use XMLPacketMaker to make an XMLPacket object. |
125 |
– |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
126 |
– |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
127 |
– |
String hostName = packet.getParam("packet.attributes.machine_name"); |
128 |
– |
if(!hostList.containsKey(hostName)) { |
129 |
– |
host = new HostDisplayPanel(); |
130 |
– |
_tabbedPane.addTab(hostName, _serverIcon, host, "Monitor " + hostName); |
131 |
– |
hostList.put(hostName, host); |
132 |
– |
_status.setText("New Host added: " + hostName); |
133 |
– |
} |
134 |
– |
((HostDisplayPanel) hostList.get(hostName)).updateHost(packet); |
135 |
– |
} |
136 |
– |
} |
137 |
– |
} catch (Exception e) { |
138 |
– |
System.err.println("ERROR RUN: " + e); |
139 |
– |
} |
140 |
– |
} |
141 |
– |
|
126 |
|
//---PRIVATE METHODS--- |
127 |
+ |
|
128 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
129 |
+ |
|
130 |
+ |
/** |
131 |
+ |
* Sets the control link status. |
132 |
+ |
* |
133 |
+ |
* @param status the message |
134 |
+ |
*/ |
135 |
+ |
public static void setControlStatus(String status) { |
136 |
+ |
_controlStatus.setText("Control Link: " + status); |
137 |
+ |
_controlStatus.repaint(); |
138 |
+ |
} |
139 |
|
|
140 |
< |
protected JPanel makeTextPanel(String text, Component item) { |
141 |
< |
JPanel panel = new JPanel(false); |
142 |
< |
JLabel label = new JLabel(text); |
143 |
< |
label.setHorizontalAlignment(JLabel.RIGHT); |
144 |
< |
//item.setHorizontalAlignment(JLabel.LEFT); |
145 |
< |
panel.setLayout(new GridLayout(1, 2)); |
146 |
< |
panel.add(label); |
147 |
< |
panel.add(item); |
152 |
< |
return panel; |
140 |
> |
/** |
141 |
> |
* Sets the data link status. |
142 |
> |
* |
143 |
> |
* @param status the message |
144 |
> |
*/ |
145 |
> |
public static void setDataStatus(String status) { |
146 |
> |
_dataStatus.setText("Data Link: " + status); |
147 |
> |
_dataStatus.repaint(); |
148 |
|
} |
149 |
|
|
150 |
+ |
/** |
151 |
+ |
* Updates the queue status. |
152 |
+ |
* |
153 |
+ |
* @param currentQueue |
154 |
+ |
* @param numElements |
155 |
+ |
*/ |
156 |
+ |
public static void setQueueStatus(int currentQueue, int numElements) { |
157 |
+ |
_queueStatus.setText("Data Queue : " + currentQueue + " Packets Recieved : " + numElements); |
158 |
+ |
_queueStatus.repaint(); |
159 |
+ |
} |
160 |
|
|
161 |
< |
//---ACCESSOR/MUTATOR METHODS--- |
161 |
> |
/** |
162 |
> |
* Adds a system message to the messages list |
163 |
> |
* |
164 |
> |
* @param message the new message |
165 |
> |
*/ |
166 |
> |
public static void addMessage(String message) { |
167 |
> |
_messages.insert(message + "\n", 0); |
168 |
> |
} |
169 |
|
|
170 |
|
//---ATTRIBUTES--- |
159 |
– |
|
160 |
– |
HostDisplayPanel host; |
161 |
– |
JTabbedPane _tabbedPane = new JTabbedPane(); |
162 |
– |
DataReader _data; |
163 |
– |
JLabel _status = new JLabel(); |
164 |
– |
ImageIcon _serverIcon = new ImageIcon("server.gif"); |
171 |
|
|
172 |
|
//---STATIC ATTRIBUTES--- |
173 |
|
|
174 |
< |
} |
174 |
> |
/** |
175 |
> |
* Displays information about the data link |
176 |
> |
*/ |
177 |
> |
static JLabel _controlStatus = new JLabel("Control Link: Disconnected", JLabel.LEFT); |
178 |
> |
{ |
179 |
> |
_controlStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
180 |
> |
} |
181 |
> |
|
182 |
> |
/** |
183 |
> |
* Displays information about the data link |
184 |
> |
*/ |
185 |
> |
static JLabel _dataStatus = new JLabel("Data Link: Disconnected", JLabel.LEFT); |
186 |
> |
{ |
187 |
> |
_dataStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
188 |
> |
} |
189 |
> |
|
190 |
> |
/** |
191 |
> |
* Displays information about the inbound data queue. |
192 |
> |
*/ |
193 |
> |
static JLabel _queueStatus = new JLabel(" ", JLabel.LEFT); |
194 |
> |
{ |
195 |
> |
_queueStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
196 |
> |
} |
197 |
> |
|
198 |
> |
/** |
199 |
> |
* The place where system messages are written. |
200 |
> |
*/ |
201 |
> |
static JTextArea _messages = new JTextArea(); |
202 |
> |
{ |
203 |
> |
_messages.setLineWrap(true); |
204 |
> |
} |
205 |
> |
} |