1 11 package org.eclipse.jdt.internal.ui.preferences.formatter; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Observable ; 18 import java.util.Observer ; 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 32 public class CommentsTabPage extends FormatterTabPage { 33 34 37 private static String [] FALSE_TRUE = { 38 DefaultCodeFormatterConstants.FALSE, 39 DefaultCodeFormatterConstants.TRUE 40 }; 41 42 45 private static String [] DO_NOT_INSERT_INSERT = { 46 JavaCore.DO_NOT_INSERT, 47 JavaCore.INSERT 48 }; 49 50 private static abstract class Controller implements Observer { 51 52 private final Collection fMasters; 53 private final Collection fSlaves; 54 55 public Controller(Collection masters, Collection slaves) { 56 fMasters= masters; 57 fSlaves= slaves; 58 for (final Iterator iter= fMasters.iterator(); iter.hasNext();) { 59 ((CheckboxPreference)iter.next()).addObserver(this); 60 } 61 } 62 63 public void update(Observable o, Object arg) { 64 boolean enabled= areSlavesEnabled(); 65 66 for (final Iterator iter= fSlaves.iterator(); iter.hasNext();) { 67 final Object 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 getMasters() { 77 return fMasters; 78 } 79 80 public Collection 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 masters, Collection slaves) { 90 super(masters, slaves); 91 update(null, null); 92 } 93 94 97 protected boolean areSlavesEnabled() { 98 for (final Iterator 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 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.") + "package mypackage;\n" + "/**\n" + " * This is the comment for the example interface.\n" + " */\n" + " interface Example {\n" + "// This is a long comment that should be split in multiple line comments in case the line comment formatting is enabled\n" + "int foo3();\n" + "/*\n" + "*\n" + "* These possibilities include:\n" + "* <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n" + "*/\n" + "int foo4();\n" + " /**\n" + " *\n" + " * These possibilities include:\n" + " * <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n" + " */\n" + " int bar();\n" + " /*\n" + " *\n" + " * These possibilities include:\n" + " * <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n" + " */\n" + " int bar2();" + " // This is a long comment that should be split in multiple line comments in case the line comment formatting is enabled\n" + " int foo2();" + " /**\n" + " * The following is some sample code which illustrates source formatting within javadoc comments:\n" + " * <pre>public class Example {final int a= 1;final boolean b= true;}</pre>\n" + " * Descriptions of parameters and return values are best appended at end of the javadoc comment.\n" + " * @param a The first parameter. For an optimum result, this should be an odd number\n" + " * between 0 and 100.\n" + " * @param b The second parameter.\n" + " * @return The result of the foo operation, usually within 0 and 1000.\n" + " */" + " int foo(int a, int b);\n" + "}"; 147 private CompilationUnitPreview fPreview; 148 149 public CommentsTabPage(ModifyDialog modifyDialog, Map workingValues) { 150 super(modifyDialog, workingValues); 151 } 152 153 protected void doCreatePreferences(Composite composite, int numColumns) { 154 155 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 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 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 javaDocMaster= new ArrayList (); 183 javaDocMaster.add(javadoc); 184 javaDocMaster.add(header); 185 186 ArrayList javaDocSlaves= new ArrayList (); 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 indentMasters= new ArrayList (); 198 indentMasters.add(javadoc); 199 indentMasters.add(header); 200 indentMasters.add(indentJavadoc); 201 202 ArrayList indentSlaves= new ArrayList (); 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 blockMasters= new ArrayList (); 212 blockMasters.add(blockComment); 213 blockMasters.add(header); 214 215 ArrayList blockSlaves= new ArrayList (); 216 blockSlaves.add(blockSettingsGroup); 217 blockSlaves.add(blankLinesBlock); 218 219 new OrController(blockMasters, blockSlaves); 220 221 ArrayList lineWidthMasters= new ArrayList (); 222 lineWidthMasters.add(javadoc); 223 lineWidthMasters.add(blockComment); 224 lineWidthMasters.add(singleLineComments); 225 lineWidthMasters.add(header); 226 227 ArrayList lineWidthSlaves= new ArrayList (); 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 241 protected JavaPreview doCreateJavaPreview(Composite parent) { 242 fPreview= new CompilationUnitPreview(fWorkingValues, parent); 243 return fPreview; 244 } 245 246 249 protected void doUpdatePreview() { 250 super.doUpdatePreview(); 251 fPreview.update(); 252 } 253 254 private CheckboxPreference createPrefTrueFalse(Composite composite, int numColumns, String text, String key) { 255 return createCheckboxPref(composite, numColumns, text, key, FALSE_TRUE); 256 } 257 258 private CheckboxPreference createPrefInsert(Composite composite, int numColumns, String text, String key) { 259 return createCheckboxPref(composite, numColumns, text, key, DO_NOT_INSERT_INSERT); 260 } 261 } 262 | Popular Tags |