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
(Generate patch)

Comparing projects/cms/source/conient/uk/org/iscream/cms/conient/datacomponents/UsersDataComponent.java (file contents):
Revision 1.2 by ajm, Tue Jan 23 00:38:24 2001 UTC vs.
Revision 1.17 by tdb, Thu Feb 14 17:17:00 2002 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 + package uk.org.iscream.cms.conient.datacomponents;
3  
4   //---IMPORTS---
5   import javax.swing.JLabel;
6   import java.awt.GridLayout;
6 import java.util.Date;
7   import java.util.StringTokenizer;
8 + import java.util.Arrays;
9   import javax.swing.JComboBox;
10 < import java.text.DateFormat;
10 > import javax.swing.SwingUtilities;
11 > import uk.org.iscream.cms.server.util.XMLPacket;
12  
13   /**
14   * This component displays the users currently logged
15 < * onto a syst
16 < * extend StringDataComponent, intercepting the value
15 < * to convert it.
15 > * onto a system.  Currently it does this simply by
16 > * putting them all in a JCombobox.
17   *
17 * It uses the JProgressBar to display the value.
18 *
18   * @author  $Author$
19   * @version $Id$
20   */
21 < public class UsersDataComponent extends DataComponent {
21 > public class UsersDataComponent extends VisibleDataComponent {
22  
23   //---FINAL ATTRIBUTES---
24  
# Line 34 | Line 33 | public class UsersDataComponent extends DataComponent
33       * that.
34       *
35       * @param name the friendly name
36 +     * @param attribute the data attribute we look after
37       */
38 <    public UsersDataComponent(String name) {
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 +        setVisible(false);
48      }
49  
50   //---PUBLIC METHODS---
51 +    
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 +        if(!isVisible()) {
60 +            setVisible(true);
61 +        }
62 +            _item.removeAllItems();
63 +            if (_cache.equals(" ")) {
64 +                _item.addItem("no users logged on");
65 +        } else {
66 +            StringTokenizer st = new StringTokenizer(_cache, ";");
67 +            while(st.hasMoreTokens()) {
68 +                _item.addItem(st.nextToken());
69 +            }
70 +        }
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 displays the data.
87 >   /**
88 >     * This takes the packet to obtain the value from, it then performs all
89 >     * approriate conversions and adds this class to the Swing Event
90 >     * Dispatching queue.
91       *
92 <     * In this case all we do is change string and pass it to our
58 <     * super class for displaying.
59 <     *
60 <     * @param value the value for this data component
92 >     * @param packet the XMLPacket to get the data from
93       * @throws DataFormatException if there was a problem converting the data for display
94       */
95 <    public void setValue(String value) throws DataFormatException {
96 <        try {
97 <            if (!_cache.equals(value)) {
98 <                _cache = value;
99 <                _item.removeAllItems();
100 <                StringTokenizer st = new StringTokenizer(value);
101 <                while(st.hasMoreTokens()) {
102 <                    _item.addItem(st.nextToken());
95 >    public void setValue(XMLPacket packet) throws DataFormatException {
96 >        String value = packet.getParam(_attribute);
97 >        // 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 >                    if(count > 1) {
123 >                        valueBuffer.append(" (");
124 >                        valueBuffer.append(count);
125 >                        valueBuffer.append(")");
126 >                    }
127 >                    valueBuffer.append(";");
128                  }
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 +        // we have to add the last one...
141 +        valueBuffer.append(lastToken);
142 +        if(count > 1) {
143 +            valueBuffer.append(" (");
144 +            valueBuffer.append(count);
145 +            valueBuffer.append(");");
146 +        }
147 +        valueBuffer.append(";");
148 +        String sortedValue = valueBuffer.toString();
149 +        try {
150 +            if (!_cache.equals(sortedValue)) {
151 +                _cache = sortedValue;
152 +                SwingUtilities.invokeLater(this);
153 +            }
154          } catch (Exception e) {
155 <            throw new DataFormatException("invalid data type for component ( " + e + ")");
155 >            throw new DataFormatException(sortedValue + " is an invalid data type for " + toString());
156          }
157 <    }        
157 >    }
158 >    
159 >    /**
160 >     * Returns the string showing the packet
161 >     * attribute that the component is looking after
162 >     *
163 >     * @return the packet reference
164 >     */
165 >    public String getPacketAttribute() {
166 >        return _attribute;
167 >    }
168  
169   //---ATTRIBUTES---
170  
171 +    /**
172 +     * The friendly name of this component
173 +     */
174 +    private String _name;
175 +    
176 +    /**
177 +     * The attribute that this component is concerned with
178 +     */
179 +    private String _attribute;
180 +    
181 +    /**
182 +     * Remebers what the last value was, so we
183 +     * only update if we have to.
184 +     */
185      String _cache = "";
186  
187      /**
# Line 85 | Line 190 | public class UsersDataComponent extends DataComponent
190      protected JLabel _label;
191      
192      /**
193 <     * Just a normal label to display our value as a String
193 >     * Just a JComboBox to display the users in
194       */
195      protected JComboBox _item = new JComboBox();
196  
197   //---STATIC ATTRIBUTES---
198  
199 < }
199 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines