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.layout.GridData; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Group; 20 21 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 22 23 public class BracesTabPage extends FormatterTabPage { 24 25 28 private static String [] FALSE_TRUE = { 29 DefaultCodeFormatterConstants.FALSE, 30 DefaultCodeFormatterConstants.TRUE 31 }; 32 33 private final String PREVIEW= 34 createPreviewHeader(FormatterMessages.BracesTabPage_preview_header) + 35 "interface Empty {}\n" + "\n" + "enum MyEnum {" + " UNDEFINED(0) {" + " void foo() {}" + " }" + "}\n" + "@interface SomeAnnotationType {" + "}" + "class Example {" + " SomeClass fField= new SomeClass() {" + " };" + " int [] myArray= {1,2,3,4,5,6};" + " int [] emptyArray= new int[] {};" + " Example() {" + " }" + " void bar(int p) {" + " for (int i= 0; i<10; i++) {" + " }" + " switch(p) {" + " case 0:" + " fField.set(0);" + " break;" + " case 1: {" + " break;" + " }" + " default:" + " fField.reset();" + " }" + " }" + "}"; 67 68 69 private CompilationUnitPreview fPreview; 70 71 72 private final String [] fBracePositions= { 73 DefaultCodeFormatterConstants.END_OF_LINE, 74 DefaultCodeFormatterConstants.NEXT_LINE, 75 DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED 76 }; 77 78 private final String [] fExtendedBracePositions= { 79 DefaultCodeFormatterConstants.END_OF_LINE, 80 DefaultCodeFormatterConstants.NEXT_LINE, 81 DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED, 82 DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP 83 }; 84 85 private final String [] fBracePositionNames= { 86 FormatterMessages.BracesTabPage_position_same_line, 87 FormatterMessages.BracesTabPage_position_next_line, 88 FormatterMessages.BracesTabPage_position_next_line_indented 89 }; 90 91 private final String [] fExtendedBracePositionNames= { 92 FormatterMessages.BracesTabPage_position_same_line, 93 FormatterMessages.BracesTabPage_position_next_line, 94 FormatterMessages.BracesTabPage_position_next_line_indented, 95 FormatterMessages.BracesTabPage_position_next_line_on_wrap 96 }; 97 98 99 104 public BracesTabPage(ModifyDialog modifyDialog, Map workingValues) { 105 super(modifyDialog, workingValues); 106 } 107 108 protected void doCreatePreferences(Composite composite, int numColumns) { 109 110 final Group group= createGroup(numColumns, composite, FormatterMessages.BracesTabPage_group_brace_positions_title); 111 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_class_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION); 112 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_anonymous_class_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ANONYMOUS_TYPE_DECLARATION); 113 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_constructor_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_CONSTRUCTOR_DECLARATION); 114 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_method_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_METHOD_DECLARATION); 115 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_enum_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ENUM_DECLARATION); 116 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_enumconst_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ENUM_CONSTANT); 117 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_annotation_type_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ANNOTATION_TYPE_DECLARATION); 118 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_blocks, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_BLOCK); 119 createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_blocks_in_case, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_BLOCK_IN_CASE); 120 createBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_switch_case, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_SWITCH); 121 122 ComboPreference arrayInitOption= createBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_array_initializer, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ARRAY_INITIALIZER); 123 final CheckboxPreference arrayInitCheckBox= createIndentedCheckboxPref(group, numColumns, FormatterMessages.BracesTabPage_option_keep_empty_array_initializer_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_EMPTY_ARRAY_INITIALIZER_ON_ONE_LINE, FALSE_TRUE); 124 125 arrayInitOption.addObserver(new Observer () { 126 public void update(Observable o, Object arg) { 127 updateOptionEnablement((ComboPreference) o, arrayInitCheckBox); 128 } 129 }); 130 updateOptionEnablement(arrayInitOption, arrayInitCheckBox); 131 } 132 133 137 protected final void updateOptionEnablement(ComboPreference arrayInitOption, CheckboxPreference arrayInitCheckBox) { 138 arrayInitCheckBox.setEnabled(!arrayInitOption.hasValue(DefaultCodeFormatterConstants.END_OF_LINE)); 139 } 140 141 protected void initializePage() { 142 fPreview.setPreviewText(PREVIEW); 143 } 144 145 protected JavaPreview doCreateJavaPreview(Composite parent) { 146 fPreview= new CompilationUnitPreview(fWorkingValues, parent); 147 return fPreview; 148 } 149 150 private ComboPreference createBracesCombo(Composite composite, int numColumns, String message, String key) { 151 return createComboPref(composite, numColumns, message, key, fBracePositions, fBracePositionNames); 152 } 153 154 private ComboPreference createExtendedBracesCombo(Composite composite, int numColumns, String message, String key) { 155 return createComboPref(composite, numColumns, message, key, fExtendedBracePositions, fExtendedBracePositionNames); 156 } 157 158 private CheckboxPreference createIndentedCheckboxPref(Composite composite, int numColumns, String message, String key, String [] values) { 159 CheckboxPreference pref= createCheckboxPref(composite, numColumns, message, key, values); 160 GridData data= (GridData) pref.getControl().getLayoutData(); 161 data.horizontalIndent= fPixelConverter.convertWidthInCharsToPixels(1); 162 return pref; 163 } 164 165 166 protected void doUpdatePreview() { 167 super.doUpdatePreview(); 168 fPreview.update(); 169 } 170 171 } 172 | Popular Tags |