b34f40afc3c1854d640969de4d8e63f071acb528
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / builder / ParserVisitor.java
1 package net.sourceforge.phpdt.internal.core.builder;
2
3 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
4 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
5 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
6
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.IResource;
10 import org.eclipse.core.resources.IResourceDelta;
11 import org.eclipse.core.resources.IResourceDeltaVisitor;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.OperationCanceledException;
15
16 /**
17  * 
18  * @see org.eclipse.core.resources.IResourceDelta
19  * @see org.eclipse.core.resources.IResourceDeltaVisitor
20  */
21 public class ParserVisitor implements IResourceDeltaVisitor {
22         final IProgressMonitor fMonitor;
23
24         final IProject fProject;
25
26         public ParserVisitor(IProject iProject, IProgressMonitor monitor) {
27                 fMonitor = monitor;
28                 fProject = iProject;
29         }
30
31         protected void checkCancel() {
32                 if (fMonitor.isCanceled()) {
33                         throw new OperationCanceledException();
34                 }
35         }
36
37         /**
38          * Visits the given resource delta.
39          * 
40          * @return <code>true</code> if the resource delta's children should be
41          *         visited; <code>false</code> if they should be skipped.
42          * @exception CoreException
43          *                if the visit fails for some reason.
44          */
45         public boolean visit(IResourceDelta delta) throws CoreException {
46
47                 IResource resource = delta.getResource();
48                 int resourceType = resource.getType();
49                 checkCancel();
50
51                 final IdentifierIndexManager indexManager = PHPeclipsePlugin
52                                 .getDefault().getIndexManager(fProject);
53
54                 switch (delta.getKind()) {
55                 case IResourceDelta.ADDED:
56                         if (resourceType == IResource.FILE) {
57                                 if ((resource.getFileExtension() != null)
58                                                 && PHPFileUtil.isPHPFile((IFile) resource)) {
59                                         fMonitor.worked(1);
60                                         fMonitor.subTask("Adding: " + resource.getFullPath());
61
62                                         // check for parsing errors
63                                         // PHPParserAction.parseFile((IFile) resource);
64                                         // update indexfile for the project:
65                                         indexManager.addFile((IFile) resource);
66                                 }
67                         }
68                         break;
69
70                 case IResourceDelta.CHANGED:
71                         if (resourceType == IResource.FILE) {
72                                 if ((resource.getFileExtension() != null)
73                                                 && PHPFileUtil.isPHPFile((IFile) resource)) {
74                                         fMonitor.worked(1);
75                                         fMonitor.subTask("Changing: " + resource.getFullPath());
76
77                                         // check for parsing errors
78                                         // PHPParserAction.parseFile((IFile) resource);
79                                         // update indexfile for the project:
80                                         indexManager.changeFile((IFile) resource);
81                                 }
82                         }
83                         break;
84
85                 case IResourceDelta.REMOVED:
86                         if (resourceType == IResource.FILE) {
87                                 if ((resource.getFileExtension() != null)
88                                                 && PHPFileUtil.isPHPFile((IFile) resource)) {
89                                         fMonitor.worked(1);
90                                         fMonitor.subTask("Removing: " + resource.getFullPath());
91
92                                         // update indexfile for the project:
93                                         indexManager.removeFile((IFile) resource);
94                                 }
95                         }
96                         break;
97                 }
98                 return true; // carry on
99         }
100
101 }