KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > FoldingConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.preferences;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.custom.StackLayout;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.events.SelectionListener;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.FillLayout;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Combo;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Label;
37
38 import org.eclipse.jface.dialogs.Dialog;
39 import org.eclipse.jface.viewers.ComboViewer;
40 import org.eclipse.jface.viewers.ISelectionChangedListener;
41 import org.eclipse.jface.viewers.IStructuredContentProvider;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.jface.viewers.LabelProvider;
44 import org.eclipse.jface.viewers.SelectionChangedEvent;
45 import org.eclipse.jface.viewers.StructuredSelection;
46 import org.eclipse.jface.viewers.Viewer;
47
48
49 import org.eclipse.jdt.internal.corext.util.Messages;
50
51 import org.eclipse.jdt.ui.PreferenceConstants;
52 import org.eclipse.jdt.ui.text.folding.IJavaFoldingPreferenceBlock;
53
54 import org.eclipse.jdt.internal.ui.JavaPlugin;
55 import org.eclipse.jdt.internal.ui.text.folding.JavaFoldingStructureProviderDescriptor;
56 import org.eclipse.jdt.internal.ui.text.folding.JavaFoldingStructureProviderRegistry;
57 import org.eclipse.jdt.internal.ui.util.PixelConverter;
58
59 /**
60  * Configures Java Editor folding preferences.
61  *
62  * @since 3.0
63  */

64 class FoldingConfigurationBlock implements IPreferenceConfigurationBlock {
65     
66     private static class ErrorPreferences implements IJavaFoldingPreferenceBlock {
67         private String JavaDoc fMessage;
68         
69         protected ErrorPreferences(String JavaDoc message) {
70             fMessage= message;
71         }
72         
73         /*
74          * @see org.eclipse.jdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
75          */

76         public Control createControl(Composite composite) {
77             Composite inner= new Composite(composite, SWT.NONE);
78             inner.setLayout(new FillLayout(SWT.VERTICAL));
79
80             Label label= new Label(inner, SWT.CENTER);
81             label.setText(fMessage);
82             
83             return inner;
84         }
85
86         public void initialize() {
87         }
88
89         public void performOk() {
90         }
91
92         public void performDefaults() {
93         }
94
95         public void dispose() {
96         }
97         
98     }
99
100     /** The overlay preference store. */
101     private final OverlayPreferenceStore fStore;
102     
103     /* The controls */
104     private Combo fProviderCombo;
105     private Button fFoldingCheckbox;
106     private ComboViewer fProviderViewer;
107     private Composite fGroup;
108     private StackLayout fStackLayout;
109     
110     /* the model */
111     private final Map JavaDoc fProviderDescriptors;
112     private final Map JavaDoc fProviderPreferences;
113     private final Map JavaDoc fProviderControls;
114     
115
116     public FoldingConfigurationBlock(OverlayPreferenceStore store) {
117         Assert.isNotNull(store);
118         fStore= store;
119         fStore.addKeys(createOverlayStoreKeys());
120         fProviderDescriptors= createListModel();
121         fProviderPreferences= new HashMap JavaDoc();
122         fProviderControls= new HashMap JavaDoc();
123     }
124
125     private Map JavaDoc createListModel() {
126         JavaFoldingStructureProviderRegistry reg= JavaPlugin.getDefault().getFoldingStructureProviderRegistry();
127         reg.reloadExtensions();
128         JavaFoldingStructureProviderDescriptor[] descs= reg.getFoldingProviderDescriptors();
129         Map JavaDoc map= new HashMap JavaDoc();
130         for (int i= 0; i < descs.length; i++) {
131             map.put(descs[i].getId(), descs[i]);
132         }
133         return map;
134     }
135
136     private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
137         
138         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
139
140         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_ENABLED));
141         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.EDITOR_FOLDING_PROVIDER));
142         
143         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
144         overlayKeys.toArray(keys);
145         return keys;
146     }
147
148     /**
149      * Creates page for folding preferences.
150      *
151      * @param parent the parent composite
152      * @return the control for the preference page
153      */

154     public Control createControl(Composite parent) {
155
156         Composite composite= new Composite(parent, SWT.NULL);
157         // assume parent page uses griddata
158
GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_CENTER | GridData.VERTICAL_ALIGN_FILL);
159         composite.setLayoutData(gd);
160         GridLayout layout= new GridLayout();
161         layout.numColumns= 2;
162         layout.marginHeight= 0;
163         layout.marginWidth= 0;
164         PixelConverter pc= new PixelConverter(composite);
165         layout.verticalSpacing= pc.convertHeightInCharsToPixels(1) / 2;
166         composite.setLayout(layout);
167         
168         
169         /* check box for new editors */
170         fFoldingCheckbox= new Button(composite, SWT.CHECK);
171         fFoldingCheckbox.setText(PreferencesMessages.FoldingConfigurationBlock_enable);
172         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
173         fFoldingCheckbox.setLayoutData(gd);
174         fFoldingCheckbox.addSelectionListener(new SelectionListener() {
175             public void widgetSelected(SelectionEvent e) {
176                 boolean enabled= fFoldingCheckbox.getSelection();
177                 fStore.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED, enabled);
178                 updateCheckboxDependencies();
179             }
180
181             public void widgetDefaultSelected(SelectionEvent e) {
182             }
183         });
184         
185         Label label= new Label(composite, SWT.CENTER);
186         gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
187         label.setLayoutData(gd);
188
189         if (fProviderDescriptors.size() > 1) {
190             /* list */
191             Composite comboComp= new Composite(composite, SWT.NONE);
192             gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
193             GridLayout gridLayout= new GridLayout(2, false);
194             gridLayout.marginWidth= 0;
195             comboComp.setLayout(gridLayout);
196         
197             Label comboLabel= new Label(comboComp, SWT.CENTER);
198             gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_CENTER);
199             comboLabel.setLayoutData(gd);
200             comboLabel.setText(PreferencesMessages.FoldingConfigurationBlock_combo_caption);
201             
202             label= new Label(composite, SWT.CENTER);
203             gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
204             label.setLayoutData(gd);
205             
206             fProviderCombo= new Combo(comboComp, SWT.READ_ONLY | SWT.DROP_DOWN);
207             gd= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_CENTER);
208             fProviderCombo.setLayoutData(gd);
209
210             fProviderViewer= createProviderViewer();
211         }
212         
213         Composite groupComp= new Composite(composite, SWT.NONE);
214         gd= new GridData(GridData.FILL_BOTH);
215         gd.horizontalSpan= 2;
216         groupComp.setLayoutData(gd);
217         GridLayout gridLayout= new GridLayout(1, false);
218         gridLayout.marginWidth= 0;
219         groupComp.setLayout(gridLayout);
220         
221         /* contributed provider preferences. */
222         fGroup= new Composite(groupComp, SWT.NONE);
223         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
224         fGroup.setLayoutData(gd);
225         fStackLayout= new StackLayout();
226         fGroup.setLayout(fStackLayout);
227         
228         return composite;
229     }
230
231     private ComboViewer createProviderViewer() {
232         /* list viewer */
233         ComboViewer viewer= new ComboViewer(fProviderCombo);
234         viewer.setContentProvider(new IStructuredContentProvider() {
235
236             /*
237              * @see org.eclipse.jface.viewers.IContentProvider#dispose()
238              */

239             public void dispose() {
240             }
241
242             /*
243              * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
244              */

245             public void inputChanged(Viewer v, Object JavaDoc oldInput, Object JavaDoc newInput) {
246             }
247
248             /*
249              * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
250              */

251             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
252                 return fProviderDescriptors.values().toArray();
253             }
254         });
255         viewer.setLabelProvider(new LabelProvider() {
256             /*
257              * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
258              */

259             public Image getImage(Object JavaDoc element) {
260                 return null;
261             }
262             
263             /*
264              * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
265              */

266             public String JavaDoc getText(Object JavaDoc element) {
267                 return ((JavaFoldingStructureProviderDescriptor) element).getName();
268             }
269         });
270         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
271
272             public void selectionChanged(SelectionChangedEvent event) {
273                 IStructuredSelection sel= (IStructuredSelection) event.getSelection();
274                 if (!sel.isEmpty()) {
275                     fStore.setValue(PreferenceConstants.EDITOR_FOLDING_PROVIDER, ((JavaFoldingStructureProviderDescriptor) sel.getFirstElement()).getId());
276                     updateListDependencies();
277                 }
278             }
279         });
280         viewer.setInput(fProviderDescriptors);
281         viewer.refresh();
282         
283         return viewer;
284     }
285
286     private void updateCheckboxDependencies() {
287     }
288
289     void updateListDependencies() {
290         String JavaDoc id= fStore.getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
291         JavaFoldingStructureProviderDescriptor desc= (JavaFoldingStructureProviderDescriptor) fProviderDescriptors.get(id);
292         IJavaFoldingPreferenceBlock prefs;
293         
294         if (desc == null) {
295             // safety in case there is no such descriptor
296
String JavaDoc message= Messages.format(PreferencesMessages.FoldingConfigurationBlock_error_not_exist, id);
297             JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, null));
298             prefs= new ErrorPreferences(message);
299         } else {
300             prefs= (IJavaFoldingPreferenceBlock) fProviderPreferences.get(id);
301             if (prefs == null) {
302                 try {
303                     prefs= desc.createPreferences();
304                     fProviderPreferences.put(id, prefs);
305                 } catch (CoreException e) {
306                     JavaPlugin.log(e);
307                     prefs= new ErrorPreferences(e.getLocalizedMessage());
308                 }
309             }
310         }
311         
312         Control control= (Control) fProviderControls.get(id);
313         if (control == null) {
314             control= prefs.createControl(fGroup);
315             if (control == null) {
316                 String JavaDoc message= PreferencesMessages.FoldingConfigurationBlock_info_no_preferences;
317                 control= new ErrorPreferences(message).createControl(fGroup);
318             } else {
319                 fProviderControls.put(id, control);
320             }
321         }
322         Dialog.applyDialogFont(control);
323         fStackLayout.topControl= control;
324         control.pack();
325         fGroup.layout();
326         fGroup.getParent().layout();
327         
328         prefs.initialize();
329     }
330
331     public void initialize() {
332         restoreFromPreferences();
333     }
334
335     public void performOk() {
336         for (Iterator JavaDoc it= fProviderPreferences.values().iterator(); it.hasNext();) {
337             IJavaFoldingPreferenceBlock prefs= (IJavaFoldingPreferenceBlock) it.next();
338             prefs.performOk();
339         }
340     }
341     
342     public void performDefaults() {
343         restoreFromPreferences();
344         for (Iterator JavaDoc it= fProviderPreferences.values().iterator(); it.hasNext();) {
345             IJavaFoldingPreferenceBlock prefs= (IJavaFoldingPreferenceBlock) it.next();
346             prefs.performDefaults();
347         }
348     }
349     
350     public void dispose() {
351         for (Iterator JavaDoc it= fProviderPreferences.values().iterator(); it.hasNext();) {
352             IJavaFoldingPreferenceBlock prefs= (IJavaFoldingPreferenceBlock) it.next();
353             prefs.dispose();
354         }
355     }
356
357     private void restoreFromPreferences() {
358         boolean enabled= fStore.getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
359         fFoldingCheckbox.setSelection(enabled);
360         updateCheckboxDependencies();
361         
362         String JavaDoc id= fStore.getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
363         Object JavaDoc provider= fProviderDescriptors.get(id);
364         
365         // Fallback to default
366
if (provider == null) {
367             String JavaDoc message= Messages.format(PreferencesMessages.FoldingConfigurationBlock_warning_providerNotFound_resetToDefault, id);
368             JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, null));
369             
370             id= JavaPlugin.getDefault().getPreferenceStore().getDefaultString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
371             
372             provider= fProviderDescriptors.get(id);
373             Assert.isNotNull(provider);
374             
375             fStore.setToDefault(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
376         }
377         
378         if (fProviderViewer == null)
379             updateListDependencies();
380         else
381             fProviderViewer.setSelection(new StructuredSelection(provider), true);
382     }
383 }
384
Popular Tags