KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > DecoratorsPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.ui.internal.dialogs;
12
13 import com.ibm.icu.text.Collator;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Comparator JavaDoc;
17
18 import org.eclipse.jface.preference.IPreferencePage;
19 import org.eclipse.jface.preference.PreferencePage;
20 import org.eclipse.jface.viewers.CheckStateChangedEvent;
21 import org.eclipse.jface.viewers.CheckboxTableViewer;
22 import org.eclipse.jface.viewers.ICheckStateListener;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.IStructuredContentProvider;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.LabelProvider;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.jface.viewers.Viewer;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.graphics.Font;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Text;
38 import org.eclipse.ui.IWorkbench;
39 import org.eclipse.ui.IWorkbenchPreferencePage;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
42 import org.eclipse.ui.internal.WorkbenchMessages;
43 import org.eclipse.ui.internal.WorkbenchPlugin;
44 import org.eclipse.ui.internal.decorators.DecoratorDefinition;
45 import org.eclipse.ui.internal.decorators.DecoratorManager;
46
47 /**
48  * The DecoratorsPreferencePage is the preference page for enabling and disabling
49  * the decorators in the image and for giving the user a description of the decorator.
50  */

51 public class DecoratorsPreferencePage extends PreferencePage implements
52         IWorkbenchPreferencePage {
53
54     private Text descriptionText;
55
56     private CheckboxTableViewer checkboxViewer;
57
58     /**
59      * @see PreferencePage#createContents(Composite)
60      */

61     protected Control createContents(Composite parent) {
62
63         Font font = parent.getFont();
64
65         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
66                 IWorkbenchHelpContextIds.DECORATORS_PREFERENCE_PAGE);
67
68         Composite mainComposite = new Composite(parent, SWT.NONE);
69         mainComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
70         mainComposite.setFont(font);
71
72         GridLayout layout = new GridLayout();
73         layout.marginWidth = 0;
74         layout.marginHeight = 0;
75         layout.verticalSpacing = 10;
76         mainComposite.setLayout(layout);
77
78         Label topLabel = new Label(mainComposite, SWT.NONE);
79         topLabel.setText(WorkbenchMessages.DecoratorsPreferencePage_explanation);
80         topLabel.setFont(font);
81
82         createDecoratorsArea(mainComposite);
83         createDescriptionArea(mainComposite);
84         populateDecorators();
85
86         return mainComposite;
87     }
88
89     /**
90      * Creates the widgets for the list of decorators.
91      */

92     private void createDecoratorsArea(Composite mainComposite) {
93
94         Font mainFont = mainComposite.getFont();
95         Composite decoratorsComposite = new Composite(mainComposite, SWT.NONE);
96         decoratorsComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
97         GridLayout decoratorsLayout = new GridLayout();
98         decoratorsLayout.marginWidth = 0;
99         decoratorsLayout.marginHeight = 0;
100         decoratorsComposite.setLayout(decoratorsLayout);
101         decoratorsComposite.setFont(mainFont);
102
103         Label decoratorsLabel = new Label(decoratorsComposite, SWT.NONE);
104         decoratorsLabel.setText(WorkbenchMessages.DecoratorsPreferencePage_decoratorsLabel);
105         decoratorsLabel.setFont(mainFont);
106
107         // Checkbox table viewer of decorators
108
checkboxViewer = CheckboxTableViewer.newCheckList(decoratorsComposite,
109                 SWT.SINGLE | SWT.TOP | SWT.BORDER);
110         checkboxViewer.getTable().setLayoutData(
111                 new GridData(GridData.FILL_BOTH));
112         checkboxViewer.getTable().setFont(decoratorsComposite.getFont());
113         checkboxViewer.setLabelProvider(new LabelProvider() {
114             public String JavaDoc getText(Object JavaDoc element) {
115                 return ((DecoratorDefinition) element).getName();
116             }
117         });
118         checkboxViewer.getTable().setFont(mainFont);
119
120         checkboxViewer.setContentProvider(new IStructuredContentProvider() {
121             private final Comparator JavaDoc comparer = new Comparator JavaDoc() {
122                 private Collator collator = Collator.getInstance();
123
124                 public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
125                     String JavaDoc s1 = ((DecoratorDefinition) arg0).getName();
126                     String JavaDoc s2 = ((DecoratorDefinition) arg1).getName();
127                     return collator.compare(s1, s2);
128                 }
129             };
130
131             public void dispose() {
132                 //Nothing to do on dispose
133
}
134
135             public void inputChanged(Viewer viewer, Object JavaDoc oldInput,
136                     Object JavaDoc newInput) {
137             }
138
139             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
140                 //Make an entry for each decorator definition
141
Object JavaDoc[] elements = (Object JavaDoc[]) inputElement;
142                 Object JavaDoc[] results = new Object JavaDoc[elements.length];
143                 System.arraycopy(elements, 0, results, 0, elements.length);
144                 Collections.sort(Arrays.asList(results), comparer);
145                 return results;
146             }
147
148         });
149
150         checkboxViewer
151                 .addSelectionChangedListener(new ISelectionChangedListener() {
152                     public void selectionChanged(SelectionChangedEvent event) {
153                         if (event.getSelection() instanceof IStructuredSelection) {
154                             IStructuredSelection sel = (IStructuredSelection) event
155                                     .getSelection();
156                             DecoratorDefinition definition = (DecoratorDefinition) sel
157                                     .getFirstElement();
158                             if (definition == null) {
159                                 clearDescription();
160                             } else {
161                                 showDescription(definition);
162                             }
163                         }
164                     }
165                 });
166
167         checkboxViewer.addCheckStateListener(new ICheckStateListener() {
168             public void checkStateChanged(CheckStateChangedEvent event) {
169                 checkboxViewer.setSelection(new StructuredSelection(event
170                         .getElement()));
171             }
172         });
173     }
174
175     /**
176      * Creates the widgets for the description.
177      */

178     private void createDescriptionArea(Composite mainComposite) {
179
180         Font mainFont = mainComposite.getFont();
181         Composite textComposite = new Composite(mainComposite, SWT.NONE);
182         textComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
183         GridLayout textLayout = new GridLayout();
184         textLayout.marginWidth = 0;
185         textLayout.marginHeight = 0;
186         textComposite.setLayout(textLayout);
187         textComposite.setFont(mainFont);
188
189         Label descriptionLabel = new Label(textComposite, SWT.NONE);
190         descriptionLabel.setText(WorkbenchMessages.DecoratorsPreferencePage_description);
191         descriptionLabel.setFont(mainFont);
192
193         descriptionText = new Text(textComposite, SWT.MULTI | SWT.WRAP
194                 | SWT.READ_ONLY | SWT.BORDER | SWT.H_SCROLL);
195         descriptionText.setLayoutData(new GridData(GridData.FILL_BOTH));
196         descriptionText.setFont(mainFont);
197     }
198
199     /**
200      * Populates the list of decorators.
201      */

202     private void populateDecorators() {
203         DecoratorDefinition[] definitions = getAllDefinitions();
204         checkboxViewer.setInput(definitions);
205         for (int i = 0; i < definitions.length; i++) {
206             checkboxViewer.setChecked(definitions[i], definitions[i]
207                     .isEnabled());
208         }
209     }
210
211     /**
212      * Show the selected description in the text.
213      */

214     private void showDescription(DecoratorDefinition definition) {
215         if (descriptionText == null || descriptionText.isDisposed()) {
216             return;
217         }
218         String JavaDoc text = definition.getDescription();
219         if (text == null || text.length() == 0) {
220             descriptionText.setText(WorkbenchMessages.PreferencePage_noDescription);
221         } else {
222             descriptionText.setText(text);
223         }
224     }
225
226     /**
227      * Clear the selected description in the text.
228      */

229     private void clearDescription() {
230         if (descriptionText == null || descriptionText.isDisposed()) {
231             return;
232         }
233         descriptionText.setText(""); //$NON-NLS-1$
234
}
235
236     /**
237      * @see PreferencePage#performDefaults()
238      */

239     protected void performDefaults() {
240         super.performDefaults();
241         DecoratorManager manager = WorkbenchPlugin.getDefault()
242                 .getDecoratorManager();
243         DecoratorDefinition[] definitions = manager
244                 .getAllDecoratorDefinitions();
245         for (int i = 0; i < definitions.length; i++) {
246             checkboxViewer.setChecked(definitions[i], definitions[i]
247                     .getDefaultValue());
248         }
249     }
250
251     /**
252      * @see IPreferencePage#performOk()
253      */

254     public boolean performOk() {
255         if (super.performOk()) {
256             DecoratorManager manager = getDecoratorManager();
257             //Clear the caches first to avoid unneccessary updates
258
manager.clearCaches();
259             DecoratorDefinition[] definitions = manager
260                     .getAllDecoratorDefinitions();
261             for (int i = 0; i < definitions.length; i++) {
262                 boolean checked = checkboxViewer.getChecked(definitions[i]);
263                 definitions[i].setEnabled(checked);
264
265             }
266             //Have the manager clear again as there may have been
267
//extra updates fired by the enablement changes.
268
manager.clearCaches();
269             manager.updateForEnablementChange();
270             return true;
271         }
272         return false;
273     }
274
275     /**
276      * @see IWorkbenchPreferencePage#init(IWorkbench)
277      */

278     public void init(IWorkbench workbench) {
279     }
280
281     /**
282      * Get the decorator definitions for the workbench.
283      */

284     private DecoratorDefinition[] getAllDefinitions() {
285         return getDecoratorManager().getAllDecoratorDefinitions();
286     }
287
288     /**
289      * Get the DecoratorManager being used for this page.
290      *
291      * @return the decorator manager
292      */

293     private DecoratorManager getDecoratorManager() {
294         return WorkbenchPlugin.getDefault().getDecoratorManager();
295     }
296
297 }
298
Popular Tags