KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > FilterUpdater


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.jdt.internal.ui.viewsupport;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResourceChangeEvent;
18 import org.eclipse.core.resources.IResourceChangeListener;
19 import org.eclipse.core.resources.IResourceDelta;
20
21 import org.eclipse.swt.widgets.Control;
22
23 import org.eclipse.jdt.core.JavaCore;
24
25
26 public class FilterUpdater implements IResourceChangeListener {
27
28     private ProblemTreeViewer fViewer;
29     
30     public FilterUpdater(ProblemTreeViewer viewer) {
31         Assert.isNotNull(viewer);
32         fViewer= viewer;
33     }
34
35     /* (non-Javadoc)
36      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
37      */

38     public void resourceChanged(IResourceChangeEvent event) {
39         if (fViewer.getInput() == null) {
40             return;
41         }
42         IResourceDelta delta= event.getDelta();
43         if (delta == null)
44             return;
45         IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
46         for (int i= 0; i < projDeltas.length; i++) {
47             IResourceDelta pDelta= projDeltas[i];
48             if ((pDelta.getFlags() & IResourceDelta.DESCRIPTION) != 0) {
49                 IProject project= (IProject) pDelta.getResource();
50                 if (needsRefiltering(project)) {
51                     final Control ctrl= fViewer.getControl();
52                     if (ctrl != null && !ctrl.isDisposed()) {
53                         // async is needed due to bug 33783
54
ctrl.getDisplay().asyncExec(new Runnable JavaDoc() {
55                             public void run() {
56                                 if (!ctrl.isDisposed())
57                                     fViewer.refresh(false);
58                             }
59                         });
60                     }
61                     return; // one refresh is good enough
62
}
63             }
64         }
65     }
66     
67     private boolean needsRefiltering(IProject project) {
68         try {
69             Object JavaDoc element= project;
70             if (project.hasNature(JavaCore.NATURE_ID)) {
71                 element= JavaCore.create(project);
72             }
73             boolean inView= fViewer.testFindItem(element) != null;
74             boolean afterFilter= !fViewer.isFiltered(element, fViewer.getInput());
75             
76             return inView != afterFilter;
77         } catch (CoreException e) {
78             return true;
79         }
80     }
81 }
82
Popular Tags