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;
25 import net.sourceforge.phpeclipse.ui.WebUI;
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;
54 * Configures Java Editor folding preferences.
58 class FoldingConfigurationBlock {
60 private static class ErrorPreferences implements
61 IJavaFoldingPreferenceBlock {
62 private String fMessage;
64 protected ErrorPreferences(String message) {
69 * @see net.sourceforge.phpdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
71 public Control createControl(Composite composite) {
72 Composite inner = new Composite(composite, SWT.NONE);
73 inner.setLayout(new FillLayout(SWT.VERTICAL));
75 Label label = new Label(inner, SWT.CENTER);
76 label.setText(fMessage);
81 public void initialize() {
84 public void performOk() {
87 public void performDefaults() {
90 public void dispose() {
95 /** The overlay preference store. */
96 private final OverlayPreferenceStore fStore;
99 private Combo fProviderCombo;
101 private Button fFoldingCheckbox;
103 private ComboViewer fProviderViewer;
105 private Map fProviderDescriptors;
107 private Composite fGroup;
109 private Map fProviderPreferences;
111 private Map fProviderControls;
113 private StackLayout fStackLayout;
115 public FoldingConfigurationBlock(OverlayPreferenceStore store) {
116 Assert.isNotNull(store);
118 fStore.addKeys(createOverlayStoreKeys());
119 fProviderDescriptors = createListModel();
120 fProviderPreferences = new HashMap();
121 fProviderControls = new HashMap();
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]);
137 private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
139 ArrayList overlayKeys = new ArrayList();
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));
148 OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys
150 overlayKeys.toArray(keys);
155 * Creates page for folding preferences.
158 * the parent composite
159 * @return the control for the preference page
161 Control createControl(Composite parent) {
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);
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,
186 updateCheckboxDependencies();
189 public void widgetDefaultSelected(SelectionEvent e) {
193 Label label = new Label(composite, SWT.CENTER);
194 gd = new GridData(GridData.FILL_HORIZONTAL
195 | GridData.VERTICAL_ALIGN_BEGINNING);
196 label.setLayoutData(gd);
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);
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$
213 label = new Label(composite, SWT.CENTER);
214 gd = new GridData(GridData.FILL_HORIZONTAL
215 | GridData.VERTICAL_ALIGN_BEGINNING);
216 label.setLayoutData(gd);
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);
224 fProviderViewer = new ComboViewer(fProviderCombo);
225 fProviderViewer.setContentProvider(new IStructuredContentProvider() {
228 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
230 public void dispose() {
234 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
235 * java.lang.Object, java.lang.Object)
237 public void inputChanged(Viewer viewer, Object oldInput,
242 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
244 public Object[] getElements(Object inputElement) {
245 return fProviderDescriptors.values().toArray();
248 fProviderViewer.setLabelProvider(new LabelProvider() {
250 * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
252 public Image getImage(Object element) {
257 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
259 public String getText(Object element) {
260 return ((JavaFoldingStructureProviderDescriptor) element)
265 .addSelectionChangedListener(new ISelectionChangedListener() {
267 public void selectionChanged(SelectionChangedEvent event) {
268 IStructuredSelection sel = (IStructuredSelection) event
270 if (!sel.isEmpty()) {
273 PreferenceConstants.EDITOR_FOLDING_PROVIDER,
274 ((JavaFoldingStructureProviderDescriptor) sel
275 .getFirstElement()).getId());
276 updateListDependencies();
280 fProviderViewer.setInput(fProviderDescriptors);
281 fProviderViewer.refresh();
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);
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);
302 private void updateCheckboxDependencies() {
305 void updateListDependencies() {
307 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
308 JavaFoldingStructureProviderDescriptor desc = (JavaFoldingStructureProviderDescriptor) fProviderDescriptors
310 IJavaFoldingPreferenceBlock prefs;
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);
320 prefs = (IJavaFoldingPreferenceBlock) fProviderPreferences.get(id);
323 prefs = desc.createPreferences();
324 fProviderPreferences.put(id, prefs);
325 } catch (CoreException e) {
327 prefs = new ErrorPreferences(e.getLocalizedMessage());
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);
340 fProviderControls.put(id, control);
343 fStackLayout.topControl = control;
346 fGroup.getParent().layout();
352 restoreFromPreferences();
356 for (Iterator it = fProviderPreferences.values().iterator(); it
358 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
364 void performDefaults() {
365 restoreFromPreferences();
366 for (Iterator it = fProviderPreferences.values().iterator(); it
368 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
370 prefs.performDefaults();
375 for (Iterator it = fProviderPreferences.values().iterator(); it
377 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
383 private void restoreFromPreferences() {
384 boolean enabled = fStore
385 .getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
386 fFoldingCheckbox.setSelection(enabled);
387 updateCheckboxDependencies();
390 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
391 Object provider = fProviderDescriptors.get(id);
392 if (provider != null) {
393 fProviderViewer.setSelection(new StructuredSelection(provider),
395 updateListDependencies();