1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
//---PACKAGE DECLARATION--- |
21 |
package uk.org.iscream.cms.conient.datacomponents; |
22 |
|
23 |
//---IMPORTS--- |
24 |
import javax.swing.JLabel; |
25 |
import java.awt.GridLayout; |
26 |
import java.util.StringTokenizer; |
27 |
import java.util.Arrays; |
28 |
import javax.swing.JComboBox; |
29 |
import javax.swing.SwingUtilities; |
30 |
import uk.org.iscream.cms.server.util.XMLPacket; |
31 |
|
32 |
/** |
33 |
* This component displays the users currently logged |
34 |
* onto a system. Currently it does this simply by |
35 |
* putting them all in a JCombobox. |
36 |
* |
37 |
* @author $Author: tdb $ |
38 |
* @version $Id: UsersDataComponent.java,v 1.17 2002/02/14 17:17:00 tdb Exp $ |
39 |
*/ |
40 |
public class UsersDataComponent extends VisibleDataComponent { |
41 |
|
42 |
//---FINAL ATTRIBUTES--- |
43 |
|
44 |
//---STATIC METHODS--- |
45 |
|
46 |
//---CONSTRUCTORS--- |
47 |
|
48 |
/** |
49 |
* Creates the component with a friendly name to be |
50 |
* used as label, but as we're a very basic |
51 |
* extension of StringDataComponent, we just construct |
52 |
* that. |
53 |
* |
54 |
* @param name the friendly name |
55 |
* @param attribute the data attribute we look after |
56 |
*/ |
57 |
public UsersDataComponent(String name, String attribute) { |
58 |
_name = name; |
59 |
_attribute = attribute; |
60 |
_label = new JLabel(name + ": "); |
61 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
62 |
setLayout(new GridLayout(1, 2)); |
63 |
_item.addItem("-uninitialised-"); |
64 |
add(_label); |
65 |
add(_item); |
66 |
setVisible(false); |
67 |
} |
68 |
|
69 |
//---PUBLIC METHODS--- |
70 |
|
71 |
/** |
72 |
* This run method updates any Swing components |
73 |
* The setValue() method adds this component |
74 |
* to the Swing Event Dispatching Queue to |
75 |
* run this method. |
76 |
*/ |
77 |
public void run() { |
78 |
if(!isVisible()) { |
79 |
setVisible(true); |
80 |
} |
81 |
_item.removeAllItems(); |
82 |
if (_cache.equals(" ")) { |
83 |
_item.addItem("no users logged on"); |
84 |
} else { |
85 |
StringTokenizer st = new StringTokenizer(_cache, ";"); |
86 |
while(st.hasMoreTokens()) { |
87 |
_item.addItem(st.nextToken()); |
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
/** |
93 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
94 |
* method to provide clean logging (every class should have this). |
95 |
* |
96 |
* @return the name of this class and its CVS revision |
97 |
*/ |
98 |
public String toString() { |
99 |
return _name + "(" + _attribute + ")"; |
100 |
} |
101 |
|
102 |
//---PRIVATE METHODS--- |
103 |
|
104 |
//---ACCESSOR/MUTATOR METHODS--- |
105 |
|
106 |
/** |
107 |
* This takes the packet to obtain the value from, it then performs all |
108 |
* approriate conversions and adds this class to the Swing Event |
109 |
* Dispatching queue. |
110 |
* |
111 |
* @param packet the XMLPacket to get the data from |
112 |
* @throws DataFormatException if there was a problem converting the data for display |
113 |
*/ |
114 |
public void setValue(XMLPacket packet) throws DataFormatException { |
115 |
String value = packet.getParam(_attribute); |
116 |
// tokenize the input |
117 |
StringTokenizer st = new StringTokenizer(value); |
118 |
// create an array to store the tokens in |
119 |
int tokenCount = st.countTokens(); |
120 |
String[] tokens = new String[tokenCount]; |
121 |
int i=0; |
122 |
while(st.hasMoreTokens()) { |
123 |
tokens[i] = st.nextToken(); |
124 |
i++; |
125 |
} |
126 |
// sort the array |
127 |
Arrays.sort(tokens); |
128 |
// put the array back into a String, sorted |
129 |
StringBuffer valueBuffer = new StringBuffer(); |
130 |
// we only add the token when we've gone past all |
131 |
// the duplicates - this allows us to keep count. |
132 |
String lastToken = ""; |
133 |
int count = 0; |
134 |
for(i=0; i < tokenCount; i++) { |
135 |
// if the token is different, we'll add the |
136 |
// last one |
137 |
if(!tokens[i].equals(lastToken)) { |
138 |
// just a check for the "first case" |
139 |
if(!lastToken.equals("")) { |
140 |
valueBuffer.append(lastToken); |
141 |
if(count > 1) { |
142 |
valueBuffer.append(" ("); |
143 |
valueBuffer.append(count); |
144 |
valueBuffer.append(")"); |
145 |
} |
146 |
valueBuffer.append(";"); |
147 |
} |
148 |
// starting a new token, so reset the |
149 |
// the last one and the count |
150 |
lastToken = tokens[i]; |
151 |
count = 1; |
152 |
} |
153 |
// if not, we'll increment our count of the |
154 |
// current one |
155 |
else { |
156 |
count++; |
157 |
} |
158 |
} |
159 |
// we have to add the last one... |
160 |
valueBuffer.append(lastToken); |
161 |
if(count > 1) { |
162 |
valueBuffer.append(" ("); |
163 |
valueBuffer.append(count); |
164 |
valueBuffer.append(");"); |
165 |
} |
166 |
valueBuffer.append(";"); |
167 |
String sortedValue = valueBuffer.toString(); |
168 |
try { |
169 |
if (!_cache.equals(sortedValue)) { |
170 |
_cache = sortedValue; |
171 |
SwingUtilities.invokeLater(this); |
172 |
} |
173 |
} catch (Exception e) { |
174 |
throw new DataFormatException(sortedValue + " is an invalid data type for " + toString()); |
175 |
} |
176 |
} |
177 |
|
178 |
/** |
179 |
* Returns the string showing the packet |
180 |
* attribute that the component is looking after |
181 |
* |
182 |
* @return the packet reference |
183 |
*/ |
184 |
public String getPacketAttribute() { |
185 |
return _attribute; |
186 |
} |
187 |
|
188 |
//---ATTRIBUTES--- |
189 |
|
190 |
/** |
191 |
* The friendly name of this component |
192 |
*/ |
193 |
private String _name; |
194 |
|
195 |
/** |
196 |
* The attribute that this component is concerned with |
197 |
*/ |
198 |
private String _attribute; |
199 |
|
200 |
/** |
201 |
* Remebers what the last value was, so we |
202 |
* only update if we have to. |
203 |
*/ |
204 |
String _cache = ""; |
205 |
|
206 |
/** |
207 |
* The friendly label for this component |
208 |
*/ |
209 |
protected JLabel _label; |
210 |
|
211 |
/** |
212 |
* Just a JComboBox to display the users in |
213 |
*/ |
214 |
protected JComboBox _item = new JComboBox(); |
215 |
|
216 |
//---STATIC ATTRIBUTES--- |
217 |
|
218 |
} |