001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.options.generaleditor;
043:
044: import java.lang.reflect.Method;
045: import java.util.Iterator;
046: import java.util.Map;
047: import java.util.Set;
048: import java.util.prefs.Preferences;
049: import org.netbeans.api.editor.mimelookup.MimeLookup;
050: import org.netbeans.api.editor.mimelookup.MimePath;
051: import org.netbeans.editor.SettingsNames;
052: import org.netbeans.modules.editor.options.BaseOptions;
053: import org.netbeans.modules.editor.settings.storage.api.EditorSettings;
054: import org.openide.util.Exceptions;
055: import org.openide.util.Lookup;
056: import org.openide.util.NbPreferences;
057:
058: public class Model {
059:
060: // code folding options
061:
062: boolean isShowCodeFolding() {
063: return getFoldingParameter(SettingsNames.CODE_FOLDING_ENABLE,
064: true);
065: }
066:
067: boolean isFoldImports() {
068: return getFoldingParameter("code-folding-collapse-import",
069: false);
070: }
071:
072: boolean isFoldInitialComment() {
073: return getFoldingParameter(
074: "code-folding-collapse-initial-comment", false);
075: }
076:
077: boolean isFoldInnerClasses() {
078: return getFoldingParameter("code-folding-collapse-innerclass",
079: false);
080: }
081:
082: boolean isFoldJavaDocComments() {
083: return getFoldingParameter("code-folding-collapse-javadoc",
084: false);
085: }
086:
087: boolean isFoldMethods() {
088: return getFoldingParameter("code-folding-collapse-method",
089: false);
090: }
091:
092: void setFoldingOptions(boolean showCodeFolding,
093: boolean foldImports, boolean foldInitialComent,
094: boolean foldInnerClasses, boolean foldJavaDoc,
095: boolean foldMethods) {
096: Set<String> mimeTypes = EditorSettings.getDefault()
097: .getAllMimeTypes();
098: for (String mimeType : mimeTypes) {
099: BaseOptions baseOptions = getOptions(mimeType);
100:
101: if (baseOptions == null) {
102: continue;
103: }
104:
105: Map<String, Boolean> m = baseOptions.getCodeFoldingProps();
106:
107: m.put(SettingsNames.CODE_FOLDING_ENABLE, Boolean
108: .valueOf(showCodeFolding));
109: m.put("code-folding-collapse-import", Boolean
110: .valueOf(foldImports));
111: m.put("code-folding-collapse-initial-comment", Boolean
112: .valueOf(foldInitialComent));
113: m.put("code-folding-collapse-innerclass", Boolean
114: .valueOf(foldInnerClasses));
115: m.put("code-folding-collapse-javadoc", Boolean
116: .valueOf(foldJavaDoc));
117: m.put("code-folding-collapse-method", Boolean
118: .valueOf(foldMethods));
119:
120: baseOptions.setCodeFoldingProps(m);
121: }
122: }
123:
124: // code completion options
125:
126: boolean isPairCharacterCompletion() {
127: return getParameter("getPairCharactersCompletion", true);
128: }
129:
130: boolean isCompletionAutoPopup() {
131: return getParameter("getCompletionAutoPopup", true);
132: }
133:
134: boolean isShowDeprecatedMembers() {
135: return getParameter("getShowDeprecatedMembers", true);
136: }
137:
138: boolean isCompletionInstantSubstitution() {
139: return getParameter("getCompletionInstantSubstitution", true);
140: }
141:
142: boolean isCompletionCaseSensitive() {
143: return getParameter("getCompletionCaseSensitive", true);
144: }
145:
146: void setCompletionOptions(boolean pairCharacterCompletion,
147: boolean completionAutoPopup, boolean showDeprecatedMembers,
148: boolean completionInstantSubstitution,
149: boolean completionCaseSensitive) {
150: Set mimeTypes = EditorSettings.getDefault().getMimeTypes();
151: for (Iterator i = mimeTypes.iterator(); i.hasNext();) {
152: String mimeType = (String) i.next();
153: BaseOptions baseOptions = getOptions(mimeType);
154:
155: if (baseOptions == null) {
156: continue;
157: }
158:
159: try {
160: Method method = baseOptions.getClass().getMethod(
161: "setPairCharactersCompletion",
162: new Class[] { Boolean.TYPE });
163: method.invoke(baseOptions, new Object[] { Boolean
164: .valueOf(pairCharacterCompletion) });
165: } catch (Exception ex) {
166: }
167: try {
168: Method method = baseOptions.getClass().getMethod(
169: "setCompletionAutoPopup",
170: new Class[] { Boolean.TYPE });
171: method.invoke(baseOptions, new Object[] { Boolean
172: .valueOf(completionAutoPopup) });
173: } catch (Exception ex) {
174: }
175: try {
176: Method method = baseOptions.getClass().getMethod(
177: "setShowDeprecatedMembers",
178: new Class[] { Boolean.TYPE });
179: method.invoke(baseOptions, new Object[] { Boolean
180: .valueOf(showDeprecatedMembers) });
181: } catch (Exception ex) {
182: }
183: try {
184: Method method = baseOptions.getClass().getMethod(
185: "setCompletionInstantSubstitution",
186: new Class[] { Boolean.TYPE });
187: method.invoke(baseOptions, new Object[] { Boolean
188: .valueOf(completionInstantSubstitution) });
189: } catch (Exception ex) {
190: }
191: try {
192: Method method = baseOptions.getClass().getMethod(
193: "setCompletionCaseSensitive",
194: new Class[] { Boolean.TYPE });
195: method.invoke(baseOptions, new Object[] { Boolean
196: .valueOf(completionCaseSensitive) });
197: } catch (Exception ex) {
198: }
199: }
200: }
201:
202: Boolean isCamelCaseJavaNavigation() {
203: Preferences p = getJavaModulePreferenes();
204: if (p == null) {
205: return null;
206: }
207: return p.getBoolean("useCamelCaseStyleNavigation", true) ? Boolean.TRUE
208: : Boolean.FALSE; // NOI18N
209: }
210:
211: void setCamelCaseNavigation(boolean value) {
212: Preferences p = getJavaModulePreferenes();
213: if (p == null) {
214: return;
215: }
216: p.putBoolean("useCamelCaseStyleNavigation", value); // NOI18N
217: }
218:
219: // private helper methods ..................................................
220:
221: private boolean getFoldingParameter(String parameterName,
222: boolean defaultValue) {
223: boolean successfullRead = false;
224: Set<String> mimeTypes = EditorSettings.getDefault()
225: .getAllMimeTypes();
226:
227: for (String mimeType : mimeTypes) {
228: BaseOptions options = getOptions(mimeType);
229:
230: if (options == null) {
231: continue;
232: }
233:
234: Map foldingParams = options.getCodeFoldingProps();
235: Boolean value = (Boolean) foldingParams.get(parameterName);
236:
237: if (value != null && value.booleanValue()) {
238: return true;
239: } else {
240: successfullRead = true;
241: }
242: }
243:
244: return successfullRead ? false : defaultValue;
245: }
246:
247: private boolean getParameter(String parameterName,
248: boolean defaultValue) {
249: boolean successfullRead = false;
250: Set<String> mimeTypes = EditorSettings.getDefault()
251: .getAllMimeTypes();
252:
253: for (String mimeType : mimeTypes) {
254: BaseOptions options = getOptions(mimeType);
255:
256: if (options == null) {
257: continue;
258: }
259:
260: try {
261: Method method = options.getClass().getMethod(
262: parameterName, new Class[0]);
263: boolean value = ((Boolean) method.invoke(options,
264: new Object[0])).booleanValue();
265: if (value) {
266: return true;
267: } else {
268: successfullRead = true;
269: }
270: } catch (Exception ex) {
271: }
272: }
273:
274: return successfullRead ? false : defaultValue;
275: }
276:
277: private static BaseOptions getOptions(String mimeType) {
278: return MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(
279: BaseOptions.class);
280: }
281:
282: private Preferences getJavaModulePreferenes() {
283: try {
284: ClassLoader cl = Lookup.getDefault().lookup(
285: ClassLoader.class);
286: Class accpClass = cl
287: .loadClass("org.netbeans.modules.editor.java.AbstractCamelCasePosition"); // NOI18N
288: if (accpClass == null) {
289: return null;
290: }
291: return NbPreferences.forModule(accpClass);
292: } catch (ClassNotFoundException ex) {
293: return null;
294: }
295: }
296:
297: }
|