42d14ebf3575bd47b21a2a00497599ce7e97c260
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.folding;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.Map;
17
18 import net.sourceforge.phpdt.internal.ui.preferences.OverlayPreferenceStore;
19 import net.sourceforge.phpdt.internal.ui.preferences.OverlayPreferenceStore.OverlayKey;
20 import net.sourceforge.phpdt.ui.PreferenceConstants;
21 import net.sourceforge.phpdt.ui.text.folding.IJavaFoldingPreferenceBlock;
22 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import net.sourceforge.phpeclipse.ui.WebUI;
24
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Label;
35
36 /**
37  * Java default folding preferences.
38  * 
39  * @since 3.0
40  */
41 public class DefaultJavaFoldingPreferenceBlock implements
42                 IJavaFoldingPreferenceBlock {
43
44         private IPreferenceStore fStore;
45
46         private OverlayPreferenceStore fOverlayStore;
47
48         private OverlayKey[] fKeys;
49
50         private Map fCheckBoxes = new HashMap();
51
52         private SelectionListener fCheckBoxListener = new SelectionListener() {
53                 public void widgetDefaultSelected(SelectionEvent e) {
54                 }
55
56                 public void widgetSelected(SelectionEvent e) {
57                         Button button = (Button) e.widget;
58                         fOverlayStore.setValue((String) fCheckBoxes.get(button), button
59                                         .getSelection());
60                 }
61         };
62
63         public DefaultJavaFoldingPreferenceBlock() {
64                 fStore = WebUI.getDefault().getPreferenceStore();
65                 fKeys = createKeys();
66                 fOverlayStore = new OverlayPreferenceStore(fStore, fKeys);
67         }
68
69         private OverlayKey[] createKeys() {
70                 ArrayList overlayKeys = new ArrayList();
71
72                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
73                                 OverlayPreferenceStore.BOOLEAN,
74                                 PreferenceConstants.EDITOR_FOLDING_JAVADOC));
75                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
76                                 OverlayPreferenceStore.BOOLEAN,
77                                 PreferenceConstants.EDITOR_FOLDING_HEADERS));
78                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
79                                 OverlayPreferenceStore.BOOLEAN,
80                                 PreferenceConstants.EDITOR_FOLDING_INNERTYPES));
81                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
82                                 OverlayPreferenceStore.BOOLEAN,
83                                 PreferenceConstants.EDITOR_FOLDING_METHODS));
84                 // overlayKeys.add(new
85                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
86                 // PreferenceConstants.EDITOR_FOLDING_IMPORTS));
87
88                 return (OverlayKey[]) overlayKeys.toArray(new OverlayKey[overlayKeys
89                                 .size()]);
90         }
91
92         /*
93          * @see net.sourceforge.phpdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
94          */
95         public Control createControl(Composite composite) {
96                 fOverlayStore.load();
97                 fOverlayStore.start();
98
99                 Composite inner = new Composite(composite, SWT.NONE);
100                 GridLayout layout = new GridLayout(1, true);
101                 layout.verticalSpacing = 3;
102                 layout.marginWidth = 0;
103                 inner.setLayout(layout);
104
105                 Label label = new Label(inner, SWT.LEFT);
106                 label.setText(FoldingMessages
107                                 .getString("DefaultJavaFoldingPreferenceBlock.title")); //$NON-NLS-1$
108
109                 addCheckBox(
110                                 inner,
111                                 FoldingMessages
112                                                 .getString("DefaultJavaFoldingPreferenceBlock.comments"), PreferenceConstants.EDITOR_FOLDING_JAVADOC, 0); //$NON-NLS-1$
113                 addCheckBox(
114                                 inner,
115                                 FoldingMessages
116                                                 .getString("DefaultJavaFoldingPreferenceBlock.headers"), PreferenceConstants.EDITOR_FOLDING_HEADERS, 0);//$NON-NLS-1$
117                 addCheckBox(inner, FoldingMessages
118                                 .getString("DefaultJavaFoldingPreferenceBlock.innerTypes"),
119                                 PreferenceConstants.EDITOR_FOLDING_INNERTYPES, 0); //$NON-NLS-1$
120                 addCheckBox(
121                                 inner,
122                                 FoldingMessages
123                                                 .getString("DefaultJavaFoldingPreferenceBlock.methods"), PreferenceConstants.EDITOR_FOLDING_METHODS, 0); //$NON-NLS-1$
124                 // addCheckBox(inner,
125                 // FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.imports"),
126                 // PreferenceConstants.EDITOR_FOLDING_IMPORTS, 0); //$NON-NLS-1$
127
128                 return inner;
129         }
130
131         private Button addCheckBox(Composite parent, String label, String key,
132                         int indentation) {
133                 Button checkBox = new Button(parent, SWT.CHECK);
134                 checkBox.setText(label);
135
136                 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
137                 gd.horizontalIndent = indentation;
138                 gd.horizontalSpan = 1;
139                 gd.grabExcessVerticalSpace = false;
140                 checkBox.setLayoutData(gd);
141                 checkBox.addSelectionListener(fCheckBoxListener);
142
143                 fCheckBoxes.put(checkBox, key);
144
145                 return checkBox;
146         }
147
148         private void initializeFields() {
149                 Iterator it = fCheckBoxes.keySet().iterator();
150                 while (it.hasNext()) {
151                         Button b = (Button) it.next();
152                         String key = (String) fCheckBoxes.get(b);
153                         b.setSelection(fOverlayStore.getBoolean(key));
154                 }
155         }
156
157         /*
158          * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#performOk()
159          */
160         public void performOk() {
161                 fOverlayStore.propagate();
162         }
163
164         /*
165          * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#initialize()
166          */
167         public void initialize() {
168                 initializeFields();
169         }
170
171         /*
172          * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#performDefaults()
173          */
174         public void performDefaults() {
175                 fOverlayStore.loadDefaults();
176                 initializeFields();
177         }
178
179         /*
180          * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#dispose()
181          */
182         public void dispose() {
183                 fOverlayStore.stop();
184         }
185 }