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