1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.conient; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.org.iscream.util.*; |
6 |
import uk.org.iscream.conient.datacomponents.*; |
7 |
import javax.swing.*; |
8 |
import javax.swing.border.*; |
9 |
import java.util.*; |
10 |
import java.awt.*; |
11 |
import java.awt.event.*; |
12 |
|
13 |
/** |
14 |
* |
15 |
* @author $Author: ajm4 $ |
16 |
* @version $Id: QueueFrame.java,v 1.11 2001/03/18 17:33:08 ajm4 Exp $ |
17 |
*/ |
18 |
public class QueueFrame extends JFrame { |
19 |
|
20 |
//---FINAL ATTRIBUTES--- |
21 |
|
22 |
/** |
23 |
* The current CVS revision of this class |
24 |
*/ |
25 |
public final String REVISION = "$Revision: 1.11 $"; |
26 |
|
27 |
//---STATIC METHODS--- |
28 |
|
29 |
//---CONSTRUCTORS--- |
30 |
|
31 |
/** |
32 |
* |
33 |
*/ |
34 |
public QueueFrame() { |
35 |
super("Server Queue Status"); |
36 |
//addVisibleDataComponent(_conent, new StringDataComponent("Host Name", "packet.attributes.machine_name")); |
37 |
JScrollPane scrollPane = new JScrollPane(_content,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED , JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
38 |
getContentPane().add(scrollPane, "Center"); |
39 |
JButton close = new JButton("Close Window"); |
40 |
close.addActionListener(new ActionListener() { |
41 |
public void actionPerformed(ActionEvent e) { |
42 |
setVisible(false); |
43 |
} |
44 |
}); |
45 |
getContentPane().add(close, "South"); |
46 |
setIconImage((new ImageIcon("./resources/server.gif")).getImage()); |
47 |
pack(); |
48 |
setVisible(true); |
49 |
} |
50 |
|
51 |
//---PUBLIC METHODS--- |
52 |
|
53 |
/** |
54 |
* Process an incoming packet, updates the components. |
55 |
* |
56 |
* @param packet the packet to process |
57 |
* |
58 |
* @return if the proceesing was successful |
59 |
*/ |
60 |
public boolean update(XMLPacket packet) { |
61 |
// iterate over the packets data |
62 |
boolean displaySucessful = true; |
63 |
String queueHashCode = packet.getParam("packet.attributes.hashCode"); |
64 |
|
65 |
// if there are no components looking after this data |
66 |
// create a new StringDataComponent for it |
67 |
if(!_queues.containsKey(queueHashCode)) { |
68 |
addQueue(packet); |
69 |
} |
70 |
|
71 |
// try and update the component, if it doesn't like what it gets |
72 |
// warn the user and set that this was an unsucessful display |
73 |
try { |
74 |
updateQueueInformation(packet); |
75 |
} catch (DataFormatException e) { |
76 |
Conient.addMessage("WARNING{queue frame}: " + e.getMessage()); |
77 |
displaySucessful = false; |
78 |
} |
79 |
return displaySucessful; |
80 |
} |
81 |
|
82 |
//---PRIVATE METHODS--- |
83 |
|
84 |
private void addQueue(XMLPacket packet) { |
85 |
HashMap newQueue = new HashMap(); |
86 |
String queueName = packet.getParam("packet.attributes.name"); |
87 |
String queueHashCode = packet.getParam("packet.attributes.hashCode"); |
88 |
VisibleDataComponent date = new DateDataComponent("Last timestamp", "packet.attributes.date"); |
89 |
VisibleDataComponent total = new StringDataComponent("Total queued to date", "packet.queue.attributes.total"); |
90 |
newQueue.put("packet.attributes.name", new StringDataComponent("Name", "packet.attributes.name")); |
91 |
newQueue.put("packet.queue.attributes.total", total); |
92 |
newQueue.put("packet.attributes.date", date); |
93 |
JPanel newQueueDisplay = new JPanel(); |
94 |
newQueueDisplay.setLayout(new BoxLayout(newQueueDisplay, BoxLayout.Y_AXIS)); |
95 |
newQueueDisplay.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), queueName)); |
96 |
newQueueDisplay.add(date); |
97 |
newQueueDisplay.add(total); |
98 |
_queuesDisplays.put(queueHashCode, newQueueDisplay); |
99 |
try { |
100 |
SwingSafeAdd task = new SwingSafeAdd(_content, newQueueDisplay); |
101 |
SwingUtilities.invokeAndWait(task); |
102 |
} catch (Exception e) { |
103 |
// don't care |
104 |
} |
105 |
pack(); |
106 |
_queues.put(queueHashCode, newQueue); |
107 |
} |
108 |
|
109 |
private void updateQueueInformation(XMLPacket packet) throws DataFormatException { |
110 |
String queueHashCode = packet.getParam("packet.attributes.hashCode"); |
111 |
|
112 |
// if it was a shutdown packet, remove the display |
113 |
if (packet.getParam("packet.attributes.shutdown") != null) { |
114 |
// *** !!! MAY NOT BE SWING SAFE !!! *** |
115 |
JPanel temp = (JPanel)_queuesDisplays.get(queueHashCode); |
116 |
temp.setVisible(false); |
117 |
_content.remove(temp); |
118 |
_queuesDisplays.remove(temp); |
119 |
pack(); |
120 |
|
121 |
// else update the display |
122 |
} else { |
123 |
HashMap components = (HashMap) _queues.get(queueHashCode); |
124 |
Set packetSet = packet.getSet(); |
125 |
Iterator i = packetSet.iterator(); |
126 |
while (i.hasNext()) { |
127 |
String dataKey = (String) i.next(); |
128 |
// if there are no components looking after this data |
129 |
// create a new StringDataComponent for it |
130 |
if(!components.containsKey(dataKey)) { |
131 |
// check if its a queue, if it is then we need to deal with it |
132 |
if(dataKey.startsWith("packet.queue.attributes.queue")) { |
133 |
VisibleDataComponent queue = new StringDataComponent("Current length of " + dataKey.substring(dataKey.lastIndexOf('.') + 1), dataKey); |
134 |
components.put(dataKey, queue); |
135 |
try { |
136 |
SwingSafeAdd task = new SwingSafeAdd((JPanel)_queuesDisplays.get(queueHashCode), queue); |
137 |
SwingUtilities.invokeAndWait(task); |
138 |
} catch (Exception e) { |
139 |
// if there was a problem |
140 |
System.err.println("ERROR Failed To Add Component - " + e); |
141 |
} |
142 |
pack(); |
143 |
} |
144 |
// note we ignore all other attributes |
145 |
} |
146 |
if(components.containsKey(dataKey)) { |
147 |
((DataComponent) components.get(dataKey)).setValue(packet); |
148 |
} |
149 |
} |
150 |
} |
151 |
} |
152 |
|
153 |
//---ACCESSOR/MUTATOR METHODS--- |
154 |
|
155 |
//---ATTRIBUTES--- |
156 |
|
157 |
/** |
158 |
* The components that we already know about |
159 |
* connected with the attributes they care about |
160 |
* are stored here. |
161 |
*/ |
162 |
private HashMap _queues = new HashMap(); |
163 |
private HashMap _queuesDisplays = new HashMap(); |
164 |
|
165 |
private Box _content = Box.createVerticalBox(); |
166 |
|
167 |
//---STATIC ATTRIBUTES--- |
168 |
|
169 |
} |