Lines 41-277
Link Here
|
41 |
* Version 2 license, then the option applies only if the new code is |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
43 |
*/ |
44 |
package org.netbeans.modules.java.ui; |
44 |
package org.netbeans.modules.java.uisupport; |
45 |
|
45 |
|
46 |
import java.awt.Component; |
46 |
import org.netbeans.modules.java.preprocessorbridge.api.FmtConstants; |
47 |
import java.awt.Container; |
|
|
48 |
import java.awt.Rectangle; |
49 |
import java.awt.event.ActionEvent; |
50 |
import java.awt.event.ActionListener; |
51 |
import java.util.Arrays; |
52 |
import java.util.HashMap; |
47 |
import java.util.HashMap; |
53 |
import java.util.HashSet; |
|
|
54 |
import java.util.LinkedList; |
55 |
import java.util.List; |
56 |
import java.util.Map; |
48 |
import java.util.Map; |
57 |
import java.util.Set; |
|
|
58 |
import java.util.prefs.AbstractPreferences; |
59 |
import java.util.prefs.BackingStoreException; |
60 |
import java.util.prefs.Preferences; |
49 |
import java.util.prefs.Preferences; |
61 |
import javax.swing.ComboBoxModel; |
|
|
62 |
import javax.swing.DefaultComboBoxModel; |
63 |
import javax.swing.JComboBox; |
64 |
import javax.swing.JComponent; |
65 |
import javax.swing.JEditorPane; |
66 |
import javax.swing.JList; |
67 |
import javax.swing.JPanel; |
68 |
import javax.swing.JSpinner; |
69 |
import javax.swing.JTable; |
70 |
import javax.swing.JTextField; |
71 |
import javax.swing.JToggleButton; |
72 |
import javax.swing.event.ChangeEvent; |
73 |
import javax.swing.event.ChangeListener; |
74 |
import javax.swing.event.DocumentEvent; |
75 |
import javax.swing.event.DocumentListener; |
76 |
import javax.swing.event.ListDataEvent; |
77 |
import javax.swing.event.ListDataListener; |
78 |
import javax.swing.event.TableModelEvent; |
79 |
import javax.swing.event.TableModelListener; |
80 |
import org.netbeans.api.editor.settings.SimpleValueNames; |
81 |
import org.netbeans.api.java.source.CodeStyle; |
50 |
import org.netbeans.api.java.source.CodeStyle; |
82 |
import static org.netbeans.api.java.source.CodeStyle.*; |
51 |
import org.netbeans.api.java.source.CodeStyle.BracePlacement; |
83 |
import org.netbeans.modules.java.source.save.Reformatter; |
52 |
import org.netbeans.api.java.source.CodeStyle.BracesGenerationStyle; |
84 |
import org.netbeans.modules.options.editor.spi.PreferencesCustomizer; |
53 |
import org.netbeans.api.java.source.CodeStyle.InsertionPoint; |
85 |
import org.netbeans.modules.options.editor.spi.PreviewProvider; |
54 |
import org.netbeans.api.java.source.CodeStyle.WrapStyle; |
86 |
|
55 |
import org.openide.util.lookup.ServiceProvider; |
87 |
import org.openide.text.CloneableEditorSupport; |
|
|
88 |
import org.openide.util.HelpCtx; |
89 |
import org.openide.util.NbBundle; |
90 |
|
56 |
|
91 |
/** |
57 |
/** |
92 |
* |
58 |
* |
93 |
* @author phrebejk |
59 |
* @author phrebejk |
94 |
*/ |
60 |
*/ |
95 |
public class FmtOptions { |
61 |
@ServiceProvider(service=FmtConstants.class) |
96 |
|
62 |
public class FmtOptions extends FmtConstants { |
97 |
public static final String expandTabToSpaces = SimpleValueNames.EXPAND_TABS; |
|
|
98 |
public static final String tabSize = SimpleValueNames.TAB_SIZE; |
99 |
public static final String spacesPerTab = SimpleValueNames.SPACES_PER_TAB; |
100 |
public static final String indentSize = SimpleValueNames.INDENT_SHIFT_WIDTH; |
101 |
public static final String continuationIndentSize = "continuationIndentSize"; //NOI18N |
102 |
public static final String labelIndent = "labelIndent"; //NOI18N |
103 |
public static final String absoluteLabelIndent = "absoluteLabelIndent"; //NOI18N |
104 |
public static final String indentTopLevelClassMembers = "indentTopLevelClassMembers"; //NOI18N |
105 |
public static final String indentCasesFromSwitch = "indentCasesFromSwitch"; //NOI18N |
106 |
public static final String rightMargin = SimpleValueNames.TEXT_LIMIT_WIDTH; |
107 |
|
108 |
public static final String addLeadingStarInComment = "addLeadingStarInComment"; //NOI18N |
109 |
|
110 |
public static final String preferLongerNames = "preferLongerNames"; //NOI18N |
111 |
public static final String fieldNamePrefix = "fieldNamePrefix"; //NOI18N |
112 |
public static final String fieldNameSuffix = "fieldNameSuffix"; //NOI18N |
113 |
public static final String staticFieldNamePrefix = "staticFieldNamePrefix"; //NOI18N |
114 |
public static final String staticFieldNameSuffix = "staticFieldNameSuffix"; //NOI18N |
115 |
public static final String parameterNamePrefix = "parameterNamePrefix"; //NOI18N |
116 |
public static final String parameterNameSuffix = "parameterNameSuffix"; //NOI18N |
117 |
public static final String localVarNamePrefix = "localVarNamePrefix"; //NOI18N |
118 |
public static final String localVarNameSuffix = "localVarNameSuffix"; //NOI18N |
119 |
public static final String qualifyFieldAccess = "qualifyFieldAccess"; //NOI18N |
120 |
public static final String useIsForBooleanGetters = "useIsForBooleanGetters"; //NOI18N |
121 |
public static final String addOverrideAnnotation = "addOverrideAnnotation"; //NOI18N |
122 |
public static final String makeLocalVarsFinal = "makeLocalVarsFinal"; //NOI18N |
123 |
public static final String makeParametersFinal = "makeParametersFinal"; //NOI18N |
124 |
public static final String classMembersOrder = "classMembersOrder"; //NOI18N |
125 |
public static final String sortMembersByVisibility = "sortMembersByVisibility"; //NOI18N |
126 |
public static final String visibilityOrder = "visibilityOrder"; //NOI18N |
127 |
public static final String classMemberInsertionPoint = "classMemberInsertionPoint"; //NOI18N |
128 |
|
129 |
public static final String classDeclBracePlacement = "classDeclBracePlacement"; //NOI18N |
130 |
public static final String methodDeclBracePlacement = "methodDeclBracePlacement"; //NOI18N |
131 |
public static final String otherBracePlacement = "otherBracePlacement"; //NOI18N |
132 |
public static final String specialElseIf = "specialElseIf"; //NOI18N |
133 |
public static final String redundantIfBraces = "redundantIfBraces"; //NOI18N |
134 |
public static final String redundantForBraces = "redundantForBraces"; //NOI18N |
135 |
public static final String redundantWhileBraces = "redundantWhileBraces"; //NOI18N |
136 |
public static final String redundantDoWhileBraces = "redundantDoWhileBraces"; //NOI18N |
137 |
public static final String alignMultilineMethodParams = "alignMultilineMethodParams"; //NOI18N |
138 |
public static final String alignMultilineCallArgs = "alignMultilineCallArgs"; //NOI18N |
139 |
public static final String alignMultilineAnnotationArgs = "alignMultilineAnnotationArgs"; //NOI18N |
140 |
public static final String alignMultilineImplements = "alignMultilineImplements"; //NOI18N |
141 |
public static final String alignMultilineThrows = "alignMultilineThrows"; //NOI18N |
142 |
public static final String alignMultilineParenthesized = "alignMultilineParenthesized"; //NOI18N |
143 |
public static final String alignMultilineBinaryOp = "alignMultilineBinaryOp"; //NOI18N |
144 |
public static final String alignMultilineTernaryOp = "alignMultilineTernaryOp"; //NOI18N |
145 |
public static final String alignMultilineAssignment = "alignMultilineAssignment"; //NOI18N |
146 |
public static final String alignMultilineTryResources = "alignMultilineTryResources"; //NOI18N |
147 |
public static final String alignMultilineDisjunctiveCatchTypes = "alignMultilineDisjunctiveCatchTypes"; //NOI18N |
148 |
public static final String alignMultilineFor = "alignMultilineFor"; //NOI18N |
149 |
public static final String alignMultilineArrayInit = "alignMultilineArrayInit"; //NOI18N |
150 |
public static final String placeElseOnNewLine = "placeElseOnNewLine"; //NOI18N |
151 |
public static final String placeWhileOnNewLine = "placeWhileOnNewLine"; //NOI18N |
152 |
public static final String placeCatchOnNewLine = "placeCatchOnNewLine"; //NOI18N |
153 |
public static final String placeFinallyOnNewLine = "placeFinallyOnNewLine"; //NOI18N |
154 |
public static final String placeNewLineAfterModifiers = "placeNewLineAfterModifiers"; //NOI18N |
155 |
|
156 |
public static final String wrapExtendsImplementsKeyword = "wrapExtendsImplementsKeyword"; //NOI18N |
157 |
public static final String wrapExtendsImplementsList = "wrapExtendsImplementsList"; //NOI18N |
158 |
public static final String wrapMethodParams = "wrapMethodParams"; //NOI18N |
159 |
public static final String wrapThrowsKeyword = "wrapThrowsKeyword"; //NOI18N |
160 |
public static final String wrapThrowsList = "wrapThrowsList"; //NOI18N |
161 |
public static final String wrapMethodCallArgs = "wrapMethodCallArgs"; //NOI18N |
162 |
public static final String wrapAnnotationArgs = "wrapAnnotationArgs"; //NOI18N |
163 |
public static final String wrapChainedMethodCalls = "wrapChainedMethodCalls"; //NOI18N |
164 |
public static final String wrapAfterDotInChainedMethodCalls = "wrapAfterDotInChainedMethodCalls"; //NOI18N |
165 |
public static final String wrapArrayInit = "wrapArrayInit"; //NOI18N |
166 |
public static final String wrapTryResources = "wrapTryResources"; //NOI18N |
167 |
public static final String wrapDisjunctiveCatchTypes = "wrapDisjunctiveCatchTypes"; //NOI18N |
168 |
public static final String wrapFor = "wrapFor"; //NOI18N |
169 |
public static final String wrapForStatement = "wrapForStatement"; //NOI18N |
170 |
public static final String wrapIfStatement = "wrapIfStatement"; //NOI18N |
171 |
public static final String wrapWhileStatement = "wrapWhileStatement"; //NOI18N |
172 |
public static final String wrapDoWhileStatement = "wrapDoWhileStatement"; //NOI18N |
173 |
public static final String wrapAssert = "wrapAssert"; //NOI18N |
174 |
public static final String wrapEnumConstants = "wrapEnumConstants"; //NOI18N |
175 |
public static final String wrapAnnotations = "wrapAnnotations"; //NOI18N |
176 |
public static final String wrapBinaryOps = "wrapBinaryOps"; //NOI18N |
177 |
public static final String wrapAfterBinaryOps = "wrapAfterBinaryOps"; //NOI18N |
178 |
public static final String wrapTernaryOps = "wrapTernaryOps"; //NOI18N |
179 |
public static final String wrapAfterTernaryOps = "wrapAfterTernaryOps"; //NOI18N |
180 |
public static final String wrapAssignOps = "wrapAssignOps"; //NOI18N |
181 |
|
182 |
public static final String blankLinesBeforePackage = "blankLinesBeforePackage"; //NOI18N |
183 |
public static final String blankLinesAfterPackage = "blankLinesAfterPackage"; //NOI18N |
184 |
public static final String blankLinesBeforeImports = "blankLinesBeforeImports"; //NOI18N |
185 |
public static final String blankLinesAfterImports = "blankLinesAfterImports"; //NOI18N |
186 |
public static final String blankLinesBeforeClass = "blankLinesBeforeClass"; //NOI18N |
187 |
public static final String blankLinesAfterClass = "blankLinesAfterClass"; //NOI18N |
188 |
public static final String blankLinesAfterClassHeader = "blankLinesAfterClassHeader"; //NOI18N |
189 |
public static final String blankLinesAfterAnonymousClassHeader = "blankLinesAfterAnonymousClassHeader"; //NOI18N |
190 |
public static final String blankLinesBeforeFields = "blankLinesBeforeFields"; //NOI18N |
191 |
public static final String blankLinesAfterFields = "blankLinesAfterFields"; //NOI18N |
192 |
public static final String blankLinesBeforeMethods = "blankLinesBeforeMethods"; //NOI18N |
193 |
public static final String blankLinesAfterMethods = "blankLinesAfterMethods"; //NOI18N |
194 |
|
195 |
public static final String spaceBeforeWhile = "spaceBeforeWhile"; //NOI18N |
196 |
public static final String spaceBeforeElse = "spaceBeforeElse"; //NOI18N |
197 |
public static final String spaceBeforeCatch = "spaceBeforeCatch"; //NOI18N |
198 |
public static final String spaceBeforeFinally = "spaceBeforeFinally"; //NOI18N |
199 |
public static final String spaceBeforeMethodDeclParen = "spaceBeforeMethodDeclParen"; //NOI18N |
200 |
public static final String spaceBeforeMethodCallParen = "spaceBeforeMethodCallParen"; //NOI18N |
201 |
public static final String spaceBeforeIfParen = "spaceBeforeIfParen"; //NOI18N |
202 |
public static final String spaceBeforeForParen = "spaceBeforeForParen"; //NOI18N |
203 |
public static final String spaceBeforeWhileParen = "spaceBeforeWhileParen"; //NOI18N |
204 |
public static final String spaceBeforeTryParen = "spaceBeforeTryParen"; //NOI18N |
205 |
public static final String spaceBeforeCatchParen = "spaceBeforeCatchParen"; //NOI18N |
206 |
public static final String spaceBeforeSwitchParen = "spaceBeforeSwitchParen"; //NOI18N |
207 |
public static final String spaceBeforeSynchronizedParen = "spaceBeforeSynchronizedParen"; //NOI18N |
208 |
public static final String spaceBeforeAnnotationParen = "spaceBeforeAnnotationParen"; //NOI18N |
209 |
public static final String spaceAroundUnaryOps = "spaceAroundUnaryOps"; //NOI18N |
210 |
public static final String spaceAroundBinaryOps = "spaceAroundBinaryOps"; //NOI18N |
211 |
public static final String spaceAroundTernaryOps = "spaceAroundTernaryOps"; //NOI18N |
212 |
public static final String spaceAroundAssignOps = "spaceAroundAssignOps"; //NOI18N |
213 |
public static final String spaceAroundAnnotationValueAssignOps = "spaceAroundAnnotationValueAssignOps"; //NOI18N |
214 |
public static final String spaceBeforeClassDeclLeftBrace = "spaceBeforeClassDeclLeftBrace"; //NOI18N |
215 |
public static final String spaceBeforeMethodDeclLeftBrace = "spaceBeforeMethodDeclLeftBrace"; //NOI18N |
216 |
public static final String spaceBeforeIfLeftBrace = "spaceBeforeIfLeftBrace"; //NOI18N |
217 |
public static final String spaceBeforeElseLeftBrace = "spaceBeforeElseLeftBrace"; //NOI18N |
218 |
public static final String spaceBeforeWhileLeftBrace = "spaceBeforeWhileLeftBrace"; //NOI18N |
219 |
public static final String spaceBeforeForLeftBrace = "spaceBeforeForLeftBrace"; //NOI18N |
220 |
public static final String spaceBeforeDoLeftBrace = "spaceBeforeDoLeftBrace"; //NOI18N |
221 |
public static final String spaceBeforeSwitchLeftBrace = "spaceBeforeSwitchLeftBrace"; //NOI18N |
222 |
public static final String spaceBeforeTryLeftBrace = "spaceBeforeTryLeftBrace"; //NOI18N |
223 |
public static final String spaceBeforeCatchLeftBrace = "spaceBeforeCatchLeftBrace"; //NOI18N |
224 |
public static final String spaceBeforeFinallyLeftBrace = "spaceBeforeFinallyLeftBrace"; //NOI18N |
225 |
public static final String spaceBeforeSynchronizedLeftBrace = "spaceBeforeSynchronizedLeftBrace"; //NOI18N |
226 |
public static final String spaceBeforeStaticInitLeftBrace = "spaceBeforeStaticInitLeftBrace"; //NOI18N |
227 |
public static final String spaceBeforeArrayInitLeftBrace = "spaceBeforeArrayInitLeftBrace"; //NOI18N |
228 |
public static final String spaceWithinParens = "spaceWithinParens"; //NOI18N |
229 |
public static final String spaceWithinMethodDeclParens = "spaceWithinMethodDeclParens"; //NOI18N |
230 |
public static final String spaceWithinMethodCallParens = "spaceWithinMethodCallParens"; //NOI18N |
231 |
public static final String spaceWithinIfParens = "spaceWithinIfParens"; //NOI18N |
232 |
public static final String spaceWithinForParens = "spaceWithinForParens"; //NOI18N |
233 |
public static final String spaceWithinWhileParens = "spaceWithinWhileParens"; //NOI18N |
234 |
public static final String spaceWithinSwitchParens = "spaceWithinSwitchParens"; //NOI18N |
235 |
public static final String spaceWithinTryParens = "spaceWithinTryParens"; //NOI18N |
236 |
public static final String spaceWithinCatchParens = "spaceWithinCatchParens"; //NOI18N |
237 |
public static final String spaceWithinSynchronizedParens = "spaceWithinSynchronizedParens"; //NOI18N |
238 |
public static final String spaceWithinTypeCastParens = "spaceWithinTypeCastParens"; //NOI18N |
239 |
public static final String spaceWithinAnnotationParens = "spaceWithinAnnotationParens"; //NOI18N |
240 |
public static final String spaceWithinBraces = "spaceWithinBraces"; //NOI18N |
241 |
public static final String spaceWithinArrayInitBrackets = "spaceWithinArrayInitBrackets"; //NOI18N |
242 |
public static final String spaceBeforeComma = "spaceBeforeComma"; //NOI18N |
243 |
public static final String spaceAfterComma = "spaceAfterComma"; //NOI18N |
244 |
public static final String spaceBeforeSemi = "spaceBeforeSemi"; //NOI18N |
245 |
public static final String spaceAfterSemi = "spaceAfterSemi"; //NOI18N |
246 |
public static final String spaceBeforeColon = "spaceBeforeColon"; //NOI18N |
247 |
public static final String spaceAfterColon = "spaceAfterColon"; //NOI18N |
248 |
public static final String spaceAfterTypeCast = "spaceAfterTypeCast"; //NOI18N |
249 |
|
250 |
public static final String useSingleClassImport = "useSingleClassImport"; //NOI18N |
251 |
public static final String usePackageImport = "usePackageImport"; //NOI18N |
252 |
public static final String useFQNs = "useFQNs"; //NOI18N |
253 |
public static final String importInnerClasses = "importInnerClasses"; //NOI18N |
254 |
public static final String allowConvertToStarImport = "allowConvertToStarImport"; //NOI18N |
255 |
public static final String countForUsingStarImport = "countForUsingStarImport"; //NOI18N |
256 |
public static final String allowConvertToStaticStarImport = "allowConvertToStaticStarImport"; //NOI18N |
257 |
public static final String countForUsingStaticStarImport = "countForUsingStaticStarImport"; //NOI18N |
258 |
public static final String packagesForStarImport = "packagesForStarImport"; //NOI18N |
259 |
public static final String separateStaticImports = "separateStaticImports"; //NOI18N |
260 |
public static final String importGroupsOrder = "importGroupsOrder"; //NOI18N |
261 |
public static final String separateImportGroups = "separateImportGroups"; //NOI18N |
262 |
|
263 |
public static final String enableCommentFormatting = "enableCommentFormatting"; //NOI18N |
264 |
public static final String enableBlockCommentFormatting = "enableBlockCommentFormatting"; //NOI18N |
265 |
public static final String wrapCommentText = "wrapCommentText"; //NOI18N |
266 |
public static final String wrapOneLineComment = "wrapOneLineComment"; //NOI18N |
267 |
public static final String preserveNewLinesInComments = "preserveNewLinesInComments"; //NOI18N |
268 |
public static final String blankLineAfterJavadocDescription = "blankLineAfterJavadocDescription"; //NOI18N |
269 |
public static final String blankLineAfterJavadocParameterDescriptions = "blankLineAfterJavadocParameterDescriptions"; //NOI18N |
270 |
public static final String blankLineAfterJavadocReturnTag = "blankLineAfterJavadocReturnTag"; //NOI18N |
271 |
public static final String generateParagraphTagOnBlankLines = "generateParagraphTagOnBlankLines"; //NOI18N |
272 |
public static final String alignJavadocParameterDescriptions = "alignJavadocParameterDescriptions"; //NOI18N |
273 |
public static final String alignJavadocReturnDescription = "alignJavadocReturnDescription"; //NOI18N |
274 |
public static final String alignJavadocExceptionDescriptions = "alignJavadocExceptionDescriptions"; //NOI18N |
275 |
|
63 |
|
276 |
public static CodeStyleProducer codeStyleProducer; |
64 |
public static CodeStyleProducer codeStyleProducer; |
277 |
|
65 |
|
Lines 283-299
Link Here
|
283 |
|
71 |
|
284 |
private static final String JAVA = "text/x-java"; //NOI18N |
72 |
private static final String JAVA = "text/x-java"; //NOI18N |
285 |
|
73 |
|
286 |
private FmtOptions() {} |
74 |
public FmtOptions() {} |
287 |
|
75 |
|
288 |
public static int getDefaultAsInt(String key) { |
76 |
|
|
|
77 |
@Override |
78 |
protected int defaultAsInt(String key) { |
289 |
return Integer.parseInt(defaults.get(key)); |
79 |
return Integer.parseInt(defaults.get(key)); |
290 |
} |
80 |
} |
291 |
|
81 |
|
292 |
public static boolean getDefaultAsBoolean(String key) { |
82 |
@Override |
|
|
83 |
protected boolean defaultAsBoolean(String key) { |
293 |
return Boolean.parseBoolean(defaults.get(key)); |
84 |
return Boolean.parseBoolean(defaults.get(key)); |
294 |
} |
85 |
} |
295 |
|
86 |
|
296 |
public static String getDefaultAsString(String key) { |
87 |
@Override |
|
|
88 |
protected String defaultAsString(String key) { |
297 |
return defaults.get(key); |
89 |
return defaults.get(key); |
298 |
} |
90 |
} |
299 |
|
91 |
|
Lines 549-1126
Link Here
|
549 |
} |
341 |
} |
550 |
|
342 |
|
551 |
} |
343 |
} |
552 |
|
|
|
553 |
|
554 |
// Support section --------------------------------------------------------- |
555 |
|
556 |
public static class CategorySupport implements ActionListener, ChangeListener, ListDataListener, TableModelListener, DocumentListener, PreviewProvider, PreferencesCustomizer { |
557 |
|
558 |
public static final String OPTION_ID = "org.netbeans.modules.java.ui.FormatingOptions.ID"; |
559 |
|
560 |
private static final int LOAD = 0; |
561 |
private static final int STORE = 1; |
562 |
private static final int ADD_LISTENERS = 2; |
563 |
|
564 |
private static final ComboItem bracePlacement[] = new ComboItem[] { |
565 |
new ComboItem( BracePlacement.SAME_LINE.name(), "LBL_bp_SAME_LINE" ), // NOI18N |
566 |
new ComboItem( BracePlacement.NEW_LINE.name(), "LBL_bp_NEW_LINE" ), // NOI18N |
567 |
new ComboItem( BracePlacement.NEW_LINE_HALF_INDENTED.name(), "LBL_bp_NEW_LINE_HALF_INDENTED" ), // NOI18N |
568 |
new ComboItem( BracePlacement.NEW_LINE_INDENTED.name(), "LBL_bp_NEW_LINE_INDENTED" ) // NOI18N |
569 |
}; |
570 |
private static final ComboItem bracesGeneration[] = new ComboItem[] { |
571 |
new ComboItem( BracesGenerationStyle.GENERATE.name(), "LBL_bg_GENERATE" ), // NOI18N |
572 |
new ComboItem( BracesGenerationStyle.LEAVE_ALONE.name(), "LBL_bg_LEAVE_ALONE" ), // NOI18N |
573 |
new ComboItem( BracesGenerationStyle.ELIMINATE.name(), "LBL_bg_ELIMINATE" ) // NOI18N |
574 |
}; |
575 |
|
576 |
private static final ComboItem wrap[] = new ComboItem[] { |
577 |
new ComboItem( WrapStyle.WRAP_ALWAYS.name(), "LBL_wrp_WRAP_ALWAYS" ), // NOI18N |
578 |
new ComboItem( WrapStyle.WRAP_IF_LONG.name(), "LBL_wrp_WRAP_IF_LONG" ), // NOI18N |
579 |
new ComboItem( WrapStyle.WRAP_NEVER.name(), "LBL_wrp_WRAP_NEVER" ) // NOI18N |
580 |
}; |
581 |
|
582 |
private static final ComboItem insertionPoint[] = new ComboItem[] { |
583 |
new ComboItem( InsertionPoint.LAST_IN_CATEGORY.name(), "LBL_ip_LAST_IN_CATEGORY" ), // NOI18N |
584 |
new ComboItem( InsertionPoint.FIRST_IN_CATEGORY.name(), "LBL_ip_FIRST_IN_CATEGORY" ), // NOI18N |
585 |
new ComboItem( InsertionPoint.CARET_LOCATION.name(), "LBL_ip_CARET_LOCATION" ) // NOI18N |
586 |
}; |
587 |
|
588 |
protected final String previewText; |
589 |
// private String forcedOptions[][]; |
590 |
|
591 |
// private boolean changed = false; |
592 |
// private boolean loaded = false; |
593 |
private final String id; |
594 |
protected final JPanel panel; |
595 |
private final List<JComponent> components = new LinkedList<JComponent>(); |
596 |
private JEditorPane previewPane; |
597 |
|
598 |
protected final Preferences preferences; |
599 |
protected final Preferences previewPrefs; |
600 |
|
601 |
protected CategorySupport(Preferences preferences, String id, JPanel panel, String previewText, String[]... forcedOptions) { |
602 |
this.preferences = preferences; |
603 |
this.id = id; |
604 |
this.panel = panel; |
605 |
this.previewText = previewText != null ? previewText : NbBundle.getMessage(FmtOptions.class, "SAMPLE_Default"); //NOI18N |
606 |
|
607 |
// Scan the panel for its components |
608 |
scan(panel, components); |
609 |
|
610 |
// Initialize the preview preferences |
611 |
Preferences forcedPrefs = new PreviewPreferences(); |
612 |
for (String[] option : forcedOptions) { |
613 |
forcedPrefs.put( option[0], option[1]); |
614 |
} |
615 |
this.previewPrefs = new ProxyPreferences(forcedPrefs, preferences); |
616 |
|
617 |
// Load and hook up all the components |
618 |
loadFrom(preferences); |
619 |
addListeners(); |
620 |
} |
621 |
|
622 |
protected void addListeners() { |
623 |
scan(ADD_LISTENERS, null); |
624 |
} |
625 |
|
626 |
protected void loadFrom(Preferences preferences) { |
627 |
// loaded = true; |
628 |
scan(LOAD, preferences); |
629 |
// loaded = false; |
630 |
} |
631 |
// |
632 |
// public void applyChanges() { |
633 |
// storeTo(preferences); |
634 |
// } |
635 |
// |
636 |
protected void storeTo(Preferences p) { |
637 |
scan(STORE, p); |
638 |
} |
639 |
|
640 |
protected void notifyChanged() { |
641 |
// if (loaded) |
642 |
// return; |
643 |
storeTo(preferences); |
644 |
refreshPreview(); |
645 |
} |
646 |
|
647 |
protected void loadListData(final JList list, final String optionID, final Preferences p) { |
648 |
} |
649 |
|
650 |
protected void storeListData(final JList list, final String optionID, final Preferences node) { |
651 |
} |
652 |
|
653 |
protected void loadTableData(final JTable table, final String optionID, final Preferences p) { |
654 |
} |
655 |
|
656 |
protected void storeTableData(final JTable table, final String optionID, final Preferences node) { |
657 |
} |
658 |
|
659 |
// ActionListener implementation --------------------------------------- |
660 |
|
661 |
public void actionPerformed(ActionEvent e) { |
662 |
notifyChanged(); |
663 |
} |
664 |
|
665 |
// ChangeListener implementation --------------------------------------- |
666 |
|
667 |
@Override |
668 |
public void stateChanged(ChangeEvent e) { |
669 |
notifyChanged(); |
670 |
} |
671 |
|
672 |
// ListDataListener implementation ----------------------------------- |
673 |
|
674 |
@Override |
675 |
public void contentsChanged(ListDataEvent e) { |
676 |
} |
677 |
|
678 |
@Override |
679 |
public void intervalAdded(ListDataEvent e) { |
680 |
notifyChanged(); |
681 |
} |
682 |
|
683 |
@Override |
684 |
public void intervalRemoved(ListDataEvent e) { |
685 |
} |
686 |
|
687 |
// TableModelListener implementation ----------------------------------- |
688 |
|
689 |
@Override |
690 |
public void tableChanged(TableModelEvent e) { |
691 |
notifyChanged(); |
692 |
} |
693 |
|
694 |
// DocumentListener implementation ------------------------------------- |
695 |
|
696 |
public void insertUpdate(DocumentEvent e) { |
697 |
notifyChanged(); |
698 |
} |
699 |
|
700 |
public void removeUpdate(DocumentEvent e) { |
701 |
notifyChanged(); |
702 |
} |
703 |
|
704 |
public void changedUpdate(DocumentEvent e) { |
705 |
notifyChanged(); |
706 |
} |
707 |
|
708 |
// PreviewProvider methods ----------------------------------------------------- |
709 |
|
710 |
public JComponent getPreviewComponent() { |
711 |
if (previewPane == null) { |
712 |
previewPane = new JEditorPane(); |
713 |
previewPane.getAccessibleContext().setAccessibleName(NbBundle.getMessage(FmtOptions.class, "AN_Preview")); //NOI18N |
714 |
previewPane.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FmtOptions.class, "AD_Preview")); //NOI18N |
715 |
previewPane.putClientProperty("HighlightsLayerIncludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.SyntaxHighlighting$"); //NOI18N |
716 |
previewPane.setEditorKit(CloneableEditorSupport.getEditorKit("text/x-java")); |
717 |
previewPane.setEditable(false); |
718 |
} |
719 |
return previewPane; |
720 |
} |
721 |
|
722 |
public void refreshPreview() { |
723 |
JEditorPane jep = (JEditorPane) getPreviewComponent(); |
724 |
try { |
725 |
int rm = previewPrefs.getInt(rightMargin, getDefaultAsInt(rightMargin)); |
726 |
jep.putClientProperty("TextLimitLine", rm); //NOI18N |
727 |
jep.getDocument().putProperty(SimpleValueNames.TEXT_LINE_WRAP, ""); //NOI18N |
728 |
jep.getDocument().putProperty(SimpleValueNames.TAB_SIZE, ""); //NOI18N |
729 |
jep.getDocument().putProperty(SimpleValueNames.TEXT_LIMIT_WIDTH, ""); //NOI18N |
730 |
} |
731 |
catch( NumberFormatException e ) { |
732 |
// Ignore it |
733 |
} |
734 |
try { |
735 |
Class.forName(CodeStyle.class.getName(), true, CodeStyle.class.getClassLoader()); |
736 |
} catch (ClassNotFoundException cnfe) { |
737 |
// ignore |
738 |
} |
739 |
|
740 |
CodeStyle codeStyle = codeStyleProducer.create(previewPrefs); |
741 |
jep.setIgnoreRepaint(true); |
742 |
jep.setText(Reformatter.reformat(previewText, codeStyle)); |
743 |
jep.setIgnoreRepaint(false); |
744 |
jep.scrollRectToVisible(new Rectangle(0,0,10,10) ); |
745 |
jep.repaint(100); |
746 |
} |
747 |
|
748 |
// PreferencesCustomizer implementation -------------------------------- |
749 |
|
750 |
public JComponent getComponent() { |
751 |
return panel; |
752 |
} |
753 |
|
754 |
public String getDisplayName() { |
755 |
return panel.getName(); |
756 |
} |
757 |
|
758 |
public String getId() { |
759 |
return id; |
760 |
} |
761 |
|
762 |
public HelpCtx getHelpCtx() { |
763 |
return null; |
764 |
} |
765 |
|
766 |
// PreferencesCustomizer.Factory implementation ------------------------ |
767 |
|
768 |
public static final class Factory implements PreferencesCustomizer.Factory { |
769 |
|
770 |
private final String id; |
771 |
private final Class<? extends JPanel> panelClass; |
772 |
private final String previewText; |
773 |
private final String[][] forcedOptions; |
774 |
|
775 |
public Factory(String id, Class<? extends JPanel> panelClass, String previewText, String[]... forcedOptions) { |
776 |
this.id = id; |
777 |
this.panelClass = panelClass; |
778 |
this.previewText = previewText; |
779 |
this.forcedOptions = forcedOptions; |
780 |
} |
781 |
|
782 |
public PreferencesCustomizer create(Preferences preferences) { |
783 |
try { |
784 |
CategorySupport categorySupport = new CategorySupport(preferences, id, panelClass.newInstance(), previewText, forcedOptions); |
785 |
if (categorySupport.panel instanceof Runnable) |
786 |
((Runnable)categorySupport.panel).run(); |
787 |
return categorySupport; |
788 |
} catch (Exception e) { |
789 |
return null; |
790 |
} |
791 |
} |
792 |
} // End of CategorySupport.Factory class |
793 |
|
794 |
// Private methods ----------------------------------------------------- |
795 |
|
796 |
private void performOperation(int operation, JComponent jc, String optionID, Preferences p) { |
797 |
switch(operation) { |
798 |
case LOAD: |
799 |
loadData(jc, optionID, p); |
800 |
break; |
801 |
case STORE: |
802 |
storeData(jc, optionID, p); |
803 |
break; |
804 |
case ADD_LISTENERS: |
805 |
addListener(jc); |
806 |
break; |
807 |
} |
808 |
} |
809 |
|
810 |
private void scan(int what, Preferences p ) { |
811 |
for (JComponent jc : components) { |
812 |
Object o = jc.getClientProperty(OPTION_ID); |
813 |
if (o instanceof String) { |
814 |
performOperation(what, jc, (String)o, p); |
815 |
} else if (o instanceof String[]) { |
816 |
for(String oid : (String[])o) { |
817 |
performOperation(what, jc, oid, p); |
818 |
} |
819 |
} |
820 |
} |
821 |
} |
822 |
|
823 |
private void scan(Container container, List<JComponent> components) { |
824 |
for (Component c : container.getComponents()) { |
825 |
if (c instanceof JComponent) { |
826 |
JComponent jc = (JComponent)c; |
827 |
Object o = jc.getClientProperty(OPTION_ID); |
828 |
if (o instanceof String || o instanceof String[]) |
829 |
components.add(jc); |
830 |
} |
831 |
if (c instanceof Container) |
832 |
scan((Container)c, components); |
833 |
} |
834 |
} |
835 |
|
836 |
/** Very smart method which tries to set the values in the components correctly |
837 |
*/ |
838 |
private void loadData( JComponent jc, String optionID, Preferences node ) { |
839 |
|
840 |
if ( jc instanceof JTextField ) { |
841 |
JTextField field = (JTextField)jc; |
842 |
field.setText( node.get(optionID, getDefaultAsString(optionID)) ); |
843 |
} |
844 |
else if ( jc instanceof JSpinner ) { |
845 |
JSpinner js = (JSpinner)jc; |
846 |
js.setValue(node.getInt(optionID, getDefaultAsInt(optionID))); |
847 |
} |
848 |
else if ( jc instanceof JToggleButton ) { |
849 |
JToggleButton toggle = (JToggleButton)jc; |
850 |
boolean df = getDefaultAsBoolean(optionID); |
851 |
toggle.setSelected( node.getBoolean(optionID, df)); |
852 |
} |
853 |
else if ( jc instanceof JComboBox ) { |
854 |
JComboBox cb = (JComboBox)jc; |
855 |
String value = node.get(optionID, getDefaultAsString(optionID) ); |
856 |
ComboBoxModel model = createModel(value); |
857 |
cb.setModel(model); |
858 |
ComboItem item = whichItem(value, model); |
859 |
cb.setSelectedItem(item); |
860 |
} |
861 |
else if ( jc instanceof JList ) { |
862 |
loadListData((JList)jc, optionID, node); |
863 |
} |
864 |
else if ( jc instanceof JTable ) { |
865 |
loadTableData((JTable)jc, optionID, node); |
866 |
} |
867 |
} |
868 |
|
869 |
private void storeData( JComponent jc, String optionID, Preferences node ) { |
870 |
|
871 |
if ( jc instanceof JTextField ) { |
872 |
JTextField field = (JTextField)jc; |
873 |
|
874 |
String text = field.getText(); |
875 |
|
876 |
// XXX test for numbers |
877 |
if ( isInteger(optionID) ) { |
878 |
try { |
879 |
int i = Integer.parseInt(text); |
880 |
} catch (NumberFormatException e) { |
881 |
return; |
882 |
} |
883 |
} |
884 |
|
885 |
// XXX: watch out, tabSize, spacesPerTab, indentSize and expandTabToSpaces |
886 |
// fall back on getGlopalXXX() values and not getDefaultAsXXX value, |
887 |
// which is why we must not remove them. Proper solution would be to |
888 |
// store formatting preferences to MimeLookup and not use NbPreferences. |
889 |
// The problem currently is that MimeLookup based Preferences do not support subnodes. |
890 |
if (!optionID.equals(tabSize) && |
891 |
!optionID.equals(spacesPerTab) && !optionID.equals(indentSize) && |
892 |
getDefaultAsString(optionID).equals(text) |
893 |
) { |
894 |
node.remove(optionID); |
895 |
} else { |
896 |
node.put(optionID, text); |
897 |
} |
898 |
} |
899 |
else if ( jc instanceof JSpinner ) { |
900 |
JSpinner js = (JSpinner)jc; |
901 |
Object value = js.getValue(); |
902 |
if (getDefaultAsInt(optionID) == ((Integer)value).intValue()) |
903 |
node.remove(optionID); |
904 |
else |
905 |
node.putInt(optionID, ((Integer)value).intValue()); |
906 |
} |
907 |
else if ( jc instanceof JToggleButton ) { |
908 |
JToggleButton toggle = (JToggleButton)jc; |
909 |
if (!optionID.equals(expandTabToSpaces) && getDefaultAsBoolean(optionID) == toggle.isSelected()) |
910 |
node.remove(optionID); |
911 |
else |
912 |
node.putBoolean(optionID, toggle.isSelected()); |
913 |
} |
914 |
else if ( jc instanceof JComboBox ) { |
915 |
JComboBox cb = (JComboBox)jc; |
916 |
// Logger.global.info( cb.getSelectedItem() + " " + optionID); |
917 |
String value = ((ComboItem) cb.getSelectedItem()).value; |
918 |
if (getDefaultAsString(optionID).equals(value)) |
919 |
node.remove(optionID); |
920 |
else |
921 |
node.put(optionID,value); |
922 |
} |
923 |
else if ( jc instanceof JList ) { |
924 |
storeListData((JList)jc, optionID, node); |
925 |
} |
926 |
else if ( jc instanceof JTable ) { |
927 |
storeTableData((JTable)jc, optionID, node); |
928 |
} |
929 |
} |
930 |
|
931 |
private void addListener( JComponent jc ) { |
932 |
if ( jc instanceof JTextField ) { |
933 |
JTextField field = (JTextField)jc; |
934 |
field.addActionListener(this); |
935 |
field.getDocument().addDocumentListener(this); |
936 |
} |
937 |
else if ( jc instanceof JSpinner ) { |
938 |
JSpinner spinner = (JSpinner)jc; |
939 |
spinner.addChangeListener(this); |
940 |
} |
941 |
else if ( jc instanceof JToggleButton ) { |
942 |
JToggleButton toggle = (JToggleButton)jc; |
943 |
toggle.addActionListener(this); |
944 |
} |
945 |
else if ( jc instanceof JComboBox) { |
946 |
JComboBox cb = (JComboBox)jc; |
947 |
cb.addActionListener(this); |
948 |
} |
949 |
else if ( jc instanceof JList) { |
950 |
JList jl = (JList)jc; |
951 |
jl.getModel().addListDataListener(this); |
952 |
} |
953 |
else if ( jc instanceof JTable) { |
954 |
JTable jt = (JTable)jc; |
955 |
jt.getModel().addTableModelListener(this); |
956 |
} |
957 |
} |
958 |
|
959 |
|
960 |
private ComboBoxModel createModel( String value ) { |
961 |
|
962 |
// is it braces placement? |
963 |
for (ComboItem comboItem : bracePlacement) { |
964 |
if ( value.equals( comboItem.value) ) { |
965 |
return new DefaultComboBoxModel( bracePlacement ); |
966 |
} |
967 |
} |
968 |
|
969 |
// is it braces generation? |
970 |
for (ComboItem comboItem : bracesGeneration) { |
971 |
if ( value.equals( comboItem.value) ) { |
972 |
return new DefaultComboBoxModel( bracesGeneration ); |
973 |
} |
974 |
} |
975 |
|
976 |
// is it wrap? |
977 |
for (ComboItem comboItem : wrap) { |
978 |
if ( value.equals( comboItem.value) ) { |
979 |
return new DefaultComboBoxModel( wrap ); |
980 |
} |
981 |
} |
982 |
|
983 |
// is it insertion point? |
984 |
for (ComboItem comboItem : insertionPoint) { |
985 |
if ( value.equals( comboItem.value) ) { |
986 |
return new DefaultComboBoxModel( insertionPoint ); |
987 |
} |
988 |
} |
989 |
|
990 |
return null; |
991 |
} |
992 |
|
993 |
private static ComboItem whichItem(String value, ComboBoxModel model) { |
994 |
|
995 |
for (int i = 0; i < model.getSize(); i++) { |
996 |
ComboItem item = (ComboItem)model.getElementAt(i); |
997 |
if ( value.equals(item.value)) { |
998 |
return item; |
999 |
} |
1000 |
} |
1001 |
return null; |
1002 |
} |
1003 |
|
1004 |
private static class ComboItem { |
1005 |
|
1006 |
String value; |
1007 |
String displayName; |
1008 |
|
1009 |
public ComboItem(String value, String key) { |
1010 |
this.value = value; |
1011 |
this.displayName = NbBundle.getMessage(FmtOptions.class, key); |
1012 |
} |
1013 |
|
1014 |
@Override |
1015 |
public String toString() { |
1016 |
return displayName; |
1017 |
} |
1018 |
|
1019 |
} |
1020 |
} |
1021 |
|
1022 |
public static class PreviewPreferences extends AbstractPreferences { |
1023 |
|
1024 |
private Map<String,Object> map = new HashMap<String, Object>(); |
1025 |
|
1026 |
public PreviewPreferences() { |
1027 |
super(null, ""); // NOI18N |
1028 |
} |
1029 |
|
1030 |
protected void putSpi(String key, String value) { |
1031 |
map.put(key, value); |
1032 |
} |
1033 |
|
1034 |
protected String getSpi(String key) { |
1035 |
return (String)map.get(key); |
1036 |
} |
1037 |
|
1038 |
protected void removeSpi(String key) { |
1039 |
map.remove(key); |
1040 |
} |
1041 |
|
1042 |
protected void removeNodeSpi() throws BackingStoreException { |
1043 |
throw new UnsupportedOperationException("Not supported yet."); |
1044 |
} |
1045 |
|
1046 |
protected String[] keysSpi() throws BackingStoreException { |
1047 |
String array[] = new String[map.keySet().size()]; |
1048 |
return map.keySet().toArray( array ); |
1049 |
} |
1050 |
|
1051 |
protected String[] childrenNamesSpi() throws BackingStoreException { |
1052 |
throw new UnsupportedOperationException("Not supported yet."); |
1053 |
} |
1054 |
|
1055 |
protected AbstractPreferences childSpi(String name) { |
1056 |
throw new UnsupportedOperationException("Not supported yet."); |
1057 |
} |
1058 |
|
1059 |
protected void syncSpi() throws BackingStoreException { |
1060 |
throw new UnsupportedOperationException("Not supported yet."); |
1061 |
} |
1062 |
|
1063 |
protected void flushSpi() throws BackingStoreException { |
1064 |
throw new UnsupportedOperationException("Not supported yet."); |
1065 |
} |
1066 |
} |
1067 |
|
1068 |
// read-only, no subnodes |
1069 |
public static final class ProxyPreferences extends AbstractPreferences { |
1070 |
|
1071 |
private final Preferences[] delegates; |
1072 |
|
1073 |
public ProxyPreferences(Preferences... delegates) { |
1074 |
super(null, ""); // NOI18N |
1075 |
this.delegates = delegates; |
1076 |
} |
1077 |
|
1078 |
protected void putSpi(String key, String value) { |
1079 |
throw new UnsupportedOperationException("Not supported yet."); |
1080 |
} |
1081 |
|
1082 |
protected String getSpi(String key) { |
1083 |
for(Preferences p : delegates) { |
1084 |
String value = p.get(key, null); |
1085 |
if (value != null) { |
1086 |
return value; |
1087 |
} |
1088 |
} |
1089 |
return null; |
1090 |
} |
1091 |
|
1092 |
protected void removeSpi(String key) { |
1093 |
throw new UnsupportedOperationException("Not supported yet."); |
1094 |
} |
1095 |
|
1096 |
protected void removeNodeSpi() throws BackingStoreException { |
1097 |
throw new UnsupportedOperationException("Not supported yet."); |
1098 |
} |
1099 |
|
1100 |
protected String[] keysSpi() throws BackingStoreException { |
1101 |
Set<String> keys = new HashSet<String>(); |
1102 |
for(Preferences p : delegates) { |
1103 |
keys.addAll(Arrays.asList(p.keys())); |
1104 |
} |
1105 |
return keys.toArray(new String[ keys.size() ]); |
1106 |
} |
1107 |
|
1108 |
protected String[] childrenNamesSpi() throws BackingStoreException { |
1109 |
throw new UnsupportedOperationException("Not supported yet."); |
1110 |
} |
1111 |
|
1112 |
protected AbstractPreferences childSpi(String name) { |
1113 |
throw new UnsupportedOperationException("Not supported yet."); |
1114 |
} |
1115 |
|
1116 |
protected void syncSpi() throws BackingStoreException { |
1117 |
throw new UnsupportedOperationException("Not supported yet."); |
1118 |
} |
1119 |
|
1120 |
protected void flushSpi() throws BackingStoreException { |
1121 |
throw new UnsupportedOperationException("Not supported yet."); |
1122 |
} |
1123 |
} // End of ProxyPreferences class |
1124 |
|
344 |
|
1125 |
public static interface CodeStyleProducer { |
345 |
public static interface CodeStyleProducer { |
1126 |
|
346 |
|