KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > ValueEditorsPreferences


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.browser.common;
22
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeValueProviderRelation;
28 import org.apache.directory.ldapstudio.browser.core.model.schema.SyntaxValueProviderRelation;
29 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
30
31 import org.eclipse.jface.preference.IPreferenceStore;
32
33
34 /**
35  * This class is used to manage and access the preferences of the Value Editors Plugin.
36  *
37  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
38  * @version $Rev$, $Date$
39  */

40 public class ValueEditorsPreferences
41 {
42
43     /** The attribute value editor cache. */
44     private Map JavaDoc<String JavaDoc, String JavaDoc> attributeValueEditorCache;
45
46     /** The syntax value editor cache. */
47     private Map JavaDoc<String JavaDoc, String JavaDoc> syntaxValueEditorCache;
48
49     
50     /**
51      * Gets a Map containing all the Attribute Value Editors.
52      *
53      * @return
54      * a Map containing all the Attribute Value Editors
55      */

56     public Map JavaDoc getAttributeValueEditorMap()
57     {
58         if ( attributeValueEditorCache == null )
59         {
60             attributeValueEditorCache = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
61             AttributeValueProviderRelation[] relations = getAttributeValueProviderRelations();
62             for ( int i = 0; i < relations.length; i++ )
63             {
64                 if ( relations[i].getAttributeNumericOidOrType() != null )
65                 {
66                     attributeValueEditorCache.put( relations[i].getAttributeNumericOidOrType()
67                         .toLowerCase(), relations[i].getValueProviderClassname() );
68                 }
69             }
70         }
71         return attributeValueEditorCache;
72     }
73
74
75     /**
76      * Gets an array containing all the Attribute Value Provider Relations.
77      *
78      * @return
79      * an array containing all the Attribute Value Provider Relations
80      */

81     public AttributeValueProviderRelation[] getAttributeValueProviderRelations()
82     {
83         AttributeValueProviderRelation[] avpr = ( AttributeValueProviderRelation[] ) load( BrowserCommonConstants.PREFERENCE_ATTRIBUTE_VALUEPROVIDER_RELATIONS );
84         return avpr;
85     }
86
87
88     /**
89      * Sets the Attribute Value Provider Relations.
90      *
91      * @param attributeValueProviderRelations
92      * an array containing all the Attribute Value Provider Relations
93      */

94     public void setAttributeValueProviderRelations( AttributeValueProviderRelation[] attributeValueProviderRelations )
95     {
96         store( BrowserCommonConstants.PREFERENCE_ATTRIBUTE_VALUEPROVIDER_RELATIONS, attributeValueProviderRelations );
97         attributeValueEditorCache = null;
98     }
99
100
101     /**
102      * Gets the default Attribute Value Provider Relations.
103      *
104      * @return
105      * an array containing all the default Attribute Value Provider Relations
106      */

107     public AttributeValueProviderRelation[] getDefaultAttributeValueProviderRelations()
108     {
109         AttributeValueProviderRelation[] avpr = ( AttributeValueProviderRelation[] ) loadDefault( BrowserCommonConstants.PREFERENCE_ATTRIBUTE_VALUEPROVIDER_RELATIONS );
110         return avpr;
111     }
112
113
114     /**
115      * Sets the default Attribute Value Provider Relations.
116      *
117      * @param attributeValueProviderRelations
118      * an array containing all the default Attribute Value Provider Relations
119      */

120     public void setDefaultAttributeValueProviderRelations(
121         AttributeValueProviderRelation[] attributeValueProviderRelations )
122     {
123         storeDefault( BrowserCommonConstants.PREFERENCE_ATTRIBUTE_VALUEPROVIDER_RELATIONS, attributeValueProviderRelations );
124     }
125
126
127
128     /**
129      * Gets a Map containing all the Syntax Value Editors.
130      *
131      * @return
132      * a Map containing all the Syntax Value Editors
133      */

134     public Map JavaDoc getSyntaxValueEditorMap()
135     {
136         if ( syntaxValueEditorCache == null )
137         {
138             syntaxValueEditorCache = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
139             SyntaxValueProviderRelation[] relations = getSyntaxValueProviderRelations();
140             for ( int i = 0; i < relations.length; i++ )
141             {
142                 if ( relations[i].getSyntaxOID() != null )
143                 {
144                     syntaxValueEditorCache.put( relations[i].getSyntaxOID().toLowerCase(), relations[i]
145                         .getValueProviderClassname() );
146                 }
147             }
148         }
149         return syntaxValueEditorCache;
150     }
151
152
153     /**
154      * Sets the Syntax Value Provider Relations.
155      *
156      * @param syntaxValueProviderRelations
157      * an array containing the Syntax Value Provider Relations to set
158      */

159     public void setSyntaxValueProviderRelations( SyntaxValueProviderRelation[] syntaxValueProviderRelations )
160     {
161         store( BrowserCommonConstants.PREFERENCE_SYNTAX_VALUEPROVIDER_RELATIONS, syntaxValueProviderRelations );
162         syntaxValueEditorCache = null;
163     }
164
165
166     /**
167      * Gets an array containing all the Syntax Value Provider Relations
168      *
169      * @return
170      * an array containing all the Syntax Value Provider Relations
171      */

172     public SyntaxValueProviderRelation[] getSyntaxValueProviderRelations()
173     {
174         SyntaxValueProviderRelation[] svpr = ( SyntaxValueProviderRelation[] ) load( BrowserCommonConstants.PREFERENCE_SYNTAX_VALUEPROVIDER_RELATIONS );
175         return svpr;
176     }
177
178
179     /**
180      * Gets an array containing all the default Syntax Value Provider Relations
181      *
182      * @return
183      * an array containing all the default Syntax Value Provider Relations
184      */

185     public SyntaxValueProviderRelation[] getDefaultSyntaxValueProviderRelations()
186     {
187         SyntaxValueProviderRelation[] svpr = ( SyntaxValueProviderRelation[] ) loadDefault( BrowserCommonConstants.PREFERENCE_SYNTAX_VALUEPROVIDER_RELATIONS );
188         return svpr;
189     }
190
191
192     /**
193      * Sets the default Syntax Value Provider Relations.
194      *
195      * @param syntaxValueProviderRelations
196      * an array containing the default Syntax Value Provider Relations to set
197      */

198     public void setDefaultSyntaxValueProviderRelations( SyntaxValueProviderRelation[] syntaxValueProviderRelations )
199     {
200         storeDefault( BrowserCommonConstants.PREFERENCE_SYNTAX_VALUEPROVIDER_RELATIONS, syntaxValueProviderRelations );
201     }
202
203
204     /**
205      * Loads the current value of the string-valued property with the given name.
206      *
207      * @param key
208      * the name of the property
209      * @return
210      * the corresponding object
211      */

212     private static Object JavaDoc load( String JavaDoc key )
213     {
214         IPreferenceStore store = BrowserCommonActivator.getDefault().getPreferenceStore();
215         String JavaDoc s = store.getString( key );
216         return Utils.deserialize( s );
217     }
218
219
220     /**
221      * Stores the current value of the string-valued property with the given name.
222      *
223      * @param key
224      * the name of the property
225      * @param o
226      * the new current value of the property
227      */

228     private static void store( String JavaDoc key, Object JavaDoc o )
229     {
230         IPreferenceStore store = BrowserCommonActivator.getDefault().getPreferenceStore();
231         String JavaDoc s = Utils.serialize( o );
232         store.setValue( key, s );
233     }
234
235
236     /**
237      * Loads the default value for the string-valued property with the given name.
238      *
239      * @param key
240      * the name of the property
241      * @return
242      * the default value of the named property
243      */

244     private static Object JavaDoc loadDefault( String JavaDoc key )
245     {
246         IPreferenceStore store = BrowserCommonActivator.getDefault().getPreferenceStore();
247         String JavaDoc s = store.getDefaultString( key );
248         return Utils.deserialize( s );
249     }
250
251
252     /**
253      * Stores the default value for the string-valued property with the given name.
254      *
255      * @param key
256      * the name of the property
257      * @param o
258      * the new default value for the property
259      */

260     private static void storeDefault( String JavaDoc key, Object JavaDoc o )
261     {
262         IPreferenceStore store = BrowserCommonActivator.getDefault().getPreferenceStore();
263         String JavaDoc s = Utils.serialize( o );
264         store.setDefault( key, s );
265     }
266 }
267
Popular Tags