ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/conient/uk/org/iscream/cms/conient/datacomponents/UsersDataComponent.java
Revision: 1.19
Committed: Tue May 21 16:47:10 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.18: +2 -1 lines
Log Message:
Added URL to GPL headers.

File Contents

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