KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > FilteredResourcesSelectionDialog


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 package org.eclipse.ui.dialogs;
12
13 import java.io.IOException JavaDoc;
14 import java.io.StringReader JavaDoc;
15 import java.io.StringWriter JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Comparator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.core.resources.IContainer;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IResourceProxy;
24 import org.eclipse.core.resources.IResourceProxyVisitor;
25 import org.eclipse.core.resources.IWorkspace;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.ListenerList;
31 import org.eclipse.core.runtime.Status;
32 import org.eclipse.jface.action.Action;
33 import org.eclipse.jface.action.IAction;
34 import org.eclipse.jface.action.IMenuManager;
35 import org.eclipse.jface.action.Separator;
36 import org.eclipse.jface.dialogs.IDialogSettings;
37 import org.eclipse.jface.text.ITextSelection;
38 import org.eclipse.jface.util.IPropertyChangeListener;
39 import org.eclipse.jface.util.PropertyChangeEvent;
40 import org.eclipse.jface.viewers.ILabelDecorator;
41 import org.eclipse.jface.viewers.ILabelProviderListener;
42 import org.eclipse.jface.viewers.ISelection;
43 import org.eclipse.jface.viewers.LabelProvider;
44 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
45 import org.eclipse.jface.viewers.Viewer;
46 import org.eclipse.jface.viewers.ViewerFilter;
47 import org.eclipse.swt.graphics.Image;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Control;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.ui.IMemento;
52 import org.eclipse.ui.IWorkbenchPage;
53 import org.eclipse.ui.IWorkbenchWindow;
54 import org.eclipse.ui.IWorkingSet;
55 import org.eclipse.ui.PlatformUI;
56 import org.eclipse.ui.ResourceWorkingSetFilter;
57 import org.eclipse.ui.WorkbenchException;
58 import org.eclipse.ui.XMLMemento;
59 import org.eclipse.ui.actions.WorkingSetFilterActionGroup;
60 import org.eclipse.ui.internal.WorkbenchMessages;
61 import org.eclipse.ui.internal.WorkbenchPlugin;
62 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
63 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
64 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
65 import org.eclipse.ui.internal.ide.model.ResourceFactory;
66 import org.eclipse.ui.model.WorkbenchLabelProvider;
67 import org.eclipse.ui.statushandlers.StatusManager;
68
69 import com.ibm.icu.text.Collator;
70
71 /**
72  * Shows a list of resources to the user with a text entry field for a string
73  * pattern used to filter the list of resources.
74  *
75  * @since 3.3
76  */

77 public class FilteredResourcesSelectionDialog extends
78         FilteredItemsSelectionDialog {
79
80     private static final String JavaDoc DIALOG_SETTINGS = "org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog"; //$NON-NLS-1$
81

82     private static final String JavaDoc WORKINGS_SET_SETTINGS = "WorkingSet"; //$NON-NLS-1$
83

84     private static final String JavaDoc SHOW_DERIVED = "ShowDerived"; //$NON-NLS-1$
85

86     private ShowDerivedResourcesAction showDerivedResourcesAction;
87
88     private ResourceItemLabelProvider resourceItemLabelProvider;
89
90     private ResourceItemDetailsLabelProvider resourceItemDetailsLabelProvider;
91
92     private WorkingSetFilterActionGroup workingSetFilterActionGroup;
93
94     private CustomWorkingSetFilter workingSetFilter = new CustomWorkingSetFilter();
95
96     private String JavaDoc title;
97
98     private IContainer container;
99
100     private int typeMask;
101
102     private boolean isDerived;
103
104     /**
105      * Creates a new instance of the class
106      *
107      * @param shell
108      * the parent shell
109      * @param multi
110      * the multi selection flag
111      * @param container
112      * the container
113      * @param typesMask
114      * the types mask
115      */

116     public FilteredResourcesSelectionDialog(Shell shell, boolean multi,
117             IContainer container, int typesMask) {
118         super(shell, multi);
119
120         setSelectionHistory(new ResourceSelectionHistory());
121
122         setTitle(IDEWorkbenchMessages.OpenResourceDialog_title);
123         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
124                 IIDEHelpContextIds.OPEN_RESOURCE_DIALOG);
125
126         this.container = container;
127         this.typeMask = typesMask;
128
129         resourceItemLabelProvider = new ResourceItemLabelProvider();
130
131         resourceItemDetailsLabelProvider = new ResourceItemDetailsLabelProvider();
132
133         setListLabelProvider(resourceItemLabelProvider);
134         setDetailsLabelProvider(resourceItemDetailsLabelProvider);
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see org.eclipse.ui.dialogs.SelectionDialog#setTitle(java.lang.String)
141      */

142     public void setTitle(String JavaDoc title) {
143         super.setTitle(title);
144         this.title = title;
145     }
146
147     /**
148      * Adds or replaces subtitle of the dialog
149      *
150      * @param text
151      * the new subtitle
152      */

153     private void setSubtitle(String JavaDoc text) {
154         if (text == null || text.length() == 0) {
155             getShell().setText(title);
156         } else {
157             getShell().setText(title + " - " + text); //$NON-NLS-1$
158
}
159     }
160
161     /*
162      * (non-Javadoc)
163      *
164      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
165      */

166     protected IDialogSettings getDialogSettings() {
167         IDialogSettings settings = IDEWorkbenchPlugin.getDefault()
168                 .getDialogSettings().getSection(DIALOG_SETTINGS);
169
170         if (settings == null) {
171             settings = IDEWorkbenchPlugin.getDefault().getDialogSettings()
172                     .addNewSection(DIALOG_SETTINGS);
173         }
174
175         return settings;
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#storeDialog(org.eclipse.jface.dialogs.IDialogSettings)
182      */

183     protected void storeDialog(IDialogSettings settings) {
184         super.storeDialog(settings);
185
186         settings.put(SHOW_DERIVED, showDerivedResourcesAction.isChecked());
187
188         XMLMemento memento = XMLMemento.createWriteRoot("workingSet"); //$NON-NLS-1$
189
workingSetFilterActionGroup.saveState(memento);
190         workingSetFilterActionGroup.dispose();
191         StringWriter JavaDoc writer = new StringWriter JavaDoc();
192         try {
193             memento.save(writer);
194             settings.put(WORKINGS_SET_SETTINGS, writer.getBuffer().toString());
195         } catch (IOException JavaDoc e) {
196             StatusManager.getManager().handle(
197                     new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH,
198                             IStatus.ERROR, "", e)); //$NON-NLS-1$
199
// don't do anything. Simply don't store the settings
200
}
201     }
202
203     /*
204      * (non-Javadoc)
205      *
206      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#restoreDialog(org.eclipse.jface.dialogs.IDialogSettings)
207      */

208     protected void restoreDialog(IDialogSettings settings) {
209         super.restoreDialog(settings);
210
211         boolean showDerived = settings.getBoolean(SHOW_DERIVED);
212         showDerivedResourcesAction.setChecked(showDerived);
213         this.isDerived = showDerived;
214
215         String JavaDoc setting = settings.get(WORKINGS_SET_SETTINGS);
216         if (setting != null) {
217             try {
218                 IMemento memento = XMLMemento.createReadRoot(new StringReader JavaDoc(
219                         setting));
220                 workingSetFilterActionGroup.restoreState(memento);
221             } catch (WorkbenchException e) {
222                 StatusManager.getManager().handle(
223                         new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH,
224                                 IStatus.ERROR, "", e)); //$NON-NLS-1$
225
// don't do anything. Simply don't restore the settings
226
}
227         }
228
229         addListFilter(workingSetFilter);
230
231         applyFilter();
232     }
233
234     /*
235      * (non-Javadoc)
236      *
237      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillViewMenu(org.eclipse.jface.action.IMenuManager)
238      */

239     protected void fillViewMenu(IMenuManager menuManager) {
240         super.fillViewMenu(menuManager);
241
242         showDerivedResourcesAction = new ShowDerivedResourcesAction();
243         menuManager.add(showDerivedResourcesAction);
244
245         workingSetFilterActionGroup = new WorkingSetFilterActionGroup(
246                 getShell(), new IPropertyChangeListener() {
247                     public void propertyChange(PropertyChangeEvent event) {
248                         String JavaDoc property = event.getProperty();
249
250                         if (WorkingSetFilterActionGroup.CHANGE_WORKING_SET
251                                 .equals(property)) {
252
253                             IWorkingSet workingSet = (IWorkingSet) event
254                                     .getNewValue();
255
256                             if (workingSet != null
257                                     && !(workingSet.isAggregateWorkingSet() && workingSet
258                                             .isEmpty())) {
259                                 workingSetFilter.setWorkingSet(workingSet);
260                                 setSubtitle(workingSet.getLabel());
261                             } else {
262                                 IWorkbenchWindow window = PlatformUI
263                                         .getWorkbench()
264                                         .getActiveWorkbenchWindow();
265
266                                 if (window != null) {
267                                     IWorkbenchPage page = window
268                                             .getActivePage();
269                                     workingSet = page.getAggregateWorkingSet();
270
271                                     if (workingSet.isAggregateWorkingSet()
272                                             && workingSet.isEmpty()) {
273                                         workingSet = null;
274                                     }
275                                 }
276
277                                 workingSetFilter.setWorkingSet(workingSet);
278                                 setSubtitle(null);
279                             }
280
281                             scheduleRefresh();
282                         }
283                     }
284                 });
285
286         menuManager.add(new Separator());
287         workingSetFilterActionGroup.fillContextMenu(menuManager);
288     }
289
290     /*
291      * (non-Javadoc)
292      *
293      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
294      */

295     protected Control createExtendedContentArea(Composite parent) {
296         return null;
297     }
298
299     /*
300      * (non-Javadoc)
301      *
302      * @see org.eclipse.ui.dialogs.SelectionDialog#getResult()
303      */

304     public Object JavaDoc[] getResult() {
305         Object JavaDoc[] result = super.getResult();
306
307         if (result == null)
308             return null;
309
310         List JavaDoc resultToReturn = new ArrayList JavaDoc();
311
312         for (int i = 0; i < result.length; i++) {
313             if (result[i] instanceof IResource) {
314                 resultToReturn.add((result[i]));
315             }
316         }
317
318         return resultToReturn.toArray();
319     }
320
321     /*
322      * (non-Javadoc)
323      *
324      * @see org.eclipse.jface.window.Window#open()
325      */

326     public int open() {
327         if (getInitialPattern() == null) {
328             IWorkbenchWindow window = PlatformUI.getWorkbench()
329                     .getActiveWorkbenchWindow();
330             if (window != null) {
331                 ISelection selection = window.getSelectionService()
332                         .getSelection();
333                 if (selection instanceof ITextSelection) {
334                     String JavaDoc text = ((ITextSelection) selection).getText();
335                     if (text != null) {
336                         text = text.trim();
337                         if (text.length() > 0) {
338                             IWorkspace workspace = ResourcesPlugin
339                                     .getWorkspace();
340                             IStatus result = workspace.validateName(text,
341                                     IResource.FILE);
342                             if (result.isOK()) {
343                                 setInitialPattern(text);
344                             }
345                         }
346                     }
347                 }
348             }
349         }
350         return super.open();
351     }
352
353     /*
354      * (non-Javadoc)
355      *
356      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
357      */

358     public String JavaDoc getElementName(Object JavaDoc item) {
359         IResource resource = (IResource) item;
360         return resource.getName();
361     }
362
363     /*
364      * (non-Javadoc)
365      *
366      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
367      */

368     protected IStatus validateItem(Object JavaDoc item) {
369         return new Status(IStatus.OK, WorkbenchPlugin.PI_WORKBENCH, 0, "", null); //$NON-NLS-1$
370
}
371
372     /*
373      * (non-Javadoc)
374      *
375      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
376      */

377     protected ItemsFilter createFilter() {
378         return new ResourceFilter(container, isDerived, typeMask);
379     }
380
381     /* (non-Javadoc)
382      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#applyFilter()
383      */

384     protected void applyFilter() {
385         super.applyFilter();
386     }
387
388     /*
389      * (non-Javadoc)
390      *
391      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
392      */

393     protected Comparator JavaDoc getItemsComparator() {
394         return new Comparator JavaDoc() {
395
396             /*
397              * (non-Javadoc)
398              *
399              * @see java.util.Comparator#compare(java.lang.Object,
400              * java.lang.Object)
401              */

402             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
403                 Collator collator = Collator.getInstance();
404                 IResource resource1 = (IResource) o1;
405                 IResource resource2 = (IResource) o2;
406                 String JavaDoc s1 = resource1.getName();
407                 String JavaDoc s2 = resource2.getName();
408                 int comparability = collator.compare(s1, s2);
409                 if (comparability == 0) {
410                     s1 = resource1.getFullPath().toString();
411                     s2 = resource2.getFullPath().toString();
412                     comparability = collator.compare(s1, s2);
413                 }
414
415                 return comparability;
416             }
417         };
418     }
419
420     /*
421      * (non-Javadoc)
422      *
423      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider,
424      * org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter,
425      * org.eclipse.core.runtime.IProgressMonitor)
426      */

427     protected void fillContentProvider(AbstractContentProvider contentProvider,
428             ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
429             throws CoreException {
430         if (itemsFilter instanceof ResourceFilter)
431             container.accept(new ResourceProxyVisitor(contentProvider,
432                     (ResourceFilter) itemsFilter, progressMonitor),
433                     IResource.NONE);
434         if (progressMonitor != null)
435             progressMonitor.done();
436
437     }
438
439     /**
440      * Sets the derived flag on the ResourceFilter instance
441      */

442     private class ShowDerivedResourcesAction extends Action {
443
444         /**
445          * Creates a new instance of the action.
446          */

447         public ShowDerivedResourcesAction() {
448             super(
449                     IDEWorkbenchMessages.FilteredResourcesSelectionDialog_showDerivedResourcesAction,
450                     IAction.AS_CHECK_BOX);
451         }
452
453         public void run() {
454             FilteredResourcesSelectionDialog.this.isDerived = isChecked();
455             applyFilter();
456         }
457     }
458
459     /**
460      * A label provider for ResourceDecorator objects. It creates labels with a
461      * resource full path for duplicates. It uses the Platform UI label
462      * decorator for providing extra resource info.
463      */

464     private class ResourceItemLabelProvider extends LabelProvider implements
465             ILabelProviderListener {
466
467         // Need to keep our own list of listeners
468
private ListenerList listeners = new ListenerList();
469
470         WorkbenchLabelProvider provider = new WorkbenchLabelProvider();
471
472         ILabelDecorator decorator = PlatformUI.getWorkbench()
473                 .getDecoratorManager().getLabelDecorator();
474
475         /**
476          * Creates a new instance of the class
477          */

478         public ResourceItemLabelProvider() {
479             super();
480             provider.addListener(this);
481             decorator.addListener(this);
482         }
483
484         /*
485          * (non-Javadoc)
486          *
487          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
488          */

489         public Image getImage(Object JavaDoc element) {
490             if (!(element instanceof IResource)) {
491                 return super.getImage(element);
492             }
493
494             IResource res = (IResource) element;
495
496             Image img = provider.getImage(res);
497
498             return decorator.decorateImage(img, res);
499         }
500
501         /*
502          * (non-Javadoc)
503          *
504          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
505          */

506         public String JavaDoc getText(Object JavaDoc element) {
507             if (!(element instanceof IResource)) {
508                 return super.getText(element);
509             }
510
511             IResource res = (IResource) element;
512
513             String JavaDoc str = res.getName();
514
515             // extra info for duplicates
516
if (isDuplicateElement(element))
517                 str = str
518                         + " - " + res.getParent().getFullPath().makeRelative().toString(); //$NON-NLS-1$
519

520             return decorator.decorateText(str, res);
521         }
522
523         /*
524          * (non-Javadoc)
525          *
526          * @see org.eclipse.jface.viewers.LabelProvider#dispose()
527          */

528         public void dispose() {
529             provider.removeListener(this);
530             provider.dispose();
531
532             decorator.removeListener(this);
533             decorator.dispose();
534
535             super.dispose();
536         }
537
538         /*
539          * (non-Javadoc)
540          *
541          * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
542          */

543         public void addListener(ILabelProviderListener listener) {
544             listeners.add(listener);
545         }
546
547         /*
548          * (non-Javadoc)
549          *
550          * @see org.eclipse.jface.viewers.LabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
551          */

552         public void removeListener(ILabelProviderListener listener) {
553             listeners.remove(listener);
554         }
555
556         /*
557          * (non-Javadoc)
558          *
559          * @see org.eclipse.jface.viewers.ILabelProviderListener#labelProviderChanged(org.eclipse.jface.viewers.LabelProviderChangedEvent)
560          */

561         public void labelProviderChanged(LabelProviderChangedEvent event) {
562             Object JavaDoc[] l = listeners.getListeners();
563             for (int i = 0; i < listeners.size(); i++) {
564                 ((ILabelProviderListener) l[i]).labelProviderChanged(event);
565             }
566         }
567
568     }
569
570     /**
571      * A label provider for details of ResourceItem objects.
572      */

573     private class ResourceItemDetailsLabelProvider extends
574             ResourceItemLabelProvider {
575         /*
576          * (non-Javadoc)
577          *
578          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
579          */

580         public Image getImage(Object JavaDoc element) {
581             if (!(element instanceof IResource)) {
582                 return super.getImage(element);
583             }
584
585             IResource parent = ((IResource) element).getParent();
586             Image img = provider.getImage(parent);
587
588             return decorator.decorateImage(img, parent);
589         }
590
591         /*
592          * (non-Javadoc)
593          *
594          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
595          */

596         public String JavaDoc getText(Object JavaDoc element) {
597             if (!(element instanceof IResource)) {
598                 return super.getText(element);
599             }
600
601             IResource parent = ((IResource) element).getParent();
602
603             if (parent.getType() == IResource.ROOT) {
604                 // Get readable name for workspace root ("Workspace"), without
605
// duplicating language-specific string here.
606
return super.decorator.decorateText(null, parent);
607             }
608
609             return super.decorator.decorateText(parent.getFullPath()
610                     .makeRelative().toString(), parent);
611         }
612
613         /*
614          * (non-Javadoc)
615          *
616          * @see org.eclipse.jface.viewers.ILabelProviderListener#labelProviderChanged(org.eclipse.jface.viewers.LabelProviderChangedEvent)
617          */

618         public void labelProviderChanged(LabelProviderChangedEvent event) {
619             Object JavaDoc[] l = super.listeners.getListeners();
620             for (int i = 0; i < super.listeners.size(); i++) {
621                 ((ILabelProviderListener) l[i]).labelProviderChanged(event);
622             }
623         }
624     }
625
626     /**
627      * Viewer filter which filters resources due to current working set
628      */

629     private class CustomWorkingSetFilter extends ViewerFilter {
630         private ResourceWorkingSetFilter resourceWorkingSetFilter = new ResourceWorkingSetFilter();
631
632         /**
633          * Returns the active working set the filter is working with.
634          *
635          * @return the active working set
636          */

637         public IWorkingSet getWorkingSet() {
638             return resourceWorkingSetFilter.getWorkingSet();
639         }
640
641         /**
642          * Sets the active working set.
643          *
644          * @param workingSet
645          * the working set the filter should work with
646          */

647         public void setWorkingSet(IWorkingSet workingSet) {
648             resourceWorkingSetFilter.setWorkingSet(workingSet);
649         }
650
651         /*
652          * (non-Javadoc)
653          *
654          * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer,
655          * java.lang.Object, java.lang.Object)
656          */

657         public boolean select(Viewer viewer, Object JavaDoc parentElement,
658                 Object JavaDoc element) {
659             return resourceWorkingSetFilter.select(viewer, parentElement,
660                         element);
661         }
662     }
663
664     /**
665      * ResourceProxyVisitor to visit resource tree and get matched resources.
666      * During visit resources it updates progress monitor and adds matched
667      * resources to ContentProvider instance.
668      */

669     private class ResourceProxyVisitor implements IResourceProxyVisitor {
670
671         private AbstractContentProvider proxyContentProvider;
672
673         private ResourceFilter resourceFilter;
674
675         private IProgressMonitor progressMonitor;
676
677         private List JavaDoc projects;
678
679         /**
680          * Creates new ResourceProxyVisitor instance.
681          *
682          * @param contentProvider
683          * @param resourceFilter
684          * @param progressMonitor
685          * @throws CoreException
686          */

687         public ResourceProxyVisitor(AbstractContentProvider contentProvider,
688                 ResourceFilter resourceFilter, IProgressMonitor progressMonitor)
689                 throws CoreException {
690             super();
691             this.proxyContentProvider = contentProvider;
692             this.resourceFilter = resourceFilter;
693             this.progressMonitor = progressMonitor;
694             IResource[] resources = container.members();
695             this.projects = new ArrayList JavaDoc(Arrays.asList(resources));
696
697             if (progressMonitor != null)
698                 progressMonitor
699                         .beginTask(
700                                 WorkbenchMessages.FilteredItemsSelectionDialog_searchJob_taskName,
701                                 projects.size());
702         }
703
704         /*
705          * (non-Javadoc)
706          *
707          * @see org.eclipse.core.resources.IResourceProxyVisitor#visit(org.eclipse.core.resources.IResourceProxy)
708          */

709         public boolean visit(IResourceProxy proxy) {
710
711             if (progressMonitor.isCanceled())
712                 return false;
713
714             IResource resource = proxy.requestResource();
715
716             if (this.projects.remove((resource.getProject()))
717                     || this.projects.remove((resource))) {
718                 progressMonitor.worked(1);
719             }
720
721             proxyContentProvider.add(resource, resourceFilter);
722
723             if (resource.getType() == IResource.FOLDER && resource.isDerived()
724                     && !resourceFilter.isShowDerived()) {
725
726                 return false;
727             }
728
729             if (resource.getType() == IResource.FILE) {
730                 return false;
731             }
732
733             return true;
734         }
735     }
736
737     /**
738      * Filters resources using pattern and showDerived flag. It overrides
739      * ItemsFilter.
740      */

741     protected class ResourceFilter extends ItemsFilter {
742
743         private boolean showDerived = false;
744
745         private IContainer filterContainer;
746
747         private int filterTypeMask;
748
749         /**
750          * Creates new ResourceFilter instance
751          *
752          * @param container
753          * @param showDerived
754          * flag which determine showing derived elements
755          * @param typeMask
756          */

757         public ResourceFilter(IContainer container, boolean showDerived,
758                 int typeMask) {
759             super();
760             this.filterContainer = container;
761             this.showDerived = showDerived;
762             this.filterTypeMask = typeMask;
763         }
764
765         /**
766          * Creates new ResourceFilter instance
767          */

768         public ResourceFilter() {
769             super();
770             this.filterContainer = container;
771             this.showDerived = isDerived;
772             this.filterTypeMask = typeMask;
773         }
774
775         /**
776          * @param item
777          * Must be instance of IResource, otherwise
778          * <code>false</code> will be returned.
779          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter#isConsistentItem(java.lang.Object)
780          */

781         public boolean isConsistentItem(Object JavaDoc item) {
782             if (!(item instanceof IResource)) {
783                 return false;
784             }
785             IResource resource = (IResource) item;
786             if (this.filterContainer.findMember(resource.getFullPath()) != null)
787                 return true;
788             return false;
789         }
790
791         /**
792          * @param item
793          * Must be instance of IResource, otherwise
794          * <code>false</code> will be returned.
795          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter#matchItem(java.lang.Object)
796          */

797         public boolean matchItem(Object JavaDoc item) {
798             if (!(item instanceof IResource)) {
799                 return false;
800             }
801             IResource resource = (IResource) item;
802             if ((!this.showDerived && resource.isDerived())
803                     || ((this.filterTypeMask & resource.getType()) == 0))
804                 return false;
805             return matches(resource.getName());
806         }
807
808         /*
809          * (non-Javadoc)
810          *
811          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter#isSubFilter(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter)
812          */

813         public boolean isSubFilter(ItemsFilter filter) {
814             if (!super.isSubFilter(filter))
815                 return false;
816             if (filter instanceof ResourceFilter)
817                 if (this.showDerived == ((ResourceFilter) filter).showDerived)
818                     return true;
819             return false;
820         }
821
822         /*
823          * (non-Javadoc)
824          *
825          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter#equalsFilter(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter)
826          */

827         public boolean equalsFilter(ItemsFilter iFilter) {
828             if (!super.equalsFilter(iFilter))
829                 return false;
830             if (iFilter instanceof ResourceFilter)
831                 if (this.showDerived == ((ResourceFilter) iFilter).showDerived)
832                     return true;
833             return false;
834         }
835
836         /**
837          * Check show derived flag for a filter
838          *
839          * @return true if filter allow derived resources false if not
840          */

841         public boolean isShowDerived() {
842             return showDerived;
843         }
844
845     }
846
847     /**
848      * <code>ResourceSelectionHistory</code> provides behavior specific to
849      * resources - storing and restoring <code>IResource</code>s state
850      * to/from XML (memento).
851      */

852     private class ResourceSelectionHistory extends SelectionHistory {
853
854         /*
855          * (non-Javadoc)
856          *
857          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#restoreItemFromMemento(org.eclipse.ui.IMemento)
858          */

859         protected Object JavaDoc restoreItemFromMemento(IMemento element) {
860             ResourceFactory resourceFactory = new ResourceFactory();
861             IResource resource = (IResource) resourceFactory
862                     .createElement(element);
863             return resource;
864         }
865
866         /*
867          * (non-Javadoc)
868          *
869          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#storeItemToMemento(java.lang.Object,
870          * org.eclipse.ui.IMemento)
871          */

872         protected void storeItemToMemento(Object JavaDoc item, IMemento element) {
873             IResource resource = (IResource) item;
874             ResourceFactory resourceFactory = new ResourceFactory(resource);
875             resourceFactory.saveState(element);
876         }
877
878     }
879
880 }
881
Popular Tags