KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
30 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
31 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
33 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
34 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
35 import org.apache.directory.ldapstudio.browser.core.model.IValue;
36 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
37 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
38 import org.eclipse.jface.resource.ImageDescriptor;
39 import org.eclipse.jface.viewers.CellEditor;
40 import org.eclipse.jface.viewers.TextCellEditor;
41
42
43 /**
44  *
45  * Abstract base class for value editors that handle string values
46  * withing the table or tree control.
47  *
48  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
49  * @version $Rev$, $Date$
50  */

51 public abstract class AbstractInPlaceStringValueEditor extends TextCellEditor implements IValueEditor
52 {
53
54     /**
55      * @deprecated will be removed soon. Just used to delegate
56      * createValue(), deleteValue() and modifyValue().
57      */

58     private TextValueEditor delegate;
59
60     /** The name of this value editor */
61     private String JavaDoc name;
62
63     /** The image of this value editor */
64     private ImageDescriptor imageDescriptor;
65
66
67     /**
68      * Creates a new instance of AbstractInPlaceStringValueEditor.
69      */

70     protected AbstractInPlaceStringValueEditor()
71     {
72         super();
73         this.delegate = new TextValueEditor();
74     }
75
76
77     /**
78      * Returns true if the user wishes to show raw values rather than
79      * user-friendly values. If true the getDisplayValue() methods
80      * shouldnot modify the value.
81      *
82      * @return true if raw values should be displayed
83      */

84     protected boolean showRawValues()
85     {
86         return BrowserCommonActivator.getDefault().getPreferenceStore()
87             .getBoolean( BrowserCommonConstants.PREFERENCE_SHOW_RAW_VALUES );
88     }
89
90
91     /**
92      * {@inheritDoc}
93      *
94      * This implementation of getDisplayValue() returns a
95      * comma-separated list of all values.
96      */

97     public String JavaDoc getDisplayValue( AttributeHierarchy attributeHierarchy )
98     {
99         if ( attributeHierarchy == null )
100         {
101             return "NULL";
102         }
103
104         List JavaDoc<IValue> valueList = new ArrayList JavaDoc<IValue>();
105         for ( Iterator JavaDoc it = attributeHierarchy.iterator(); it.hasNext(); )
106         {
107             IAttribute attribute = ( IAttribute ) it.next();
108             valueList.addAll( Arrays.asList( attribute.getValues() ) );
109         }
110
111         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
112         for ( Iterator JavaDoc<IValue> it = valueList.iterator(); it.hasNext(); )
113         {
114             IValue value = it.next();
115             sb.append( getDisplayValue( value ) );
116             if ( it.hasNext() )
117                 sb.append( ", " );
118         }
119         return sb.toString();
120     }
121
122
123     /**
124      * {@inheritDoc}
125      *
126      * This implementation just returns the raw value
127      */

128     public String JavaDoc getDisplayValue( IValue value )
129     {
130         Object JavaDoc obj = this.getRawValue( value );
131         return obj == null ? "NULL" : obj.toString();
132     }
133
134
135     /**
136      * {@inheritDoc}
137      *
138      * This implementation returns IValue.EMPTY_xx_VALUE if there are no values
139      * in attributeHierarchy or calls getRawValue(IValue) if attributeHierarchy
140      * contains exactly one value. Otherwise null is returned.
141      */

142     public Object JavaDoc getRawValue( AttributeHierarchy attributeHierarchy )
143     {
144         if ( attributeHierarchy == null )
145         {
146             return null;
147         }
148         else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 0 )
149         {
150             if ( attributeHierarchy.getAttribute().isString() )
151             {
152                 return IValue.EMPTY_STRING_VALUE;
153             }
154             else
155             {
156                 return IValue.EMPTY_BINARY_VALUE;
157             }
158         }
159         else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 1 )
160         {
161             return getRawValue( attributeHierarchy.getAttribute().getValues()[0] );
162         }
163         else
164         {
165             return null;
166         }
167     }
168
169
170     /**
171      * {@inheritDoc}
172      *
173      * This implementation returns the string value
174      * of the given value.
175      */

176     public Object JavaDoc getRawValue( IValue value )
177     {
178         if ( value == null )
179         {
180             return null;
181         }
182         else if ( value.isString() )
183         {
184             return value.getStringValue();
185         }
186         else if ( value.isBinary() )
187         {
188             return isEditable( value.getBinaryValue() ) ? value.getStringValue() : null;
189         }
190         else
191         {
192             return null;
193         }
194     }
195
196
197     /**
198      * Small helper.
199      */

200     private boolean isEditable( byte[] b )
201     {
202         if ( b == null )
203         {
204             return false;
205         }
206
207         for ( int i = 0; i < b.length; i++ )
208         {
209             if ( !( b[i] == '\n' || b[i] == '\r' || ( b[i] >= '\u0020' && b[i] <= '\u007F' ) ) )
210             {
211                 return false;
212             }
213         }
214
215         return true;
216     }
217
218
219     /**
220      * {@inheritDoc}
221      *
222      * This implementation returns the value itself if it is
223      * of type byte[] or a byte[] with the UTF-8 encoded string
224      * value if it is of type String.
225      */

226     public Object JavaDoc getRawValue( IConnection connection, Object JavaDoc value )
227     {
228         if ( value == null )
229         {
230             return null;
231         }
232         else if ( value instanceof String JavaDoc )
233         {
234             return value;
235         }
236         else if ( value instanceof byte[] )
237         {
238             String JavaDoc s = LdifUtils.utf8decode( ( byte[] ) value );
239             for ( int i = 0; i < s.length(); i++ )
240             {
241                 if ( Character.isISOControl( s.charAt( i ) ) && s.charAt( i ) != '\n' && s.charAt( i ) != '\r' )
242                 {
243                     return null;
244                 }
245             }
246             return s;
247         }
248         else
249         {
250             return null;
251         }
252     }
253
254
255     /**
256      * {@inheritDoc}
257      *
258      * This implementation always return the string value
259      * as String.
260      */

261     public Object JavaDoc getStringOrBinaryValue( Object JavaDoc rawValue )
262     {
263         if ( rawValue == null )
264         {
265             return null;
266         }
267         else if ( rawValue instanceof String JavaDoc )
268         {
269             return rawValue;
270         }
271         else
272         {
273             return null;
274         }
275     }
276
277
278     /**
279      * {@inheritDoc}
280      */

281     public CellEditor getCellEditor()
282     {
283         return this;
284     }
285
286
287     /**
288      * {@inheritDoc}
289      */

290     protected Object JavaDoc doGetValue()
291     {
292         return "".equals( text.getText() ) ? null : text.getText();
293     }
294
295
296     /**
297      * {@inheritDoc}
298      */

299     protected void doSetValue( Object JavaDoc value )
300     {
301         if ( value != null && value instanceof IValue.EmptyValue )
302         {
303             value = ( ( IValue.EmptyValue ) value ).getStringValue();
304         }
305         super.doSetValue( value );
306     }
307
308
309     /**
310      * {@inheritDoc}
311      */

312     public void setValueEditorName( String JavaDoc name )
313     {
314         this.name = name;
315     }
316
317
318     /**
319      * {@inheritDoc}
320      */

321     public String JavaDoc getValueEditorName()
322     {
323         return name;
324     }
325
326
327     /**
328      * {@inheritDoc}
329      */

330     public void setValueEditorImageDescriptor( ImageDescriptor imageDescriptor )
331     {
332         this.imageDescriptor = imageDescriptor;
333     }
334
335
336     /**
337      * {@inheritDoc}
338      */

339     public ImageDescriptor getValueEditorImageDescriptor()
340     {
341         return imageDescriptor;
342     }
343
344
345     /**
346      * {@inheritDoc}
347      */

348     public final void createValue( IEntry entry, String JavaDoc attributeDescription, Object JavaDoc newRawValue )
349         throws ModelModificationException
350     {
351         delegate.createValue( entry, attributeDescription, newRawValue );
352     }
353
354
355     /**
356      * {@inheritDoc}
357      */

358     public final void deleteAttribute( AttributeHierarchy ah ) throws ModelModificationException
359     {
360         delegate.deleteAttribute( ah );
361     }
362
363
364     /**
365      * {@inheritDoc}
366      */

367     public final void deleteValue( IValue oldValue ) throws ModelModificationException
368     {
369         delegate.deleteValue( oldValue );
370     }
371
372
373     /**
374      * {@inheritDoc}
375      */

376     public final void modifyValue( IValue oldValue, Object JavaDoc newRawValue ) throws ModelModificationException
377     {
378         delegate.modifyValue( oldValue, newRawValue );
379     }
380
381 }
382
Popular Tags