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;
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;
53 * Configures Java Editor folding preferences.
57 class FoldingConfigurationBlock {
59 private static class ErrorPreferences implements
60 IJavaFoldingPreferenceBlock {
61 private String fMessage;
63 protected ErrorPreferences(String message) {
68 * @see net.sourceforge.phpdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
70 public Control createControl(Composite composite) {
71 Composite inner = new Composite(composite, SWT.NONE);
72 inner.setLayout(new FillLayout(SWT.VERTICAL));
74 Label label = new Label(inner, SWT.CENTER);
75 label.setText(fMessage);
80 public void initialize() {
83 public void performOk() {
86 public void performDefaults() {
89 public void dispose() {
94 /** The overlay preference store. */
95 private final OverlayPreferenceStore fStore;
98 private Combo fProviderCombo;
100 private Button fFoldingCheckbox;
102 private ComboViewer fProviderViewer;
104 private Map fProviderDescriptors;
106 private Composite fGroup;
108 private Map fProviderPreferences;
110 private Map fProviderControls;
112 private StackLayout fStackLayout;
114 public FoldingConfigurationBlock(OverlayPreferenceStore store) {
115 Assert.isNotNull(store);
117 fStore.addKeys(createOverlayStoreKeys());
118 fProviderDescriptors = createListModel();
119 fProviderPreferences = new HashMap();
120 fProviderControls = new HashMap();
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]);
136 private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
138 ArrayList overlayKeys = new ArrayList();
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));
147 OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys
149 overlayKeys.toArray(keys);
154 * Creates page for folding preferences.
157 * the parent composite
158 * @return the control for the preference page
160 Control createControl(Composite parent) {
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);
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,
185 updateCheckboxDependencies();
188 public void widgetDefaultSelected(SelectionEvent e) {
192 Label label = new Label(composite, SWT.CENTER);
193 gd = new GridData(GridData.FILL_HORIZONTAL
194 | GridData.VERTICAL_ALIGN_BEGINNING);
195 label.setLayoutData(gd);
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);
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$
212 label = new Label(composite, SWT.CENTER);
213 gd = new GridData(GridData.FILL_HORIZONTAL
214 | GridData.VERTICAL_ALIGN_BEGINNING);
215 label.setLayoutData(gd);
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);
223 fProviderViewer = new ComboViewer(fProviderCombo);
224 fProviderViewer.setContentProvider(new IStructuredContentProvider() {
227 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
229 public void dispose() {
233 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
234 * java.lang.Object, java.lang.Object)
236 public void inputChanged(Viewer viewer, Object oldInput,
241 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
243 public Object[] getElements(Object inputElement) {
244 return fProviderDescriptors.values().toArray();
247 fProviderViewer.setLabelProvider(new LabelProvider() {
249 * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
251 public Image getImage(Object element) {
256 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
258 public String getText(Object element) {
259 return ((JavaFoldingStructureProviderDescriptor) element)
264 .addSelectionChangedListener(new ISelectionChangedListener() {
266 public void selectionChanged(SelectionChangedEvent event) {
267 IStructuredSelection sel = (IStructuredSelection) event
269 if (!sel.isEmpty()) {
272 PreferenceConstants.EDITOR_FOLDING_PROVIDER,
273 ((JavaFoldingStructureProviderDescriptor) sel
274 .getFirstElement()).getId());
275 updateListDependencies();
279 fProviderViewer.setInput(fProviderDescriptors);
280 fProviderViewer.refresh();
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);
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);
301 private void updateCheckboxDependencies() {
304 void updateListDependencies() {
306 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
307 JavaFoldingStructureProviderDescriptor desc = (JavaFoldingStructureProviderDescriptor) fProviderDescriptors
309 IJavaFoldingPreferenceBlock prefs;
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);
319 prefs = (IJavaFoldingPreferenceBlock) fProviderPreferences.get(id);
322 prefs = desc.createPreferences();
323 fProviderPreferences.put(id, prefs);
324 } catch (CoreException e) {
325 PHPeclipsePlugin.log(e);
326 prefs = new ErrorPreferences(e.getLocalizedMessage());
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);
339 fProviderControls.put(id, control);
342 fStackLayout.topControl = control;
345 fGroup.getParent().layout();
351 restoreFromPreferences();
355 for (Iterator it = fProviderPreferences.values().iterator(); it
357 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
363 void performDefaults() {
364 restoreFromPreferences();
365 for (Iterator it = fProviderPreferences.values().iterator(); it
367 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
369 prefs.performDefaults();
374 for (Iterator it = fProviderPreferences.values().iterator(); it
376 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
382 private void restoreFromPreferences() {
383 boolean enabled = fStore
384 .getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
385 fFoldingCheckbox.setSelection(enabled);
386 updateCheckboxDependencies();
389 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
390 Object provider = fProviderDescriptors.get(id);
391 if (provider != null) {
392 fProviderViewer.setSelection(new StructuredSelection(provider),
394 updateListDependencies();