1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.conient.datacomponents; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import javax.swing.JLabel; |
6 |
import javax.swing.JTextField; |
7 |
import java.awt.GridLayout; |
8 |
import javax.swing.SwingUtilities; |
9 |
|
10 |
/** |
11 |
* Used to display some information about processes |
12 |
* |
13 |
* Used in combination with others of this type and displayed |
14 |
* correctly, this will help display the number of processes |
15 |
* |
16 |
* It simply displays the value as a String |
17 |
* in a JTextField. |
18 |
* |
19 |
* @author $Author: ajm4 $ |
20 |
* @version $Id: ProcessesDataComponent.java,v 1.1 2001/02/04 00:07:35 ajm4 Exp $ |
21 |
*/ |
22 |
public class ProcessesDataComponent extends VisibleDataComponent { |
23 |
|
24 |
//---FINAL ATTRIBUTES--- |
25 |
|
26 |
/** |
27 |
* The current CVS revision of this class |
28 |
*/ |
29 |
public final String REVISION = "$Revision: 1.1 $"; |
30 |
|
31 |
/** |
32 |
* The default length of the JTextField |
33 |
*/ |
34 |
protected final int DEFAULT_TEXT_LENGTH = 3; |
35 |
|
36 |
//---STATIC METHODS--- |
37 |
|
38 |
//---CONSTRUCTORS--- |
39 |
|
40 |
/** |
41 |
* Creates the component with a friendly name to be |
42 |
* used as label |
43 |
* |
44 |
* @param name the friendly name |
45 |
* @param attribute the data attribute we look after |
46 |
*/ |
47 |
public ProcessesDataComponent(String name, String attribute) { |
48 |
_name = name; |
49 |
_attribute = attribute; |
50 |
_item = new JTextField("", DEFAULT_TEXT_LENGTH); |
51 |
_item.setEditable(false); |
52 |
_label = new JLabel(_name); |
53 |
_label.setHorizontalAlignment(JLabel.CENTER); |
54 |
_item.setHorizontalAlignment(JTextField.CENTER); |
55 |
setLayout(new GridLayout(2, 1)); |
56 |
_item.setText("-uninitialised-"); |
57 |
add(_label); |
58 |
add(_item); |
59 |
} |
60 |
|
61 |
//---PUBLIC METHODS--- |
62 |
|
63 |
/** |
64 |
* This run method updates any Swing components |
65 |
* The setValue() method adds this component |
66 |
* to the Swing Event Dispatching Queue to |
67 |
* run this method. |
68 |
*/ |
69 |
public void run() { |
70 |
_item.setText(_cache); |
71 |
} |
72 |
|
73 |
/** |
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 |
//---PRIVATE METHODS--- |
84 |
|
85 |
//---ACCESSOR/MUTATOR METHODS--- |
86 |
|
87 |
/** |
88 |
* This takes the String value of the parameter that this component |
89 |
* is monitoring direct from the packet, it then performs all |
90 |
* approriate conversions and adds this class to the Swing Event |
91 |
* Dispatching queue. |
92 |
* |
93 |
* @param value the value for this data component |
94 |
* @throws DataFormatException if there was a problem converting the data for display |
95 |
*/ |
96 |
public void setValue(String value) throws DataFormatException { |
97 |
try { |
98 |
if(!_cache.equals(value)) { |
99 |
_cache = value; |
100 |
SwingUtilities.invokeLater(this); |
101 |
} |
102 |
} catch (Exception e) { |
103 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
104 |
} |
105 |
} |
106 |
|
107 |
/** |
108 |
* Returns the string showing the packet |
109 |
* attribute that the component is looking after |
110 |
* |
111 |
* @return the packet reference |
112 |
*/ |
113 |
public String getPacketAttribute() { |
114 |
return _attribute; |
115 |
} |
116 |
|
117 |
//---ATTRIBUTES--- |
118 |
|
119 |
/** |
120 |
* The friendly name of this component |
121 |
*/ |
122 |
protected String _name; |
123 |
|
124 |
/** |
125 |
* The attribute that this component is concerned with |
126 |
*/ |
127 |
protected String _attribute; |
128 |
|
129 |
/** |
130 |
* The friendly label for this component |
131 |
*/ |
132 |
protected JLabel _label; |
133 |
|
134 |
/** |
135 |
* Remebers what the last value was, so we |
136 |
* only update if we have to. |
137 |
*/ |
138 |
String _cache = ""; |
139 |
|
140 |
/** |
141 |
* The length of the JTextField |
142 |
*/ |
143 |
protected int _displayLength; |
144 |
|
145 |
/** |
146 |
* Just a normal label to display our value as a String |
147 |
*/ |
148 |
protected JTextField _item; |
149 |
|
150 |
//---STATIC ATTRIBUTES--- |
151 |
|
152 |
} |