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;
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;
56 * Configures Java Editor folding preferences.
60 class FoldingConfigurationBlock {
62 private static class ErrorPreferences implements
63 IJavaFoldingPreferenceBlock {
64 private String fMessage;
66 protected ErrorPreferences(String message) {
71 * @see net.sourceforge.phpdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
73 public Control createControl(Composite composite) {
74 Composite inner = new Composite(composite, SWT.NONE);
75 inner.setLayout(new FillLayout(SWT.VERTICAL));
77 Label label = new Label(inner, SWT.CENTER);
78 label.setText(fMessage);
83 public void initialize() {
86 public void performOk() {
89 public void performDefaults() {
92 public void dispose() {
97 /** The overlay preference store. */
98 private final OverlayPreferenceStore fStore;
101 private Combo fProviderCombo;
103 private Button fFoldingCheckbox;
105 private ComboViewer fProviderViewer;
107 private Map fProviderDescriptors;
109 private Composite fGroup;
111 private Map fProviderPreferences;
113 private Map fProviderControls;
115 private StackLayout fStackLayout;
117 public FoldingConfigurationBlock(OverlayPreferenceStore store) {
118 Assert.isNotNull(store);
120 fStore.addKeys(createOverlayStoreKeys());
121 fProviderDescriptors = createListModel();
122 fProviderPreferences = new HashMap();
123 fProviderControls = new HashMap();
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]);
139 private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
141 ArrayList overlayKeys = new ArrayList();
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));
150 OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys
152 overlayKeys.toArray(keys);
157 * Creates page for folding preferences.
160 * the parent composite
161 * @return the control for the preference page
163 Control createControl(Composite parent) {
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);
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,
188 updateCheckboxDependencies();
191 public void widgetDefaultSelected(SelectionEvent e) {
195 Label label = new Label(composite, SWT.CENTER);
196 gd = new GridData(GridData.FILL_HORIZONTAL
197 | GridData.VERTICAL_ALIGN_BEGINNING);
198 label.setLayoutData(gd);
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);
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$
215 label = new Label(composite, SWT.CENTER);
216 gd = new GridData(GridData.FILL_HORIZONTAL
217 | GridData.VERTICAL_ALIGN_BEGINNING);
218 label.setLayoutData(gd);
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);
226 fProviderViewer = new ComboViewer(fProviderCombo);
227 fProviderViewer.setContentProvider(new IStructuredContentProvider() {
230 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
232 public void dispose() {
236 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
237 * java.lang.Object, java.lang.Object)
239 public void inputChanged(Viewer viewer, Object oldInput,
244 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
246 public Object[] getElements(Object inputElement) {
247 return fProviderDescriptors.values().toArray();
250 fProviderViewer.setLabelProvider(new LabelProvider() {
252 * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
254 public Image getImage(Object element) {
259 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
261 public String getText(Object element) {
262 return ((JavaFoldingStructureProviderDescriptor) element)
267 .addSelectionChangedListener(new ISelectionChangedListener() {
269 public void selectionChanged(SelectionChangedEvent event) {
270 IStructuredSelection sel = (IStructuredSelection) event
272 if (!sel.isEmpty()) {
275 PreferenceConstants.EDITOR_FOLDING_PROVIDER,
276 ((JavaFoldingStructureProviderDescriptor) sel
277 .getFirstElement()).getId());
278 updateListDependencies();
282 fProviderViewer.setInput(fProviderDescriptors);
283 fProviderViewer.refresh();
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);
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);
304 private void updateCheckboxDependencies() {
307 void updateListDependencies() {
309 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
310 JavaFoldingStructureProviderDescriptor desc = (JavaFoldingStructureProviderDescriptor) fProviderDescriptors
312 IJavaFoldingPreferenceBlock prefs;
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);
322 prefs = (IJavaFoldingPreferenceBlock) fProviderPreferences.get(id);
325 prefs = desc.createPreferences();
326 fProviderPreferences.put(id, prefs);
327 } catch (CoreException e) {
329 prefs = new ErrorPreferences(e.getLocalizedMessage());
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);
342 fProviderControls.put(id, control);
345 fStackLayout.topControl = control;
348 fGroup.getParent().layout();
354 restoreFromPreferences();
358 for (Iterator it = fProviderPreferences.values().iterator(); it
360 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
366 void performDefaults() {
367 restoreFromPreferences();
368 for (Iterator it = fProviderPreferences.values().iterator(); it
370 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
372 prefs.performDefaults();
377 for (Iterator it = fProviderPreferences.values().iterator(); it
379 IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
385 private void restoreFromPreferences() {
386 boolean enabled = fStore
387 .getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
388 fFoldingCheckbox.setSelection(enabled);
389 updateCheckboxDependencies();
392 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
393 Object provider = fProviderDescriptors.get(id);
394 if (provider != null) {
395 fProviderViewer.setSelection(new StructuredSelection(provider),
397 updateListDependencies();