1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.conient; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.ac.ukc.iscream.util.*; |
6 |
import java.util.HashMap; |
7 |
import javax.swing.ImageIcon; |
8 |
import javax.swing.JTabbedPane; |
9 |
import javax.swing.JPanel; |
10 |
|
11 |
/** |
12 |
* This thread reads data from the DataReader |
13 |
* it then asks the appropriate HostDisplayPanel |
14 |
* to update its data. |
15 |
* |
16 |
* @author $Author: tdb1 $ |
17 |
* @version $Id: ConnectionHandler.java,v 1.9 2001/01/24 01:12:30 tdb1 Exp $ |
18 |
*/ |
19 |
public class DataPanel extends JPanel implements Runnable { |
20 |
|
21 |
//---FINAL ATTRIBUTES--- |
22 |
|
23 |
/** |
24 |
* The current CVS revision of this class |
25 |
*/ |
26 |
public final String REVISION = "$Revision: 1.4 $"; |
27 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
//---CONSTRUCTORS--- |
31 |
|
32 |
//---PUBLIC METHODS--- |
33 |
|
34 |
/** |
35 |
* Starts the DataPanel running |
36 |
*/ |
37 |
public void run() { |
38 |
// add the tabbed pane to ourselves |
39 |
add(_tabbedPane); |
40 |
|
41 |
// the hosts we are interested in go here |
42 |
HashMap hostList = new HashMap(); |
43 |
|
44 |
try { |
45 |
while(_running) { |
46 |
|
47 |
String xml = (String) _dataQueue.get(_myQueue); |
48 |
SwingClient.setQueueStatus(_dataQueue.queueSize(_myQueue), _dataQueue.elementCount()); |
49 |
if (xml == null) { |
50 |
// shouldn't really happen...but not sure |
51 |
//_status.insert("No XML to update...",0); |
52 |
} else { |
53 |
|
54 |
// Get a string without any null characters in it. |
55 |
// -- maybe String.trim() would be better ? |
56 |
if (xml.indexOf(0) != -1) { |
57 |
xml = xml.substring(0, xml.indexOf(0)); |
58 |
} |
59 |
else { |
60 |
xml = xml.substring(0, xml.length()); |
61 |
} |
62 |
|
63 |
// Use XMLPacketMaker to make an XMLPacket object. |
64 |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
65 |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
66 |
String hostName = packet.getParam("packet.attributes.machine_name"); |
67 |
if(!hostList.containsKey(hostName)) { |
68 |
|
69 |
HostDisplayPanel host = new HostDisplayPanel(); |
70 |
_tabbedPane.addTab(hostName, _serverIcon, host, "Monitor " + hostName); |
71 |
hostList.put(hostName, host); |
72 |
SwingClient.addMessage("New Host added: " + hostName); |
73 |
} |
74 |
if (!((HostDisplayPanel) hostList.get(hostName)).updateHost(packet)) { |
75 |
throw new Exception(hostName + " sent an invalid data packet stopping data update!"); |
76 |
} |
77 |
} |
78 |
} |
79 |
} catch (Exception e) { |
80 |
SwingClient.addMessage("ERROR: +" + e.getMessage()); |
81 |
} |
82 |
} |
83 |
|
84 |
/** |
85 |
* This method allows other classes |
86 |
* to shutdown this data panel. |
87 |
*/ |
88 |
public void shutdown() { |
89 |
_running = false; |
90 |
} |
91 |
|
92 |
//---PRIVATE METHODS--- |
93 |
|
94 |
//---ACCESSOR/MUTATOR METHODS--- |
95 |
|
96 |
//---ATTRIBUTES--- |
97 |
|
98 |
/** |
99 |
* The state of this thread. |
100 |
*/ |
101 |
boolean _running = true; |
102 |
|
103 |
/** |
104 |
* Assigns the queue that this panel |
105 |
* will use to obtain data |
106 |
* |
107 |
* @param queue the queue |
108 |
*/ |
109 |
public void setQueue(Queue queue) { |
110 |
_dataQueue = queue; |
111 |
_myQueue = _dataQueue.getQueue(); |
112 |
} |
113 |
|
114 |
/** |
115 |
* Removes all the tabs on display |
116 |
* Used to tidy up when a new data |
117 |
* channel is opened. |
118 |
*/ |
119 |
public void cleanUpTabs() { |
120 |
_tabbedPane.removeAll(); |
121 |
} |
122 |
|
123 |
/** |
124 |
* The tabbed pane is where HostDisplayPanel's |
125 |
* are placed |
126 |
*/ |
127 |
JTabbedPane _tabbedPane = new JTabbedPane(); |
128 |
|
129 |
/** |
130 |
* The queue new data will be read from |
131 |
*/ |
132 |
Queue _dataQueue; |
133 |
|
134 |
/** |
135 |
* Our queue number |
136 |
*/ |
137 |
int _myQueue; |
138 |
|
139 |
/** |
140 |
* An icon to represent a host |
141 |
*/ |
142 |
ImageIcon _serverIcon = new ImageIcon("server.gif"); |
143 |
} |