KickJava   Java API By Example, From Geeks To Geeks.

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


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.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Group;
21 import org.eclipse.swt.widgets.Label;
22
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
25
26
27 public class ControlStatementsTabPage extends FormatterTabPage {
28     
29     /**
30      * Constant array for boolean selection
31      */

32     private static String JavaDoc[] FALSE_TRUE = {
33         DefaultCodeFormatterConstants.FALSE,
34         DefaultCodeFormatterConstants.TRUE
35     };
36     
37     /**
38      * Constant array for insert / not_insert.
39      */

40     private static String JavaDoc[] DO_NOT_INSERT_INSERT = {
41         JavaCore.DO_NOT_INSERT,
42         JavaCore.INSERT
43     };
44     
45     
46     private final String JavaDoc PREVIEW=
47     createPreviewHeader(FormatterMessages.ControlStatementsTabPage_preview_header) +
48     "class Example {" + //$NON-NLS-1$
49
" void bar() {" + //$NON-NLS-1$
50
" do {} while (true);" + //$NON-NLS-1$
51
" try {} catch (Exception e) { } finally { }" + //$NON-NLS-1$
52
" }" + //$NON-NLS-1$
53
" void foo2() {" + //$NON-NLS-1$
54
" if (true) { " + //$NON-NLS-1$
55
" return;" + //$NON-NLS-1$
56
" }" + //$NON-NLS-1$
57
" if (true) {" + //$NON-NLS-1$
58
" return;" + //$NON-NLS-1$
59
" } else if (false) {" + //$NON-NLS-1$
60
" return; " + //$NON-NLS-1$
61
" } else {" + //$NON-NLS-1$
62
" return;" + //$NON-NLS-1$
63
" }" + //$NON-NLS-1$
64
" }" + //$NON-NLS-1$
65
" void foo(int state) {" + //$NON-NLS-1$
66
" if (true) return;" + //$NON-NLS-1$
67
" if (true) " + //$NON-NLS-1$
68
" return;" + //$NON-NLS-1$
69
" else if (false)" + //$NON-NLS-1$
70
" return;" + //$NON-NLS-1$
71
" else return;" + //$NON-NLS-1$
72
" }" + //$NON-NLS-1$
73
"}"; //$NON-NLS-1$
74

75     
76     
77     private CompilationUnitPreview fPreview;
78     
79     protected CheckboxPreference fThenStatementPref, fSimpleIfPref;
80
81     
82     public ControlStatementsTabPage(ModifyDialog modifyDialog, Map JavaDoc workingValues) {
83         super(modifyDialog, workingValues);
84     }
85
86     protected void doCreatePreferences(Composite composite, int numColumns) {
87         
88         final Group generalGroup= createGroup(numColumns, composite, FormatterMessages.ControlStatementsTabPage_general_group_title);
89         createOption(generalGroup, numColumns, FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_else_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_ELSE_IN_IF_STATEMENT, DO_NOT_INSERT_INSERT);
90         createOption(generalGroup, numColumns, FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_catch_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_CATCH_IN_TRY_STATEMENT, DO_NOT_INSERT_INSERT);
91         createOption(generalGroup, numColumns, FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_finally_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_FINALLY_IN_TRY_STATEMENT, DO_NOT_INSERT_INSERT);
92         createOption(generalGroup, numColumns, FormatterMessages.ControlStatementsTabPage_general_group_insert_new_line_before_while_in_do_statements, DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_WHILE_IN_DO_STATEMENT, DO_NOT_INSERT_INSERT);
93                 
94         final Group ifElseGroup= createGroup(numColumns, composite, FormatterMessages.ControlStatementsTabPage_if_else_group_title);
95         fThenStatementPref= createOption(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_then_on_same_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_THEN_STATEMENT_ON_SAME_LINE, FALSE_TRUE);
96         
97         Label l= new Label(ifElseGroup, SWT.NONE);
98         GridData gd= new GridData();
99         gd.widthHint= fPixelConverter.convertWidthInCharsToPixels(4);
100         l.setLayoutData(gd);
101         
102         fSimpleIfPref= createOption(ifElseGroup, numColumns - 1, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_simple_if_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_IF_ON_ONE_LINE, FALSE_TRUE);
103         
104         fThenStatementPref.addObserver( new Observer JavaDoc() {
105             public void update(Observable JavaDoc o, Object JavaDoc arg) {
106                 fSimpleIfPref.setEnabled(!fThenStatementPref.getChecked());
107             }
108             
109         });
110         
111         fSimpleIfPref.setEnabled(!fThenStatementPref.getChecked());
112         
113         createOption(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_else_on_same_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_ELSE_STATEMENT_ON_SAME_LINE, FALSE_TRUE);
114         createCheckboxPref(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_else_if_on_one_line, DefaultCodeFormatterConstants.FORMATTER_COMPACT_ELSE_IF, FALSE_TRUE);
115         createCheckboxPref(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_guardian_clause_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_GUARDIAN_CLAUSE_ON_ONE_LINE, FALSE_TRUE);
116     }
117     
118     protected void initializePage() {
119         fPreview.setPreviewText(PREVIEW);
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doCreateJavaPreview(org.eclipse.swt.widgets.Composite)
124      */

125     protected JavaPreview doCreateJavaPreview(Composite parent) {
126         fPreview= new CompilationUnitPreview(fWorkingValues, parent);
127         return fPreview;
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doUpdatePreview()
132      */

133     protected void doUpdatePreview() {
134         super.doUpdatePreview();
135         fPreview.update();
136     }
137
138     private CheckboxPreference createOption(Composite composite, int span, String JavaDoc name, String JavaDoc key, String JavaDoc [] values) {
139         return createCheckboxPref(composite, span, name, key, values);
140     }
141 }
142
Popular Tags