KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > viewer > tableviewer > AttributeValue


1 package com.ca.directory.jxplorer.viewer.tableviewer;
2
3 import com.ca.directory.jxplorer.editor.*;
4 import com.ca.commons.cbutil.CBIntText;
5
6 import java.util.logging.Logger JavaDoc;
7
8
9 /**
10  * TableAttributeEditor utility class; encapsulates the idea of
11  * an editable (String) attribute value, as displayed in a table.
12  * It also checks to see if an attribute is Binary, and sets a
13  * 'binary' flag as a warning to cell editors to use special
14  * handling.
15  */

16  
17 public class AttributeValue implements editablebinary, editablestring
18 {
19     public Object JavaDoc value;
20     Object JavaDoc backup;
21     boolean changed;
22     boolean binary;
23     String JavaDoc id;
24     String JavaDoc[] options = null;
25
26     private final static Logger JavaDoc log = Logger.getLogger(AttributeValue.class.getName());
27
28     /**
29      * Whether this is a naming value.
30      */

31      
32     boolean naming;
33     
34     /**
35      * initialise with an object, take a backup, and set changed to false
36      */

37     public AttributeValue(String JavaDoc ID, Object JavaDoc v)
38     {
39         id = ID;
40         value = v;
41         backup = v;
42         changed=false;
43         naming=false;
44         testBinary();
45     }
46
47     /**
48      * sets whether this value is a naming value (e.g. the 'Smith' in cn=Smith).
49      */

50      
51     public void setNamingStatus(boolean state)
52     {
53         naming = state;
54     }
55
56     /**
57      * Returns whether this value is a naming value.
58      */

59      
60     public boolean isNaming()
61     {
62         return naming;
63     }
64
65     /**
66      * Whether the Attribute Value has been given a set of
67      * suggested values.
68      */

69         
70     public boolean hasOptions() { return (options!=null); }
71     
72     /**
73      * adds a list of suggested options to the Attribute Value...
74      */

75      
76     public void setOptions(String JavaDoc[] ops)
77     {
78         options = ops;
79     }
80
81     /**
82      * adds a list of suggested options to the Attribute Value...
83      */

84      
85     public String JavaDoc[] getOptions() { return options; }
86
87     
88     /**
89      * update object
90      */

91     public void update(Object JavaDoc data)
92     {
93         value = data;
94         testBinary();
95         if (binary==false)
96         {
97             value = getStringValue(); // special space handling...
98
}
99         changed = (value != backup); // if value not the same as original, it's changed!
100

101     }
102     
103     /**
104      * synonym for update, used for EditableBinary interface...
105      */

106     public void setValue(byte[] b)
107     {
108         update(b);
109     }
110     
111     
112     /**
113      * synonym for update, used for EditableString interface...
114      *
115      */

116     public void setStringValue(String JavaDoc b)
117     {
118         update(b);
119     }
120     
121     
122     public byte[] getValue()
123     {
124         if (binary)
125         {
126             if (value instanceof String JavaDoc) // may happen if it's been initialised with an empty string.
127
{
128                 log.warning("warning - Attribute Value " + value + " mis represented as byte array data");
129                 return null; //TE this was commented out, but it seems to fix the problem of clicking reset which disables the binary fields until submit is clicked.
130
}
131             else
132                 return ((byte[]) value);
133         }
134         else
135             return null;
136     }
137
138      
139     /**
140      *
141      */

142
143     public String JavaDoc getStringValue()
144     {
145         if (binary == false)
146         {
147             String JavaDoc val = ((String JavaDoc) value);
148             if (naming) // special handling for spaces - bug 4886
149
{
150                 int len = val.length();
151                 val = val.trim();
152                 if (val.length() == 0 && len>0)
153                     val = " ";
154             }
155             return val;
156         }
157         else
158             return null;
159     }
160
161
162
163     /**
164      * reset the value back to what it was initialised with.
165      */

166     public void reset()
167     {
168         value = backup;
169         changed = false;
170         testBinary();
171     }
172     
173     /**
174      * sets the binary-ness of the attribute (this may be known externally
175      * from schema).
176      */

177     public void setBinary(boolean bin)
178     {
179         binary = bin;
180         if (bin==true && value instanceof String JavaDoc)
181             value = null;
182     }
183     
184     /**
185      * Checks whether the object is binary (actually, it checks if
186      * the object <b>isn't</b> a String...)
187      */

188     public boolean testBinary()
189     {
190         if (binary==true) return true; // once it's set to binary, it stays that way.
191

192         binary = (value==null)?false:((value instanceof String JavaDoc)?false:true);
193         return binary;
194     }
195
196     public boolean isEmpty()
197     {
198         if (value==null) return true;
199         if (binary)
200         {
201             if (value instanceof byte[])
202                 if (((byte[])value).length == 0)
203                     return true; // XXX don't believe this code ever runs...
204

205             return false;
206         }
207         
208         return (value.toString().length()==0);
209     }
210
211     /**
212      * Returns value as string. NB. - returns null values and empty
213      * Strings as 1 character blanks, in order to get buggy Swing
214      * printing junk to work.
215      */

216      
217     public String JavaDoc toString()
218     {
219         if (value == null) return " "; // XXX Hack to get Broken swing printing working
220

221         //TE: moved this line of code to before the following one...seems to fix the displaying of
222
// '(binary Value)' to empty attribute values when the Reset button is clicked (bug 2319).
223
if (value.toString().length() == 0) return " "; // XXX Hack to get Broken swing printing working
224

225         if (binary) return CBIntText.get("(non string data)");
226
227         return value.toString();
228     }
229     
230     public String JavaDoc getID() { return id; }
231     public boolean changed() { return changed; }
232     public Object JavaDoc value() { return value; }
233     public Object JavaDoc backup() { return backup; }
234     public boolean isBinary() { return binary; }
235 }
Popular Tags