KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > formatter > IndentationTabPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * istvan@benedek-home.de
11  * - 103706 [formatter] indent empty lines
12  *******************************************************************************/

13 package org.eclipse.jdt.internal.ui.preferences.formatter;
14
15 import java.util.Map JavaDoc;
16 import java.util.Observable JavaDoc;
17 import java.util.Observer JavaDoc;
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     /**
31      * Constant array for boolean selection
32      */

33     private static String JavaDoc[] FALSE_TRUE = {
34         DefaultCodeFormatterConstants.FALSE,
35         DefaultCodeFormatterConstants.TRUE
36     };
37     
38     private final String JavaDoc PREVIEW=
39     createPreviewHeader(FormatterMessages.IndentationTabPage_preview_header) +
40     "class Example {" + //$NON-NLS-1$
41
" int [] myArray= {1,2,3,4,5,6};" + //$NON-NLS-1$
42
" int theInt= 1;" + //$NON-NLS-1$
43
" String someString= \"Hello\";" + //$NON-NLS-1$
44
" double aDouble= 3.0;" + //$NON-NLS-1$
45
" void foo(int a, int b, int c, int d, int e, int f) {" + //$NON-NLS-1$
46
" switch(a) {" + //$NON-NLS-1$
47
" case 0: " + //$NON-NLS-1$
48
" Other.doFoo();" + //$NON-NLS-1$
49
" break;" + //$NON-NLS-1$
50
" default:" + //$NON-NLS-1$
51
" Other.doBaz();" + //$NON-NLS-1$
52
" }" + //$NON-NLS-1$
53
" }" + //$NON-NLS-1$
54
" void bar(List v) {" + //$NON-NLS-1$
55
" for (int i= 0; i < 10; i++) {" + //$NON-NLS-1$
56
" v.add(new Integer(i));" + //$NON-NLS-1$
57
" }" + //$NON-NLS-1$
58
" }" + //$NON-NLS-1$
59
"}" + //$NON-NLS-1$
60
"\n" + //$NON-NLS-1$
61
"enum MyEnum {" + //$NON-NLS-1$
62
" UNDEFINED(0) {" + //$NON-NLS-1$
63
" void foo() {}" + //$NON-NLS-1$
64
" }" + //$NON-NLS-1$
65
"}" + //$NON-NLS-1$
66
"@interface MyAnnotation {" + //$NON-NLS-1$
67
" int count() default 1;" + //$NON-NLS-1$
68
"}";//$NON-NLS-1$
69

70     private CompilationUnitPreview fPreview;
71     private String JavaDoc fOldTabChar= null;
72     
73     public IndentationTabPage(ModifyDialog modifyDialog, Map JavaDoc 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 JavaDoc[] tabPolicyValues= new String JavaDoc[] {JavaCore.SPACE, JavaCore.TAB, DefaultCodeFormatterConstants.MIXED};
82         final String JavaDoc[] tabPolicyLabels= new String JavaDoc[] {
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 JavaDoc tabchar= (String JavaDoc) fWorkingValues.get(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
93         updateTabPreferences(tabchar, tabSize, indentSize, onlyForLeading);
94         tabPolicy.addObserver(new Observer JavaDoc() {
95             public void update(Observable JavaDoc o, Object JavaDoc arg) {
96                 updateTabPreferences((String JavaDoc) arg, tabSize, indentSize, onlyForLeading);
97             }
98         });
99         tabSize.addObserver(new Observer JavaDoc() {
100             public void update(Observable JavaDoc o, Object JavaDoc 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 // final Group blockGroup= createGroup(numColumns, composite, FormatterMessages.getString("IndentationTabPage.block_group.title")); //$NON-NLS-1$
116
//createCheckboxPref(classGroup, numColumns, FormatterMessages.getString("IndentationTabPage.block_group.option.indent_statements_within_blocks_and_methods"), DefaultCodeFormatterConstants.FORMATTER_INDENT_BLOCK_STATEMENTS, FALSE_TRUE); //$NON-NLS-1$
117
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 // final Group switchGroup= createGroup(numColumns, composite, FormatterMessages.getString("IndentationTabPage.switch_group.title")); //$NON-NLS-1$
122
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     /* (non-Javadoc)
133      * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doCreateJavaPreview(org.eclipse.swt.widgets.Composite)
134      */

135     protected JavaPreview doCreateJavaPreview(Composite parent) {
136         fPreview= new CompilationUnitPreview(fWorkingValues, parent);
137         return fPreview;
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doUpdatePreview()
142      */

143     protected void doUpdatePreview() {
144         super.doUpdatePreview();
145         fPreview.update();
146     }
147
148     private void updateTabPreferences(String JavaDoc tabPolicy, NumberPreference tabPreference, NumberPreference indentPreference, CheckboxPreference onlyForLeading) {
149         /*
150          * If the tab-char is SPACE (or TAB), INDENTATION_SIZE
151          * preference is not used by the core formatter. We piggy back the
152          * visual tab length setting in that preference in that case. If the
153          * user selects MIXED, we use the previous TAB_SIZE preference as the
154          * new INDENTATION_SIZE (as this is what it really is) and set the
155          * visual tab size to the value piggy backed in the INDENTATION_SIZE
156          * preference. See also CodeFormatterUtil.
157          */

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 JavaDoc tabSize= fWorkingValues.get(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE);
190         Object JavaDoc 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