KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

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

28     private static String JavaDoc[] FALSE_TRUE = {
29         DefaultCodeFormatterConstants.FALSE,
30         DefaultCodeFormatterConstants.TRUE
31     };
32     
33     private final String JavaDoc PREVIEW=
34     createPreviewHeader(FormatterMessages.BracesTabPage_preview_header) +
35     "interface Empty {}\n" + //$NON-NLS-1$
36
"\n" + //$NON-NLS-1$
37
"enum MyEnum {" + //$NON-NLS-1$
38
" UNDEFINED(0) {" + //$NON-NLS-1$
39
" void foo() {}" + //$NON-NLS-1$
40
" }" + //$NON-NLS-1$
41
"}\n" + //$NON-NLS-1$
42
"@interface SomeAnnotationType {" + //$NON-NLS-1$
43
"}" + //$NON-NLS-1$
44
"class Example {" + //$NON-NLS-1$
45
" SomeClass fField= new SomeClass() {" + //$NON-NLS-1$
46
" };" + //$NON-NLS-1$
47
" int [] myArray= {1,2,3,4,5,6};" + //$NON-NLS-1$
48
" int [] emptyArray= new int[] {};" + //$NON-NLS-1$
49
" Example() {" + //$NON-NLS-1$
50
" }" + //$NON-NLS-1$
51
" void bar(int p) {" + //$NON-NLS-1$
52
" for (int i= 0; i<10; i++) {" + //$NON-NLS-1$
53
" }" + //$NON-NLS-1$
54
" switch(p) {" + //$NON-NLS-1$
55
" case 0:" + //$NON-NLS-1$
56
" fField.set(0);" + //$NON-NLS-1$
57
" break;" + //$NON-NLS-1$
58
" case 1: {" + //$NON-NLS-1$
59
" break;" + //$NON-NLS-1$
60
" }" + //$NON-NLS-1$
61
" default:" + //$NON-NLS-1$
62
" fField.reset();" + //$NON-NLS-1$
63
" }" + //$NON-NLS-1$
64
" }" + //$NON-NLS-1$
65
"}"; //$NON-NLS-1$
66

67     
68     
69     private CompilationUnitPreview fPreview;
70     
71     
72     private final String JavaDoc [] fBracePositions= {
73         DefaultCodeFormatterConstants.END_OF_LINE,
74         DefaultCodeFormatterConstants.NEXT_LINE,
75         DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED
76     };
77     
78     private final String JavaDoc [] 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 JavaDoc [] 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 JavaDoc [] 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     /**
100      * Create a new BracesTabPage.
101      * @param modifyDialog
102      * @param workingValues
103      */

104     public BracesTabPage(ModifyDialog modifyDialog, Map JavaDoc 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 JavaDoc() {
126             public void update(Observable JavaDoc o, Object JavaDoc arg) {
127                 updateOptionEnablement((ComboPreference) o, arrayInitCheckBox);
128             }
129         });
130         updateOptionEnablement(arrayInitOption, arrayInitCheckBox);
131     }
132     
133     /**
134      * @param arrayInitOption
135      * @param arrayInitCheckBox
136      */

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 JavaDoc message, String JavaDoc key) {
151         return createComboPref(composite, numColumns, message, key, fBracePositions, fBracePositionNames);
152     }
153     
154     private ComboPreference createExtendedBracesCombo(Composite composite, int numColumns, String JavaDoc message, String JavaDoc key) {
155         return createComboPref(composite, numColumns, message, key, fExtendedBracePositions, fExtendedBracePositionNames);
156     }
157     
158     private CheckboxPreference createIndentedCheckboxPref(Composite composite, int numColumns, String JavaDoc message, String JavaDoc key, String JavaDoc [] 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