]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | * Ray V. (voidstar@gmail.com) - Contribution for bug 282988 | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.jdt.internal.ui.preferences.formatter; | |
13 | ||
14 | import java.util.ArrayList; | |
15 | import java.util.Collection; | |
16 | import java.util.Iterator; | |
17 | import java.util.Map; | |
18 | import java.util.Observable; | |
19 | import java.util.Observer; | |
20 | ||
21 | import org.eclipse.swt.SWT; | |
22 | import org.eclipse.swt.layout.GridData; | |
23 | import org.eclipse.swt.widgets.Composite; | |
24 | import org.eclipse.swt.widgets.Control; | |
25 | import org.eclipse.swt.widgets.Group; | |
26 | ||
27 | import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; | |
28 | ||
29 | ||
30 | /** | |
31 | * Tab page for the comment formatter settings. | |
32 | */ | |
33 | public class CommentsTabPage extends FormatterTabPage { | |
34 | ||
35 | private static abstract class Controller implements Observer { | |
36 | ||
37 | private final Collection<CheckboxPreference> fMasters; | |
38 | private final Collection<Object> fSlaves; | |
39 | ||
40 | public Controller(Collection<CheckboxPreference> masters, Collection<Object> slaves) { | |
41 | fMasters= masters; | |
42 | fSlaves= slaves; | |
43 | for (final Iterator<CheckboxPreference> iter= fMasters.iterator(); iter.hasNext();) { | |
44 | iter.next().addObserver(this); | |
45 | } | |
46 | } | |
47 | ||
48 | public void update(Observable o, Object arg) { | |
49 | boolean enabled= areSlavesEnabled(); | |
50 | ||
51 | for (final Iterator<Object> iter= fSlaves.iterator(); iter.hasNext();) { | |
52 | final Object obj= iter.next(); | |
53 | if (obj instanceof Preference) { | |
54 | ((Preference)obj).setEnabled(enabled); | |
55 | } else if (obj instanceof Control) { | |
56 | ((Group)obj).setEnabled(enabled); | |
57 | } | |
58 | } | |
59 | } | |
60 | ||
61 | public Collection<CheckboxPreference> getMasters() { | |
62 | return fMasters; | |
63 | } | |
64 | ||
65 | protected abstract boolean areSlavesEnabled(); | |
66 | } | |
67 | ||
68 | final static class OrController extends Controller { | |
69 | ||
70 | public OrController(Collection<CheckboxPreference> masters, Collection<Object> slaves) { | |
71 | super(masters, slaves); | |
72 | update(null, null); | |
73 | } | |
74 | ||
75 | /** | |
76 | * {@inheritDoc} | |
77 | */ | |
78 | @Override | |
79 | protected boolean areSlavesEnabled() { | |
80 | for (final Iterator<CheckboxPreference> iter= getMasters().iterator(); iter.hasNext();) { | |
81 | if (iter.next().getChecked()) | |
82 | return true; | |
83 | } | |
84 | return false; | |
85 | } | |
86 | } | |
87 | ||
88 | private final String PREVIEW= | |
89 | createPreviewHeader("An example for comment formatting. This example is meant to illustrate the various possibilities offered by <i>Eclipse</i> in order to format comments.") + //$NON-NLS-1$ | |
90 | "package mypackage;\n" + //$NON-NLS-1$ | |
91 | "/**\n" + //$NON-NLS-1$ | |
92 | " * This is the comment for the example interface.\n" + //$NON-NLS-1$ | |
93 | " */\n" + //$NON-NLS-1$ | |
94 | " interface Example {\n" + //$NON-NLS-1$ | |
95 | "// This is a long comment with\twhitespace that should be split in multiple line comments in case the line comment formatting is enabled\n" + //$NON-NLS-1$ | |
96 | "int foo3();\n" + //$NON-NLS-1$ | |
97 | " \n" + //$NON-NLS-1$ | |
98 | "//\tvoid commented() {\n" + //$NON-NLS-1$ | |
99 | "//\t\t\tSystem.out.println(\"indented\");\n" + //$NON-NLS-1$ | |
100 | "//\t}\n" + //$NON-NLS-1$ | |
101 | "\n" + //$NON-NLS-1$ | |
102 | "\t//\tvoid indentedCommented() {\n" + //$NON-NLS-1$ | |
103 | "\t//\t\t\tSystem.out.println(\"indented\");\n" + //$NON-NLS-1$ | |
104 | "\t//\t}\n" + //$NON-NLS-1$ | |
105 | "\n" + //$NON-NLS-1$ | |
106 | "/* block comment on first column*/\n" + //$NON-NLS-1$ | |
107 | " int bar();\n" + //$NON-NLS-1$ | |
108 | "\t/*\n" + //$NON-NLS-1$ | |
109 | "\t*\n" + //$NON-NLS-1$ | |
110 | "\t* These possibilities include:\n" + //$NON-NLS-1$ | |
111 | "\t* <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n" + //$NON-NLS-1$ | |
112 | "\t*/\n" + //$NON-NLS-1$ | |
113 | " int bar2(); // This is a long comment that should be split in multiple line comments in case the line comment formatting is enabled\n" + //$NON-NLS-1$ | |
114 | " /**\n" + //$NON-NLS-1$ | |
115 | " * The following is some sample code which illustrates source formatting within javadoc comments:\n" + //$NON-NLS-1$ | |
116 | " * <pre>public class Example {final int a= 1;final boolean b= true;}</pre>\n" + //$NON-NLS-1$ | |
117 | " * Descriptions of parameters and return values are best appended at end of the javadoc comment.\n" + //$NON-NLS-1$ | |
118 | " * @param a The first parameter. For an optimum result, this should be an odd number\n" + //$NON-NLS-1$ | |
119 | " * between 0 and 100.\n" + //$NON-NLS-1$ | |
120 | " * @param b The second parameter.\n" + //$NON-NLS-1$ | |
121 | " * @return The result of the foo operation, usually within 0 and 1000.\n" + //$NON-NLS-1$ | |
122 | " */" + //$NON-NLS-1$ | |
123 | " int foo(int a, int b);\n" + //$NON-NLS-1$ | |
124 | "}\n" +//$NON-NLS-1$ | |
125 | "class Test {\n" +//$NON-NLS-1$ | |
126 | "\t\tvoid trailingCommented() {\n" + //$NON-NLS-1$ | |
127 | "\t\t\t\tSystem.out.println(\"indented\");\t\t// comment\n" + //$NON-NLS-1$ | |
128 | "\t\t\t\tSystem.out.println(\"indent\");\t\t// comment\n" + //$NON-NLS-1$ | |
129 | "\t\t}\n" + //$NON-NLS-1$ | |
130 | "}"; //$NON-NLS-1$ | |
131 | ||
132 | private CompilationUnitPreview fPreview; | |
133 | ||
134 | public CommentsTabPage(ModifyDialog modifyDialog, Map<String, String> workingValues) { | |
135 | super(modifyDialog, workingValues); | |
136 | } | |
137 | ||
138 | @Override | |
139 | protected void doCreatePreferences(Composite composite, int numColumns) { | |
140 | final int indent= fPixelConverter.convertWidthInCharsToPixels(4); | |
141 | ||
142 | // global group | |
143 | final Group globalGroup= createGroup(numColumns, composite, FormatterMessages.CommentsTabPage_group1_title); | |
144 | final CheckboxPreference javadoc= createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.commentsTabPage_enable_javadoc_comment_formatting, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT, false); | |
145 | final CheckboxPreference blockComment= createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.CommentsTabPage_enable_block_comment_formatting, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT, false); | |
146 | final CheckboxPreference singleLineComments= createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.CommentsTabPage_enable_line_comment_formatting, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT, false); | |
147 | final CheckboxPreference singleLineCommentsOnFirstColumn= createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.CommentsTabPage_format_line_comments_on_first_column, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN, false); | |
148 | ((GridData)singleLineCommentsOnFirstColumn.getControl().getLayoutData()).horizontalIndent= indent; | |
149 | final CheckboxPreference header= createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.CommentsTabPage_format_header, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER, false); | |
150 | GridData spacerData= new GridData(0, 0); | |
151 | spacerData.horizontalSpan= numColumns; | |
152 | new Composite(globalGroup, SWT.NONE).setLayoutData(spacerData); | |
153 | createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.CommentsTabPage_preserve_white_space_before_line_comment, DefaultCodeFormatterConstants.FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, false); | |
154 | createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.CommentsTabPage_never_indent_line_comments_on_first_column, DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, false); | |
155 | createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.CommentsTabPage_never_indent_block_comments_on_first_column, DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, false); | |
156 | createPrefFalseTrue(globalGroup, numColumns, FormatterMessages.CommentsTabPage_do_not_join_lines, DefaultCodeFormatterConstants.FORMATTER_JOIN_LINES_IN_COMMENTS, true); | |
157 | ||
158 | // javadoc comment formatting settings | |
159 | final Group settingsGroup= createGroup(numColumns, composite, FormatterMessages.CommentsTabPage_group2_title); | |
160 | final CheckboxPreference html= createPrefFalseTrue(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_format_html, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HTML, false); | |
161 | final CheckboxPreference code= createPrefFalseTrue(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_format_code_snippets, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_SOURCE, false); | |
162 | final CheckboxPreference blankJavadoc= createPrefInsert(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_blank_line_before_javadoc_tags, DefaultCodeFormatterConstants.FORMATTER_COMMENT_INSERT_EMPTY_LINE_BEFORE_ROOT_TAGS); | |
163 | final CheckboxPreference indentJavadoc= createPrefFalseTrue(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_indent_javadoc_tags, DefaultCodeFormatterConstants.FORMATTER_COMMENT_INDENT_ROOT_TAGS, false); | |
164 | final CheckboxPreference indentDesc= createPrefFalseTrue(settingsGroup, numColumns , FormatterMessages.CommentsTabPage_indent_description_after_param, DefaultCodeFormatterConstants.FORMATTER_COMMENT_INDENT_PARAMETER_DESCRIPTION, false); | |
165 | ((GridData)indentDesc.getControl().getLayoutData()).horizontalIndent= indent; | |
166 | final CheckboxPreference nlParam= createPrefInsert(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_new_line_after_param_tags, DefaultCodeFormatterConstants.FORMATTER_COMMENT_INSERT_NEW_LINE_FOR_PARAMETER); | |
167 | final CheckboxPreference nlBoundariesJavadoc= createPrefFalseTrue(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_new_lines_at_javadoc_boundaries, DefaultCodeFormatterConstants.FORMATTER_COMMENT_NEW_LINES_AT_JAVADOC_BOUNDARIES, false); | |
168 | final CheckboxPreference blankLinesJavadoc= createPrefFalseTrue(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_clear_blank_lines, DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT, false); | |
169 | ||
170 | // block comment settings | |
171 | final Group blockSettingsGroup= createGroup(numColumns, composite, FormatterMessages.CommentsTabPage_group4_title); | |
172 | final CheckboxPreference nlBoundariesBlock= createPrefFalseTrue(blockSettingsGroup, numColumns, FormatterMessages.CommentsTabPage_new_lines_at_comment_boundaries, DefaultCodeFormatterConstants.FORMATTER_COMMENT_NEW_LINES_AT_BLOCK_BOUNDARIES, false); | |
173 | final CheckboxPreference blankLinesBlock= createPrefFalseTrue(blockSettingsGroup, numColumns, FormatterMessages.CommentsTabPage_remove_blank_block_comment_lines, DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT, false); | |
174 | ||
175 | // line width settings | |
176 | final Group widthGroup= createGroup(numColumns, composite, FormatterMessages.CommentsTabPage_group3_title); | |
177 | final NumberPreference lineWidth= createNumberPref(widthGroup, numColumns, FormatterMessages.CommentsTabPage_line_width, DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, 0, 9999); | |
178 | ||
179 | ||
180 | ArrayList<CheckboxPreference> lineFirstColumnMasters= new ArrayList<CheckboxPreference>(); | |
181 | lineFirstColumnMasters.add(singleLineComments); | |
182 | ||
183 | ArrayList<Object> lineFirstColumnSlaves= new ArrayList<Object>(); | |
184 | lineFirstColumnSlaves.add(singleLineCommentsOnFirstColumn); | |
185 | ||
186 | new Controller(lineFirstColumnMasters, lineFirstColumnSlaves) { | |
187 | @Override | |
188 | protected boolean areSlavesEnabled() { | |
189 | return singleLineComments.getChecked(); | |
190 | } | |
191 | }.update(null, null); | |
192 | ||
193 | ||
194 | ArrayList<CheckboxPreference> javaDocMaster= new ArrayList<CheckboxPreference>(); | |
195 | javaDocMaster.add(javadoc); | |
196 | javaDocMaster.add(header); | |
197 | ||
198 | ArrayList<Object> javaDocSlaves= new ArrayList<Object>(); | |
199 | javaDocSlaves.add(settingsGroup); | |
200 | javaDocSlaves.add(html); | |
201 | javaDocSlaves.add(code); | |
202 | javaDocSlaves.add(blankJavadoc); | |
203 | javaDocSlaves.add(indentJavadoc); | |
204 | javaDocSlaves.add(nlParam); | |
205 | javaDocSlaves.add(nlBoundariesJavadoc); | |
206 | javaDocSlaves.add(blankLinesJavadoc); | |
207 | ||
208 | new OrController(javaDocMaster, javaDocSlaves); | |
209 | ||
210 | ||
211 | ArrayList<CheckboxPreference> indentMasters= new ArrayList<CheckboxPreference>(); | |
212 | indentMasters.add(javadoc); | |
213 | indentMasters.add(header); | |
214 | indentMasters.add(indentJavadoc); | |
215 | ||
216 | ArrayList<Object> indentSlaves= new ArrayList<Object>(); | |
217 | indentSlaves.add(indentDesc); | |
218 | ||
219 | new Controller(indentMasters, indentSlaves) { | |
220 | @Override | |
221 | protected boolean areSlavesEnabled() { | |
222 | return (javadoc.getChecked() || header.getChecked()) && indentJavadoc.getChecked(); | |
223 | } | |
224 | }.update(null, null); | |
225 | ||
226 | ||
227 | ArrayList<CheckboxPreference> lineWidthMasters= blockComment.generated_1735756286725459971(javadoc, header, blockSettingsGroup, nlBoundariesBlock, blankLinesBlock); | |
228 | lineWidthMasters.add(singleLineComments); | |
229 | lineWidthMasters.add(header); | |
230 | ||
231 | ArrayList<Object> lineWidthSlaves= new ArrayList<Object>(); | |
232 | lineWidthSlaves.add(widthGroup); | |
233 | lineWidthSlaves.add(lineWidth); | |
234 | ||
235 | new OrController(lineWidthMasters, lineWidthSlaves); | |
236 | } | |
237 | ||
238 | @Override | |
239 | protected void initializePage() { | |
240 | fPreview.setPreviewText(PREVIEW); | |
241 | } | |
242 | ||
243 | /* (non-Javadoc) | |
244 | * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doCreateJavaPreview(org.eclipse.swt.widgets.Composite) | |
245 | */ | |
246 | @Override | |
247 | protected JavaPreview doCreateJavaPreview(Composite parent) { | |
248 | fPreview= new CompilationUnitPreview(fWorkingValues, parent); | |
249 | return fPreview; | |
250 | } | |
251 | ||
252 | /* (non-Javadoc) | |
253 | * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doUpdatePreview() | |
254 | */ | |
255 | @Override | |
256 | protected void doUpdatePreview() { | |
257 | super.doUpdatePreview(); | |
258 | fPreview.update(); | |
259 | } | |
260 | ||
261 | private CheckboxPreference createPrefFalseTrue(Composite composite, int numColumns, String text, String key, boolean invertPreference) { | |
262 | if (invertPreference) | |
263 | return createCheckboxPref(composite, numColumns, text, key, TRUE_FALSE); | |
264 | return createCheckboxPref(composite, numColumns, text, key, FALSE_TRUE); | |
265 | } | |
266 | ||
267 | private CheckboxPreference createPrefInsert(Composite composite, int numColumns, String text, String key) { | |
268 | return createCheckboxPref(composite, numColumns, text, key, DO_NOT_INSERT_INSERT); | |
269 | } | |
270 | } |