KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > folding > DefaultJavaFoldingPreferenceBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.text.folding;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.events.SelectionListener;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Label;
27
28 import org.eclipse.jface.preference.IPreferenceStore;
29
30 import org.eclipse.jdt.ui.PreferenceConstants;
31 import org.eclipse.jdt.ui.text.folding.IJavaFoldingPreferenceBlock;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 import org.eclipse.jdt.internal.ui.preferences.OverlayPreferenceStore;
35 import org.eclipse.jdt.internal.ui.preferences.OverlayPreferenceStore.OverlayKey;
36
37
38 /**
39  * Java default folding preferences.
40  *
41  * @since 3.0
42  */

43 public class DefaultJavaFoldingPreferenceBlock implements IJavaFoldingPreferenceBlock {
44
45     private IPreferenceStore fStore;
46     private OverlayPreferenceStore fOverlayStore;
47     private OverlayKey[] fKeys;
48     private Map JavaDoc fCheckBoxes= new HashMap JavaDoc();
49     private SelectionListener fCheckBoxListener= new SelectionListener() {
50         public void widgetDefaultSelected(SelectionEvent e) {
51         }
52         public void widgetSelected(SelectionEvent e) {
53             Button button= (Button) e.widget;
54             fOverlayStore.setValue((String JavaDoc) fCheckBoxes.get(button), button.getSelection());
55         }
56     };
57
58
59     public DefaultJavaFoldingPreferenceBlock() {
60         fStore= JavaPlugin.getDefault().getPreferenceStore();
61         fKeys= createKeys();
62         fOverlayStore= new OverlayPreferenceStore(fStore, fKeys);
63     }
64
65     private OverlayKey[] createKeys() {
66         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
67
68         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_JAVADOC));
69         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_INNERTYPES));
70         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_METHODS));
71         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_IMPORTS));
72         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_HEADERS));
73
74         return (OverlayKey[]) overlayKeys.toArray(new OverlayKey[overlayKeys.size()]);
75     }
76
77     /*
78      * @see org.eclipse.jdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
79      */

80     public Control createControl(Composite composite) {
81         fOverlayStore.load();
82         fOverlayStore.start();
83
84         Composite inner= new Composite(composite, SWT.NONE);
85         GridLayout layout= new GridLayout(1, true);
86         layout.verticalSpacing= 3;
87         layout.marginWidth= 0;
88         inner.setLayout(layout);
89
90         Label label= new Label(inner, SWT.LEFT);
91         label.setText(FoldingMessages.DefaultJavaFoldingPreferenceBlock_title);
92
93         addCheckBox(inner, FoldingMessages.DefaultJavaFoldingPreferenceBlock_comments, PreferenceConstants.EDITOR_FOLDING_JAVADOC, 0);
94         addCheckBox(inner, FoldingMessages.DefaultJavaFoldingPreferenceBlock_headers, PreferenceConstants.EDITOR_FOLDING_HEADERS, 0);
95         addCheckBox(inner, FoldingMessages.DefaultJavaFoldingPreferenceBlock_innerTypes, PreferenceConstants.EDITOR_FOLDING_INNERTYPES, 0);
96         addCheckBox(inner, FoldingMessages.DefaultJavaFoldingPreferenceBlock_methods, PreferenceConstants.EDITOR_FOLDING_METHODS, 0);
97         addCheckBox(inner, FoldingMessages.DefaultJavaFoldingPreferenceBlock_imports, PreferenceConstants.EDITOR_FOLDING_IMPORTS, 0);
98
99         return inner;
100     }
101
102     private Button addCheckBox(Composite parent, String JavaDoc label, String JavaDoc key, int indentation) {
103         Button checkBox= new Button(parent, SWT.CHECK);
104         checkBox.setText(label);
105
106         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
107         gd.horizontalIndent= indentation;
108         gd.horizontalSpan= 1;
109         gd.grabExcessVerticalSpace= false;
110         checkBox.setLayoutData(gd);
111         checkBox.addSelectionListener(fCheckBoxListener);
112
113         fCheckBoxes.put(checkBox, key);
114
115         return checkBox;
116     }
117
118     private void initializeFields() {
119         Iterator JavaDoc it= fCheckBoxes.keySet().iterator();
120         while (it.hasNext()) {
121             Button b= (Button) it.next();
122             String JavaDoc key= (String JavaDoc) fCheckBoxes.get(b);
123             b.setSelection(fOverlayStore.getBoolean(key));
124         }
125     }
126
127     /*
128      * @see org.eclipse.jdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#performOk()
129      */

130     public void performOk() {
131         fOverlayStore.propagate();
132     }
133
134
135     /*
136      * @see org.eclipse.jdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#initialize()
137      */

138     public void initialize() {
139         initializeFields();
140     }
141
142     /*
143      * @see org.eclipse.jdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#performDefaults()
144      */

145     public void performDefaults() {
146         fOverlayStore.loadDefaults();
147         initializeFields();
148     }
149
150     /*
151      * @see org.eclipse.jdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#dispose()
152      */

153     public void dispose() {
154         fOverlayStore.stop();
155     }
156 }
157
Popular Tags