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