1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import javax.swing.*; |
5 |
import javax.swing.border.*; |
6 |
import java.awt.Color; |
7 |
|
8 |
import java.awt.*; |
9 |
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; |
16 |
|
17 |
/** |
18 |
* NASTY AND BASIC, PLEASE DON'T COMPLAIN |
19 |
* |
20 |
* @author $Author: ajm4 $ |
21 |
* @version $Id: SwingClient.java,v 1.4 2001/01/15 00:12:31 ajm4 Exp $ |
22 |
*/ |
23 |
public class SwingClient extends JFrame implements Runnable { |
24 |
|
25 |
//---FINAL ATTRIBUTES--- |
26 |
|
27 |
/** |
28 |
* The current CVS revision of this class |
29 |
*/ |
30 |
public final String REVISION = "$Revision: 1.4 $"; |
31 |
|
32 |
private final int width = 400; |
33 |
private final int height = 700; |
34 |
|
35 |
//---STATIC METHODS--- |
36 |
|
37 |
public static void main(String[] args) { |
38 |
try { |
39 |
String host = null; |
40 |
if (args.length != 1) { |
41 |
Object response = null; |
42 |
while(!(response instanceof String)) { |
43 |
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 |
} |
49 |
|
50 |
int port = 4510; |
51 |
Socket socket = new Socket(host, port); |
52 |
BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
53 |
PrintWriter outBound = new PrintWriter(socket.getOutputStream()); |
54 |
DataReader data = new DataReader(inBound); |
55 |
|
56 |
SwingClient client = new SwingClient(data); |
57 |
Thread clientThread = new Thread(client); |
58 |
clientThread.start(); |
59 |
} catch (Exception e) { |
60 |
System.err.println("ERROR START: " + e); |
61 |
System.exit(1); |
62 |
} |
63 |
} |
64 |
|
65 |
//---CONSTRUCTORS--- |
66 |
|
67 |
/** |
68 |
* Creates a new Swing Client. |
69 |
*/ |
70 |
public SwingClient(DataReader data) { |
71 |
_data = data; |
72 |
// set up the Frame |
73 |
setTitle("I-Scream Client"); |
74 |
setSize(width, height); |
75 |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
76 |
Box box = Box.createVerticalBox(); |
77 |
|
78 |
// create the i-scream logo at the top |
79 |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
80 |
JLabel comment = new JLabel(" I-Scream Client"); |
81 |
comment.setForeground( new Color(0, 0, 102)); |
82 |
Box header = Box.createHorizontalBox(); |
83 |
header.add(comment); |
84 |
header.add(Box.createHorizontalGlue()); |
85 |
header.add(iscream); |
86 |
|
87 |
//_tabbedPane.setSelectedIndex(0); |
88 |
|
89 |
// build the frame |
90 |
box.add(header); |
91 |
box.add(_tabbedPane); |
92 |
box.add(_status); |
93 |
getContentPane().add(box); |
94 |
|
95 |
// show the window |
96 |
show(); |
97 |
_status.setText("Running..."); |
98 |
} |
99 |
|
100 |
//---PUBLIC METHODS--- |
101 |
|
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 |
|
142 |
//---PRIVATE METHODS--- |
143 |
|
144 |
protected JPanel makeTextPanel(String text, Component item) { |
145 |
JPanel panel = new JPanel(false); |
146 |
JLabel label = new JLabel(text); |
147 |
label.setHorizontalAlignment(JLabel.RIGHT); |
148 |
//item.setHorizontalAlignment(JLabel.LEFT); |
149 |
panel.setLayout(new GridLayout(1, 2)); |
150 |
panel.add(label); |
151 |
panel.add(item); |
152 |
return panel; |
153 |
} |
154 |
|
155 |
|
156 |
//---ACCESSOR/MUTATOR METHODS--- |
157 |
|
158 |
//---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"); |
165 |
|
166 |
//---STATIC ATTRIBUTES--- |
167 |
|
168 |
} |