Fix #1693310 Missing function/method declaration in the completion list
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / FoldingConfigurationBlock.java
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
12 package net.sourceforge.phpdt.internal.ui.preferences;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.Map;
18
19 import net.sourceforge.phpdt.internal.ui.text.folding.JavaFoldingStructureProviderDescriptor;
20 import net.sourceforge.phpdt.internal.ui.text.folding.JavaFoldingStructureProviderRegistry;
21 import net.sourceforge.phpdt.internal.ui.util.PixelConverter;
22 import net.sourceforge.phpdt.ui.PreferenceConstants;
23 import net.sourceforge.phpdt.ui.text.folding.IJavaFoldingPreferenceBlock;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jface.text.Assert;
30 import org.eclipse.jface.viewers.ComboViewer;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.IStructuredContentProvider;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.LabelProvider;
35 import org.eclipse.jface.viewers.SelectionChangedEvent;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.custom.StackLayout;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.events.SelectionListener;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.FillLayout;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
47 import org.eclipse.swt.widgets.Combo;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Control;
50 import org.eclipse.swt.widgets.Label;
51
52 /**
53  * Configures Java Editor folding preferences.
54  * 
55  * @since 3.0
56  */
57 class FoldingConfigurationBlock {
58
59         private static class ErrorPreferences implements
60                         IJavaFoldingPreferenceBlock {
61                 private String fMessage;
62
63                 protected ErrorPreferences(String message) {
64                         fMessage = message;
65                 }
66
67                 /*
68                  * @see net.sourceforge.phpdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
69                  */
70                 public Control createControl(Composite composite) {
71                         Composite inner = new Composite(composite, SWT.NONE);
72                         inner.setLayout(new FillLayout(SWT.VERTICAL));
73
74                         Label label = new Label(inner, SWT.CENTER);
75                         label.setText(fMessage);
76
77                         return inner;
78                 }
79
80                 public void initialize() {
81                 }
82
83                 public void performOk() {
84                 }
85
86                 public void performDefaults() {
87                 }
88
89                 public void dispose() {
90                 }
91
92         }
93
94         /** The overlay preference store. */
95         private final OverlayPreferenceStore fStore;
96
97         /* The controls */
98         private Combo fProviderCombo;
99
100         private Button fFoldingCheckbox;
101
102         private ComboViewer fProviderViewer;
103
104         private Map fProviderDescriptors;
105
106         private Composite fGroup;
107
108         private Map fProviderPreferences;
109
110         private Map fProviderControls;
111
112         private StackLayout fStackLayout;
113
114         public FoldingConfigurationBlock(OverlayPreferenceStore store) {
115                 Assert.isNotNull(store);
116                 fStore = store;
117                 fStore.addKeys(createOverlayStoreKeys());
118                 fProviderDescriptors = createListModel();
119                 fProviderPreferences = new HashMap();
120                 fProviderControls = new HashMap();
121         }
122
123         private Map createListModel() {
124                 JavaFoldingStructureProviderRegistry reg = PHPeclipsePlugin
125                                 .getDefault().getFoldingStructureProviderRegistry();
126                 reg.reloadExtensions();
127                 JavaFoldingStructureProviderDescriptor[] descs = reg
128                                 .getFoldingProviderDescriptors();
129                 Map map = new HashMap();
130                 for (int i = 0; i < descs.length; i++) {
131                         map.put(descs[i].getId(), descs[i]);
132                 }
133                 return map;
134         }
135
136         private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
137
138                 ArrayList overlayKeys = new ArrayList();
139
140                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
141                                 OverlayPreferenceStore.BOOLEAN,
142                                 PreferenceConstants.EDITOR_FOLDING_ENABLED));
143                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
144                                 OverlayPreferenceStore.STRING,
145                                 PreferenceConstants.EDITOR_FOLDING_PROVIDER));
146
147                 OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys
148                                 .size()];
149                 overlayKeys.toArray(keys);
150                 return keys;
151         }
152
153         /**
154          * Creates page for folding preferences.
155          * 
156          * @param parent
157          *            the parent composite
158          * @return the control for the preference page
159          */
160         Control createControl(Composite parent) {
161
162                 Composite composite = new Composite(parent, SWT.NULL);
163                 // assume parent page uses griddata
164                 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER
165                                 | GridData.VERTICAL_ALIGN_FILL);
166                 composite.setLayoutData(gd);
167                 GridLayout layout = new GridLayout();
168                 layout.numColumns = 2;
169                 PixelConverter pc = new PixelConverter(composite);
170                 layout.verticalSpacing = pc.convertHeightInCharsToPixels(1) / 2;
171                 composite.setLayout(layout);
172
173                 /* check box for new editors */
174                 fFoldingCheckbox = new Button(composite, SWT.CHECK);
175                 fFoldingCheckbox.setText(PreferencesMessages
176                                 .getString("FoldingConfigurationBlock.enable")); //$NON-NLS-1$
177                 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
178                                 | GridData.VERTICAL_ALIGN_BEGINNING);
179                 fFoldingCheckbox.setLayoutData(gd);
180                 fFoldingCheckbox.addSelectionListener(new SelectionListener() {
181                         public void widgetSelected(SelectionEvent e) {
182                                 boolean enabled = fFoldingCheckbox.getSelection();
183                                 fStore.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED,
184                                                 enabled);
185                                 updateCheckboxDependencies();
186                         }
187
188                         public void widgetDefaultSelected(SelectionEvent e) {
189                         }
190                 });
191
192                 Label label = new Label(composite, SWT.CENTER);
193                 gd = new GridData(GridData.FILL_HORIZONTAL
194                                 | GridData.VERTICAL_ALIGN_BEGINNING);
195                 label.setLayoutData(gd);
196
197                 /* list */
198                 Composite comboComp = new Composite(composite, SWT.NONE);
199                 gd = new GridData(GridData.FILL_HORIZONTAL
200                                 | GridData.VERTICAL_ALIGN_BEGINNING);
201                 GridLayout gridLayout = new GridLayout(2, false);
202                 gridLayout.marginWidth = 0;
203                 comboComp.setLayout(gridLayout);
204
205                 Label comboLabel = new Label(comboComp, SWT.CENTER);
206                 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
207                                 | GridData.VERTICAL_ALIGN_CENTER);
208                 comboLabel.setLayoutData(gd);
209                 comboLabel.setText(PreferencesMessages
210                                 .getString("FoldingConfigurationBlock.combo_caption")); //$NON-NLS-1$
211
212                 label = new Label(composite, SWT.CENTER);
213                 gd = new GridData(GridData.FILL_HORIZONTAL
214                                 | GridData.VERTICAL_ALIGN_BEGINNING);
215                 label.setLayoutData(gd);
216
217                 fProviderCombo = new Combo(comboComp, SWT.READ_ONLY | SWT.DROP_DOWN);
218                 gd = new GridData(GridData.HORIZONTAL_ALIGN_END
219                                 | GridData.VERTICAL_ALIGN_CENTER);
220                 fProviderCombo.setLayoutData(gd);
221
222                 /* list viewer */
223                 fProviderViewer = new ComboViewer(fProviderCombo);
224                 fProviderViewer.setContentProvider(new IStructuredContentProvider() {
225
226                         /*
227                          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
228                          */
229                         public void dispose() {
230                         }
231
232                         /*
233                          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
234                          *      java.lang.Object, java.lang.Object)
235                          */
236                         public void inputChanged(Viewer viewer, Object oldInput,
237                                         Object newInput) {
238                         }
239
240                         /*
241                          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
242                          */
243                         public Object[] getElements(Object inputElement) {
244                                 return fProviderDescriptors.values().toArray();
245                         }
246                 });
247                 fProviderViewer.setLabelProvider(new LabelProvider() {
248                         /*
249                          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
250                          */
251                         public Image getImage(Object element) {
252                                 return null;
253                         }
254
255                         /*
256                          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
257                          */
258                         public String getText(Object element) {
259                                 return ((JavaFoldingStructureProviderDescriptor) element)
260                                                 .getName();
261                         }
262                 });
263                 fProviderViewer
264                                 .addSelectionChangedListener(new ISelectionChangedListener() {
265
266                                         public void selectionChanged(SelectionChangedEvent event) {
267                                                 IStructuredSelection sel = (IStructuredSelection) event
268                                                                 .getSelection();
269                                                 if (!sel.isEmpty()) {
270                                                         fStore
271                                                                         .setValue(
272                                                                                         PreferenceConstants.EDITOR_FOLDING_PROVIDER,
273                                                                                         ((JavaFoldingStructureProviderDescriptor) sel
274                                                                                                         .getFirstElement()).getId());
275                                                         updateListDependencies();
276                                                 }
277                                         }
278                                 });
279                 fProviderViewer.setInput(fProviderDescriptors);
280                 fProviderViewer.refresh();
281
282                 Composite groupComp = new Composite(composite, SWT.NONE);
283                 gd = new GridData(GridData.FILL_BOTH);
284                 gd.horizontalSpan = 2;
285                 groupComp.setLayoutData(gd);
286                 gridLayout = new GridLayout(1, false);
287                 gridLayout.marginWidth = 0;
288                 groupComp.setLayout(gridLayout);
289
290                 /* contributed provider preferences. */
291                 fGroup = new Composite(groupComp, SWT.NONE);
292                 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
293                                 | GridData.VERTICAL_ALIGN_BEGINNING);
294                 fGroup.setLayoutData(gd);
295                 fStackLayout = new StackLayout();
296                 fGroup.setLayout(fStackLayout);
297
298                 return composite;
299         }
300
301         private void updateCheckboxDependencies() {
302         }
303
304         void updateListDependencies() {
305                 String id = fStore
306                                 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
307                 JavaFoldingStructureProviderDescriptor desc = (JavaFoldingStructureProviderDescriptor) fProviderDescriptors
308                                 .get(id);
309                 IJavaFoldingPreferenceBlock prefs;
310
311                 if (desc == null) {
312                         // safety in case there is no such descriptor
313                         String message = PreferencesMessages
314                                         .getString("FoldingConfigurationBlock.error.not_exist"); //$NON-NLS-1$
315                         PHPeclipsePlugin.log(new Status(IStatus.WARNING, PHPeclipsePlugin
316                                         .getPluginId(), IStatus.OK, message, null));
317                         prefs = new ErrorPreferences(message);
318                 } else {
319                         prefs = (IJavaFoldingPreferenceBlock) fProviderPreferences.get(id);
320                         if (prefs == null) {
321                                 try {
322                                         prefs = desc.createPreferences();
323                                         fProviderPreferences.put(id, prefs);
324                                 } catch (CoreException e) {
325                                         PHPeclipsePlugin.log(e);
326                                         prefs = new ErrorPreferences(e.getLocalizedMessage());
327                                 }
328                         }
329                 }
330
331                 Control control = (Control) fProviderControls.get(id);
332                 if (control == null) {
333                         control = prefs.createControl(fGroup);
334                         if (control == null) {
335                                 String message = PreferencesMessages
336                                                 .getString("FoldingConfigurationBlock.info.no_preferences"); //$NON-NLS-1$
337                                 control = new ErrorPreferences(message).createControl(fGroup);
338                         } else {
339                                 fProviderControls.put(id, control);
340                         }
341                 }
342                 fStackLayout.topControl = control;
343                 control.pack();
344                 fGroup.layout();
345                 fGroup.getParent().layout();
346
347                 prefs.initialize();
348         }
349
350         void initialize() {
351                 restoreFromPreferences();
352         }
353
354         void performOk() {
355                 for (Iterator it = fProviderPreferences.values().iterator(); it
356                                 .hasNext();) {
357                         IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
358                                         .next();
359                         prefs.performOk();
360                 }
361         }
362
363         void performDefaults() {
364                 restoreFromPreferences();
365                 for (Iterator it = fProviderPreferences.values().iterator(); it
366                                 .hasNext();) {
367                         IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
368                                         .next();
369                         prefs.performDefaults();
370                 }
371         }
372
373         void dispose() {
374                 for (Iterator it = fProviderPreferences.values().iterator(); it
375                                 .hasNext();) {
376                         IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
377                                         .next();
378                         prefs.dispose();
379                 }
380         }
381
382         private void restoreFromPreferences() {
383                 boolean enabled = fStore
384                                 .getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
385                 fFoldingCheckbox.setSelection(enabled);
386                 updateCheckboxDependencies();
387
388                 String id = fStore
389                                 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
390                 Object provider = fProviderDescriptors.get(id);
391                 if (provider != null) {
392                         fProviderViewer.setSelection(new StructuredSelection(provider),
393                                         true);
394                         updateListDependencies();
395                 }
396         }
397 }