1 13 package org.eclipse.jdt.internal.ui.preferences.formatter; 14 15 import java.util.Map ; 16 import java.util.Observable ; 17 import java.util.Observer ; 18 19 import org.eclipse.core.runtime.Assert; 20 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Group; 23 24 import org.eclipse.jdt.core.JavaCore; 25 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 26 27 28 public class IndentationTabPage extends FormatterTabPage { 29 30 33 private static String [] FALSE_TRUE = { 34 DefaultCodeFormatterConstants.FALSE, 35 DefaultCodeFormatterConstants.TRUE 36 }; 37 38 private final String PREVIEW= 39 createPreviewHeader(FormatterMessages.IndentationTabPage_preview_header) + 40 "class Example {" + " int [] myArray= {1,2,3,4,5,6};" + " int theInt= 1;" + " String someString= \"Hello\";" + " double aDouble= 3.0;" + " void foo(int a, int b, int c, int d, int e, int f) {" + " switch(a) {" + " case 0: " + " Other.doFoo();" + " break;" + " default:" + " Other.doBaz();" + " }" + " }" + " void bar(List v) {" + " for (int i= 0; i < 10; i++) {" + " v.add(new Integer(i));" + " }" + " }" + "}" + "\n" + "enum MyEnum {" + " UNDEFINED(0) {" + " void foo() {}" + " }" + "}" + "@interface MyAnnotation {" + " int count() default 1;" + "}"; 70 private CompilationUnitPreview fPreview; 71 private String fOldTabChar= null; 72 73 public IndentationTabPage(ModifyDialog modifyDialog, Map workingValues) { 74 super(modifyDialog, workingValues); 75 } 76 77 protected void doCreatePreferences(Composite composite, int numColumns) { 78 79 final Group generalGroup= createGroup(numColumns, composite, FormatterMessages.IndentationTabPage_general_group_title); 80 81 final String [] tabPolicyValues= new String [] {JavaCore.SPACE, JavaCore.TAB, DefaultCodeFormatterConstants.MIXED}; 82 final String [] tabPolicyLabels= new String [] { 83 FormatterMessages.IndentationTabPage_general_group_option_tab_policy_SPACE, 84 FormatterMessages.IndentationTabPage_general_group_option_tab_policy_TAB, 85 FormatterMessages.IndentationTabPage_general_group_option_tab_policy_MIXED 86 }; 87 final ComboPreference tabPolicy= createComboPref(generalGroup, numColumns, FormatterMessages.IndentationTabPage_general_group_option_tab_policy, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, tabPolicyValues, tabPolicyLabels); 88 final CheckboxPreference onlyForLeading= createCheckboxPref(generalGroup, numColumns, FormatterMessages.IndentationTabPage_use_tabs_only_for_leading_indentations, DefaultCodeFormatterConstants.FORMATTER_USE_TABS_ONLY_FOR_LEADING_INDENTATIONS, FALSE_TRUE); 89 final NumberPreference indentSize= createNumberPref(generalGroup, numColumns, FormatterMessages.IndentationTabPage_general_group_option_indent_size, DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, 0, 32); 90 final NumberPreference tabSize= createNumberPref(generalGroup, numColumns, FormatterMessages.IndentationTabPage_general_group_option_tab_size, DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, 0, 32); 91 92 String tabchar= (String ) fWorkingValues.get(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR); 93 updateTabPreferences(tabchar, tabSize, indentSize, onlyForLeading); 94 tabPolicy.addObserver(new Observer () { 95 public void update(Observable o, Object arg) { 96 updateTabPreferences((String ) arg, tabSize, indentSize, onlyForLeading); 97 } 98 }); 99 tabSize.addObserver(new Observer () { 100 public void update(Observable o, Object arg) { 101 indentSize.updateWidget(); 102 } 103 }); 104 105 final Group typeMemberGroup= createGroup(numColumns, composite, FormatterMessages.IndentationTabPage_field_alignment_group_title); 106 createCheckboxPref(typeMemberGroup, numColumns, FormatterMessages.IndentationTabPage_field_alignment_group_align_fields_in_columns, DefaultCodeFormatterConstants.FORMATTER_ALIGN_TYPE_MEMBERS_ON_COLUMNS, FALSE_TRUE); 107 108 final Group classGroup = createGroup(numColumns, composite, FormatterMessages.IndentationTabPage_indent_group_title); 109 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_class_group_option_indent_declarations_within_class_body, DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_TYPE_HEADER, FALSE_TRUE); 110 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_class_group_option_indent_declarations_within_enum_decl, DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ENUM_DECLARATION_HEADER, FALSE_TRUE); 111 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_class_group_option_indent_declarations_within_enum_const, DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ENUM_CONSTANT_HEADER, FALSE_TRUE); 112 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_class_group_option_indent_declarations_within_annot_decl, DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ANNOTATION_DECLARATION_HEADER, FALSE_TRUE); 113 114 115 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_block_group_option_indent_statements_compare_to_body, DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BODY, FALSE_TRUE); 118 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_block_group_option_indent_statements_compare_to_block, DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BLOCK, FALSE_TRUE); 119 120 121 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_switch_group_option_indent_statements_within_switch_body, DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, FALSE_TRUE); 123 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_switch_group_option_indent_statements_within_case_body, DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES, FALSE_TRUE); 124 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_switch_group_option_indent_break_statements, DefaultCodeFormatterConstants.FORMATTER_INDENT_BREAKS_COMPARE_TO_CASES, FALSE_TRUE); 125 createCheckboxPref(classGroup, numColumns, FormatterMessages.IndentationTabPage_indent_empty_lines, DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, FALSE_TRUE); 126 } 127 128 public void initializePage() { 129 fPreview.setPreviewText(PREVIEW); 130 } 131 132 135 protected JavaPreview doCreateJavaPreview(Composite parent) { 136 fPreview= new CompilationUnitPreview(fWorkingValues, parent); 137 return fPreview; 138 } 139 140 143 protected void doUpdatePreview() { 144 super.doUpdatePreview(); 145 fPreview.update(); 146 } 147 148 private void updateTabPreferences(String tabPolicy, NumberPreference tabPreference, NumberPreference indentPreference, CheckboxPreference onlyForLeading) { 149 158 if (DefaultCodeFormatterConstants.MIXED.equals(tabPolicy)) { 159 if (JavaCore.SPACE.equals(fOldTabChar) || JavaCore.TAB.equals(fOldTabChar)) 160 swapTabValues(); 161 tabPreference.setEnabled(true); 162 tabPreference.setKey(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE); 163 indentPreference.setEnabled(true); 164 indentPreference.setKey(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE); 165 onlyForLeading.setEnabled(true); 166 } else if (JavaCore.SPACE.equals(tabPolicy)) { 167 if (DefaultCodeFormatterConstants.MIXED.equals(fOldTabChar)) 168 swapTabValues(); 169 tabPreference.setEnabled(true); 170 tabPreference.setKey(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE); 171 indentPreference.setEnabled(true); 172 indentPreference.setKey(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE); 173 onlyForLeading.setEnabled(false); 174 } else if (JavaCore.TAB.equals(tabPolicy)) { 175 if (DefaultCodeFormatterConstants.MIXED.equals(fOldTabChar)) 176 swapTabValues(); 177 tabPreference.setEnabled(true); 178 tabPreference.setKey(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE); 179 indentPreference.setEnabled(false); 180 indentPreference.setKey(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE); 181 onlyForLeading.setEnabled(true); 182 } else { 183 Assert.isTrue(false); 184 } 185 fOldTabChar= tabPolicy; 186 } 187 188 private void swapTabValues() { 189 Object tabSize= fWorkingValues.get(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE); 190 Object indentSize= fWorkingValues.get(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE); 191 fWorkingValues.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, indentSize); 192 fWorkingValues.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, tabSize); 193 } 194 } 195 | Popular Tags |