KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.ui.internal.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.jface.action.ToolBarManager;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.preference.IPreferenceNode;
21 import org.eclipse.jface.preference.IPreferencePage;
22 import org.eclipse.jface.preference.PreferenceContentProvider;
23 import org.eclipse.jface.preference.PreferenceDialog;
24 import org.eclipse.jface.preference.PreferenceManager;
25 import org.eclipse.jface.preference.PreferencePage;
26 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.jface.viewers.ISelectionChangedListener;
28 import org.eclipse.jface.viewers.ITreeContentProvider;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.jface.viewers.TreeViewer;
32 import org.eclipse.jface.viewers.ViewerFilter;
33 import org.eclipse.osgi.util.NLS;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.graphics.Font;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.activities.WorkbenchActivityHelper;
43 import org.eclipse.ui.dialogs.FilteredTree;
44 import org.eclipse.ui.dialogs.PatternFilter;
45 import org.eclipse.ui.internal.WorkbenchMessages;
46 import org.eclipse.ui.internal.misc.StatusUtil;
47 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
48 import org.eclipse.ui.preferences.IWorkingCopyManager;
49 import org.eclipse.ui.preferences.WorkingCopyManager;
50 import org.eclipse.ui.statushandlers.StatusManager;
51 import org.osgi.service.prefs.BackingStoreException;
52
53 /**
54  * Baseclass for preference dialogs that will show two tabs of preferences -
55  * filtered and unfiltered.
56  *
57  * @since 3.0
58  */

59 public abstract class FilteredPreferenceDialog extends PreferenceDialog implements IWorkbenchPreferenceContainer{
60
61     protected class PreferenceFilteredTree extends FilteredTree{
62         /**
63          * An (optional) additional filter on the TreeViewer.
64          */

65         private ViewerFilter viewerFilter;
66         
67         /**
68          * Initial title of dialog. This is only used if the additional filter provided
69          * by the addFilter(ViewerFilter) method is utilized.
70          */

71         private String JavaDoc cachedTitle;
72         
73         /**
74          * Constructor.
75          *
76          * @param parent parent Composite
77          * @param treeStyle SWT style bits for Tree
78          * @param filter the PatternFilter to use for the TreeViewer
79          */

80         PreferenceFilteredTree(Composite parent, int treeStyle,
81                 PatternFilter filter) {
82             super(parent, treeStyle, filter);
83         }
84
85         /**
86          * Add an additional, optional filter to the viewer.
87          * If the filter text is cleared, this filter will be
88          * removed from the TreeViewer.
89          *
90          * @param filter
91          */

92         protected void addFilter(ViewerFilter filter) {
93             viewerFilter = filter;
94             getViewer().addFilter(filter);
95             setInitialText(WorkbenchMessages.FilteredTree_FilterMessage);
96             
97             if(filterText != null){
98                 setFilterText(WorkbenchMessages.FilteredTree_FilterMessage);
99                 textChanged();
100             }
101             
102             cachedTitle = getShell().getText();
103             getShell().setText(
104                     NLS.bind(
105                             WorkbenchMessages.FilteredTree_FilteredDialogTitle,
106                     cachedTitle));
107         }
108
109         /*
110          * (non-Javadoc)
111          * @see org.eclipse.ui.dialogs.FilteredTree#updateToolbar(boolean)
112          */

113         protected void updateToolbar(boolean visible) {
114             if (filterToolBar != null) {
115                 filterToolBar.getControl().setVisible(
116                         viewerFilter != null || visible);
117             }
118         }
119
120         /* (non-Javadoc)
121          * @see org.eclipse.ui.dialogs.FilteredTree#clearText()
122          */

123         protected void clearText() {
124             setFilterText(""); //$NON-NLS-1$
125
// remove the filter if text is cleared
126
if(viewerFilter != null){
127                 getViewer().removeFilter(viewerFilter);
128                 viewerFilter = null;
129                 getShell().setText(cachedTitle);
130             }
131             textChanged();
132         }
133     }
134     
135     protected PreferenceFilteredTree filteredTree;
136
137     private Object JavaDoc pageData;
138     
139     IWorkingCopyManager workingCopyManager;
140     
141     private Collection JavaDoc updateJobs = new ArrayList JavaDoc();
142     
143     /**
144      * The preference page history.
145      *
146      * @since 3.1
147      */

148     PreferencePageHistory history;
149
150     /**
151      * Creates a new preference dialog under the control of the given preference
152      * manager.
153      *
154      * @param parentShell
155      * the parent shell
156      * @param manager
157      * the preference manager
158      */

159     public FilteredPreferenceDialog(Shell parentShell, PreferenceManager manager) {
160         super(parentShell, manager);
161         history = new PreferencePageHistory(this);
162     }
163
164     /**
165      * Differs from super implementation in that if the node is found but should
166      * be filtered based on a call to
167      * <code>WorkbenchActivityHelper.filterItem()</code> then
168      * <code>null</code> is returned.
169      *
170      * @see org.eclipse.jface.preference.PreferenceDialog#findNodeMatching(java.lang.String)
171      */

172     protected IPreferenceNode findNodeMatching(String JavaDoc nodeId) {
173         IPreferenceNode node = super.findNodeMatching(nodeId);
174         if (WorkbenchActivityHelper.filterItem(node)) {
175             return null;
176         }
177         return node;
178     }
179
180     /* (non-Javadoc)
181      * @see org.eclipse.jface.preference.PreferenceDialog#createTreeViewer(org.eclipse.swt.widgets.Composite)
182      */

183     protected TreeViewer createTreeViewer(Composite parent) {
184         int styleBits = SWT.SINGLE | SWT.H_SCROLL;
185         filteredTree = new PreferenceFilteredTree(parent, styleBits,
186                 new PreferencePatternFilter());
187         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
188         gd.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN;
189         filteredTree.setBackground(parent.getDisplay().getSystemColor(
190                 SWT.COLOR_LIST_BACKGROUND));
191
192         TreeViewer tree = filteredTree.getViewer();
193
194         setContentAndLabelProviders(tree);
195         tree.setInput(getPreferenceManager());
196         
197         //if the tree has only one or zero pages, make the combo area disable
198
if(hasAtMostOnePage(tree)){
199             Text filterText = filteredTree.getFilterControl();
200             if (filterText != null) {
201                 filteredTree.getFilterControl().setEnabled(false);
202             }
203         }
204         
205         tree.addFilter(new CapabilityFilter());
206
207         tree.addSelectionChangedListener(new ISelectionChangedListener() {
208             /*
209              * (non-Javadoc)
210              *
211              * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
212              */

213             public void selectionChanged(SelectionChangedEvent event) {
214                 handleTreeSelectionChanged(event);
215             }
216         });
217
218         super.addListeners(filteredTree.getViewer());
219         return filteredTree.getViewer();
220     }
221
222
223     /**
224      * Return whether or not there are less than two pages.
225      * @param tree
226      * @return <code>true</code> if there are less than two
227      * pages.
228      */

229     private boolean hasAtMostOnePage(TreeViewer tree){
230         ITreeContentProvider contentProvider = (ITreeContentProvider ) tree.getContentProvider();
231         Object JavaDoc[] children= contentProvider.getElements(tree.getInput());
232         
233         if(children.length <= 1){
234             if(children.length == 0) {
235                 return true;
236             }
237             return !contentProvider.hasChildren(children[0]);
238         }
239         return false;
240     }
241     /**
242      * Set the content and label providers for the treeViewer
243      *
244      * @param treeViewer
245      */

246     protected void setContentAndLabelProviders(TreeViewer treeViewer) {
247         treeViewer.setLabelProvider(new PreferenceBoldLabelProvider(filteredTree));
248         treeViewer.setContentProvider(new PreferenceContentProvider());
249     }
250
251     /**
252      * A selection has been made in the tree.
253      * @param event SelectionChangedEvent
254      */

255     protected void handleTreeSelectionChanged(SelectionChangedEvent event) {
256         //Do nothing by default
257
}
258
259     /* (non-Javadoc)
260      * @see org.eclipse.jface.preference.PreferenceDialog#createTreeAreaContents(org.eclipse.swt.widgets.Composite)
261      */

262     protected Control createTreeAreaContents(Composite parent) {
263         Composite leftArea = new Composite(parent, SWT.NONE);
264         leftArea.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
265         leftArea.setFont(parent.getFont());
266         GridLayout leftLayout = new GridLayout();
267         leftLayout.numColumns = 1;
268         leftLayout.marginHeight = 0;
269         leftLayout.marginTop = IDialogConstants.VERTICAL_MARGIN;
270         leftLayout.marginWidth = 0;
271         leftLayout.marginLeft = IDialogConstants.HORIZONTAL_MARGIN;
272         leftLayout.horizontalSpacing = 0;
273         leftLayout.verticalSpacing = 0;
274         leftArea.setLayout(leftLayout);
275
276         // Build the tree an put it into the composite.
277
TreeViewer viewer = createTreeViewer(leftArea);
278         setTreeViewer(viewer);
279
280         updateTreeFont(JFaceResources.getDialogFont());
281         GridData viewerData = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL);
282         viewer.getControl().getParent().setLayoutData(viewerData);
283
284         layoutTreeAreaControl(leftArea);
285
286         return leftArea;
287     }
288
289
290
291     /**
292      * Show only the supplied ids.
293      *
294      * @param filteredIds
295      */

296     public void showOnly(String JavaDoc[] filteredIds) {
297         filteredTree.addFilter(new PreferenceNodeFilter(filteredIds));
298     }
299
300     /**
301      * Set the data to be applied to a page after it is created.
302      * @param pageData Object
303      */

304     public void setPageData(Object JavaDoc pageData) {
305         this.pageData = pageData;
306     }
307
308     /* (non-Javadoc)
309      * @see org.eclipse.jface.preference.PreferenceDialog#createPage(org.eclipse.jface.preference.IPreferenceNode)
310      */

311     protected void createPage(IPreferenceNode node) {
312
313         super.createPage(node);
314         if (this.pageData == null) {
315             return;
316         }
317         //Apply the data if it has been set.
318
IPreferencePage page = node.getPage();
319         if (page instanceof PreferencePage) {
320             ((PreferencePage) page).applyData(this.pageData);
321         }
322
323     }
324     
325     /* (non-Javadoc)
326      * @see org.eclipse.jface.preference.PreferenceDialog#getCurrentPage()
327      */

328     public IPreferencePage getCurrentPage() {
329         return super.getCurrentPage();
330     }
331
332     /* (non-Javadoc)
333      * @see org.eclipse.ui.preferences.IWorkbenchPreferenceContainer#openPage(java.lang.String, java.lang.Object)
334      */

335     public boolean openPage(String JavaDoc pageId, Object JavaDoc data) {
336         setPageData(data);
337         setCurrentPageId(pageId);
338         IPreferencePage page = getCurrentPage();
339         if (page instanceof PreferencePage) {
340             ((PreferencePage) page).applyData(data);
341         }
342         return true;
343     }
344
345     /**
346      * Selects the current page based on the given preference page identifier.
347      * If no node can be found, then nothing will change.
348      *
349      * @param preferencePageId
350      * The preference page identifier to select; should not be
351      * <code>null</code>.
352      */

353     public final void setCurrentPageId(final String JavaDoc preferencePageId) {
354         final IPreferenceNode node = findNodeMatching(preferencePageId);
355         if (node != null) {
356             getTreeViewer().setSelection(new StructuredSelection(node));
357             showPage(node);
358         }
359     }
360     
361     /* (non-Javadoc)
362      * @see org.eclipse.ui.preferences.IWorkbenchPreferenceContainer#getWorkingCopyManager()
363      */

364     public IWorkingCopyManager getWorkingCopyManager() {
365         if(workingCopyManager == null){
366             workingCopyManager = new WorkingCopyManager();
367         }
368         return workingCopyManager;
369     }
370     /* (non-Javadoc)
371      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
372      */

373     protected void okPressed() {
374         super.okPressed();
375         
376         if(getReturnCode() == FAILED) {
377             return;
378         }
379         
380         if (workingCopyManager != null) {
381             try {
382                 workingCopyManager.applyChanges();
383             } catch (BackingStoreException e) {
384                 String JavaDoc msg = e.getMessage();
385                 if (msg == null) {
386                     msg = WorkbenchMessages.FilteredPreferenceDialog_PreferenceSaveFailed;
387                 }
388                 StatusUtil
389                         .handleStatus(
390                                 WorkbenchMessages.PreferencesExportDialog_ErrorDialogTitle
391                                         + ": " + msg, e, StatusManager.SHOW, //$NON-NLS-1$
392
getShell());
393             }
394         }
395
396         // Run the update jobs
397
Iterator JavaDoc updateIterator = updateJobs.iterator();
398         while (updateIterator.hasNext()) {
399             ((Job) updateIterator.next()).schedule();
400         }
401     }
402     
403     /* (non-Javadoc)
404      * @see org.eclipse.ui.preferences.IWorkbenchPreferenceContainer#registerUpdateJob(org.eclipse.core.runtime.jobs.Job)
405      */

406     public void registerUpdateJob(Job job){
407         updateJobs.add(job);
408     }
409
410     /**
411      * Get the toolbar for the container
412      *
413      * @return Control
414      */

415     Control getContainerToolBar(Composite composite) {
416     
417         ToolBarManager historyManager = new ToolBarManager(SWT.HORIZONTAL | SWT.FLAT);
418         historyManager.createControl(composite);
419     
420         history.createHistoryControls(historyManager.getControl(), historyManager);
421         
422         historyManager.update(false);
423     
424         return historyManager.getControl();
425     }
426
427
428
429     /* (non-Javadoc)
430      * @see org.eclipse.jface.preference.PreferenceDialog#showPage(org.eclipse.jface.preference.IPreferenceNode)
431      */

432     protected boolean showPage(IPreferenceNode node) {
433         final boolean success = super.showPage(node);
434         if (success) {
435             history.addHistoryEntry(new PreferenceHistoryEntry(node.getId(), node.getLabelText(),
436                     null));
437         }
438         return success;
439     }
440
441     /* (non-Javadoc)
442      * @see org.eclipse.jface.window.Window#close()
443      */

444     public boolean close() {
445         history.dispose();
446         return super.close();
447     }
448     
449     /* (non-Javadoc)
450      * @see org.eclipse.jface.preference.PreferenceDialog#createTitleArea(org.eclipse.swt.widgets.Composite)
451      */

452     protected Composite createTitleArea(Composite parent) {
453                 
454         GridLayout parentLayout = (GridLayout) parent.getLayout();
455         parentLayout.numColumns = 2;
456         parentLayout.marginHeight = 0;
457         parentLayout.marginTop = IDialogConstants.VERTICAL_MARGIN;
458         parent.setLayout(parentLayout);
459         
460         Composite titleComposite = super.createTitleArea(parent);
461         
462         Composite toolbarArea=new Composite(parent, SWT.NONE);
463         GridLayout toolbarLayout = new GridLayout();
464         toolbarLayout.marginHeight = 0;
465         toolbarLayout.verticalSpacing = 0;
466         toolbarArea.setLayout(toolbarLayout);
467         toolbarArea.setLayoutData(new GridData(SWT.END, SWT.FILL, false, true));
468         Control topBar = getContainerToolBar(toolbarArea);
469         topBar.setLayoutData(new GridData(SWT.END, SWT.FILL, false, true));
470         
471         return titleComposite;
472     }
473
474     protected void selectSavedItem() {
475         getTreeViewer().setInput(getPreferenceManager());
476         super.selectSavedItem();
477         if(getTreeViewer().getTree().getItemCount() > 1) {
478             //unfortunately super will force focus to the list but we want the type ahead combo to get it.
479
Text filterText = filteredTree.getFilterControl();
480             if (filterText != null) {
481                 filterText.setFocus();
482             }
483         }
484     }
485     
486     /* (non-Javadoc)
487      * @see org.eclipse.jface.preference.PreferenceDialog#updateTreeFont(org.eclipse.swt.graphics.Font)
488      */

489     protected void updateTreeFont(Font dialogFont) {
490         applyDialogFont(filteredTree);
491     }
492 }
493
Popular Tags