KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > indentation > IndentationModel


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.indentation;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.api.editor.mimelookup.MimeLookup;
28 import org.netbeans.api.editor.mimelookup.MimePath;
29 import org.netbeans.modules.editor.options.BaseOptions;
30 import org.netbeans.modules.editor.settings.storage.api.EditorSettings;
31
32 import org.openide.text.IndentEngine;
33 import org.openide.util.Lookup;
34
35
36 class IndentationModel {
37
38     private boolean originalExpandedTabs;
39     private boolean originalAddStar;
40     private boolean originalNewLine;
41     private boolean originalSpace;
42     private int originalStatementIndent = 0;
43     private int originalIndent = 0;
44     
45     private boolean changed = false;
46
47     
48     IndentationModel () {
49         // save original values
50
originalExpandedTabs = isExpandTabs ();
51         originalAddStar = getJavaFormatLeadingStarInComment ();
52         originalNewLine = getJavaFormatNewlineBeforeBrace ();
53         originalSpace = getJavaFormatSpaceBeforeParenthesis ();
54         originalStatementIndent = getJavaFormatStatementContinuationIndent ().
55                 intValue ();
56         originalIndent = getSpacesPerTab ().intValue ();
57     }
58     
59     boolean isExpandTabs () {
60         return ((Boolean JavaDoc) getParameter ("isExpandTabs", Boolean.FALSE)).
61             booleanValue ();
62     }
63     
64     void setExpandTabs (boolean expand) {
65         setParameter ("setExpandTabs", Boolean.valueOf (expand), Boolean.TYPE);
66         updateChanged ();
67     }
68     
69     boolean getJavaFormatLeadingStarInComment () {
70         return ((Boolean JavaDoc) getParameter (
71             "getJavaFormatLeadingStarInComment", Boolean.FALSE
72         )).booleanValue ();
73     }
74     
75     void setJavaFormatLeadingStarInComment (boolean star) {
76         setParameter (
77             "setJavaFormatLeadingStarInComment",
78             Boolean.valueOf (star),
79             Boolean.TYPE
80         );
81         updateChanged ();
82     }
83     
84     boolean getJavaFormatSpaceBeforeParenthesis () {
85         return ((Boolean JavaDoc) getParameter (
86             "getJavaFormatSpaceBeforeParenthesis", Boolean.FALSE
87         )).booleanValue ();
88     }
89     
90     void setJavaFormatSpaceBeforeParenthesis (boolean space) {
91         setParameter (
92             "setJavaFormatSpaceBeforeParenthesis",
93             Boolean.valueOf (space),
94             Boolean.TYPE
95         );
96         updateChanged ();
97     }
98     
99     boolean getJavaFormatNewlineBeforeBrace () {
100         return ((Boolean JavaDoc) getParameter (
101             "getJavaFormatNewlineBeforeBrace", Boolean.FALSE
102         )).booleanValue ();
103     }
104     
105     void setJavaFormatNewlineBeforeBrace (boolean newLine) {
106         setParameter (
107             "setJavaFormatNewlineBeforeBrace",
108             Boolean.valueOf (newLine),
109             Boolean.TYPE
110         );
111         updateChanged ();
112     }
113     
114     Integer JavaDoc getJavaFormatStatementContinuationIndent () {
115         return (Integer JavaDoc) getParameter (
116             "getJavaFormatStatementContinuationIndent", new Integer JavaDoc (4)
117         );
118     }
119     
120     void setJavaFormatStatementContinuationIndent (Integer JavaDoc continuation) {
121     if (continuation.intValue () > 0)
122             setParameter (
123                 "setJavaFormatStatementContinuationIndent",
124                 continuation,
125                 Integer.TYPE
126             );
127         updateChanged ();
128     }
129     
130     Integer JavaDoc getSpacesPerTab () {
131         return (Integer JavaDoc) getParameter (
132             "getSpacesPerTab", new Integer JavaDoc (4)
133         );
134     }
135     
136     void setSpacesPerTab (Integer JavaDoc spaces) {
137     if (spaces.intValue () > 0)
138             setParameter (
139                 "setSpacesPerTab",
140                 spaces,
141                 Integer.TYPE
142             );
143         updateChanged ();
144     }
145
146     boolean isChanged () {
147         return changed;
148     }
149
150     void applyChanges() {
151         applyParameterToAll ("setJavaFormatLeadingStarInComment", Boolean.valueOf (getJavaFormatLeadingStarInComment ()), Boolean.TYPE); //NOI18N
152
applyParameterToAll ("setJavaFormatNewlineBeforeBrace", Boolean.valueOf (getJavaFormatNewlineBeforeBrace ()), Boolean.TYPE); //NOI18N
153
applyParameterToAll ("setJavaFormatSpaceBeforeParenthesis", Boolean.valueOf (getJavaFormatSpaceBeforeParenthesis ()), Boolean.TYPE); //NOI18N
154
applyParameterToAll ("setJavaFormatStatementContinuationIndent", getJavaFormatStatementContinuationIndent (), Integer.TYPE); //NOI18N
155
applyParameterToAll ("setExpandTabs", Boolean.valueOf(isExpandTabs ()), Boolean.TYPE); //NOI18N
156
applyParameterToAll ("setSpacesPerTab", getSpacesPerTab (), Integer.TYPE); //NOI18N
157
}
158     
159     void revertChanges () {
160         if (!changed) return; // no changes
161
if (getJavaFormatLeadingStarInComment () != originalAddStar)
162             setJavaFormatLeadingStarInComment (originalAddStar);
163         if (getJavaFormatNewlineBeforeBrace () != originalNewLine)
164             setJavaFormatNewlineBeforeBrace (originalNewLine);
165         if (getJavaFormatSpaceBeforeParenthesis () != originalSpace)
166             setJavaFormatSpaceBeforeParenthesis (originalSpace);
167         if (isExpandTabs () != originalExpandedTabs)
168             setExpandTabs (originalExpandedTabs);
169         if (getJavaFormatStatementContinuationIndent ().intValue () !=
170                 originalStatementIndent &&
171             originalStatementIndent > 0
172         )
173             setJavaFormatStatementContinuationIndent
174                 (new Integer JavaDoc (originalStatementIndent));
175         if (getSpacesPerTab ().intValue () !=
176                 originalIndent &&
177             originalIndent > 0
178         )
179             setSpacesPerTab
180                 (new Integer JavaDoc (originalIndent));
181     }
182     
183     // private helper methods ..................................................
184

185     private void updateChanged () {
186         changed =
187                 isExpandTabs () != originalExpandedTabs ||
188                 getJavaFormatLeadingStarInComment () != originalAddStar ||
189                 getJavaFormatNewlineBeforeBrace () != originalNewLine ||
190                 getJavaFormatSpaceBeforeParenthesis () != originalSpace ||
191                 getJavaFormatStatementContinuationIndent ().intValue () !=
192                     originalStatementIndent ||
193                 getSpacesPerTab ().intValue () != originalIndent;
194     }
195     
196     private IndentEngine javaIndentEngine = null;
197     
198     private IndentEngine getDefaultIndentEngine() {
199         if (javaIndentEngine == null) {
200             BaseOptions options = getOptions ("text/x-java");
201             if (options == null) {
202                 options = getOptions ("text/plain");
203             }
204             javaIndentEngine = options == null ? null : options.getIndentEngine ();
205         }
206         return javaIndentEngine;
207     }
208     
209     private Object JavaDoc getParameter (String JavaDoc parameterName, Object JavaDoc defaultValue) {
210         IndentEngine eng = getDefaultIndentEngine();
211         if (eng != null) {
212             try {
213                 Method JavaDoc method = eng.getClass ().getMethod (
214                     parameterName,
215                     new Class JavaDoc [0]
216                 );
217                 return method.invoke (eng, new Object JavaDoc [0]);
218             } catch (Exception JavaDoc ex) {
219             }
220         }
221         return defaultValue;
222     }
223     
224     private void setParameter (
225         String JavaDoc parameterName,
226         Object JavaDoc parameterValue,
227         Class JavaDoc parameterType
228     ) {
229         IndentEngine eng = getDefaultIndentEngine();
230         if (eng != null) {
231             try {
232                 Method JavaDoc method = eng.getClass ().getMethod (
233                     parameterName,
234                     new Class JavaDoc [] {parameterType}
235                 );
236                 method.invoke (eng, new Object JavaDoc [] {parameterValue});
237             } catch (Exception JavaDoc ex) {
238             }
239         }
240     }
241     
242     private void applyParameterToAll (
243         String JavaDoc parameterName,
244         Object JavaDoc parameterValue,
245         Class JavaDoc parameterType
246     ) {
247         HashSet JavaDoc mimeTypeBoundEngines = new HashSet JavaDoc();
248         Set JavaDoc mimeTypes = EditorSettings.getDefault().getMimeTypes();
249         for(Iterator JavaDoc i = mimeTypes.iterator(); i.hasNext(); ) {
250             String JavaDoc mimeType = (String JavaDoc) i.next();
251             BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
252             
253             if (baseOptions == null) {
254                 continue;
255             }
256             
257             IndentEngine indentEngine = baseOptions.getIndentEngine ();
258             mimeTypeBoundEngines.add(indentEngine);
259             
260             try {
261                 // HACK
262
if (baseOptions.getClass ().getName ().equals ("org.netbeans.modules.java.editor.options.JavaOptions") &&
263                     !indentEngine.getClass ().getName ().equals ("org.netbeans.modules.editor.java.JavaIndentEngine")
264                 ) {
265                     Class JavaDoc javaIndentEngineClass = getClassLoader ().loadClass
266                         ("org.netbeans.modules.editor.java.JavaIndentEngine");
267                     indentEngine = (IndentEngine) Lookup.getDefault ().lookup
268                         (javaIndentEngineClass);
269                     baseOptions.setIndentEngine (indentEngine);
270                 }
271                 if (baseOptions.getClass ().getName ().equals ("org.netbeans.modules.web.core.syntax.JSPOptions") &&
272                     !indentEngine.getClass ().getName ().equals ("org.netbeans.modules.web.core.syntax.JspIndentEngine")
273                 ) {
274                     Class JavaDoc jspIndentEngineClass = getClassLoader ().loadClass
275                         ("org.netbeans.modules.web.core.syntax.JspIndentEngine");
276                     indentEngine = (IndentEngine) Lookup.getDefault ().lookup
277                         (jspIndentEngineClass);
278                     baseOptions.setIndentEngine (indentEngine);
279                 }
280                 Method JavaDoc method = indentEngine.getClass ().getMethod (
281                     parameterName,
282                     new Class JavaDoc [] {parameterType}
283                 );
284                 method.invoke (indentEngine, new Object JavaDoc [] {parameterValue});
285             } catch (Exception JavaDoc ex) {
286             }
287         }
288         
289         // There can be other engines that are not currently hooked up with
290
// and BaseOptions/mime-type.
291

292         Collection JavaDoc allEngines = Lookup.getDefault().lookupAll(IndentEngine.class);
293         for (Iterator JavaDoc it = allEngines.iterator(); it.hasNext(); ) {
294             IndentEngine indentEngine = (IndentEngine) it.next();
295             if (!mimeTypeBoundEngines.contains(indentEngine)) {
296                 try {
297                     Method JavaDoc method = indentEngine.getClass().getMethod(
298                         parameterName,
299                         new Class JavaDoc [] { parameterType }
300                     );
301                     method.invoke(indentEngine, new Object JavaDoc [] { parameterValue });
302                 } catch (Exception JavaDoc e) {
303                     // ignore
304
}
305             }
306         }
307     }
308     
309     private ClassLoader JavaDoc classLoader;
310     private ClassLoader JavaDoc getClassLoader () {
311         if (classLoader == null)
312             classLoader = (ClassLoader JavaDoc) Lookup.getDefault ().lookup
313                 (ClassLoader JavaDoc.class);
314         return classLoader;
315     }
316     
317     private static BaseOptions getOptions (String JavaDoc mimeType) {
318         return (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
319     }
320 }
321
322
323
Popular Tags