KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > generaleditor > Model


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.options.generaleditor;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.netbeans.api.editor.mimelookup.MimeLookup;
27 import org.netbeans.api.editor.mimelookup.MimePath;
28 import org.netbeans.editor.SettingsNames;
29 import org.netbeans.modules.editor.options.BaseOptions;
30 import org.netbeans.modules.editor.settings.storage.api.EditorSettings;
31
32
33 public class Model {
34
35     // code folding options
36

37     boolean isShowCodeFolding () {
38         return getFoldingParameter (SettingsNames.CODE_FOLDING_ENABLE, true);
39     }
40
41     boolean isFoldImports () {
42         return getFoldingParameter ("code-folding-collapse-import", false);
43     }
44     
45     boolean isFoldInitialComment () {
46         return getFoldingParameter ("code-folding-collapse-initial-comment", false);
47     }
48     
49     boolean isFoldInnerClasses () {
50         return getFoldingParameter ("code-folding-collapse-innerclass", false);
51     }
52     
53     boolean isFoldJavaDocComments () {
54         return getFoldingParameter ("code-folding-collapse-javadoc", false);
55     }
56     
57     boolean isFoldMethods () {
58         return getFoldingParameter ("code-folding-collapse-method", false);
59     }
60     
61     void setFoldingOptions (
62         boolean showCodeFolding,
63         boolean foldImports,
64         boolean foldInitialComent,
65         boolean foldInnerClasses,
66         boolean foldJavaDoc,
67         boolean foldMethods
68     ) {
69         if (javaOptions == null)
70             javaOptions = getOptions ("text/x-java");
71         if (javaOptions == null) return;
72         Map JavaDoc javaFoldingMap = javaOptions.getCodeFoldingProps ();
73
74         javaFoldingMap.put (
75             SettingsNames.CODE_FOLDING_ENABLE,
76             Boolean.valueOf (showCodeFolding)
77         );
78         javaFoldingMap.put (
79             "code-folding-collapse-import",
80             Boolean.valueOf (foldImports)
81         );
82         javaFoldingMap.put (
83             "code-folding-collapse-initial-comment",
84             Boolean.valueOf (foldInitialComent)
85         );
86         javaFoldingMap.put (
87             "code-folding-collapse-innerclass",
88             Boolean.valueOf (foldInnerClasses)
89         );
90         javaFoldingMap.put (
91             "code-folding-collapse-javadoc",
92             Boolean.valueOf (foldJavaDoc)
93         );
94         javaFoldingMap.put (
95             "code-folding-collapse-method",
96             Boolean.valueOf (foldMethods)
97         );
98         javaOptions.setCodeFoldingProps (javaFoldingMap);
99         
100         Set JavaDoc mimeTypes = EditorSettings.getDefault().getMimeTypes();
101         for(Iterator JavaDoc i = mimeTypes.iterator(); i.hasNext(); ) {
102             String JavaDoc mimeType = (String JavaDoc) i.next();
103             BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
104             
105             if (baseOptions == null) {
106                 continue;
107             }
108             
109             Map JavaDoc m = baseOptions.getCodeFoldingProps ();
110             m.put (
111                 SettingsNames.CODE_FOLDING_ENABLE,
112                 Boolean.valueOf (showCodeFolding)
113             );
114             baseOptions.setCodeFoldingProps (m);
115         }
116     }
117     
118     
119     // code completion options
120

121     boolean isPairCharacterCompletion () {
122         return getParameter ("getPairCharactersCompletion", true);
123     }
124     
125     boolean isCompletionAutoPopup () {
126         return getParameter ("getCompletionAutoPopup", true);
127     }
128     
129     boolean isShowDeprecatedMembers () {
130         return getParameter ("getShowDeprecatedMembers", true);
131     }
132     
133     boolean isCompletionInstantSubstitution () {
134         return getParameter ("getCompletionInstantSubstitution", true);
135     }
136     
137     boolean isCompletionCaseSensitive () {
138         return getParameter ("getCompletionCaseSensitive", true);
139     }
140     
141     void setCompletionOptions (
142         boolean pairCharacterCompletion,
143         boolean completionAutoPopup,
144         boolean showDeprecatedMembers,
145         boolean completionInstantSubstitution,
146         boolean completionCaseSensitive
147     ) {
148         Set JavaDoc mimeTypes = EditorSettings.getDefault().getMimeTypes();
149         for(Iterator JavaDoc i = mimeTypes.iterator(); i.hasNext(); ) {
150             String JavaDoc mimeType = (String JavaDoc) i.next();
151             BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
152
153             if (baseOptions == null) {
154                 continue;
155             }
156             
157             try {
158                 Method JavaDoc method = baseOptions.getClass ().getMethod (
159                     "setPairCharactersCompletion",
160                     new Class JavaDoc [] {Boolean.TYPE}
161                 );
162                 method.invoke (
163                     baseOptions,
164                     new Object JavaDoc [] {Boolean.valueOf (pairCharacterCompletion)}
165                 );
166             } catch (Exception JavaDoc ex) {
167             }
168             try {
169                 Method JavaDoc method = baseOptions.getClass ().getMethod (
170                     "setCompletionAutoPopup",
171                     new Class JavaDoc [] {Boolean.TYPE}
172                 );
173                 method.invoke (
174                     baseOptions,
175                     new Object JavaDoc [] {Boolean.valueOf (completionAutoPopup)}
176                 );
177             } catch (Exception JavaDoc ex) {
178             }
179             try {
180                 Method JavaDoc method = baseOptions.getClass ().getMethod (
181                     "setShowDeprecatedMembers",
182                     new Class JavaDoc [] {Boolean.TYPE}
183                 );
184                 method.invoke (
185                     baseOptions,
186                     new Object JavaDoc [] {Boolean.valueOf (showDeprecatedMembers)}
187                 );
188             } catch (Exception JavaDoc ex) {
189             }
190             try {
191                 Method JavaDoc method = baseOptions.getClass ().getMethod (
192                     "setCompletionInstantSubstitution",
193                     new Class JavaDoc [] {Boolean.TYPE}
194                 );
195                 method.invoke (
196                     baseOptions,
197                     new Object JavaDoc [] {Boolean.valueOf (completionInstantSubstitution)}
198                 );
199             } catch (Exception JavaDoc ex) {
200             }
201             try {
202                 Method JavaDoc method = baseOptions.getClass ().getMethod (
203                     "setCompletionCaseSensitive",
204                     new Class JavaDoc [] {Boolean.TYPE}
205                 );
206                 method.invoke (
207                     baseOptions,
208                     new Object JavaDoc [] {Boolean.valueOf (completionCaseSensitive)}
209                 );
210             } catch (Exception JavaDoc ex) {
211             }
212         }
213     }
214     
215     
216     // private helper methods ..................................................
217

218     private boolean getFoldingParameter (
219         String JavaDoc parameterName,
220         boolean defaultValue
221     ) {
222         BaseOptions options = getOptions ("text/x-java");
223         if (options == null)
224             options = getOptions ("text/plain");
225         if (options == null) return defaultValue;
226         Map JavaDoc javaFoldingMap = options.getCodeFoldingProps ();
227         Boolean JavaDoc b = (Boolean JavaDoc) javaFoldingMap.get (parameterName);
228         if (b != null) return b.booleanValue ();
229         return defaultValue;
230     }
231     
232     private BaseOptions javaOptions;
233     
234     private boolean getParameter (String JavaDoc parameterName, boolean defaultValue) {
235         if (javaOptions == null) {
236             javaOptions = getOptions ("text/x-java");
237             if (javaOptions == null)
238                 javaOptions = getOptions ("text/plain");
239         }
240         if (javaOptions == null) return defaultValue;
241         try {
242             Method JavaDoc method = javaOptions.getClass ().getMethod (
243                 parameterName,
244                 new Class JavaDoc [0]
245             );
246             return ((Boolean JavaDoc) method.invoke (javaOptions, new Object JavaDoc [0])).
247                 booleanValue ();
248         } catch (Exception JavaDoc ex) {
249         }
250         return defaultValue;
251     }
252     
253     private static BaseOptions getOptions (String JavaDoc mimeType) {
254         return (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
255     }
256 }
257
258
259
Popular Tags