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