1 11 package org.eclipse.jdt.internal.ui.preferences.formatter; 12 13 import java.util.Map ; 14 import java.util.Observable ; 15 import java.util.Observer ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Group; 21 import org.eclipse.swt.widgets.Label; 22 23 import org.eclipse.jdt.core.JavaCore; 24 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 25 26 27 public class ControlStatementsTabPage extends FormatterTabPage { 28 29 32 private static String [] FALSE_TRUE = { 33 DefaultCodeFormatterConstants.FALSE, 34 DefaultCodeFormatterConstants.TRUE 35 }; 36 37 40 private static String [] DO_NOT_INSERT_INSERT = { 41 JavaCore.DO_NOT_INSERT, 42 JavaCore.INSERT 43 }; 44 45 46 private final String PREVIEW= 47 createPreviewHeader(FormatterMessages.ControlStatementsTabPage_preview_header) + 48 "class Example {" + " void bar() {" + " do {} while (true);" + " try {} catch (Exception e) { } finally { }" + " }" + " void foo2() {" + " if (true) { " + " return;" + " }" + " if (true) {" + " return;" + " } else if (false) {" + " return; " + " } else {" + " return;" + " }" + " }" + " void foo(int state) {" + " if (true) return;" + " if (true) " + " return;" + " else if (false)" + " return;" + " else return;" + " }" + "}"; 75 76 77 private CompilationUnitPreview fPreview; 78 79 protected CheckboxPreference fThenStatementPref, fSimpleIfPref; 80 81 82 public ControlStatementsTabPage(ModifyDialog modifyDialog, Map workingValues) { 83 super(modifyDialog, workingValues); 84 } 85 86 protected void doCreatePreferences(Composite composite, int numColumns) { 87 88 final Group generalGroup= createGroup(numColumns, composite, FormatterMessages.ControlStatementsTabPage_general_group_title); 89 createOption(generalGroup, numColumns, FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_else_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_ELSE_IN_IF_STATEMENT, DO_NOT_INSERT_INSERT); 90 createOption(generalGroup, numColumns, FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_catch_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_CATCH_IN_TRY_STATEMENT, DO_NOT_INSERT_INSERT); 91 createOption(generalGroup, numColumns, FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_finally_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_FINALLY_IN_TRY_STATEMENT, DO_NOT_INSERT_INSERT); 92 createOption(generalGroup, numColumns, FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_while_in_do_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_WHILE_IN_DO_STATEMENT, DO_NOT_INSERT_INSERT); 93 94 final Group ifElseGroup= createGroup(numColumns, composite, FormatterMessages.ControlStatementsTabPage_if_else_group_title); 95 fThenStatementPref= createOption(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_then_on_same_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_THEN_STATEMENT_ON_SAME_LINE, FALSE_TRUE); 96 97 Label l= new Label(ifElseGroup, SWT.NONE); 98 GridData gd= new GridData(); 99 gd.widthHint= fPixelConverter.convertWidthInCharsToPixels(4); 100 l.setLayoutData(gd); 101 102 fSimpleIfPref= createOption(ifElseGroup, numColumns - 1, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_simple_if_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_IF_ON_ONE_LINE, FALSE_TRUE); 103 104 fThenStatementPref.addObserver( new Observer () { 105 public void update(Observable o, Object arg) { 106 fSimpleIfPref.setEnabled(!fThenStatementPref.getChecked()); 107 } 108 109 }); 110 111 fSimpleIfPref.setEnabled(!fThenStatementPref.getChecked()); 112 113 createOption(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_else_on_same_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_ELSE_STATEMENT_ON_SAME_LINE, FALSE_TRUE); 114 createCheckboxPref(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_else_if_on_one_line, DefaultCodeFormatterConstants.FORMATTER_COMPACT_ELSE_IF, FALSE_TRUE); 115 createCheckboxPref(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_guardian_clause_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_GUARDIAN_CLAUSE_ON_ONE_LINE, FALSE_TRUE); 116 } 117 118 protected void initializePage() { 119 fPreview.setPreviewText(PREVIEW); 120 } 121 122 125 protected JavaPreview doCreateJavaPreview(Composite parent) { 126 fPreview= new CompilationUnitPreview(fWorkingValues, parent); 127 return fPreview; 128 } 129 130 133 protected void doUpdatePreview() { 134 super.doUpdatePreview(); 135 fPreview.update(); 136 } 137 138 private CheckboxPreference createOption(Composite composite, int span, String name, String key, String [] values) { 139 return createCheckboxPref(composite, span, name, key, values); 140 } 141 } 142 | Popular Tags |