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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.preferences;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Iterator;
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;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
30 //import org.eclipse.jface.text.Assert;
31 import org.eclipse.core.runtime.Assert;
32 import org.eclipse.jface.viewers.ComboViewer;
33 import org.eclipse.jface.viewers.ISelectionChangedListener;
34 import org.eclipse.jface.viewers.IStructuredContentProvider;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.LabelProvider;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.custom.StackLayout;
42 import org.eclipse.swt.events.SelectionEvent;
43 import org.eclipse.swt.events.SelectionListener;
44 import org.eclipse.swt.graphics.Image;
45 import org.eclipse.swt.layout.FillLayout;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Button;
49 import org.eclipse.swt.widgets.Combo;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Control;
52 import org.eclipse.swt.widgets.Label;
55 * Configures Java Editor folding preferences.
59 class FoldingConfigurationBlock {
61 private static class ErrorPreferences implements
62 IJavaFoldingPreferenceBlock {
63 private String fMessage;
65 protected ErrorPreferences(String message) {
70 * @see net.sourceforge.phpdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
72 public Control createControl(Composite composite) {
73 Composite inner = new Composite(composite, SWT.NONE);
74 inner.setLayout(new FillLayout(SWT.VERTICAL));
76 Label label = new Label(inner, SWT.CENTER);
77 label.setText(fMessage);
82 public void initialize() {
85 public void performOk() {
88 public void performDefaults() {
91 public void dispose() {
96 /** The overlay preference store. */
97 private final OverlayPreferenceStore fStore;
100 private Combo fProviderCombo;
102 private Button fFoldingCheckbox;
104 private ComboViewer fProviderViewer;
106 private Map fProviderDescriptors;
108 private Composite fGroup;
110 private Map fProviderPreferences;
112 private Map fProviderControls;
114 private StackLayout fStackLayout;
116 public FoldingConfigurationBlock(OverlayPreferenceStore store) {
117 Assert.isNotNull(store);
119 fStore.addKeys(createOverlayStoreKeys());
120 fProviderDescriptors = createListModel();
121 fProviderPreferences = new HashMap();
122 fProviderControls = new HashMap();
125 private Map createListModel() {
126 JavaFoldingStructureProviderRegistry reg = PHPeclipsePlugin
127 .getDefault().getFoldingStructureProviderRegistry();
128 reg.reloadExtensions();
129 JavaFoldingStructureProviderDescriptor[] descs = reg
130 .getFoldingProviderDescriptors();
131 Map map = new HashMap();
132 for (int i = 0; i < descs.length; i++) {
133 map.put(descs[i].getId(), descs[i]);
138 private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
140 ArrayList overlayKeys = new ArrayList();
142 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
143 OverlayPreferenceStore.BOOLEAN,
144 PreferenceConstants.EDITOR_FOLDING_ENABLED));
145 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
146 OverlayPreferenceStore.STRING,
147 PreferenceConstants.EDITOR_FOLDING_PROVIDER));
149 OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys
151 overlayKeys.toArray(keys);
156 * Creates page for folding preferences.
159 * the parent composite
160 * @return the control for the preference page
162 Control createControl(Composite parent) {
164 Composite composite = new Composite(parent, SWT.NULL);
165 // assume parent page uses griddata
166 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER
167 | GridData.VERTICAL_ALIGN_FILL);
168 composite.setLayoutData(gd);
169 GridLayout layout = new GridLayout();
170 layout.numColumns = 2;
171 PixelConverter pc = new PixelConverter(composite);
172 layout.verticalSpacing = pc.convertHeightInCharsToPixels(1) / 2;
173 composite.setLayout(layout);
175 /* check box for new editors */
176 fFoldingCheckbox = new Button(composite, SWT.CHECK);
177 fFoldingCheckbox.setText(PreferencesMessages
178 .getString("FoldingConfigurationBlock.enable")); //$NON-NLS-1$
179 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
180 | GridData.VERTICAL_ALIGN_BEGINNING);
181 fFoldingCheckbox.setLayoutData(gd);
182 fFoldingCheckbox.addSelectionListener(new SelectionListener() {
183 public void widgetSelected(SelectionEvent e) {
184 boolean enabled = fFoldingCheckbox.getSelection();
185 fStore.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED,
187 updateCheckboxDependencies();
190 public void widgetDefaultSelected(SelectionEvent e) {
194 Label label = new Label(composite, SWT.CENTER);
195 gd = new GridData(GridData.FILL_HORIZONTAL
196 | GridData.VERTICAL_ALIGN_BEGINNING);
197 label.setLayoutData(gd);
200 Composite comboComp = new Composite(composite, SWT.NONE);
201 gd = new GridData(GridData.FILL_HORIZONTAL
202 | GridData.VERTICAL_ALIGN_BEGINNING);
203 GridLayout gridLayout = new GridLayout(2, false);
204 gridLayout.marginWidth = 0;
205 comboComp.setLayout(gridLayout);
207 Label comboLabel = new Label(comboComp, SWT.CENTER);
208 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
209 | GridData.VERTICAL_ALIGN_CENTER);
210 comboLabel.setLayoutData(gd);
211 comboLabel.setText(PreferencesMessages
212 .getString("FoldingConfigurationBlock.combo_caption")); //$NON-NLS-1$
214 label = new Label(composite, SWT.CENTER);
215 gd = new GridData(GridData.FILL_HORIZONTAL
216 | GridData.VERTICAL_ALIGN_BEGINNING);
217 label.setLayoutData(gd);
219 fProviderCombo = new Combo(comboComp, SWT.READ_ONLY | SWT.DROP_DOWN);
220 gd = new GridData(GridData.HORIZONTAL_ALIGN_END
221 | GridData.VERTICAL_ALIGN_CENTER);
222 fProviderCombo.setLayoutData(gd);
225 fProviderViewer = new ComboViewer(fProviderCombo);
226 fProviderViewer.setContentProvider(new IStructuredContentProvider() {
229 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
231 public void dispose() {
235 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
236 * java.lang.Object, java.lang.Object)
238 public void inputChanged(Viewer viewer, Object oldInput,
243 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
245 public Object[] getElements(Object inputElement) {
246 return fProviderDescriptors.values().toArray();
249 fProviderViewer.setLabelProvider(new LabelProvider() {
251 * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
253 public Image getImage(Object element) {
258 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
260 public String getText(Object element) {
261 return ((JavaFoldingStructureProviderDescriptor) element)
266 .addSelectionChangedListener(new ISelectionChangedListener() {
268 public void selectionChanged(SelectionChangedEvent event) {
269 IStructuredSelection sel = (IStructuredSelection) event
271 if (!sel.isEmpty()) {
274 PreferenceConstants.EDITOR_FOLDING_PROVIDER,
275 ((JavaFoldingStructureProviderDescriptor) sel
276 .getFirstElement()).getId());
277 updateListDependencies();
281 fProviderViewer.setInput(fProviderDescriptors);
282 fProviderViewer.refresh();
284 Composite groupComp = new Composite(composite, SWT.NONE);
285 gd = new GridData(GridData.FILL_BOTH);
286 gd.horizontalSpan = 2;
287 groupComp.setLayoutData(gd);
288 gridLayout = new GridLayout(1, false);
289 gridLayout.marginWidth = 0;
290 groupComp.setLayout(gridLayout);
292 /* contributed provider preferences. */
293 fGroup = new Composite(groupComp, SWT.NONE);
294 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
295 | GridData.VERTICAL_ALIGN_BEGINNING);
296 fGroup.setLayoutData(gd);
297 fStackLayout = new StackLayout();
298 fGroup.setLayout(fStackLayout);
303 private void updateCheckboxDependencies() {
306 void updateListDependencies() {
308 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
309 JavaFoldingStructureProviderDescriptor desc = (JavaFoldingStructureProviderDescriptor) fProviderDescriptors
311 IJavaFoldingPreferenceBlock prefs;
314 // safety in case there is no such descriptor
315 String message = PreferencesMessages
316 .getString("FoldingConfigurationBlock.error.not_exist"); //$NON-NLS-1$
317 PHPeclipsePlugin.log(new Status(IStatus.WARNING, PHPeclipsePlugin
318 .getPluginId(), IStatus.OK, message, null));
319 prefs = new ErrorPreferences(message);
321 prefs = (IJavaFoldingPreferenceBlock) fProviderPreferences.get(id);
324 prefs = desc.createPreferences();
325 fProviderPreferences.put(id, prefs);
326 } catch (CoreException e) {
327 PHPeclipsePlugin.log(e);
328 prefs = new ErrorPreferences(e.getLocalizedMessage());
333 Control control = (Control) fProviderControls.get(id);
334 if (control == null) {
335 control = prefs.createControl(fGroup);
336 if (control == null) {
337 String message = PreferencesMessages
338 .getString("FoldingConfigurationBlock.info.no_preferences"); //$NON-NLS-1$
339 control = new ErrorPreferences(message).createControl(fGroup);
341 fProviderControls.put(id, control);
344 fStackLayout.topControl = control;
347 fGroup.getParent().layout();
353 restoreFromPreferences();
357 for (Iterator it = fProviderPreferences.values().iterator(); it
359 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
365 void performDefaults() {
366 restoreFromPreferences();
367 for (Iterator it = fProviderPreferences.values().iterator(); it
369 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
371 prefs.performDefaults();
376 for (Iterator it = fProviderPreferences.values().iterator(); it
378 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
384 private void restoreFromPreferences() {
385 boolean enabled = fStore
386 .getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
387 fFoldingCheckbox.setSelection(enabled);
388 updateCheckboxDependencies();
391 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
392 Object provider = fProviderDescriptors.get(id);
393 if (provider != null) {
394 fProviderViewer.setSelection(new StructuredSelection(provider),
396 updateListDependencies();