1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.conient.datacomponents; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import javax.swing.JLabel; |
6 |
import java.awt.GridLayout; |
7 |
import java.util.Date; |
8 |
import java.util.StringTokenizer; |
9 |
import javax.swing.JComboBox; |
10 |
import java.text.DateFormat; |
11 |
import javax.swing.SwingUtilities; |
12 |
|
13 |
/** |
14 |
* This component displays the users currently logged |
15 |
* onto a system. Currently it does this simply by |
16 |
* putting them all in a JCombobox. |
17 |
* |
18 |
* @author $Author: tdb1 $ |
19 |
* @version $Id: UsersDataComponent.java,v 1.8 2001/01/29 14:00:51 tdb1 Exp $ |
20 |
*/ |
21 |
public class UsersDataComponent extends VisibleDataComponent { |
22 |
|
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 |
* @param attribute the data attribute we look after |
37 |
*/ |
38 |
public UsersDataComponent(String name, String attribute) { |
39 |
_name = name; |
40 |
_attribute = attribute; |
41 |
_label = new JLabel(name + ": "); |
42 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
43 |
setLayout(new GridLayout(1, 2)); |
44 |
_item.addItem("-uninitialised-"); |
45 |
add(_label); |
46 |
add(_item); |
47 |
} |
48 |
|
49 |
//---PUBLIC METHODS--- |
50 |
|
51 |
/** |
52 |
* This run method updates any Swing components |
53 |
* The setValue() method adds this component |
54 |
* to the Swing Event Dispatching Queue to |
55 |
* run this method. |
56 |
*/ |
57 |
public void run() { |
58 |
_item.removeAllItems(); |
59 |
if (_cache.equals(" ")) { |
60 |
_item.addItem("no users logged on"); |
61 |
} else { |
62 |
StringTokenizer st = new StringTokenizer(_cache); |
63 |
while(st.hasMoreTokens()) { |
64 |
_item.addItem(st.nextToken()); |
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
/** |
70 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
71 |
* method to provide clean logging (every class should have this). |
72 |
* |
73 |
* @return the name of this class and its CVS revision |
74 |
*/ |
75 |
public String toString() { |
76 |
return _name + "(" + _attribute + ")"; |
77 |
} |
78 |
|
79 |
//---PRIVATE METHODS--- |
80 |
|
81 |
//---ACCESSOR/MUTATOR METHODS--- |
82 |
|
83 |
/** |
84 |
* This takes the String value of the parameter that this component |
85 |
* is monitoring direct from the packet, it then performs all |
86 |
* approriate conversions and adds this class to the Swing Event |
87 |
* Dispatching queue. |
88 |
* |
89 |
* @param value the value for this data component |
90 |
* @throws DataFormatException if there was a problem converting the data for display |
91 |
*/ |
92 |
public void setValue(String value) throws DataFormatException { |
93 |
try { |
94 |
if (!_cache.equals(value)) { |
95 |
_cache = value; |
96 |
SwingUtilities.invokeLater(this); |
97 |
} |
98 |
} catch (Exception e) { |
99 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
100 |
} |
101 |
} |
102 |
|
103 |
//---ATTRIBUTES--- |
104 |
|
105 |
/** |
106 |
* The friendly name of this component |
107 |
*/ |
108 |
private String _name; |
109 |
|
110 |
/** |
111 |
* The attribute that this component is concerned with |
112 |
*/ |
113 |
private String _attribute; |
114 |
|
115 |
/** |
116 |
* Remebers what the last value was, so we |
117 |
* only update if we have to. |
118 |
*/ |
119 |
String _cache = ""; |
120 |
|
121 |
/** |
122 |
* The friendly label for this component |
123 |
*/ |
124 |
protected JLabel _label; |
125 |
|
126 |
/** |
127 |
* Just a JComboBox to display the users in |
128 |
*/ |
129 |
protected JComboBox _item = new JComboBox(); |
130 |
|
131 |
//---STATIC ATTRIBUTES--- |
132 |
|
133 |
} |