KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > BrowserCorePreferences


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.core;
22
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.core.model.schema.BinaryAttribute;
28 import org.apache.directory.ldapstudio.browser.core.model.schema.BinarySyntax;
29 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
30 import org.eclipse.core.runtime.Preferences;
31
32
33 /**
34  * This class is used to manage and access the preferences of the Browser Core Plugin
35  *
36  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
37  * @version $Rev$, $Date$
38  */

39 public class BrowserCorePreferences
40 {
41     private Set JavaDoc<String JavaDoc> binaryAttributeCache;
42
43     private Set JavaDoc<String JavaDoc> binarySyntaxCache;
44
45
46     /**
47      * Gets the oids and names of the binary attributes
48      *
49      * @return
50      * the oids and names of the binary attributes
51      */

52     public Set JavaDoc getBinaryAttributeOidsAndNames()
53     {
54         if ( binaryAttributeCache == null )
55         {
56             binaryAttributeCache = new HashSet JavaDoc<String JavaDoc>();
57             BinaryAttribute[] binaryAttributes =getBinaryAttributes();
58             for ( int i = 0; i < binaryAttributes.length; i++ )
59             {
60                 if ( binaryAttributes[i].getAttributeNumericOidOrName() != null )
61                 {
62                     binaryAttributeCache.add( binaryAttributes[i].getAttributeNumericOidOrName() );
63                 }
64             }
65         }
66         return binaryAttributeCache;
67     }
68
69
70     /**
71      * Gets an array containing the binary attributes
72      *
73      * @return
74      * an array containing the binary attributes
75      */

76     public BinaryAttribute[] getBinaryAttributes()
77     {
78         BinaryAttribute[] binaryAttributes = ( BinaryAttribute[] ) load( BrowserCoreConstants.PREFERENCE_BINARY_ATTRIBUTES );
79         return binaryAttributes;
80     }
81
82
83     /**
84      * Sets the binary attributes
85      *
86      * @param binaryAttributes
87      * the binary attributes to set
88      */

89     public void setBinaryAttributes( BinaryAttribute[] binaryAttributes )
90     {
91         store( BrowserCoreConstants.PREFERENCE_BINARY_ATTRIBUTES, binaryAttributes );
92         binaryAttributeCache = null;
93     }
94
95
96     /**
97      * Gets the default binary attributes
98      *
99      * @return
100      * the default binary attributes
101      */

102     public BinaryAttribute[] getDefaultBinaryAttributes()
103     {
104         BinaryAttribute[] binaryAttributes = ( BinaryAttribute[] ) loadDefault( BrowserCoreConstants.PREFERENCE_BINARY_ATTRIBUTES );
105         return binaryAttributes;
106     }
107
108
109     /**
110      * Sets the default binary attributes
111      *
112      * @param defaultBinaryAttributes
113      * the default binary attributes to set
114      */

115     public void setDefaultBinaryAttributes( BinaryAttribute[] defaultBinaryAttributes )
116     {
117         storeDefault( BrowserCoreConstants.PREFERENCE_BINARY_ATTRIBUTES, defaultBinaryAttributes );
118     }
119
120
121     /**
122      * Gets the binary syntax oids.
123      *
124      * @return the binary syntax oids
125      */

126     public Set JavaDoc<String JavaDoc> getBinarySyntaxOids()
127     {
128         if ( binarySyntaxCache == null )
129         {
130             binarySyntaxCache = new HashSet JavaDoc<String JavaDoc>();
131             BinarySyntax[] binarySyntaxes = getBinarySyntaxes();
132             for ( int i = 0; i < binarySyntaxes.length; i++ )
133             {
134                 if ( binarySyntaxes[i].getSyntaxNumericOid() != null )
135                 {
136                     binarySyntaxCache.add( binarySyntaxes[i].getSyntaxNumericOid() );
137                 }
138             }
139         }
140         return binarySyntaxCache;
141     }
142
143
144     /**
145      * Gets the binary syntaxes
146      *
147      * @return
148      * the binary syntaxes
149      */

150     public BinarySyntax[] getBinarySyntaxes()
151     {
152         BinarySyntax[] binarySyntaxes = ( BinarySyntax[] ) load( BrowserCoreConstants.PREFERENCE_BINARY_SYNTAXES );
153         return binarySyntaxes;
154     }
155
156
157     /**
158      * Sets the binary syntaxes
159      *
160      * @param binarySyntaxes
161      * the binary syntaxes to set
162      */

163     public void setBinarySyntaxes( BinarySyntax[] binarySyntaxes )
164     {
165         store( BrowserCoreConstants.PREFERENCE_BINARY_SYNTAXES, binarySyntaxes );
166         binarySyntaxCache = null;
167     }
168
169
170     /**
171      * Gets the default binary syntaxes
172      *
173      * @return
174      * the default binary syntaxes
175      */

176     public BinarySyntax[] getDefaultBinarySyntaxes()
177     {
178         BinarySyntax[] binarySyntaxes = ( BinarySyntax[] ) loadDefault( BrowserCoreConstants.PREFERENCE_BINARY_SYNTAXES );
179         return binarySyntaxes;
180     }
181
182
183     /**
184      * Sets the default binary syntaxes
185      *
186      * @param defaultBinarySyntaxes
187      * the default binary syntaxes to set
188      */

189     public void setDefaultBinarySyntaxes( BinarySyntax[] defaultBinarySyntaxes )
190     {
191         storeDefault( BrowserCoreConstants.PREFERENCE_BINARY_SYNTAXES, defaultBinarySyntaxes );
192     }
193
194
195     /**
196      * Loads the current value of the string-valued property with the given name.
197      *
198      * @param key
199      * the name of the property
200      * @return
201      * the corresponding object
202      */

203     private static Object JavaDoc load( String JavaDoc key )
204     {
205         Preferences store = BrowserCorePlugin.getDefault().getPluginPreferences();
206         String JavaDoc s = store.getString( key );
207         return Utils.deserialize( s );
208     }
209
210
211     /**
212      * Stores the current value of the string-valued property with the given name.
213      *
214      * @param key
215      * the name of the property
216      * @param o
217      * the new current value of the property
218      */

219     private static void store( String JavaDoc key, Object JavaDoc o )
220     {
221         Preferences store = BrowserCorePlugin.getDefault().getPluginPreferences();
222         String JavaDoc s = Utils.serialize( o );
223         store.setValue( key, s );
224         BrowserCorePlugin.getDefault().savePluginPreferences();
225     }
226
227
228     /**
229      * Loads the default value for the string-valued property with the given name.
230      *
231      * @param key
232      * the name of the property
233      * @return
234      * the default value of the named property
235      */

236     private static Object JavaDoc loadDefault( String JavaDoc key )
237     {
238         Preferences store = BrowserCorePlugin.getDefault().getPluginPreferences();
239         String JavaDoc s = store.getDefaultString( key );
240         return Utils.deserialize( s );
241     }
242
243
244     /**
245      * Stores the default value for the string-valued property with the given name.
246      *
247      * @param key
248      * the name of the property
249      * @param o
250      * the new default value for the property
251      */

252     private static void storeDefault( String JavaDoc key, Object JavaDoc o )
253     {
254         Preferences store = BrowserCorePlugin.getDefault().getPluginPreferences();
255         String JavaDoc s = Utils.serialize( o );
256         store.setDefault( key, s );
257         BrowserCorePlugin.getDefault().savePluginPreferences();
258     }
259 }
260
Popular Tags