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.Map;
013: import java.util.Observable;
014: import java.util.Observer;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Group;
020: import org.eclipse.swt.widgets.Label;
021:
022: import org.eclipse.jdt.core.JavaCore;
023: import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
024:
025: public class ControlStatementsTabPage extends FormatterTabPage {
026:
027: /**
028: * Constant array for boolean selection
029: */
030: private static String[] FALSE_TRUE = {
031: DefaultCodeFormatterConstants.FALSE,
032: DefaultCodeFormatterConstants.TRUE };
033:
034: /**
035: * Constant array for insert / not_insert.
036: */
037: private static String[] DO_NOT_INSERT_INSERT = {
038: JavaCore.DO_NOT_INSERT, JavaCore.INSERT };
039:
040: private final String PREVIEW = createPreviewHeader(FormatterMessages.ControlStatementsTabPage_preview_header)
041: + "class Example {" + //$NON-NLS-1$
042: " void bar() {" + //$NON-NLS-1$
043: " do {} while (true);" + //$NON-NLS-1$
044: " try {} catch (Exception e) { } finally { }" + //$NON-NLS-1$
045: " }" + //$NON-NLS-1$
046: " void foo2() {" + //$NON-NLS-1$
047: " if (true) { " + //$NON-NLS-1$
048: " return;" + //$NON-NLS-1$
049: " }" + //$NON-NLS-1$
050: " if (true) {" + //$NON-NLS-1$
051: " return;" + //$NON-NLS-1$
052: " } else if (false) {" + //$NON-NLS-1$
053: " return; " + //$NON-NLS-1$
054: " } else {" + //$NON-NLS-1$
055: " return;" + //$NON-NLS-1$
056: " }" + //$NON-NLS-1$
057: " }" + //$NON-NLS-1$
058: " void foo(int state) {" + //$NON-NLS-1$
059: " if (true) return;" + //$NON-NLS-1$
060: " if (true) " + //$NON-NLS-1$
061: " return;" + //$NON-NLS-1$
062: " else if (false)" + //$NON-NLS-1$
063: " return;" + //$NON-NLS-1$
064: " else return;" + //$NON-NLS-1$
065: " }" + //$NON-NLS-1$
066: "}"; //$NON-NLS-1$
067:
068: private CompilationUnitPreview fPreview;
069:
070: protected CheckboxPreference fThenStatementPref, fSimpleIfPref;
071:
072: public ControlStatementsTabPage(ModifyDialog modifyDialog,
073: Map workingValues) {
074: super (modifyDialog, workingValues);
075: }
076:
077: protected void doCreatePreferences(Composite composite,
078: int numColumns) {
079:
080: final Group generalGroup = createGroup(
081: numColumns,
082: composite,
083: FormatterMessages.ControlStatementsTabPage_general_group_title);
084: createOption(
085: generalGroup,
086: numColumns,
087: FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_else_statements,
088: DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_ELSE_IN_IF_STATEMENT,
089: DO_NOT_INSERT_INSERT);
090: createOption(
091: generalGroup,
092: numColumns,
093: FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_catch_statements,
094: DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_CATCH_IN_TRY_STATEMENT,
095: DO_NOT_INSERT_INSERT);
096: createOption(
097: generalGroup,
098: numColumns,
099: FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_finally_statements,
100: DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_FINALLY_IN_TRY_STATEMENT,
101: DO_NOT_INSERT_INSERT);
102: createOption(
103: generalGroup,
104: numColumns,
105: FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_while_in_do_statements,
106: DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_WHILE_IN_DO_STATEMENT,
107: DO_NOT_INSERT_INSERT);
108:
109: final Group ifElseGroup = createGroup(
110: numColumns,
111: composite,
112: FormatterMessages.ControlStatementsTabPage_if_else_group_title);
113: fThenStatementPref = createOption(
114: ifElseGroup,
115: numColumns,
116: FormatterMessages.ControlStatementsTabPage_if_else_group_keep_then_on_same_line,
117: DefaultCodeFormatterConstants.FORMATTER_KEEP_THEN_STATEMENT_ON_SAME_LINE,
118: FALSE_TRUE);
119:
120: Label l = new Label(ifElseGroup, SWT.NONE);
121: GridData gd = new GridData();
122: gd.widthHint = fPixelConverter.convertWidthInCharsToPixels(4);
123: l.setLayoutData(gd);
124:
125: fSimpleIfPref = createOption(
126: ifElseGroup,
127: numColumns - 1,
128: FormatterMessages.ControlStatementsTabPage_if_else_group_keep_simple_if_on_one_line,
129: DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_IF_ON_ONE_LINE,
130: FALSE_TRUE);
131:
132: fThenStatementPref.addObserver(new Observer() {
133: public void update(Observable o, Object arg) {
134: fSimpleIfPref.setEnabled(!fThenStatementPref
135: .getChecked());
136: }
137:
138: });
139:
140: fSimpleIfPref.setEnabled(!fThenStatementPref.getChecked());
141:
142: createOption(
143: ifElseGroup,
144: numColumns,
145: FormatterMessages.ControlStatementsTabPage_if_else_group_keep_else_on_same_line,
146: DefaultCodeFormatterConstants.FORMATTER_KEEP_ELSE_STATEMENT_ON_SAME_LINE,
147: FALSE_TRUE);
148: createCheckboxPref(
149: ifElseGroup,
150: numColumns,
151: FormatterMessages.ControlStatementsTabPage_if_else_group_keep_else_if_on_one_line,
152: DefaultCodeFormatterConstants.FORMATTER_COMPACT_ELSE_IF,
153: FALSE_TRUE);
154: createCheckboxPref(
155: ifElseGroup,
156: numColumns,
157: FormatterMessages.ControlStatementsTabPage_if_else_group_keep_guardian_clause_on_one_line,
158: DefaultCodeFormatterConstants.FORMATTER_KEEP_GUARDIAN_CLAUSE_ON_ONE_LINE,
159: FALSE_TRUE);
160: }
161:
162: protected void initializePage() {
163: fPreview.setPreviewText(PREVIEW);
164: }
165:
166: /* (non-Javadoc)
167: * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doCreateJavaPreview(org.eclipse.swt.widgets.Composite)
168: */
169: protected JavaPreview doCreateJavaPreview(Composite parent) {
170: fPreview = new CompilationUnitPreview(fWorkingValues, parent);
171: return fPreview;
172: }
173:
174: /* (non-Javadoc)
175: * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doUpdatePreview()
176: */
177: protected void doUpdatePreview() {
178: super .doUpdatePreview();
179: fPreview.update();
180: }
181:
182: private CheckboxPreference createOption(Composite composite,
183: int span, String name, String key, String[] values) {
184: return createCheckboxPref(composite, span, name, key, values);
185: }
186: }
|