001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import com.ibm.icu.text.Collator;
013: import java.util.Arrays;
014: import java.util.Collections;
015: import java.util.Comparator;
016:
017: import org.eclipse.jface.preference.IPreferencePage;
018: import org.eclipse.jface.preference.PreferencePage;
019: import org.eclipse.jface.viewers.CheckStateChangedEvent;
020: import org.eclipse.jface.viewers.CheckboxTableViewer;
021: import org.eclipse.jface.viewers.ICheckStateListener;
022: import org.eclipse.jface.viewers.ISelectionChangedListener;
023: import org.eclipse.jface.viewers.IStructuredContentProvider;
024: import org.eclipse.jface.viewers.IStructuredSelection;
025: import org.eclipse.jface.viewers.LabelProvider;
026: import org.eclipse.jface.viewers.SelectionChangedEvent;
027: import org.eclipse.jface.viewers.StructuredSelection;
028: import org.eclipse.jface.viewers.Viewer;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.graphics.Font;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Control;
035: import org.eclipse.swt.widgets.Label;
036: import org.eclipse.swt.widgets.Text;
037: import org.eclipse.ui.IWorkbench;
038: import org.eclipse.ui.IWorkbenchPreferencePage;
039: import org.eclipse.ui.PlatformUI;
040: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
041: import org.eclipse.ui.internal.WorkbenchMessages;
042: import org.eclipse.ui.internal.WorkbenchPlugin;
043: import org.eclipse.ui.internal.decorators.DecoratorDefinition;
044: import org.eclipse.ui.internal.decorators.DecoratorManager;
045:
046: /**
047: * The DecoratorsPreferencePage is the preference page for enabling and disabling
048: * the decorators in the image and for giving the user a description of the decorator.
049: */
050: public class DecoratorsPreferencePage extends PreferencePage implements
051: IWorkbenchPreferencePage {
052:
053: private Text descriptionText;
054:
055: private CheckboxTableViewer checkboxViewer;
056:
057: /**
058: * @see PreferencePage#createContents(Composite)
059: */
060: protected Control createContents(Composite parent) {
061:
062: Font font = parent.getFont();
063:
064: PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
065: IWorkbenchHelpContextIds.DECORATORS_PREFERENCE_PAGE);
066:
067: Composite mainComposite = new Composite(parent, SWT.NONE);
068: mainComposite.setLayoutData(new GridData(
069: GridData.FILL_HORIZONTAL));
070: mainComposite.setFont(font);
071:
072: GridLayout layout = new GridLayout();
073: layout.marginWidth = 0;
074: layout.marginHeight = 0;
075: layout.verticalSpacing = 10;
076: mainComposite.setLayout(layout);
077:
078: Label topLabel = new Label(mainComposite, SWT.NONE);
079: topLabel
080: .setText(WorkbenchMessages.DecoratorsPreferencePage_explanation);
081: topLabel.setFont(font);
082:
083: createDecoratorsArea(mainComposite);
084: createDescriptionArea(mainComposite);
085: populateDecorators();
086:
087: return mainComposite;
088: }
089:
090: /**
091: * Creates the widgets for the list of decorators.
092: */
093: private void createDecoratorsArea(Composite mainComposite) {
094:
095: Font mainFont = mainComposite.getFont();
096: Composite decoratorsComposite = new Composite(mainComposite,
097: SWT.NONE);
098: decoratorsComposite.setLayoutData(new GridData(
099: GridData.FILL_BOTH));
100: GridLayout decoratorsLayout = new GridLayout();
101: decoratorsLayout.marginWidth = 0;
102: decoratorsLayout.marginHeight = 0;
103: decoratorsComposite.setLayout(decoratorsLayout);
104: decoratorsComposite.setFont(mainFont);
105:
106: Label decoratorsLabel = new Label(decoratorsComposite, SWT.NONE);
107: decoratorsLabel
108: .setText(WorkbenchMessages.DecoratorsPreferencePage_decoratorsLabel);
109: decoratorsLabel.setFont(mainFont);
110:
111: // Checkbox table viewer of decorators
112: checkboxViewer = CheckboxTableViewer.newCheckList(
113: decoratorsComposite, SWT.SINGLE | SWT.TOP | SWT.BORDER);
114: checkboxViewer.getTable().setLayoutData(
115: new GridData(GridData.FILL_BOTH));
116: checkboxViewer.getTable()
117: .setFont(decoratorsComposite.getFont());
118: checkboxViewer.setLabelProvider(new LabelProvider() {
119: public String getText(Object element) {
120: return ((DecoratorDefinition) element).getName();
121: }
122: });
123: checkboxViewer.getTable().setFont(mainFont);
124:
125: checkboxViewer
126: .setContentProvider(new IStructuredContentProvider() {
127: private final Comparator comparer = new Comparator() {
128: private Collator collator = Collator
129: .getInstance();
130:
131: public int compare(Object arg0, Object arg1) {
132: String s1 = ((DecoratorDefinition) arg0)
133: .getName();
134: String s2 = ((DecoratorDefinition) arg1)
135: .getName();
136: return collator.compare(s1, s2);
137: }
138: };
139:
140: public void dispose() {
141: //Nothing to do on dispose
142: }
143:
144: public void inputChanged(Viewer viewer,
145: Object oldInput, Object newInput) {
146: }
147:
148: public Object[] getElements(Object inputElement) {
149: //Make an entry for each decorator definition
150: Object[] elements = (Object[]) inputElement;
151: Object[] results = new Object[elements.length];
152: System.arraycopy(elements, 0, results, 0,
153: elements.length);
154: Collections.sort(Arrays.asList(results),
155: comparer);
156: return results;
157: }
158:
159: });
160:
161: checkboxViewer
162: .addSelectionChangedListener(new ISelectionChangedListener() {
163: public void selectionChanged(
164: SelectionChangedEvent event) {
165: if (event.getSelection() instanceof IStructuredSelection) {
166: IStructuredSelection sel = (IStructuredSelection) event
167: .getSelection();
168: DecoratorDefinition definition = (DecoratorDefinition) sel
169: .getFirstElement();
170: if (definition == null) {
171: clearDescription();
172: } else {
173: showDescription(definition);
174: }
175: }
176: }
177: });
178:
179: checkboxViewer.addCheckStateListener(new ICheckStateListener() {
180: public void checkStateChanged(CheckStateChangedEvent event) {
181: checkboxViewer.setSelection(new StructuredSelection(
182: event.getElement()));
183: }
184: });
185: }
186:
187: /**
188: * Creates the widgets for the description.
189: */
190: private void createDescriptionArea(Composite mainComposite) {
191:
192: Font mainFont = mainComposite.getFont();
193: Composite textComposite = new Composite(mainComposite, SWT.NONE);
194: textComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
195: GridLayout textLayout = new GridLayout();
196: textLayout.marginWidth = 0;
197: textLayout.marginHeight = 0;
198: textComposite.setLayout(textLayout);
199: textComposite.setFont(mainFont);
200:
201: Label descriptionLabel = new Label(textComposite, SWT.NONE);
202: descriptionLabel
203: .setText(WorkbenchMessages.DecoratorsPreferencePage_description);
204: descriptionLabel.setFont(mainFont);
205:
206: descriptionText = new Text(textComposite, SWT.MULTI | SWT.WRAP
207: | SWT.READ_ONLY | SWT.BORDER | SWT.H_SCROLL);
208: descriptionText.setLayoutData(new GridData(GridData.FILL_BOTH));
209: descriptionText.setFont(mainFont);
210: }
211:
212: /**
213: * Populates the list of decorators.
214: */
215: private void populateDecorators() {
216: DecoratorDefinition[] definitions = getAllDefinitions();
217: checkboxViewer.setInput(definitions);
218: for (int i = 0; i < definitions.length; i++) {
219: checkboxViewer.setChecked(definitions[i], definitions[i]
220: .isEnabled());
221: }
222: }
223:
224: /**
225: * Show the selected description in the text.
226: */
227: private void showDescription(DecoratorDefinition definition) {
228: if (descriptionText == null || descriptionText.isDisposed()) {
229: return;
230: }
231: String text = definition.getDescription();
232: if (text == null || text.length() == 0) {
233: descriptionText
234: .setText(WorkbenchMessages.PreferencePage_noDescription);
235: } else {
236: descriptionText.setText(text);
237: }
238: }
239:
240: /**
241: * Clear the selected description in the text.
242: */
243: private void clearDescription() {
244: if (descriptionText == null || descriptionText.isDisposed()) {
245: return;
246: }
247: descriptionText.setText(""); //$NON-NLS-1$
248: }
249:
250: /**
251: * @see PreferencePage#performDefaults()
252: */
253: protected void performDefaults() {
254: super .performDefaults();
255: DecoratorManager manager = WorkbenchPlugin.getDefault()
256: .getDecoratorManager();
257: DecoratorDefinition[] definitions = manager
258: .getAllDecoratorDefinitions();
259: for (int i = 0; i < definitions.length; i++) {
260: checkboxViewer.setChecked(definitions[i], definitions[i]
261: .getDefaultValue());
262: }
263: }
264:
265: /**
266: * @see IPreferencePage#performOk()
267: */
268: public boolean performOk() {
269: if (super .performOk()) {
270: DecoratorManager manager = getDecoratorManager();
271: //Clear the caches first to avoid unneccessary updates
272: manager.clearCaches();
273: DecoratorDefinition[] definitions = manager
274: .getAllDecoratorDefinitions();
275: for (int i = 0; i < definitions.length; i++) {
276: boolean checked = checkboxViewer
277: .getChecked(definitions[i]);
278: definitions[i].setEnabled(checked);
279:
280: }
281: //Have the manager clear again as there may have been
282: //extra updates fired by the enablement changes.
283: manager.clearCaches();
284: manager.updateForEnablementChange();
285: return true;
286: }
287: return false;
288: }
289:
290: /**
291: * @see IWorkbenchPreferencePage#init(IWorkbench)
292: */
293: public void init(IWorkbench workbench) {
294: }
295:
296: /**
297: * Get the decorator definitions for the workbench.
298: */
299: private DecoratorDefinition[] getAllDefinitions() {
300: return getDecoratorManager().getAllDecoratorDefinitions();
301: }
302:
303: /**
304: * Get the DecoratorManager being used for this page.
305: *
306: * @return the decorator manager
307: */
308: private DecoratorManager getDecoratorManager() {
309: return WorkbenchPlugin.getDefault().getDecoratorManager();
310: }
311:
312: }
|