]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
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 | ||
24 | public class BracesTabPage extends FormatterTabPage { | |
25 | ||
26 | private final String PREVIEW= | |
27 | createPreviewHeader(FormatterMessages.BracesTabPage_preview_header) + | |
28 | "interface Empty {}\n" + //$NON-NLS-1$ | |
29 | "\n" + //$NON-NLS-1$ | |
30 | "enum MyEnum {" + //$NON-NLS-1$ | |
31 | " UNDEFINED(0) {" + //$NON-NLS-1$ | |
32 | " void foo() {}" + //$NON-NLS-1$ | |
33 | " }" + //$NON-NLS-1$ | |
34 | "}\n" + //$NON-NLS-1$ | |
35 | "@interface SomeAnnotationType {" + //$NON-NLS-1$ | |
36 | "}" + //$NON-NLS-1$ | |
37 | "class Example {" + //$NON-NLS-1$ | |
38 | " SomeClass fField= new SomeClass() {" + //$NON-NLS-1$ | |
39 | " };" + //$NON-NLS-1$ | |
40 | " int [] myArray= {1,2,3,4,5,6};" + //$NON-NLS-1$ | |
41 | " int [] emptyArray= new int[] {};" + //$NON-NLS-1$ | |
42 | " Example() {" + //$NON-NLS-1$ | |
43 | " }" + //$NON-NLS-1$ | |
44 | " void bar(int p) {" + //$NON-NLS-1$ | |
45 | " for (int i= 0; i<10; i++) {" + //$NON-NLS-1$ | |
46 | " }" + //$NON-NLS-1$ | |
47 | " switch(p) {" + //$NON-NLS-1$ | |
48 | " case 0:" + //$NON-NLS-1$ | |
49 | " fField.set(0);" + //$NON-NLS-1$ | |
50 | " break;" + //$NON-NLS-1$ | |
51 | " case 1: {" + //$NON-NLS-1$ | |
52 | " break;" + //$NON-NLS-1$ | |
53 | " }" + //$NON-NLS-1$ | |
54 | " default:" + //$NON-NLS-1$ | |
55 | " fField.reset();" + //$NON-NLS-1$ | |
56 | " }" + //$NON-NLS-1$ | |
57 | " }" + //$NON-NLS-1$ | |
58 | "}"; //$NON-NLS-1$ | |
59 | ||
60 | ||
61 | ||
62 | private CompilationUnitPreview fPreview; | |
63 | ||
64 | ||
65 | private final String [] fBracePositions= { | |
66 | DefaultCodeFormatterConstants.END_OF_LINE, | |
67 | DefaultCodeFormatterConstants.NEXT_LINE, | |
68 | DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED | |
69 | }; | |
70 | ||
71 | private final String [] fExtendedBracePositions= { | |
72 | DefaultCodeFormatterConstants.END_OF_LINE, | |
73 | DefaultCodeFormatterConstants.NEXT_LINE, | |
74 | DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED, | |
75 | DefaultCodeFormatterConstants.NEXT_LINE_ON_WRAP | |
76 | }; | |
77 | ||
78 | private final String [] fBracePositionNames= { | |
79 | FormatterMessages.BracesTabPage_position_same_line, | |
80 | FormatterMessages.BracesTabPage_position_next_line, | |
81 | FormatterMessages.BracesTabPage_position_next_line_indented | |
82 | }; | |
83 | ||
84 | private final String [] fExtendedBracePositionNames= { | |
85 | FormatterMessages.BracesTabPage_position_same_line, | |
86 | FormatterMessages.BracesTabPage_position_next_line, | |
87 | FormatterMessages.BracesTabPage_position_next_line_indented, | |
88 | FormatterMessages.BracesTabPage_position_next_line_on_wrap | |
89 | }; | |
90 | ||
91 | ||
92 | /** | |
93 | * Creates a new BracesTabPage. | |
94 | * | |
95 | * @param modifyDialog the modify dialog | |
96 | * @param workingValues the working values | |
97 | */ | |
98 | public BracesTabPage(ModifyDialog modifyDialog, Map<String, String> workingValues) { | |
99 | super(modifyDialog, workingValues); | |
100 | } | |
101 | ||
102 | @Override | |
103 | protected void doCreatePreferences(Composite composite, int numColumns) { | |
104 | ||
105 | final Group group= createGroup(numColumns, composite, FormatterMessages.BracesTabPage_group_brace_positions_title); | |
106 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_class_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION); | |
107 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_anonymous_class_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ANONYMOUS_TYPE_DECLARATION); | |
108 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_constructor_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_CONSTRUCTOR_DECLARATION); | |
109 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_method_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_METHOD_DECLARATION); | |
110 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_enum_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ENUM_DECLARATION); | |
111 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_enumconst_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ENUM_CONSTANT); | |
112 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_annotation_type_declaration, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ANNOTATION_TYPE_DECLARATION); | |
113 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_blocks, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_BLOCK); | |
114 | createExtendedBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_blocks_in_case, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_BLOCK_IN_CASE); | |
115 | createBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_switch_case, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_SWITCH); | |
116 | ||
117 | ComboPreference arrayInitOption= createBracesCombo(group, numColumns, FormatterMessages.BracesTabPage_option_array_initializer, DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ARRAY_INITIALIZER); | |
118 | 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); | |
119 | ||
120 | arrayInitOption.addObserver(new Observer() { | |
121 | public void update(Observable o, Object arg) { | |
122 | updateOptionEnablement((ComboPreference) o, arrayInitCheckBox); | |
123 | } | |
124 | }); | |
125 | updateOptionEnablement(arrayInitOption, arrayInitCheckBox); | |
126 | } | |
127 | ||
128 | protected final void updateOptionEnablement(ComboPreference arrayInitOption, CheckboxPreference arrayInitCheckBox) { | |
129 | arrayInitCheckBox.setEnabled(!arrayInitOption.hasValue(DefaultCodeFormatterConstants.END_OF_LINE)); | |
130 | } | |
131 | ||
132 | @Override | |
133 | protected void initializePage() { | |
134 | fPreview.setPreviewText(PREVIEW); | |
135 | } | |
136 | ||
137 | @Override | |
138 | protected JavaPreview doCreateJavaPreview(Composite parent) { | |
139 | fPreview= new CompilationUnitPreview(fWorkingValues, parent); | |
140 | return fPreview; | |
141 | } | |
142 | ||
143 | private ComboPreference createBracesCombo(Composite composite, int numColumns, String message, String key) { | |
144 | return createComboPref(composite, numColumns, message, key, fBracePositions, fBracePositionNames); | |
145 | } | |
146 | ||
147 | private ComboPreference createExtendedBracesCombo(Composite composite, int numColumns, String message, String key) { | |
148 | return createComboPref(composite, numColumns, message, key, fExtendedBracePositions, fExtendedBracePositionNames); | |
149 | } | |
150 | ||
151 | private CheckboxPreference createIndentedCheckboxPref(Composite composite, int numColumns, String message, String key, String [] values) { | |
152 | CheckboxPreference pref= createCheckboxPref(composite, numColumns, message, key, values); | |
153 | GridData data= (GridData) pref.getControl().getLayoutData(); | |
154 | data.horizontalIndent= fPixelConverter.convertWidthInCharsToPixels(1); | |
155 | return pref; | |
156 | } | |
157 | ||
158 | ||
159 | @Override | |
160 | protected void doUpdatePreview() { | |
161 | super.doUpdatePreview(); | |
162 | fPreview.update(); | |
163 | } | |
164 | ||
165 | } |