1 19 20 package org.netbeans.modules.options.indentation; 21 22 import java.lang.reflect.Method ; 23 import java.util.Collection ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 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 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 ) 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 ) 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 ) 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 ) 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 getJavaFormatStatementContinuationIndent () { 115 return (Integer ) getParameter ( 116 "getJavaFormatStatementContinuationIndent", new Integer (4) 117 ); 118 } 119 120 void setJavaFormatStatementContinuationIndent (Integer continuation) { 121 if (continuation.intValue () > 0) 122 setParameter ( 123 "setJavaFormatStatementContinuationIndent", 124 continuation, 125 Integer.TYPE 126 ); 127 updateChanged (); 128 } 129 130 Integer getSpacesPerTab () { 131 return (Integer ) getParameter ( 132 "getSpacesPerTab", new Integer (4) 133 ); 134 } 135 136 void setSpacesPerTab (Integer 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); applyParameterToAll ("setJavaFormatNewlineBeforeBrace", Boolean.valueOf (getJavaFormatNewlineBeforeBrace ()), Boolean.TYPE); applyParameterToAll ("setJavaFormatSpaceBeforeParenthesis", Boolean.valueOf (getJavaFormatSpaceBeforeParenthesis ()), Boolean.TYPE); applyParameterToAll ("setJavaFormatStatementContinuationIndent", getJavaFormatStatementContinuationIndent (), Integer.TYPE); applyParameterToAll ("setExpandTabs", Boolean.valueOf(isExpandTabs ()), Boolean.TYPE); applyParameterToAll ("setSpacesPerTab", getSpacesPerTab (), Integer.TYPE); } 158 159 void revertChanges () { 160 if (!changed) return; 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 (originalStatementIndent)); 175 if (getSpacesPerTab ().intValue () != 176 originalIndent && 177 originalIndent > 0 178 ) 179 setSpacesPerTab 180 (new Integer (originalIndent)); 181 } 182 183 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 getParameter (String parameterName, Object defaultValue) { 210 IndentEngine eng = getDefaultIndentEngine(); 211 if (eng != null) { 212 try { 213 Method method = eng.getClass ().getMethod ( 214 parameterName, 215 new Class [0] 216 ); 217 return method.invoke (eng, new Object [0]); 218 } catch (Exception ex) { 219 } 220 } 221 return defaultValue; 222 } 223 224 private void setParameter ( 225 String parameterName, 226 Object parameterValue, 227 Class parameterType 228 ) { 229 IndentEngine eng = getDefaultIndentEngine(); 230 if (eng != null) { 231 try { 232 Method method = eng.getClass ().getMethod ( 233 parameterName, 234 new Class [] {parameterType} 235 ); 236 method.invoke (eng, new Object [] {parameterValue}); 237 } catch (Exception ex) { 238 } 239 } 240 } 241 242 private void applyParameterToAll ( 243 String parameterName, 244 Object parameterValue, 245 Class parameterType 246 ) { 247 HashSet mimeTypeBoundEngines = new HashSet (); 248 Set mimeTypes = EditorSettings.getDefault().getMimeTypes(); 249 for(Iterator i = mimeTypes.iterator(); i.hasNext(); ) { 250 String mimeType = (String ) 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 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 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 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 method = indentEngine.getClass ().getMethod ( 281 parameterName, 282 new Class [] {parameterType} 283 ); 284 method.invoke (indentEngine, new Object [] {parameterValue}); 285 } catch (Exception ex) { 286 } 287 } 288 289 292 Collection allEngines = Lookup.getDefault().lookupAll(IndentEngine.class); 293 for (Iterator it = allEngines.iterator(); it.hasNext(); ) { 294 IndentEngine indentEngine = (IndentEngine) it.next(); 295 if (!mimeTypeBoundEngines.contains(indentEngine)) { 296 try { 297 Method method = indentEngine.getClass().getMethod( 298 parameterName, 299 new Class [] { parameterType } 300 ); 301 method.invoke(indentEngine, new Object [] { parameterValue }); 302 } catch (Exception e) { 303 } 305 } 306 } 307 } 308 309 private ClassLoader classLoader; 310 private ClassLoader getClassLoader () { 311 if (classLoader == null) 312 classLoader = (ClassLoader ) Lookup.getDefault ().lookup 313 (ClassLoader .class); 314 return classLoader; 315 } 316 317 private static BaseOptions getOptions (String mimeType) { 318 return (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class); 319 } 320 } 321 322 323 | Popular Tags |