b873cc6037c16fa1cf20f9398c0c1937658219ab
[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
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.events.SelectionListener;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34
35
36 /**
37  * Java default folding preferences.
38  * 
39  * @since 3.0
40  */
41 public class DefaultJavaFoldingPreferenceBlock implements IJavaFoldingPreferenceBlock {
42
43         private IPreferenceStore fStore;
44         private OverlayPreferenceStore fOverlayStore;
45         private OverlayKey[] fKeys;
46         private Map fCheckBoxes= new HashMap();
47         private SelectionListener fCheckBoxListener= new SelectionListener() {
48                 public void widgetDefaultSelected(SelectionEvent e) {
49                 }
50                 public void widgetSelected(SelectionEvent e) {
51                         Button button= (Button) e.widget;
52                         fOverlayStore.setValue((String) fCheckBoxes.get(button), button.getSelection());
53                 }
54         };
55         
56
57         public DefaultJavaFoldingPreferenceBlock() {
58                 fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
59                 fKeys= createKeys();
60                 fOverlayStore= new OverlayPreferenceStore(fStore, fKeys);
61         }
62         
63         private OverlayKey[] createKeys() {
64                 ArrayList overlayKeys= new ArrayList();
65
66                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_JAVADOC));
67 //              overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_INNERTYPES));
68                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_METHODS));
69 //              overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_IMPORTS));
70                 
71                 return (OverlayKey[]) overlayKeys.toArray(new OverlayKey[overlayKeys.size()]);
72         }
73
74         /*
75          * @see net.sourceforge.phpdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
76          */
77         public Control createControl(Composite composite) {
78                 fOverlayStore.load();
79                 fOverlayStore.start();
80                 
81                 Composite inner= new Composite(composite, SWT.NONE);
82                 GridLayout layout= new GridLayout(1, true);
83                 layout.verticalSpacing= 3;
84                 layout.marginWidth= 0;
85                 inner.setLayout(layout);
86                 
87                 Label label= new Label(inner, SWT.LEFT);
88                 label.setText(FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.title")); //$NON-NLS-1$
89                 
90 //              addCheckBox(inner, FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.comments"), PreferenceConstants.EDITOR_FOLDING_JAVADOC, 0); //$NON-NLS-1$
91 //              addCheckBox(inner, FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.innerTypes"), PreferenceConstants.EDITOR_FOLDING_INNERTYPES, 0); //$NON-NLS-1$
92                 addCheckBox(inner, FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.methods"), PreferenceConstants.EDITOR_FOLDING_METHODS, 0); //$NON-NLS-1$
93 //              addCheckBox(inner, FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.imports"), PreferenceConstants.EDITOR_FOLDING_IMPORTS, 0); //$NON-NLS-1$
94                 
95                 return inner;
96         }
97         
98         private Button addCheckBox(Composite parent, String label, String key, int indentation) {               
99                 Button checkBox= new Button(parent, SWT.CHECK);
100                 checkBox.setText(label);
101                 
102                 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
103                 gd.horizontalIndent= indentation;
104                 gd.horizontalSpan= 1;
105                 gd.grabExcessVerticalSpace= false;
106                 checkBox.setLayoutData(gd);
107                 checkBox.addSelectionListener(fCheckBoxListener);
108                 
109                 fCheckBoxes.put(checkBox, key);
110                 
111                 return checkBox;
112         }
113         
114         private void initializeFields() {
115                 Iterator it= fCheckBoxes.keySet().iterator();
116                 while (it.hasNext()) {
117                         Button b= (Button) it.next();
118                         String key= (String) fCheckBoxes.get(b);
119                         b.setSelection(fOverlayStore.getBoolean(key));
120                 }
121         }
122         
123         /*
124          * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#performOk()
125          */
126         public void performOk() {
127                 fOverlayStore.propagate();
128         }
129         
130         
131         /*
132          * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#initialize()
133          */
134         public void initialize() {
135                 initializeFields();
136         }
137         
138         /*
139          * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#performDefaults()
140          */
141         public void performDefaults() {
142                 fOverlayStore.loadDefaults();
143                 initializeFields();
144         }
145         
146         /*
147          * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#dispose()
148          */
149         public void dispose() {
150                 fOverlayStore.stop();
151         }
152 }