KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > formatter > CommentsTabPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.preferences.formatter;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Observable JavaDoc;
18 import java.util.Observer JavaDoc;
19
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Group;
24
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
27
28
29 /**
30  * Tab page for the comment formatter settings.
31  */

32 public class CommentsTabPage extends FormatterTabPage {
33     
34     /**
35      * Constant array for boolean selection
36      */

37     private static String JavaDoc[] FALSE_TRUE = {
38         DefaultCodeFormatterConstants.FALSE,
39         DefaultCodeFormatterConstants.TRUE
40     };
41     
42     /**
43      * Constant array for insert / not_insert.
44      */

45     private static String JavaDoc[] DO_NOT_INSERT_INSERT = {
46         JavaCore.DO_NOT_INSERT,
47         JavaCore.INSERT
48     };
49     
50     private static abstract class Controller implements Observer JavaDoc {
51         
52         private final Collection JavaDoc fMasters;
53         private final Collection JavaDoc fSlaves;
54         
55         public Controller(Collection JavaDoc masters, Collection JavaDoc slaves) {
56             fMasters= masters;
57             fSlaves= slaves;
58             for (final Iterator JavaDoc iter= fMasters.iterator(); iter.hasNext();) {
59                 ((CheckboxPreference)iter.next()).addObserver(this);
60             }
61         }
62         
63         public void update(Observable JavaDoc o, Object JavaDoc arg) {
64             boolean enabled= areSlavesEnabled();
65             
66             for (final Iterator JavaDoc iter= fSlaves.iterator(); iter.hasNext();) {
67                 final Object JavaDoc obj= iter.next();
68                 if (obj instanceof Preference) {
69                     ((Preference)obj).setEnabled(enabled);
70                 } else if (obj instanceof Control) {
71                     ((Group)obj).setEnabled(enabled);
72                 }
73             }
74         }
75         
76         public Collection JavaDoc getMasters() {
77             return fMasters;
78         }
79         
80         public Collection JavaDoc getSlaves() {
81             return fSlaves;
82         }
83         
84         protected abstract boolean areSlavesEnabled();
85     }
86     
87     private final static class OrController extends Controller {
88         
89         public OrController(Collection JavaDoc masters, Collection JavaDoc slaves) {
90             super(masters, slaves);
91             update(null, null);
92         }
93         
94         /**
95          * {@inheritDoc}
96          */

97         protected boolean areSlavesEnabled() {
98             for (final Iterator JavaDoc iter= getMasters().iterator(); iter.hasNext();) {
99                 if (((CheckboxPreference)iter.next()).getChecked())
100                     return true;
101             }
102             return false;
103         }
104     }
105     
106     private final String JavaDoc PREVIEW=
107         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$
108
"package mypackage;\n" + //$NON-NLS-1$
109
"/**\n" + //$NON-NLS-1$
110
" * This is the comment for the example interface.\n" + //$NON-NLS-1$
111
" */\n" + //$NON-NLS-1$
112
" interface Example {\n" + //$NON-NLS-1$
113
"// 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
"int foo3();\n" + //$NON-NLS-1$
115
"/*\n" + //$NON-NLS-1$
116
"*\n" + //$NON-NLS-1$
117
"* These possibilities include:\n" + //$NON-NLS-1$
118
"* <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n" + //$NON-NLS-1$
119
"*/\n" + //$NON-NLS-1$
120
"int foo4();\n" + //$NON-NLS-1$
121
" /**\n" + //$NON-NLS-1$
122
" *\n" + //$NON-NLS-1$
123
" * These possibilities include:\n" + //$NON-NLS-1$
124
" * <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n" + //$NON-NLS-1$
125
" */\n" + //$NON-NLS-1$
126
" int bar();\n" + //$NON-NLS-1$
127
" /*\n" + //$NON-NLS-1$
128
" *\n" + //$NON-NLS-1$
129
" * These possibilities include:\n" + //$NON-NLS-1$
130
" * <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n" + //$NON-NLS-1$
131
" */\n" + //$NON-NLS-1$
132
" int bar2();" + //$NON-NLS-1$
133
" // 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$
134
" int foo2();" + //$NON-NLS-1$
135
" /**\n" + //$NON-NLS-1$
136
" * The following is some sample code which illustrates source formatting within javadoc comments:\n" + //$NON-NLS-1$
137
" * <pre>public class Example {final int a= 1;final boolean b= true;}</pre>\n" + //$NON-NLS-1$
138
" * Descriptions of parameters and return values are best appended at end of the javadoc comment.\n" + //$NON-NLS-1$
139
" * @param a The first parameter. For an optimum result, this should be an odd number\n" + //$NON-NLS-1$
140
" * between 0 and 100.\n" + //$NON-NLS-1$
141
" * @param b The second parameter.\n" + //$NON-NLS-1$
142
" * @return The result of the foo operation, usually within 0 and 1000.\n" + //$NON-NLS-1$
143
" */" + //$NON-NLS-1$
144
" int foo(int a, int b);\n" + //$NON-NLS-1$
145
"}"; //$NON-NLS-1$
146

147     private CompilationUnitPreview fPreview;
148
149     public CommentsTabPage(ModifyDialog modifyDialog, Map JavaDoc workingValues) {
150         super(modifyDialog, workingValues);
151     }
152
153     protected void doCreatePreferences(Composite composite, int numColumns) {
154         
155         // global group
156
final Group globalGroup= createGroup(numColumns, composite, FormatterMessages.CommentsTabPage_group1_title);
157         final CheckboxPreference javadoc= createPrefTrueFalse(globalGroup, numColumns, FormatterMessages.commentsTabPage_enable_javadoc_comment_formatting, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT);
158         final CheckboxPreference blockComment= createPrefTrueFalse(globalGroup, numColumns, FormatterMessages.CommentsTabPage_enable_block_comment_formatting, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT);
159         final CheckboxPreference singleLineComments= createPrefTrueFalse(globalGroup, numColumns, FormatterMessages.CommentsTabPage_enable_line_comment_formatting, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT);
160         final CheckboxPreference header= createPrefTrueFalse(globalGroup, numColumns, FormatterMessages.CommentsTabPage_format_header, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER);
161         createPrefTrueFalse(globalGroup, numColumns, FormatterMessages.CommentsTabPage_never_indent_block_comments_on_first_column, DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN);
162         createPrefTrueFalse(globalGroup, numColumns, FormatterMessages.CommentsTabPage_never_indent_line_comments_on_first_column, DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN);
163
164         // javadoc comment formatting settings
165
final Group settingsGroup= createGroup(numColumns, composite, FormatterMessages.CommentsTabPage_group2_title);
166         final CheckboxPreference html= createPrefTrueFalse(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_format_html, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HTML);
167         final CheckboxPreference code= createPrefTrueFalse(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_format_code_snippets, DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_SOURCE);
168         final CheckboxPreference blankJavadoc= createPrefInsert(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_blank_line_before_javadoc_tags, DefaultCodeFormatterConstants.FORMATTER_COMMENT_INSERT_EMPTY_LINE_BEFORE_ROOT_TAGS);
169         final CheckboxPreference indentJavadoc= createPrefTrueFalse(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_indent_javadoc_tags, DefaultCodeFormatterConstants.FORMATTER_COMMENT_INDENT_ROOT_TAGS);
170         final CheckboxPreference indentDesc= createCheckboxPref(settingsGroup, numColumns , FormatterMessages.CommentsTabPage_indent_description_after_param, DefaultCodeFormatterConstants.FORMATTER_COMMENT_INDENT_PARAMETER_DESCRIPTION, FALSE_TRUE);
171         ((GridData)indentDesc.getControl().getLayoutData()).horizontalIndent= fPixelConverter.convertWidthInCharsToPixels(4);
172         final CheckboxPreference nlParam= createPrefInsert(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_new_line_after_param_tags, DefaultCodeFormatterConstants.FORMATTER_COMMENT_INSERT_NEW_LINE_FOR_PARAMETER);
173         final CheckboxPreference blankLinesJavadoc= createPrefTrueFalse(settingsGroup, numColumns, FormatterMessages.CommentsTabPage_clear_blank_lines, DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT);
174         
175         // block comment settings
176
final Group blockSettingsGroup= createGroup(numColumns, composite, FormatterMessages.CommentsTabPage_group4_title);
177         final CheckboxPreference blankLinesBlock= createPrefTrueFalse(blockSettingsGroup, numColumns, FormatterMessages.CommentsTabPage_remove_blank_block_comment_lines, DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT);
178         
179         final Group widthGroup= createGroup(numColumns, composite, FormatterMessages.CommentsTabPage_group3_title);
180         final NumberPreference lineWidth= createNumberPref(widthGroup, numColumns, FormatterMessages.CommentsTabPage_line_width, DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, 0, 9999);
181
182         ArrayList JavaDoc javaDocMaster= new ArrayList JavaDoc();
183         javaDocMaster.add(javadoc);
184         javaDocMaster.add(header);
185         
186         ArrayList JavaDoc javaDocSlaves= new ArrayList JavaDoc();
187         javaDocSlaves.add(settingsGroup);
188         javaDocSlaves.add(html);
189         javaDocSlaves.add(code);
190         javaDocSlaves.add(blankJavadoc);
191         javaDocSlaves.add(indentJavadoc);
192         javaDocSlaves.add(nlParam);
193         javaDocSlaves.add(blankLinesJavadoc);
194         
195         new OrController(javaDocMaster, javaDocSlaves);
196         
197         ArrayList JavaDoc indentMasters= new ArrayList JavaDoc();
198         indentMasters.add(javadoc);
199         indentMasters.add(header);
200         indentMasters.add(indentJavadoc);
201         
202         ArrayList JavaDoc indentSlaves= new ArrayList JavaDoc();
203         indentSlaves.add(indentDesc);
204         
205         new Controller(indentMasters, indentSlaves) {
206             protected boolean areSlavesEnabled() {
207                 return (javadoc.getChecked() || header.getChecked()) && indentJavadoc.getChecked();
208             }
209         }.update(null, null);
210         
211         ArrayList JavaDoc blockMasters= new ArrayList JavaDoc();
212         blockMasters.add(blockComment);
213         blockMasters.add(header);
214         
215         ArrayList JavaDoc blockSlaves= new ArrayList JavaDoc();
216         blockSlaves.add(blockSettingsGroup);
217         blockSlaves.add(blankLinesBlock);
218         
219         new OrController(blockMasters, blockSlaves);
220         
221         ArrayList JavaDoc lineWidthMasters= new ArrayList JavaDoc();
222         lineWidthMasters.add(javadoc);
223         lineWidthMasters.add(blockComment);
224         lineWidthMasters.add(singleLineComments);
225         lineWidthMasters.add(header);
226
227         ArrayList JavaDoc lineWidthSlaves= new ArrayList JavaDoc();
228         lineWidthSlaves.add(widthGroup);
229         lineWidthSlaves.add(lineWidth);
230         
231         new OrController(lineWidthMasters, lineWidthSlaves);
232     }
233     
234     protected void initializePage() {
235         fPreview.setPreviewText(PREVIEW);
236     }
237     
238     /* (non-Javadoc)
239      * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doCreateJavaPreview(org.eclipse.swt.widgets.Composite)
240      */

241     protected JavaPreview doCreateJavaPreview(Composite parent) {
242         fPreview= new CompilationUnitPreview(fWorkingValues, parent);
243         return fPreview;
244     }
245
246     /* (non-Javadoc)
247      * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doUpdatePreview()
248      */

249     protected void doUpdatePreview() {
250         super.doUpdatePreview();
251         fPreview.update();
252     }
253     
254     private CheckboxPreference createPrefTrueFalse(Composite composite, int numColumns, String JavaDoc text, String JavaDoc key) {
255         return createCheckboxPref(composite, numColumns, text, key, FALSE_TRUE);
256     }
257     
258     private CheckboxPreference createPrefInsert(Composite composite, int numColumns, String JavaDoc text, String JavaDoc key) {
259         return createCheckboxPref(composite, numColumns, text, key, DO_NOT_INSERT_INSERT);
260     }
261 }
262
Popular Tags