KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > valueeditors > AbstractDialogStringValueEditor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.valueeditors;
22
23
24 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
25 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
26 import org.apache.directory.ldapstudio.browser.core.jobs.CreateValuesJob;
27 import org.apache.directory.ldapstudio.browser.core.jobs.DeleteAttributesValueJob;
28 import org.apache.directory.ldapstudio.browser.core.jobs.ModifyValueJob;
29 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
30 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33 import org.apache.directory.ldapstudio.browser.core.model.IValue;
34 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
35 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
36
37
38 /**
39  *
40  * Abstract base class for value editors that handle string values
41  * in a dialog.
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public abstract class AbstractDialogStringValueEditor extends AbstractDialogValueEditor
47 {
48
49     /**
50      * Creates a new instance of AbstractDialogStringValueEditor.
51      */

52     protected AbstractDialogStringValueEditor()
53     {
54         super();
55     }
56
57
58     /**
59      * {@inheritDoc}
60      *
61      * This implementation just returns the raw value
62      */

63     public String JavaDoc getDisplayValue( IValue value )
64     {
65         Object JavaDoc obj = this.getRawValue( value );
66         return obj == null ? "NULL" : obj.toString();
67     }
68
69
70     /**
71      * {@inheritDoc}
72      *
73      * This implementation returns IValue.EMPTY_STRING_VALUE if
74      * the attribute is string.
75      */

76     protected Object JavaDoc getEmptyRawValue( IAttribute attribute )
77     {
78         if ( attribute.isString() )
79         {
80             return IValue.EMPTY_STRING_VALUE;
81         }
82         else
83         {
84             return IValue.EMPTY_BINARY_VALUE;
85         }
86     }
87
88
89     /**
90      * {@inheritDoc}
91      *
92      * This implementation returns the string value
93      * of the given value.
94      */

95     public Object JavaDoc getRawValue( IValue value )
96     {
97         if ( value == null )
98         {
99             return null;
100         }
101         else if ( value.isString() )
102         {
103             return value.getStringValue();
104         }
105         else if ( value.isBinary() )
106         {
107             return isEditable( value.getBinaryValue() ) ? value.getStringValue() : null;
108         }
109         else
110         {
111             return null;
112         }
113     }
114
115
116     /**
117      * {@inheritDoc}
118      *
119      * This implementation returns the value itself if it is
120      * of type String. If the value is of type byte[] then the binary
121      * data is converted to a String using UTF-8 encoding.
122      */

123     public Object JavaDoc getRawValue( IConnection connection, Object JavaDoc value )
124     {
125         if ( value == null )
126         {
127             return null;
128         }
129         else if ( value instanceof String JavaDoc )
130         {
131             return value;
132         }
133         else if ( value instanceof byte[] )
134         {
135             String JavaDoc s = LdifUtils.utf8decode( ( byte[] ) value );
136             for ( int i = 0; i < s.length(); i++ )
137             {
138                 if ( Character.isISOControl( s.charAt( i ) ) && s.charAt( i ) != '\n' && s.charAt( i ) != '\r' )
139                 {
140                     return null;
141                 }
142             }
143             return s;
144         }
145         else
146         {
147             return null;
148         }
149     }
150
151
152     /**
153      * Small helper.
154      */

155     private boolean isEditable( byte[] b )
156     {
157         if ( b == null )
158         {
159             return false;
160         }
161
162         for ( int i = 0; i < b.length; i++ )
163         {
164             if ( !( b[i] == '\n' || b[i] == '\r' || ( b[i] >= '\u0020' && b[i] <= '\u007F' ) ) )
165             {
166                 return false;
167             }
168         }
169
170         return true;
171     }
172
173
174     /**
175      * {@inheritDoc}
176      *
177      * This implementation always return the string value
178      * as String.
179      */

180     public Object JavaDoc getStringOrBinaryValue( Object JavaDoc rawValue )
181     {
182         if ( rawValue == null )
183         {
184             return null;
185         }
186         else if ( rawValue instanceof String JavaDoc )
187         {
188             return rawValue;
189         }
190         else
191         {
192             return null;
193         }
194     }
195
196
197     /**
198      * {@inheritDoc}
199      */

200     public final void createValue( IEntry entry, String JavaDoc attributeDescription, Object JavaDoc newRawValue )
201         throws ModelModificationException
202     {
203         if ( entry != null && attributeDescription != null && newRawValue != null && newRawValue instanceof String JavaDoc )
204         {
205             if ( entry.getAttribute( attributeDescription ) != null )
206             {
207                 this.modify( entry.getAttribute( attributeDescription ), newRawValue );
208             }
209             else
210             {
211                 EventRegistry.suspendEventFireingInCurrentThread();
212                 IAttribute attribute = new Attribute( entry, attributeDescription );
213                 entry.addAttribute( attribute );
214                 EventRegistry.resumeEventFireingInCurrentThread();
215
216                 Object JavaDoc newValue;
217                 if ( entry.getConnection().getSchema().getAttributeTypeDescription( attributeDescription )
218                     .getSyntaxDescription().isString() )
219                 {
220                     newValue = ( String JavaDoc ) newRawValue;
221                 }
222                 else
223                 {
224                     newValue = LdifUtils.utf8encode( ( String JavaDoc ) newRawValue );
225                 }
226
227                 new CreateValuesJob( attribute, newValue ).execute();
228             }
229         }
230     }
231
232
233     private final void modify( IAttribute attribute, Object JavaDoc newRawValue ) throws ModelModificationException
234     {
235         if ( attribute != null && newRawValue != null && newRawValue instanceof String JavaDoc )
236         {
237             if ( attribute.getValueSize() == 0 )
238             {
239                 String JavaDoc newValue = ( String JavaDoc ) newRawValue;
240                 new CreateValuesJob( attribute, newValue ).execute();
241             }
242             else if ( attribute.getValueSize() == 1 )
243             {
244                 this.modifyValue( attribute.getValues()[0], newRawValue );
245             }
246         }
247     }
248
249
250     /**
251      * {@inheritDoc}
252      */

253     public final void modifyValue( IValue oldValue, Object JavaDoc newRawValue ) throws ModelModificationException
254     {
255         if ( oldValue != null && newRawValue != null && newRawValue instanceof String JavaDoc )
256         {
257
258             String JavaDoc newValue = ( String JavaDoc ) newRawValue;
259             IAttribute attribute = oldValue.getAttribute();
260             if ( !oldValue.getStringValue().equals( newValue ) )
261             {
262                 if ( oldValue.isEmpty() )
263                 {
264                     EventRegistry.suspendEventFireingInCurrentThread();
265                     attribute.deleteEmptyValue();
266                     EventRegistry.resumeEventFireingInCurrentThread();
267                     new CreateValuesJob( attribute, newValue ).execute();
268                 }
269                 else
270                 {
271                     new ModifyValueJob( attribute, oldValue, newValue ).execute();
272                 }
273             }
274         }
275     }
276
277
278     /**
279      * {@inheritDoc}
280      */

281     public final void deleteAttribute( AttributeHierarchy ah ) throws ModelModificationException
282     {
283         if ( ah != null )
284         {
285             new DeleteAttributesValueJob( ah ).execute();
286         }
287     }
288
289
290     /**
291      * {@inheritDoc}
292      */

293     public final void deleteValue( IValue oldValue ) throws ModelModificationException
294     {
295         if ( oldValue != null )
296         {
297             new DeleteAttributesValueJob( oldValue ).execute();
298         }
299     }
300
301 }
302
Popular Tags