001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences.formatter;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.Iterator;
015: import java.util.Map;
016: import java.util.Observable;
017: import java.util.Observer;
018:
019: import org.eclipse.swt.layout.GridData;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.swt.widgets.Group;
023:
024: import org.eclipse.jdt.core.JavaCore;
025: import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
026:
027: /**
028: * Tab page for the comment formatter settings.
029: */
030: public class CommentsTabPage extends FormatterTabPage {
031:
032: /**
033: * Constant array for boolean selection
034: */
035: private static String[] FALSE_TRUE = {
036: DefaultCodeFormatterConstants.FALSE,
037: DefaultCodeFormatterConstants.TRUE };
038:
039: /**
040: * Constant array for insert / not_insert.
041: */
042: private static String[] DO_NOT_INSERT_INSERT = {
043: JavaCore.DO_NOT_INSERT, JavaCore.INSERT };
044:
045: private static abstract class Controller implements Observer {
046:
047: private final Collection fMasters;
048: private final Collection fSlaves;
049:
050: public Controller(Collection masters, Collection slaves) {
051: fMasters = masters;
052: fSlaves = slaves;
053: for (final Iterator iter = fMasters.iterator(); iter
054: .hasNext();) {
055: ((CheckboxPreference) iter.next()).addObserver(this );
056: }
057: }
058:
059: public void update(Observable o, Object arg) {
060: boolean enabled = areSlavesEnabled();
061:
062: for (final Iterator iter = fSlaves.iterator(); iter
063: .hasNext();) {
064: final Object obj = iter.next();
065: if (obj instanceof Preference) {
066: ((Preference) obj).setEnabled(enabled);
067: } else if (obj instanceof Control) {
068: ((Group) obj).setEnabled(enabled);
069: }
070: }
071: }
072:
073: public Collection getMasters() {
074: return fMasters;
075: }
076:
077: public Collection getSlaves() {
078: return fSlaves;
079: }
080:
081: protected abstract boolean areSlavesEnabled();
082: }
083:
084: private final static class OrController extends Controller {
085:
086: public OrController(Collection masters, Collection slaves) {
087: super (masters, slaves);
088: update(null, null);
089: }
090:
091: /**
092: * {@inheritDoc}
093: */
094: protected boolean areSlavesEnabled() {
095: for (final Iterator iter = getMasters().iterator(); iter
096: .hasNext();) {
097: if (((CheckboxPreference) iter.next()).getChecked())
098: return true;
099: }
100: return false;
101: }
102: }
103:
104: private final String PREVIEW = 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$
105: "package mypackage;\n"
106: + //$NON-NLS-1$
107: "/**\n"
108: + //$NON-NLS-1$
109: " * This is the comment for the example interface.\n"
110: + //$NON-NLS-1$
111: " */\n"
112: + //$NON-NLS-1$
113: " interface Example {\n"
114: + //$NON-NLS-1$
115: "// This is a long comment that should be split in multiple line comments in case the line comment formatting is enabled\n"
116: + //$NON-NLS-1$
117: "int foo3();\n"
118: + //$NON-NLS-1$
119: "/*\n"
120: + //$NON-NLS-1$
121: "*\n"
122: + //$NON-NLS-1$
123: "* These possibilities include:\n"
124: + //$NON-NLS-1$
125: "* <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n"
126: + //$NON-NLS-1$
127: "*/\n"
128: + //$NON-NLS-1$
129: "int foo4();\n"
130: + //$NON-NLS-1$
131: " /**\n"
132: + //$NON-NLS-1$
133: " *\n"
134: + //$NON-NLS-1$
135: " * These possibilities include:\n"
136: + //$NON-NLS-1$
137: " * <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n"
138: + //$NON-NLS-1$
139: " */\n"
140: + //$NON-NLS-1$
141: " int bar();\n"
142: + //$NON-NLS-1$
143: " /*\n"
144: + //$NON-NLS-1$
145: " *\n"
146: + //$NON-NLS-1$
147: " * These possibilities include:\n"
148: + //$NON-NLS-1$
149: " * <ul><li>Formatting of header comments.</li><li>Formatting of Javadoc tags</li></ul>\n"
150: + //$NON-NLS-1$
151: " */\n"
152: + //$NON-NLS-1$
153: " int bar2();"
154: + //$NON-NLS-1$
155: " // This is a long comment that should be split in multiple line comments in case the line comment formatting is enabled\n"
156: + //$NON-NLS-1$
157: " int foo2();"
158: + //$NON-NLS-1$
159: " /**\n"
160: + //$NON-NLS-1$
161: " * The following is some sample code which illustrates source formatting within javadoc comments:\n"
162: + //$NON-NLS-1$
163: " * <pre>public class Example {final int a= 1;final boolean b= true;}</pre>\n"
164: + //$NON-NLS-1$
165: " * Descriptions of parameters and return values are best appended at end of the javadoc comment.\n"
166: + //$NON-NLS-1$
167: " * @param a The first parameter. For an optimum result, this should be an odd number\n"
168: + //$NON-NLS-1$
169: " * between 0 and 100.\n"
170: + //$NON-NLS-1$
171: " * @param b The second parameter.\n"
172: + //$NON-NLS-1$
173: " * @return The result of the foo operation, usually within 0 and 1000.\n"
174: + //$NON-NLS-1$
175: " */" + //$NON-NLS-1$
176: " int foo(int a, int b);\n" + //$NON-NLS-1$
177: "}"; //$NON-NLS-1$
178:
179: private CompilationUnitPreview fPreview;
180:
181: public CommentsTabPage(ModifyDialog modifyDialog, Map workingValues) {
182: super (modifyDialog, workingValues);
183: }
184:
185: protected void doCreatePreferences(Composite composite,
186: int numColumns) {
187:
188: // global group
189: final Group globalGroup = createGroup(numColumns, composite,
190: FormatterMessages.CommentsTabPage_group1_title);
191: final CheckboxPreference javadoc = createPrefTrueFalse(
192: globalGroup,
193: numColumns,
194: FormatterMessages.commentsTabPage_enable_javadoc_comment_formatting,
195: DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT);
196: final CheckboxPreference blockComment = createPrefTrueFalse(
197: globalGroup,
198: numColumns,
199: FormatterMessages.CommentsTabPage_enable_block_comment_formatting,
200: DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT);
201: final CheckboxPreference singleLineComments = createPrefTrueFalse(
202: globalGroup,
203: numColumns,
204: FormatterMessages.CommentsTabPage_enable_line_comment_formatting,
205: DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT);
206: final CheckboxPreference header = createPrefTrueFalse(
207: globalGroup,
208: numColumns,
209: FormatterMessages.CommentsTabPage_format_header,
210: DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER);
211: createPrefTrueFalse(
212: globalGroup,
213: numColumns,
214: FormatterMessages.CommentsTabPage_never_indent_block_comments_on_first_column,
215: DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN);
216: createPrefTrueFalse(
217: globalGroup,
218: numColumns,
219: FormatterMessages.CommentsTabPage_never_indent_line_comments_on_first_column,
220: DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN);
221:
222: // javadoc comment formatting settings
223: final Group settingsGroup = createGroup(numColumns, composite,
224: FormatterMessages.CommentsTabPage_group2_title);
225: final CheckboxPreference html = createPrefTrueFalse(
226: settingsGroup,
227: numColumns,
228: FormatterMessages.CommentsTabPage_format_html,
229: DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HTML);
230: final CheckboxPreference code = createPrefTrueFalse(
231: settingsGroup,
232: numColumns,
233: FormatterMessages.CommentsTabPage_format_code_snippets,
234: DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_SOURCE);
235: final CheckboxPreference blankJavadoc = createPrefInsert(
236: settingsGroup,
237: numColumns,
238: FormatterMessages.CommentsTabPage_blank_line_before_javadoc_tags,
239: DefaultCodeFormatterConstants.FORMATTER_COMMENT_INSERT_EMPTY_LINE_BEFORE_ROOT_TAGS);
240: final CheckboxPreference indentJavadoc = createPrefTrueFalse(
241: settingsGroup,
242: numColumns,
243: FormatterMessages.CommentsTabPage_indent_javadoc_tags,
244: DefaultCodeFormatterConstants.FORMATTER_COMMENT_INDENT_ROOT_TAGS);
245: final CheckboxPreference indentDesc = createCheckboxPref(
246: settingsGroup,
247: numColumns,
248: FormatterMessages.CommentsTabPage_indent_description_after_param,
249: DefaultCodeFormatterConstants.FORMATTER_COMMENT_INDENT_PARAMETER_DESCRIPTION,
250: FALSE_TRUE);
251: ((GridData) indentDesc.getControl().getLayoutData()).horizontalIndent = fPixelConverter
252: .convertWidthInCharsToPixels(4);
253: final CheckboxPreference nlParam = createPrefInsert(
254: settingsGroup,
255: numColumns,
256: FormatterMessages.CommentsTabPage_new_line_after_param_tags,
257: DefaultCodeFormatterConstants.FORMATTER_COMMENT_INSERT_NEW_LINE_FOR_PARAMETER);
258: final CheckboxPreference blankLinesJavadoc = createPrefTrueFalse(
259: settingsGroup,
260: numColumns,
261: FormatterMessages.CommentsTabPage_clear_blank_lines,
262: DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_JAVADOC_COMMENT);
263:
264: // block comment settings
265: final Group blockSettingsGroup = createGroup(numColumns,
266: composite,
267: FormatterMessages.CommentsTabPage_group4_title);
268: final CheckboxPreference blankLinesBlock = createPrefTrueFalse(
269: blockSettingsGroup,
270: numColumns,
271: FormatterMessages.CommentsTabPage_remove_blank_block_comment_lines,
272: DefaultCodeFormatterConstants.FORMATTER_COMMENT_CLEAR_BLANK_LINES_IN_BLOCK_COMMENT);
273:
274: final Group widthGroup = createGroup(numColumns, composite,
275: FormatterMessages.CommentsTabPage_group3_title);
276: final NumberPreference lineWidth = createNumberPref(
277: widthGroup,
278: numColumns,
279: FormatterMessages.CommentsTabPage_line_width,
280: DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH,
281: 0, 9999);
282:
283: ArrayList javaDocMaster = new ArrayList();
284: javaDocMaster.add(javadoc);
285: javaDocMaster.add(header);
286:
287: ArrayList javaDocSlaves = new ArrayList();
288: javaDocSlaves.add(settingsGroup);
289: javaDocSlaves.add(html);
290: javaDocSlaves.add(code);
291: javaDocSlaves.add(blankJavadoc);
292: javaDocSlaves.add(indentJavadoc);
293: javaDocSlaves.add(nlParam);
294: javaDocSlaves.add(blankLinesJavadoc);
295:
296: new OrController(javaDocMaster, javaDocSlaves);
297:
298: ArrayList indentMasters = new ArrayList();
299: indentMasters.add(javadoc);
300: indentMasters.add(header);
301: indentMasters.add(indentJavadoc);
302:
303: ArrayList indentSlaves = new ArrayList();
304: indentSlaves.add(indentDesc);
305:
306: new Controller(indentMasters, indentSlaves) {
307: protected boolean areSlavesEnabled() {
308: return (javadoc.getChecked() || header.getChecked())
309: && indentJavadoc.getChecked();
310: }
311: }.update(null, null);
312:
313: ArrayList blockMasters = new ArrayList();
314: blockMasters.add(blockComment);
315: blockMasters.add(header);
316:
317: ArrayList blockSlaves = new ArrayList();
318: blockSlaves.add(blockSettingsGroup);
319: blockSlaves.add(blankLinesBlock);
320:
321: new OrController(blockMasters, blockSlaves);
322:
323: ArrayList lineWidthMasters = new ArrayList();
324: lineWidthMasters.add(javadoc);
325: lineWidthMasters.add(blockComment);
326: lineWidthMasters.add(singleLineComments);
327: lineWidthMasters.add(header);
328:
329: ArrayList lineWidthSlaves = new ArrayList();
330: lineWidthSlaves.add(widthGroup);
331: lineWidthSlaves.add(lineWidth);
332:
333: new OrController(lineWidthMasters, lineWidthSlaves);
334: }
335:
336: protected void initializePage() {
337: fPreview.setPreviewText(PREVIEW);
338: }
339:
340: /* (non-Javadoc)
341: * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doCreateJavaPreview(org.eclipse.swt.widgets.Composite)
342: */
343: protected JavaPreview doCreateJavaPreview(Composite parent) {
344: fPreview = new CompilationUnitPreview(fWorkingValues, parent);
345: return fPreview;
346: }
347:
348: /* (non-Javadoc)
349: * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doUpdatePreview()
350: */
351: protected void doUpdatePreview() {
352: super .doUpdatePreview();
353: fPreview.update();
354: }
355:
356: private CheckboxPreference createPrefTrueFalse(Composite composite,
357: int numColumns, String text, String key) {
358: return createCheckboxPref(composite, numColumns, text, key,
359: FALSE_TRUE);
360: }
361:
362: private CheckboxPreference createPrefInsert(Composite composite,
363: int numColumns, String text, String key) {
364: return createCheckboxPref(composite, numColumns, text, key,
365: DO_NOT_INSERT_INSERT);
366: }
367: }
|