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-2007 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: package org.netbeans.modules.java.ui;
042:
043: import java.awt.Component;
044: import java.awt.Container;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.beans.PropertyChangeListener;
048: import java.beans.PropertyChangeSupport;
049: import java.util.HashMap;
050: import java.util.Map;
051: import java.util.prefs.BackingStoreException;
052: import java.util.prefs.Preferences;
053: import javax.swing.ComboBoxModel;
054: import javax.swing.DefaultComboBoxModel;
055: import javax.swing.JCheckBox;
056: import javax.swing.JComboBox;
057: import javax.swing.JComponent;
058: import javax.swing.JEditorPane;
059: import javax.swing.JPanel;
060: import javax.swing.JTextField;
061: import javax.swing.event.DocumentEvent;
062: import javax.swing.event.DocumentListener;
063: import javax.swing.text.EditorKit;
064: import org.netbeans.api.editor.mimelookup.MimeLookup;
065: import org.netbeans.api.editor.mimelookup.MimePath;
066: import org.netbeans.editor.Settings;
067: import org.netbeans.api.java.source.CodeStyle;
068: import org.netbeans.editor.Formatter;
069: import org.netbeans.editor.SettingsNames;
070: import org.netbeans.modules.java.source.save.Reformatter;
071: import org.netbeans.spi.options.OptionsPanelController;
072: import org.openide.util.Exceptions;
073:
074: import static org.netbeans.api.java.source.CodeStyle.*;
075: import org.openide.util.HelpCtx;
076: import org.openide.util.Lookup;
077: import org.openide.util.NbBundle;
078: import org.openide.util.NbPreferences;
079:
080: /**
081: *
082: * @author phrebejk
083: */
084: public class FmtOptions {
085:
086: public static final String expandTabToSpaces = "expandTabToSpaces"; //NOI18N
087: public static final String tabSize = "tabSize"; //NOI18N
088: public static final String indentSize = "indentSize"; //NOI18N
089: public static final String continuationIndentSize = "continuationIndentSize"; //NOI18N
090: public static final String labelIndent = "labelIndent"; //NOI18N
091: public static final String absoluteLabelIndent = "absoluteLabelIndent"; //NOI18N
092: public static final String indentTopLevelClassMembers = "indentTopLevelClassMembers"; //NOI18N
093: public static final String indentCasesFromSwitch = "indentCasesFromSwitch"; //NOI18N
094: public static final String rightMargin = "rightMargin"; //NOI18N
095:
096: public static final String preferLongerNames = "preferLongerNames"; //NOI18N
097: public static final String fieldNamePrefix = "fieldNamePrefix"; //NOI18N
098: public static final String fieldNameSuffix = "fieldNameSuffix"; //NOI18N
099: public static final String staticFieldNamePrefix = "staticFieldNamePrefix"; //NOI18N
100: public static final String staticFieldNameSuffix = "staticFieldNameSuffix"; //NOI18N
101: public static final String parameterNamePrefix = "parameterNamePrefix"; //NOI18N
102: public static final String parameterNameSuffix = "parameterNameSuffix"; //NOI18N
103: public static final String localVarNamePrefix = "localVarNamePrefix"; //NOI18N
104: public static final String localVarNameSuffix = "localVarNameSuffix"; //NOI18N
105: public static final String qualifyFieldAccess = "qualifyFieldAccess"; //NOI18N
106: public static final String useIsForBooleanGetters = "useIsForBooleanGetters"; //NOI18N
107: public static final String addOverrideAnnotation = "addOverrideAnnotation"; //NOI18N
108: public static final String makeLocalVarsFinal = "makeLocalVarsFinal"; //NOI18N
109: public static final String makeParametersFinal = "makeParametersFinal"; //NOI18N
110: public static final String classMembersOrder = "classMembersOrder"; //NOI18N
111:
112: public static final String classDeclBracePlacement = "classDeclBracePlacement"; //NOI18N
113: public static final String methodDeclBracePlacement = "methodDeclBracePlacement"; //NOI18N
114: public static final String otherBracePlacement = "otherBracePlacement"; //NOI18N
115: public static final String specialElseIf = "specialElseIf"; //NOI18N
116: public static final String redundantIfBraces = "redundantIfBraces"; //NOI18N
117: public static final String redundantForBraces = "redundantForBraces"; //NOI18N
118: public static final String redundantWhileBraces = "redundantWhileBraces"; //NOI18N
119: public static final String redundantDoWhileBraces = "redundantDoWhileBraces"; //NOI18N
120: public static final String alignMultilineMethodParams = "alignMultilineMethodParams"; //NOI18N
121: public static final String alignMultilineCallArgs = "alignMultilineCallArgs"; //NOI18N
122: public static final String alignMultilineAnnotationArgs = "alignMultilineAnnotationArgs"; //NOI18N
123: public static final String alignMultilineImplements = "alignMultilineImplements"; //NOI18N
124: public static final String alignMultilineThrows = "alignMultilineThrows"; //NOI18N
125: public static final String alignMultilineParenthesized = "alignMultilineParenthesized"; //NOI18N
126: public static final String alignMultilineBinaryOp = "alignMultilineBinaryOp"; //NOI18N
127: public static final String alignMultilineTernaryOp = "alignMultilineTernaryOp"; //NOI18N
128: public static final String alignMultilineAssignment = "alignMultilineAssignment"; //NOI18N
129: public static final String alignMultilineFor = "alignMultilineFor"; //NOI18N
130: public static final String alignMultilineArrayInit = "alignMultilineArrayInit"; //NOI18N
131: public static final String placeElseOnNewLine = "placeElseOnNewLine"; //NOI18N
132: public static final String placeWhileOnNewLine = "placeWhileOnNewLine"; //NOI18N
133: public static final String placeCatchOnNewLine = "placeCatchOnNewLine"; //NOI18N
134: public static final String placeFinallyOnNewLine = "placeFinallyOnNewLine"; //NOI18N
135: public static final String placeNewLineAfterModifiers = "placeNewLineAfterModifiers"; //NOI18N
136:
137: public static final String wrapExtendsImplementsKeyword = "wrapExtendsImplementsKeyword"; //NOI18N
138: public static final String wrapExtendsImplementsList = "wrapExtendsImplementsList"; //NOI18N
139: public static final String wrapMethodParams = "wrapMethodParams"; //NOI18N
140: public static final String wrapThrowsKeyword = "wrapThrowsKeyword"; //NOI18N
141: public static final String wrapThrowsList = "wrapThrowsList"; //NOI18N
142: public static final String wrapMethodCallArgs = "wrapMethodCallArgs"; //NOI18N
143: public static final String wrapAnnotationArgs = "wrapAnnotationArgs"; //NOI18N
144: public static final String wrapChainedMethodCalls = "wrapChainedMethodCalls"; //NOI18N
145: public static final String wrapArrayInit = "wrapArrayInit"; //NOI18N
146: public static final String wrapFor = "wrapFor"; //NOI18N
147: public static final String wrapForStatement = "wrapForStatement"; //NOI18N
148: public static final String wrapIfStatement = "wrapIfStatement"; //NOI18N
149: public static final String wrapWhileStatement = "wrapWhileStatement"; //NOI18N
150: public static final String wrapDoWhileStatement = "wrapDoWhileStatement"; //NOI18N
151: public static final String wrapAssert = "wrapAssert"; //NOI18N
152: public static final String wrapEnumConstants = "wrapEnumConstants"; //NOI18N
153: public static final String wrapAnnotations = "wrapAnnotations"; //NOI18N
154: public static final String wrapBinaryOps = "wrapBinaryOps"; //NOI18N
155: public static final String wrapTernaryOps = "wrapTernaryOps"; //NOI18N
156: public static final String wrapAssignOps = "wrapAssignOps"; //NOI18N
157:
158: public static final String blankLinesBeforePackage = "blankLinesBeforePackage"; //NOI18N
159: public static final String blankLinesAfterPackage = "blankLinesAfterPackage"; //NOI18N
160: public static final String blankLinesBeforeImports = "blankLinesBeforeImports"; //NOI18N
161: public static final String blankLinesAfterImports = "blankLinesAfterImports"; //NOI18N
162: public static final String blankLinesBeforeClass = "blankLinesBeforeClass"; //NOI18N
163: public static final String blankLinesAfterClass = "blankLinesAfterClass"; //NOI18N
164: public static final String blankLinesAfterClassHeader = "blankLinesAfterClassHeader"; //NOI18N
165: public static final String blankLinesBeforeFields = "blankLinesBeforeFields"; //NOI18N
166: public static final String blankLinesAfterFields = "blankLinesAfterFields"; //NOI18N
167: public static final String blankLinesBeforeMethods = "blankLinesBeforeMethods"; //NOI18N
168: public static final String blankLinesAfterMethods = "blankLinesAfterMethods"; //NOI18N
169:
170: public static final String spaceBeforeWhile = "spaceBeforeWhile"; //NOI18N
171: public static final String spaceBeforeElse = "spaceBeforeElse"; //NOI18N
172: public static final String spaceBeforeCatch = "spaceBeforeCatch"; //NOI18N
173: public static final String spaceBeforeFinally = "spaceBeforeFinally"; //NOI18N
174: public static final String spaceBeforeMethodDeclParen = "spaceBeforeMethodDeclParen"; //NOI18N
175: public static final String spaceBeforeMethodCallParen = "spaceBeforeMethodCallParen"; //NOI18N
176: public static final String spaceBeforeIfParen = "spaceBeforeIfParen"; //NOI18N
177: public static final String spaceBeforeForParen = "spaceBeforeForParen"; //NOI18N
178: public static final String spaceBeforeWhileParen = "spaceBeforeWhileParen"; //NOI18N
179: public static final String spaceBeforeCatchParen = "spaceBeforeCatchParen"; //NOI18N
180: public static final String spaceBeforeSwitchParen = "spaceBeforeSwitchParen"; //NOI18N
181: public static final String spaceBeforeSynchronizedParen = "spaceBeforeSynchronizedParen"; //NOI18N
182: public static final String spaceBeforeAnnotationParen = "spaceBeforeAnnotationParen"; //NOI18N
183: public static final String spaceAroundUnaryOps = "spaceAroundUnaryOps"; //NOI18N
184: public static final String spaceAroundBinaryOps = "spaceAroundBinaryOps"; //NOI18N
185: public static final String spaceAroundTernaryOps = "spaceAroundTernaryOps"; //NOI18N
186: public static final String spaceAroundAssignOps = "spaceAroundAssignOps"; //NOI18N
187: public static final String spaceBeforeClassDeclLeftBrace = "spaceBeforeClassDeclLeftBrace"; //NOI18N
188: public static final String spaceBeforeMethodDeclLeftBrace = "spaceBeforeMethodDeclLeftBrace"; //NOI18N
189: public static final String spaceBeforeIfLeftBrace = "spaceBeforeIfLeftBrace"; //NOI18N
190: public static final String spaceBeforeElseLeftBrace = "spaceBeforeElseLeftBrace"; //NOI18N
191: public static final String spaceBeforeWhileLeftBrace = "spaceBeforeWhileLeftBrace"; //NOI18N
192: public static final String spaceBeforeForLeftBrace = "spaceBeforeForLeftBrace"; //NOI18N
193: public static final String spaceBeforeDoLeftBrace = "spaceBeforeDoLeftBrace"; //NOI18N
194: public static final String spaceBeforeSwitchLeftBrace = "spaceBeforeSwitchLeftBrace"; //NOI18N
195: public static final String spaceBeforeTryLeftBrace = "spaceBeforeTryLeftBrace"; //NOI18N
196: public static final String spaceBeforeCatchLeftBrace = "spaceBeforeCatchLeftBrace"; //NOI18N
197: public static final String spaceBeforeFinallyLeftBrace = "spaceBeforeFinallyLeftBrace"; //NOI18N
198: public static final String spaceBeforeSynchronizedLeftBrace = "spaceBeforeSynchronizedLeftBrace"; //NOI18N
199: public static final String spaceBeforeStaticInitLeftBrace = "spaceBeforeStaticInitLeftBrace"; //NOI18N
200: public static final String spaceBeforeArrayInitLeftBrace = "spaceBeforeArrayInitLeftBrace"; //NOI18N
201: public static final String spaceWithinParens = "spaceWithinParens"; //NOI18N
202: public static final String spaceWithinMethodDeclParens = "spaceWithinMethodDeclParens"; //NOI18N
203: public static final String spaceWithinMethodCallParens = "spaceWithinMethodCallParens"; //NOI18N
204: public static final String spaceWithinIfParens = "spaceWithinIfParens"; //NOI18N
205: public static final String spaceWithinForParens = "spaceWithinForParens"; //NOI18N
206: public static final String spaceWithinWhileParens = "spaceWithinWhileParens"; //NOI18N
207: public static final String spaceWithinSwitchParens = "spaceWithinSwitchParens"; //NOI18N
208: public static final String spaceWithinCatchParens = "spaceWithinCatchParens"; //NOI18N
209: public static final String spaceWithinSynchronizedParens = "spaceWithinSynchronizedParens"; //NOI18N
210: public static final String spaceWithinTypeCastParens = "spaceWithinTypeCastParens"; //NOI18N
211: public static final String spaceWithinAnnotationParens = "spaceWithinAnnotationParens"; //NOI18N
212: public static final String spaceWithinBraces = "spaceWithinBraces"; //NOI18N
213: public static final String spaceWithinArrayInitBrackets = "spaceWithinArrayInitBrackets"; //NOI18N
214: public static final String spaceBeforeComma = "spaceBeforeComma"; //NOI18N
215: public static final String spaceAfterComma = "spaceAfterComma"; //NOI18N
216: public static final String spaceBeforeSemi = "spaceBeforeSemi"; //NOI18N
217: public static final String spaceAfterSemi = "spaceAfterSemi"; //NOI18N
218: public static final String spaceBeforeColon = "spaceBeforeColon"; //NOI18N
219: public static final String spaceAfterColon = "spaceAfterColon"; //NOI18N
220: public static final String spaceAfterTypeCast = "spaceAfterTypeCast"; //NOI18N
221:
222: public static final String useSingleClassImport = "useSingleClassImport"; //NOI18N
223: public static final String useFQNs = "useFQNs"; //NOI18N
224: public static final String countForUsingStarImport = "countForUsingStarImport"; //NOI18N
225: public static final String countForUsingStaticStarImport = "countForUsingStaticStarImport"; //NOI18N
226: public static final String packagesForStarImport = "packagesForStarImport"; //NOI18N
227: public static final String importsOrder = "importsOrder"; //NOI18N
228:
229: public static CodeStyleProducer codeStyleProducer;
230:
231: public static Preferences lastValues;
232:
233: private static Class<? extends EditorKit> kitClass;
234:
235: private static final String DEFAULT_PROFILE = "default"; // NOI18N
236:
237: private FmtOptions() {
238: }
239:
240: public static int getDefaultAsInt(String key) {
241: return Integer.parseInt(defaults.get(key));
242: }
243:
244: public static boolean getDefaultAsBoolean(String key) {
245: return Boolean.parseBoolean(defaults.get(key));
246: }
247:
248: public static String getDefaultAsString(String key) {
249: return defaults.get(key);
250: }
251:
252: public static Preferences getPreferences(String profileId) {
253: return NbPreferences.forModule(CodeStyle.class).node(
254: "CodeStyle").node(profileId);
255: }
256:
257: public static boolean getGlobalExpandTabToSpaces() {
258: Formatter f = (Formatter) Settings.getValue(getKitClass(),
259: "formatter");
260: if (f != null)
261: return f.expandTabs();
262: return getDefaultAsBoolean(expandTabToSpaces);
263: }
264:
265: public static int getGlobalTabSize() {
266: Integer i = (Integer) Settings.getValue(getKitClass(),
267: SettingsNames.TAB_SIZE);
268: return i != null ? i.intValue() : getDefaultAsInt(tabSize);
269: }
270:
271: public static int getGlobalIndentSize() {
272: Formatter f = (Formatter) Settings.getValue(getKitClass(),
273: "formatter");
274: if (f != null)
275: return f.getShiftWidth();
276: return getDefaultAsInt(indentSize);
277: }
278:
279: public static int getGlobalRightMargin() {
280: Integer i = (Integer) Settings.getValue(getKitClass(),
281: SettingsNames.TEXT_LIMIT_WIDTH);
282: return i != null ? i.intValue() : getDefaultAsInt(rightMargin);
283: }
284:
285: public static Class<? extends EditorKit> getKitClass() {
286: if (kitClass == null) {
287: EditorKit kit = MimeLookup.getLookup(
288: MimePath.get("text/x-java"))
289: .lookup(EditorKit.class); //NOI18N
290: kitClass = kit != null ? kit.getClass() : EditorKit.class;
291: }
292: return kitClass;
293: }
294:
295: public static void flush() {
296: try {
297: getPreferences(getCurrentProfileId()).flush();
298: } catch (BackingStoreException e) {
299: Exceptions.printStackTrace(e);
300: }
301: }
302:
303: public static String getCurrentProfileId() {
304: return DEFAULT_PROFILE;
305: }
306:
307: public static CodeStyle createCodeStyle(Preferences p) {
308: CodeStyle.getDefault(null);
309: return codeStyleProducer.create(p);
310: }
311:
312: public static boolean isInteger(String optionID) {
313: String value = defaults.get(optionID);
314:
315: try {
316: Integer.parseInt(value);
317: return true;
318: } catch (NumberFormatException numberFormatException) {
319: return false;
320: }
321: }
322:
323: public static String getLastValue(String optionID) {
324: Preferences p = lastValues == null ? getPreferences(getCurrentProfileId())
325: : lastValues;
326: return p.get(optionID, getDefaultAsString(optionID));
327: }
328:
329: // Private section ---------------------------------------------------------
330:
331: private static final String TRUE = "true"; // NOI18N
332: private static final String FALSE = "false"; // NOI18N
333:
334: private static final String WRAP_ALWAYS = WrapStyle.WRAP_ALWAYS
335: .name();
336: private static final String WRAP_IF_LONG = WrapStyle.WRAP_IF_LONG
337: .name();
338: private static final String WRAP_NEVER = WrapStyle.WRAP_NEVER
339: .name();
340:
341: private static final String BP_NEW_LINE = BracePlacement.NEW_LINE
342: .name();
343: private static final String BP_NEW_LINE_HALF_INDENTED = BracePlacement.NEW_LINE_HALF_INDENTED
344: .name();
345: private static final String BP_NEW_LINE_INDENTED = BracePlacement.NEW_LINE_INDENTED
346: .name();
347: private static final String BP_SAME_LINE = BracePlacement.SAME_LINE
348: .name();
349:
350: private static final String BGS_ELIMINATE = BracesGenerationStyle.ELIMINATE
351: .name();
352: private static final String BGS_LEAVE_ALONE = BracesGenerationStyle.LEAVE_ALONE
353: .name();
354: private static final String BGS_GENERATE = BracesGenerationStyle.GENERATE
355: .name();
356:
357: private static Map<String, String> defaults;
358:
359: static {
360: createDefaults();
361: }
362:
363: private static void createDefaults() {
364: String defaultValues[][] = { { expandTabToSpaces, TRUE }, //NOI18N
365: { tabSize, "4" }, //NOI18N
366: { indentSize, "4" }, //NOI18N
367: { continuationIndentSize, "8" }, //NOI18N
368: { labelIndent, "0" }, //NOI18N
369: { absoluteLabelIndent, FALSE }, //NOI18N
370: { indentTopLevelClassMembers, TRUE }, //NOI18N
371: { indentCasesFromSwitch, TRUE }, //NOI18N
372: { rightMargin, "120" }, //NOI18N
373:
374: { preferLongerNames, TRUE }, //NOI18N
375: { fieldNamePrefix, "" }, //NOI18N // XXX null
376: { fieldNameSuffix, "" }, //NOI18N // XXX null
377: { staticFieldNamePrefix, "" }, //NOI18N // XXX null
378: { staticFieldNameSuffix, "" }, //NOI18N // XXX null
379: { parameterNamePrefix, "" }, //NOI18N // XXX null
380: { parameterNameSuffix, "" }, //NOI18N // XXX null
381: { localVarNamePrefix, "" }, //NOI18N // XXX null
382: { localVarNameSuffix, "" }, //NOI18N // XXX null
383: { qualifyFieldAccess, FALSE }, //NOI18N // XXX
384: { useIsForBooleanGetters, TRUE }, //NOI18N
385: { addOverrideAnnotation, TRUE }, //NOI18N
386: { makeLocalVarsFinal, FALSE }, //NOI18N
387: { makeParametersFinal, FALSE }, //NOI18N
388: { classMembersOrder, "" }, //NOI18N // XXX
389:
390: { classDeclBracePlacement, BP_SAME_LINE }, //NOI18N
391: { methodDeclBracePlacement, BP_SAME_LINE }, //NOI18N
392: { otherBracePlacement, BP_SAME_LINE }, //NOI18N
393: { specialElseIf, TRUE }, //NOI18N
394: { redundantIfBraces, BGS_GENERATE }, //NOI18N
395: { redundantForBraces, BGS_GENERATE }, //NOI18N
396: { redundantWhileBraces, BGS_GENERATE }, //NOI18N
397: { redundantDoWhileBraces, BGS_GENERATE }, //NOI18N
398: { alignMultilineMethodParams, FALSE }, //NOI18N
399: { alignMultilineCallArgs, FALSE }, //NOI18N
400: { alignMultilineAnnotationArgs, FALSE }, //NOI18N
401: { alignMultilineImplements, FALSE }, //NOI18N
402: { alignMultilineThrows, FALSE }, //NOI18N
403: { alignMultilineParenthesized, FALSE }, //NOI18N
404: { alignMultilineBinaryOp, FALSE }, //NOI18N
405: { alignMultilineTernaryOp, FALSE }, //NOI18N
406: { alignMultilineAssignment, FALSE }, //NOI18N
407: { alignMultilineFor, FALSE }, //NOI18N
408: { alignMultilineArrayInit, FALSE }, //NOI18N
409: { placeElseOnNewLine, FALSE }, //NOI18N
410: { placeWhileOnNewLine, FALSE }, //NOI18N
411: { placeCatchOnNewLine, FALSE }, //NOI18N
412: { placeFinallyOnNewLine, FALSE }, //NOI18N
413: { placeNewLineAfterModifiers, FALSE }, //NOI18N
414:
415: { wrapExtendsImplementsKeyword, WRAP_NEVER }, //NOI18N
416: { wrapExtendsImplementsList, WRAP_NEVER }, //NOI18N
417: { wrapMethodParams, WRAP_NEVER }, //NOI18N
418: { wrapThrowsKeyword, WRAP_NEVER }, //NOI18N
419: { wrapThrowsList, WRAP_NEVER }, //NOI18N
420: { wrapMethodCallArgs, WRAP_NEVER }, //NOI18N
421: { wrapAnnotationArgs, WRAP_NEVER }, //NOI18N
422: { wrapChainedMethodCalls, WRAP_NEVER }, //NOI18N
423: { wrapArrayInit, WRAP_NEVER }, //NOI18N
424: { wrapFor, WRAP_NEVER }, //NOI18N
425: { wrapForStatement, WRAP_ALWAYS }, //NOI18N
426: { wrapIfStatement, WRAP_ALWAYS }, //NOI18N
427: { wrapWhileStatement, WRAP_ALWAYS }, //NOI18N
428: { wrapDoWhileStatement, WRAP_ALWAYS }, //NOI18N
429: { wrapAssert, WRAP_NEVER }, //NOI18N
430: { wrapEnumConstants, WRAP_NEVER }, //NOI18N
431: { wrapAnnotations, WRAP_ALWAYS }, //NOI18N
432: { wrapBinaryOps, WRAP_NEVER }, //NOI18N
433: { wrapTernaryOps, WRAP_NEVER }, //NOI18N
434: { wrapAssignOps, WRAP_NEVER }, //NOI18N
435:
436: { blankLinesBeforePackage, "0" }, //NOI18N
437: { blankLinesAfterPackage, "1" }, //NOI18N
438: { blankLinesBeforeImports, "1" }, //NOI18N
439: { blankLinesAfterImports, "1" }, //NOI18N
440: { blankLinesBeforeClass, "1" }, //NOI18N
441: { blankLinesAfterClass, "0" }, //NOI18N
442: { blankLinesAfterClassHeader, "1" }, //NOI18N
443: { blankLinesBeforeFields, "0" }, //NOI18N
444: { blankLinesAfterFields, "0" }, //NOI18N
445: { blankLinesBeforeMethods, "1" }, //NOI18N
446: { blankLinesAfterMethods, "0" }, //NOI18N
447:
448: { spaceBeforeWhile, TRUE }, //NOI18N // XXX
449: { spaceBeforeElse, TRUE }, //NOI18N // XXX
450: { spaceBeforeCatch, TRUE }, //NOI18N // XXX
451: { spaceBeforeFinally, TRUE }, //NOI18N // XXX
452: { spaceBeforeMethodDeclParen, FALSE }, //NOI18N
453: { spaceBeforeMethodCallParen, FALSE }, //NOI18N
454: { spaceBeforeIfParen, TRUE }, //NOI18N
455: { spaceBeforeForParen, TRUE }, //NOI18N
456: { spaceBeforeWhileParen, TRUE }, //NOI18N
457: { spaceBeforeCatchParen, TRUE }, //NOI18N
458: { spaceBeforeSwitchParen, TRUE }, //NOI18N
459: { spaceBeforeSynchronizedParen, TRUE }, //NOI18N
460: { spaceBeforeAnnotationParen, FALSE }, //NOI18N
461: { spaceAroundUnaryOps, FALSE }, //NOI18N
462: { spaceAroundBinaryOps, TRUE }, //NOI18N
463: { spaceAroundTernaryOps, TRUE }, //NOI18N
464: { spaceAroundAssignOps, TRUE }, //NOI18N
465: { spaceBeforeClassDeclLeftBrace, TRUE }, //NOI18N
466: { spaceBeforeMethodDeclLeftBrace, TRUE }, //NOI18N
467: { spaceBeforeIfLeftBrace, TRUE }, //NOI18N
468: { spaceBeforeElseLeftBrace, TRUE }, //NOI18N
469: { spaceBeforeWhileLeftBrace, TRUE }, //NOI18N
470: { spaceBeforeForLeftBrace, TRUE }, //NOI18N
471: { spaceBeforeDoLeftBrace, TRUE }, //NOI18N
472: { spaceBeforeSwitchLeftBrace, TRUE }, //NOI18N
473: { spaceBeforeTryLeftBrace, TRUE }, //NOI18N
474: { spaceBeforeCatchLeftBrace, TRUE }, //NOI18N
475: { spaceBeforeFinallyLeftBrace, TRUE }, //NOI18N
476: { spaceBeforeSynchronizedLeftBrace, TRUE }, //NOI18N
477: { spaceBeforeStaticInitLeftBrace, TRUE }, //NOI18N
478: { spaceBeforeArrayInitLeftBrace, FALSE }, //NOI18N
479: { spaceWithinParens, FALSE }, //NOI18N
480: { spaceWithinMethodDeclParens, FALSE }, //NOI18N
481: { spaceWithinMethodCallParens, FALSE }, //NOI18N
482: { spaceWithinIfParens, FALSE }, //NOI18N
483: { spaceWithinForParens, FALSE }, //NOI18N
484: { spaceWithinWhileParens, FALSE }, //NOI18N
485: { spaceWithinSwitchParens, FALSE }, //NOI18N
486: { spaceWithinCatchParens, FALSE }, //NOI18N
487: { spaceWithinSynchronizedParens, FALSE }, //NOI18N
488: { spaceWithinTypeCastParens, FALSE }, //NOI18N
489: { spaceWithinAnnotationParens, FALSE }, //NOI18N
490: { spaceWithinBraces, FALSE }, //NOI18N
491: { spaceWithinArrayInitBrackets, FALSE }, //NOI18N
492: { spaceBeforeComma, FALSE }, //NOI18N
493: { spaceAfterComma, TRUE }, //NOI18N
494: { spaceBeforeSemi, FALSE }, //NOI18N
495: { spaceAfterSemi, TRUE }, //NOI18N
496: { spaceBeforeColon, TRUE }, //NOI18N
497: { spaceAfterColon, TRUE }, //NOI18N
498: { spaceAfterTypeCast, TRUE }, //NOI18N
499:
500: { useSingleClassImport, TRUE }, //NOI18N
501: { useFQNs, FALSE }, //NOI18N
502: { countForUsingStarImport, "5" }, //NOI18N
503: { countForUsingStaticStarImport, "3" }, //NOI18N // XXX
504: { packagesForStarImport, "" }, //NOI18N // XXX
505: { importsOrder, "" }, //NOI18N // XXX
506: };
507:
508: defaults = new HashMap<String, String>();
509:
510: for (java.lang.String[] strings : defaultValues) {
511: defaults.put(strings[0], strings[1]);
512: }
513:
514: }
515:
516: // Support section ---------------------------------------------------------
517:
518: public static class CategorySupport extends
519: FormatingOptionsPanel.Category implements ActionListener,
520: DocumentListener {
521:
522: public static final String OPTION_ID = "org.netbeans.modules.java.ui.FormatingOptions.ID";
523:
524: private static final int LOAD = 0;
525: private static final int STORE = 1;
526: private static final int ADD_LISTENERS = 2;
527:
528: private static final ComboItem bracePlacement[] = new ComboItem[] {
529: new ComboItem(BracePlacement.SAME_LINE.name(),
530: "LBL_bp_SAME_LINE"), // NOI18N
531: new ComboItem(BracePlacement.NEW_LINE.name(),
532: "LBL_bp_NEW_LINE"), // NOI18N
533: new ComboItem(BracePlacement.NEW_LINE_HALF_INDENTED
534: .name(), "LBL_bp_NEW_LINE_HALF_INDENTED"), // NOI18N
535: new ComboItem(BracePlacement.NEW_LINE_INDENTED.name(),
536: "LBL_bp_NEW_LINE_INDENTED") // NOI18N
537: };
538: private static final ComboItem bracesGeneration[] = new ComboItem[] {
539: new ComboItem(BracesGenerationStyle.GENERATE.name(),
540: "LBL_bg_GENERATE"), // NOI18N
541: new ComboItem(BracesGenerationStyle.LEAVE_ALONE.name(),
542: "LBL_bg_LEAVE_ALONE"), // NOI18N
543: new ComboItem(BracesGenerationStyle.ELIMINATE.name(),
544: "LBL_bg_ELIMINATE") // NOI18N
545: };
546:
547: private static final ComboItem wrap[] = new ComboItem[] {
548: new ComboItem(WrapStyle.WRAP_ALWAYS.name(),
549: "LBL_wrp_WRAP_ALWAYS"), // NOI18N
550: new ComboItem(WrapStyle.WRAP_IF_LONG.name(),
551: "LBL_wrp_WRAP_IF_LONG"), // NOI18N
552: new ComboItem(WrapStyle.WRAP_NEVER.name(),
553: "LBL_wrp_WRAP_NEVER") // NOI18N
554: };
555:
556: private String previewText = NbBundle.getMessage(
557: FmtOptions.class, "SAMPLE_Default");
558: private String forcedOptions[][];
559:
560: private boolean changed = false;
561: private JPanel panel;
562: private final PropertyChangeSupport pcs = new PropertyChangeSupport(
563: this );
564:
565: public CategorySupport(String nameKey, JPanel panel,
566: String previewText, String[]... forcedOptions) {
567: super (nameKey);
568: this .panel = panel;
569: this .previewText = previewText == null ? this .previewText
570: : previewText;
571: this .forcedOptions = forcedOptions;
572: addListeners();
573: }
574:
575: protected void addListeners() {
576: scan(panel, ADD_LISTENERS, null);
577: }
578:
579: public void update() {
580: scan(panel, LOAD, null);
581: }
582:
583: public void applyChanges() {
584: scan(panel, STORE, null);
585: }
586:
587: public void storeTo(Preferences preferences) {
588: scan(panel, STORE, preferences);
589: }
590:
591: public void refreshPreview(JEditorPane pane, Preferences p) {
592:
593: for (String[] option : forcedOptions) {
594: p.put(option[0], option[1]);
595: }
596:
597: try {
598: int rm = p.getInt(rightMargin,
599: getDefaultAsInt(rightMargin));
600: pane.putClientProperty("TextLimitLine", rm);
601: } catch (NumberFormatException e) {
602: // Ignore it
603: }
604:
605: CodeStyle codeStyle = FmtOptions.createCodeStyle(p);
606: pane.setText(Reformatter.reformat(previewText, codeStyle));
607: }
608:
609: public void cancel() {
610: // Usually does not need to do anything
611: }
612:
613: public boolean isValid() {
614: return true; // Should almost always be OK
615: }
616:
617: public boolean isChanged() {
618: return changed;
619: }
620:
621: public JComponent getComponent(Lookup masterLookup) {
622: return panel;
623: }
624:
625: public HelpCtx getHelpCtx() {
626: return null;
627: }
628:
629: public void addPropertyChangeListener(PropertyChangeListener l) {
630: pcs.addPropertyChangeListener(l);
631: }
632:
633: public void removePropertyChangeListener(
634: PropertyChangeListener l) {
635: pcs.removePropertyChangeListener(l);
636: }
637:
638: void changed() {
639: if (!changed) {
640: changed = true;
641: pcs.firePropertyChange(
642: OptionsPanelController.PROP_CHANGED, false,
643: true);
644: }
645: pcs.firePropertyChange(OptionsPanelController.PROP_VALID,
646: null, null);
647: }
648:
649: // ActionListener implementation ---------------------------------------
650:
651: public void actionPerformed(ActionEvent e) {
652: changed();
653: }
654:
655: // DocumentListener implementation -------------------------------------
656:
657: public void insertUpdate(DocumentEvent e) {
658: changed();
659: }
660:
661: public void removeUpdate(DocumentEvent e) {
662: changed();
663: }
664:
665: public void changedUpdate(DocumentEvent e) {
666: changed();
667: }
668:
669: // Private methods -----------------------------------------------------
670:
671: private void scan(Container container, int what, Preferences p) {
672: for (Component c : container.getComponents()) {
673: if (c instanceof JComponent) {
674: JComponent jc = (JComponent) c;
675: Object o = jc.getClientProperty(OPTION_ID);
676: if (o != null && o instanceof String) {
677: switch (what) {
678: case LOAD:
679: loadData(jc, (String) o);
680: break;
681: case STORE:
682: storeData(jc, (String) o, p);
683: break;
684: case ADD_LISTENERS:
685: addListener(jc);
686: break;
687: }
688: }
689: }
690: if (c instanceof Container) {
691: scan((Container) c, what, p);
692: }
693: }
694:
695: }
696:
697: /** Very smart method which tries to set the values in the components correctly
698: */
699: private void loadData(JComponent jc, String optionID) {
700:
701: Preferences node = getPreferences(getCurrentProfileId());
702:
703: if (jc instanceof JTextField) {
704: JTextField field = (JTextField) jc;
705: field.setText(node.get(optionID,
706: getDefaultAsString(optionID)));
707: } else if (jc instanceof JCheckBox) {
708: JCheckBox checkBox = (JCheckBox) jc;
709: boolean df = getDefaultAsBoolean(optionID);
710: checkBox.setSelected(node.getBoolean(optionID, df));
711: } else if (jc instanceof JComboBox) {
712: JComboBox cb = (JComboBox) jc;
713: String value = node.get(optionID,
714: getDefaultAsString(optionID));
715: ComboBoxModel model = createModel(value);
716: cb.setModel(model);
717: ComboItem item = whichItem(value, model);
718: cb.setSelectedItem(item);
719: }
720:
721: }
722:
723: private void storeData(JComponent jc, String optionID,
724: Preferences p) {
725:
726: Preferences node = p == null ? getPreferences(getCurrentProfileId())
727: : p;
728:
729: if (jc instanceof JTextField) {
730: JTextField field = (JTextField) jc;
731:
732: String text = field.getText();
733:
734: if (isInteger(optionID)) {
735: try {
736: int i = Integer.parseInt(text);
737: } catch (NumberFormatException e) {
738: text = getLastValue(optionID);
739: }
740: }
741:
742: // XXX test for numbers
743: node.put(optionID, text);
744: } else if (jc instanceof JCheckBox) {
745: JCheckBox checkBox = (JCheckBox) jc;
746: node.putBoolean(optionID, checkBox.isSelected());
747: } else if (jc instanceof JComboBox) {
748: JComboBox cb = (JComboBox) jc;
749: // Logger.global.info( cb.getSelectedItem() + " " + optionID);
750: node.put(optionID,
751: ((ComboItem) cb.getSelectedItem()).value);
752: }
753: }
754:
755: private void addListener(JComponent jc) {
756: if (jc instanceof JTextField) {
757: JTextField field = (JTextField) jc;
758: field.addActionListener(this );
759: field.getDocument().addDocumentListener(this );
760: } else if (jc instanceof JCheckBox) {
761: JCheckBox checkBox = (JCheckBox) jc;
762: checkBox.addActionListener(this );
763: } else if (jc instanceof JComboBox) {
764: JComboBox cb = (JComboBox) jc;
765: cb.addActionListener(this );
766: }
767: }
768:
769: private ComboBoxModel createModel(String value) {
770:
771: // is it braces placement?
772: for (ComboItem comboItem : bracePlacement) {
773: if (value.equals(comboItem.value)) {
774: return new DefaultComboBoxModel(bracePlacement);
775: }
776: }
777:
778: // is it braces generation?
779: for (ComboItem comboItem : bracesGeneration) {
780: if (value.equals(comboItem.value)) {
781: return new DefaultComboBoxModel(bracesGeneration);
782: }
783: }
784:
785: // is it wrap
786: for (ComboItem comboItem : wrap) {
787: if (value.equals(comboItem.value)) {
788: return new DefaultComboBoxModel(wrap);
789: }
790: }
791:
792: return null;
793: }
794:
795: private static ComboItem whichItem(String value,
796: ComboBoxModel model) {
797:
798: for (int i = 0; i < model.getSize(); i++) {
799: ComboItem item = (ComboItem) model.getElementAt(i);
800: if (value.equals(item.value)) {
801: return item;
802: }
803: }
804: return null;
805: }
806:
807: private static class ComboItem {
808:
809: String value;
810: String displayName;
811:
812: public ComboItem(String value, String key) {
813: this .value = value;
814: this .displayName = NbBundle.getMessage(
815: FmtOptions.class, key);
816: }
817:
818: @Override
819: public String toString() {
820: return displayName;
821: }
822:
823: }
824:
825: }
826:
827: public static interface CodeStyleProducer {
828:
829: public CodeStyle create(Preferences preferences);
830:
831: }
832:
833: }
|