KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedHashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
34 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
35 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
36 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
37 import org.apache.directory.ldapstudio.browser.core.model.IValue;
38 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeTypeDescription;
39 import org.apache.directory.ldapstudio.browser.core.model.schema.LdapSyntaxDescription;
40 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
41 import org.eclipse.core.runtime.IConfigurationElement;
42 import org.eclipse.core.runtime.IExtension;
43 import org.eclipse.core.runtime.IExtensionPoint;
44 import org.eclipse.core.runtime.IExtensionRegistry;
45 import org.eclipse.core.runtime.IStatus;
46 import org.eclipse.core.runtime.Platform;
47 import org.eclipse.core.runtime.Status;
48 import org.eclipse.jface.resource.ImageDescriptor;
49 import org.eclipse.swt.widgets.Composite;
50 import org.eclipse.ui.plugin.AbstractUIPlugin;
51
52
53 /**
54  * A ValueEditorManager is used to manage value editors. It provides methods to get
55  * the best or alternative value editors for a given attribute or value. It takes
56  * user preferences into account when determine the best value editor. At least
57  * it provides default text and binary value editors.
58  *
59  * The available value editors are specified by the extension point
60  * <code>org.apache.directory.ldapstudio.valueeditors</code>.
61  *
62  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
63  * @version $Rev$, $Date$
64  */

65 public class ValueEditorManager
66 {
67     /** The extension point ID for value editors */
68     private static final String JavaDoc EXTENSION_POINT = "org.apache.directory.ldapstudio.valueeditors";
69
70     /** The composite used to create the value editors **/
71     private Composite parent;
72
73     /**
74      * The value editor explicitly selected by the user. If this
75      * member is not null it is always returned as current value editor.
76      */

77     private IValueEditor userSelectedValueEditor;
78
79     /** The special value editor for multi-valued attributes */
80     private MultivaluedValueEditor multiValuedValueEditor;
81
82     /** The default string editor for single-line values */
83     private IValueEditor defaultStringSingleLineValueEditor;
84
85     /** The default string editor for multi-line values */
86     private IValueEditor defaultStringMultiLineValueEditor;
87
88     /** The default binary editor */
89     private IValueEditor defaultBinaryValueEditor;
90
91     /** A Map wich all available value editors. */
92     private Map JavaDoc<String JavaDoc, IValueEditor> class2ValueEditors;
93
94
95     /**
96      * Creates a new instance of ValueEditorManager.
97      *
98      * @param parent the composite used to create the value editors
99      */

100     public ValueEditorManager( Composite parent )
101     {
102         this.parent = parent;
103         userSelectedValueEditor = null;
104
105         // init value editor map
106
class2ValueEditors = new HashMap JavaDoc<String JavaDoc, IValueEditor>();
107         Collection JavaDoc<IValueEditor> valueEditors = createValueEditors( parent );
108         for ( IValueEditor valueEditor : valueEditors )
109         {
110             class2ValueEditors.put( valueEditor.getClass().getName(), valueEditor );
111         }
112
113         // special case: multivalued editor
114
multiValuedValueEditor = new MultivaluedValueEditor( this.parent, this );
115         multiValuedValueEditor.setValueEditorName( "Mulitvalued Editor" );
116         multiValuedValueEditor.setValueEditorImageDescriptor( BrowserCommonActivator.getDefault().getImageDescriptor(
117             BrowserCommonConstants.IMG_MULTIVALUEDEDITOR ) );
118
119         // get default editors from value editor map
120
defaultStringSingleLineValueEditor = class2ValueEditors.get( InPlaceTextValueEditor.class.getName() );
121         defaultStringMultiLineValueEditor = class2ValueEditors.get( TextValueEditor.class.getName() );
122         defaultBinaryValueEditor = class2ValueEditors.get( HexValueEditor.class.getName() );
123     }
124
125
126     /**
127      * Disposes all value editors.
128      */

129     public void dispose()
130     {
131         if ( this.parent != null )
132         {
133             this.userSelectedValueEditor = null;
134             this.multiValuedValueEditor.dispose();
135             this.defaultStringSingleLineValueEditor.dispose();
136             this.defaultStringMultiLineValueEditor.dispose();
137             this.defaultBinaryValueEditor.dispose();
138
139             for ( Iterator JavaDoc it = this.class2ValueEditors.values().iterator(); it.hasNext(); )
140             {
141                 IValueEditor vp = ( IValueEditor ) it.next();
142                 vp.dispose();
143             }
144
145             this.parent = null;
146         }
147     }
148
149
150     /**
151      * Sets the value editor explicitly selected by the user. Set
152      * userSelectedValueEditor to null to remove the selection.
153      *
154      * @param userSelectedValueEditor the user selected value editor, may be null.
155      */

156     public void setUserSelectedValueEditor( IValueEditor userSelectedValueEditor )
157     {
158         this.userSelectedValueEditor = userSelectedValueEditor;
159     }
160
161
162     /**
163      * Returns the current (best) value editor for the given attribute.
164      *
165      * <ol>
166      * <li>If a user selected value editor is selected, this is returned.
167      * <li>If a specific value editor is defined for the attribute type this
168      * value editor is returned. See preferences.
169      * <li>If a specific value editor is defined for the attribute's syntax this
170      * value editor is returned. See preferences.
171      * <li>Otherwise a default value editor is returned. If the attribute is
172      * binary the default Hex Editor is returned. Otherwise the default
173      * Text Editor is returned.
174      * </ol>
175      *
176      * @param schema the schema
177      * @param attributeType the attribute type
178      * @return the current value editor
179      */

180     public IValueEditor getCurrentValueEditor( Schema schema, String JavaDoc attributeType )
181     {
182
183         // check user-selected (forced) value editor
184
if ( this.userSelectedValueEditor != null )
185         {
186             return this.userSelectedValueEditor;
187         }
188
189         // check attribute preferences
190
AttributeTypeDescription atd = schema.getAttributeTypeDescription( attributeType );
191         Map JavaDoc attributeValueEditorMap = BrowserCommonActivator.getDefault().getValueEditorsPreferences().getAttributeValueEditorMap();
192         if ( atd.getNumericOID() != null && attributeValueEditorMap.containsKey( atd.getNumericOID().toLowerCase() ) )
193         {
194             return ( IValueEditor ) this.class2ValueEditors.get( attributeValueEditorMap.get( atd.getNumericOID()
195                 .toLowerCase() ) );
196         }
197         String JavaDoc[] names = atd.getNames();
198         for ( int i = 0; i < names.length; i++ )
199         {
200             if ( attributeValueEditorMap.containsKey( names[i].toLowerCase() ) )
201             {
202                 return ( IValueEditor ) this.class2ValueEditors.get( attributeValueEditorMap.get( names[i]
203                     .toLowerCase() ) );
204             }
205         }
206
207         // check syntax preferences
208
LdapSyntaxDescription lsd = atd.getSyntaxDescription();
209         Map JavaDoc syntaxValueEditorMap = BrowserCommonActivator.getDefault().getValueEditorsPreferences().getSyntaxValueEditorMap();
210         if ( lsd.getNumericOID() != null && syntaxValueEditorMap.containsKey( lsd.getNumericOID().toLowerCase() ) )
211         {
212             return ( IValueEditor ) this.class2ValueEditors.get( syntaxValueEditorMap.get( lsd.getNumericOID()
213                 .toLowerCase() ) );
214         }
215
216         // return default
217
if ( lsd.isBinary() )
218         {
219             return this.defaultBinaryValueEditor;
220         }
221         else
222         {
223             return this.defaultStringSingleLineValueEditor;
224         }
225
226     }
227
228
229     /**
230      * Returns the current (best) value editor for the given attribute.
231      *
232      * @param entry the entry
233      * @param attributeType the attributge type
234      * @return the current value editor
235      * @see #getCurrentValueEditor( Schema, String )
236      */

237     public IValueEditor getCurrentValueEditor( IEntry entry, String JavaDoc attributeType )
238     {
239         return getCurrentValueEditor( entry.getConnection().getSchema(), attributeType );
240     }
241
242
243     /**
244      * Returns the current (best) value editor for the given value.
245      *
246      * @param value the value
247      * @return the current value editor
248      * @see #getCurrentValueEditor( Schema, String )
249      */

250     public IValueEditor getCurrentValueEditor( IValue value )
251     {
252
253         IValueEditor ve = this.getCurrentValueEditor( value.getAttribute().getEntry(), value.getAttribute()
254             .getDescription() );
255
256         // here the value is known, we can check for single-line or multi-line
257
if ( ve == this.defaultStringSingleLineValueEditor )
258         {
259             if ( value.getStringValue().indexOf( '\n' ) == -1 && value.getStringValue().indexOf( '\r' ) == -1 )
260             {
261                 ve = this.defaultStringSingleLineValueEditor;
262             }
263             else
264             {
265                 ve = this.defaultStringMultiLineValueEditor;
266             }
267         }
268
269         return ve;
270     }
271
272
273     /**
274      * Returns the current (best) value editor for the given attribute.
275      *
276      * @param attributeHierarchy the attribute hierarchy
277      * @return the current value editor
278      * @see #getCurrentValueEditor( Schema, String )
279      */

280     public IValueEditor getCurrentValueEditor( AttributeHierarchy attributeHierarchy )
281     {
282         if ( attributeHierarchy == null )
283         {
284             return null;
285         }
286         else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 0 )
287         {
288             return this.getCurrentValueEditor( attributeHierarchy.getAttribute().getEntry(), attributeHierarchy
289                 .getAttribute().getDescription() );
290         }
291         else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 1 )
292         {
293             // special case objectClass and RDN: always return MV-editor
294
// perhaps this should be moved somewhere else
295
if ( attributeHierarchy.getAttribute().isObjectClassAttribute() )
296             {
297                 return this.multiValuedValueEditor;
298             }
299             if ( attributeHierarchy.getAttribute().getValues()[0].isRdnPart() )
300             {
301                 return this.multiValuedValueEditor;
302             }
303
304             return this.getCurrentValueEditor( attributeHierarchy.getAttribute().getValues()[0] );
305         }
306         else
307         {
308             return this.multiValuedValueEditor;
309         }
310     }
311
312
313     /**
314      * Returns alternative value editors for the given attribute. For now these
315      * are the three default editors.
316      *
317      * @param entry the entry
318      * @param attributeName the attribute
319      * @return alternative value editors
320      */

321     public IValueEditor[] getAlternativeValueEditors( IEntry entry, String JavaDoc attributeName )
322     {
323         Schema schema = entry.getConnection().getSchema();
324         return getAlternativeValueEditors( schema, attributeName );
325     }
326
327
328     /**
329      * Returns alternative value editors for the given attribute. For now these
330      * are the three default editors.
331      *
332      * @param schema the schema
333      * @param attributeName the attribute
334      * @return alternative value editors
335      */

336     public IValueEditor[] getAlternativeValueEditors( Schema schema, String JavaDoc attributeName )
337     {
338         List JavaDoc<IValueEditor> alternativeList = new ArrayList JavaDoc<IValueEditor>();
339
340         AttributeTypeDescription atd = schema.getAttributeTypeDescription( attributeName );
341
342         if ( atd.getSyntaxDescription().isBinary() )
343         {
344             alternativeList.add( this.defaultBinaryValueEditor );
345             alternativeList.add( this.defaultStringSingleLineValueEditor );
346             alternativeList.add( this.defaultStringMultiLineValueEditor );
347         }
348         else if ( atd.getSyntaxDescription().isString() )
349         {
350             alternativeList.add( this.defaultStringSingleLineValueEditor );
351             alternativeList.add( this.defaultStringMultiLineValueEditor );
352             alternativeList.add( this.defaultBinaryValueEditor );
353         }
354
355         alternativeList.add( this.multiValuedValueEditor );
356
357         alternativeList.remove( getCurrentValueEditor( schema, attributeName ) );
358
359         return (org.apache.directory.ldapstudio.valueeditors.IValueEditor[] ) alternativeList.toArray( new IValueEditor[alternativeList.size()] );
360     }
361
362
363     /**
364      * Returns alternative value editors for the given value. For now these
365      * are the three default editors.
366      *
367      * @param value the value
368      * @return lternative value editors
369      */

370     public IValueEditor[] getAlternativeValueEditors( IValue value )
371     {
372         List JavaDoc<IValueEditor> alternativeList = new ArrayList JavaDoc<IValueEditor>();
373
374         if ( value.isBinary() )
375         {
376             alternativeList.add( this.defaultBinaryValueEditor );
377             alternativeList.add( this.defaultStringSingleLineValueEditor );
378             alternativeList.add( this.defaultStringMultiLineValueEditor );
379         }
380         else if ( value.isString() )
381         {
382             alternativeList.add( this.defaultStringSingleLineValueEditor );
383             alternativeList.add( this.defaultStringMultiLineValueEditor );
384             alternativeList.add( this.defaultBinaryValueEditor );
385         }
386
387         alternativeList.add( this.multiValuedValueEditor );
388
389         alternativeList.remove( getCurrentValueEditor( value ) );
390
391         return (org.apache.directory.ldapstudio.valueeditors.IValueEditor[] ) alternativeList.toArray( new IValueEditor[alternativeList.size()] );
392     }
393
394
395     /**
396      * Returns alternative value editors for the given value. For now these
397      * are the three default editors.
398      *
399      * @param ah the attribute hierarchy
400      * @return alternative value editors
401      */

402     public IValueEditor[] getAlternativeValueEditors( AttributeHierarchy ah )
403     {
404         if ( ah == null )
405         {
406             return new IValueEditor[0];
407         }
408         else if ( ah.size() == 1 && ah.getAttribute().getValueSize() == 0 )
409         {
410             return this.getAlternativeValueEditors( ah.getAttribute().getEntry(), ah.getAttribute().getDescription() );
411         }
412         else if ( ah.size() == 1 && ah.getAttribute().getValueSize() == 1 )
413         {
414
415             // special case objectClass and RDN: no alternative to the MV-Editor
416
// perhaps this should be moved somewhere else
417
if ( ah.getAttribute().isObjectClassAttribute() )
418             {
419                 return new IValueEditor[0];
420             }
421             if ( ah.getAttribute().getValues()[0].isRdnPart() )
422             {
423                 return new IValueEditor[0];
424             }
425
426             return this.getAlternativeValueEditors( ah.getAttribute().getValues()[0] );
427         }
428         else
429         /* if(attribute.getValueSize() > 1) */{
430             return new IValueEditor[0];
431         }
432     }
433
434
435     /**
436      * Returns all available value editors.
437      *
438      * @return all available value editors
439      */

440     public IValueEditor[] getAllValueEditors()
441     {
442         // use a set to avoid double entries
443
Set JavaDoc<IValueEditor> list = new LinkedHashSet JavaDoc<IValueEditor>();
444
445         list.add( this.defaultStringSingleLineValueEditor );
446         list.add( this.defaultStringMultiLineValueEditor );
447         list.add( defaultBinaryValueEditor );
448
449         list.addAll( this.class2ValueEditors.values() );
450
451         list.add( this.multiValuedValueEditor );
452
453         return list.toArray( new IValueEditor[list.size()] );
454     }
455
456
457     /**
458      * Returns the default binary editor (a HexEditor).
459      *
460      * @return the default binary editor
461      */

462     public IValueEditor getDefaultBinaryValueEditor()
463     {
464         return defaultBinaryValueEditor;
465     }
466
467
468     /**
469      * Returns the default string editor (a TextEditor).
470      *
471      * @return the default string editor
472      */

473     public IValueEditor getDefaultStringValueEditor()
474     {
475         return defaultStringMultiLineValueEditor;
476     }
477
478
479     /**
480      * Returns the multi-valued editor.
481      *
482      * @return the multi-valued editor
483      */

484     public MultivaluedValueEditor getMultiValuedValueEditor()
485     {
486         return multiValuedValueEditor;
487     }
488
489
490     /**
491      * Creates and returns the value editors specified by value editors extensions.
492      *
493      * @param parent the parent composite
494      * @return the value editors
495      */

496     private Collection JavaDoc<IValueEditor> createValueEditors( Composite parent )
497     {
498         Collection JavaDoc<IValueEditor> valueEditors = new ArrayList JavaDoc<IValueEditor>();
499
500         Collection JavaDoc<ValueEditorExtension> valueEditorProxys = getValueEditorProxys();
501         for ( ValueEditorExtension proxy : valueEditorProxys )
502         {
503             try
504             {
505                 IValueEditor valueEditor = ( IValueEditor ) proxy.member.createExecutableExtension( "class" );
506                 valueEditor.create( parent );
507                 valueEditor.setValueEditorName( proxy.name );
508                 valueEditor.setValueEditorImageDescriptor( proxy.icon );
509                 valueEditors.add( valueEditor );
510             }
511             catch ( Exception JavaDoc e )
512             {
513                 BrowserCommonActivator.getDefault().getLog().log(
514                     new Status( IStatus.ERROR, BrowserCommonActivator.PLUGIN_ID, 1, "Unable to create ValueEditor "
515                         + proxy.className, e ) );
516             }
517         }
518
519         return valueEditors;
520     }
521
522
523     /**
524      * Returns all value editor proxies specified by value editor extensions.
525      *
526      * @return the value editor proxies
527      */

528     public static Collection JavaDoc<ValueEditorExtension> getValueEditorProxys()
529     {
530         Collection JavaDoc<ValueEditorExtension> valueEditorProxies = new ArrayList JavaDoc<ValueEditorExtension>();
531
532         IExtensionRegistry registry = Platform.getExtensionRegistry();
533         IExtensionPoint extensionPoint = registry.getExtensionPoint( EXTENSION_POINT );
534         IConfigurationElement[] members = extensionPoint.getConfigurationElements();
535
536         // For each extension:
537
for ( int m = 0; m < members.length; m++ )
538         {
539             ValueEditorExtension proxy = new ValueEditorExtension();
540             valueEditorProxies.add( proxy );
541
542             IConfigurationElement member = members[m];
543             IExtension extension = member.getDeclaringExtension();
544             String JavaDoc extendingPluginId = extension.getNamespaceIdentifier();
545
546             proxy.member = member;
547             proxy.name = member.getAttribute( "name" );
548             String JavaDoc iconPath = member.getAttribute( "icon" );
549             proxy.icon = AbstractUIPlugin.imageDescriptorFromPlugin( extendingPluginId, iconPath );
550             if ( proxy.icon == null )
551             {
552                 proxy.icon = ImageDescriptor.getMissingImageDescriptor();
553             }
554             proxy.className = member.getAttribute( "class" );
555
556             IConfigurationElement[] children = member.getChildren();
557             for ( int c = 0; c < children.length; c++ )
558             {
559                 IConfigurationElement element = children[c];
560                 String JavaDoc type = element.getName();
561                 if ( "syntax".equals( type ) )
562                 {
563                     String JavaDoc syntaxOID = element.getAttribute( "syntaxOID" );
564                     proxy.syntaxOids.add( syntaxOID );
565                 }
566                 else if ( "attribute".equals( type ) )
567                 {
568                     String JavaDoc attributeType = element.getAttribute( "attributeType" );
569                     proxy.attributeTypes.add( attributeType );
570                 }
571             }
572         }
573
574         return valueEditorProxies;
575     }
576
577     /**
578      * This class is a bean to hold the data defined in value editor extension
579      *
580      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
581      * @version $Rev$, $Date$
582      */

583     public static class ValueEditorExtension
584     {
585         
586         /** The name. */
587         public String JavaDoc name = null;
588         
589         /** The icon. */
590         public ImageDescriptor icon = null;
591         
592         /** The class name. */
593         public String JavaDoc className = null;
594         
595         /** The syntax oids. */
596         public Collection JavaDoc<String JavaDoc> syntaxOids = new ArrayList JavaDoc<String JavaDoc>( 3 );
597         
598         /** The attribute types. */
599         public Collection JavaDoc<String JavaDoc> attributeTypes = new ArrayList JavaDoc<String JavaDoc>( 3 );
600
601         /** The configuration element. */
602         private IConfigurationElement member = null;
603     }
604
605 }
606
Popular Tags