~vil/pydev/upstream

« back to all changes in this revision

Viewing changes to org.python.pydev/src/org/python/pydev/builder/PydevInternalResourceDeltaVisitor.java

  • Committer: Vladimír Lapáček
  • Date: 2006-08-30 18:38:44 UTC
  • Revision ID: vladimir.lapacek@gmail.com-20060830183844-f4d82c1239a7770a
Initial import of upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 11/09/2005
 
3
 */
 
4
package org.python.pydev.builder;
 
5
 
 
6
import org.eclipse.core.resources.IProject;
 
7
import org.eclipse.core.resources.IResource;
 
8
import org.eclipse.core.resources.IResourceDelta;
 
9
import org.eclipse.core.resources.IResourceDeltaVisitor;
 
10
import org.eclipse.core.runtime.CoreException;
 
11
import org.eclipse.core.runtime.IProgressMonitor;
 
12
import org.python.pydev.builder.pycremover.PycRemoverBuilderVisitor;
 
13
import org.python.pydev.core.REF;
 
14
import org.python.pydev.core.log.Log;
 
15
import org.python.pydev.editor.codecompletion.revisited.PythonPathHelper;
 
16
import org.python.pydev.plugin.nature.PythonNature;
 
17
 
 
18
public abstract class PydevInternalResourceDeltaVisitor extends PyDevBuilderVisitor implements IResourceDeltaVisitor{
 
19
    
 
20
    PydevInternalResourceDeltaVisitor(IProgressMonitor monitor, int totalResources){
 
21
        this.monitor = monitor;
 
22
        this.totalResources = totalResources;
 
23
    }
 
24
    
 
25
    //variables used to communicate the progress
 
26
    /**
 
27
     * this monitor might be set externally so that we can comunicate the progress to the user
 
28
     * (set externally)
 
29
     */
 
30
    public IProgressMonitor monitor;
 
31
    /**
 
32
     * number of total resources to be visited (only used when the monitor is set)
 
33
     * (set externally)
 
34
     */
 
35
    public int totalResources;
 
36
    /**
 
37
     * number of resources visited to the moment 
 
38
     * (updated in this class)
 
39
     */
 
40
    public int currentResourcesVisited = 0;
 
41
    //end variables used to communicate the progress
 
42
 
 
43
    /**
 
44
     * Visits the resource delta tree determining which files to rebuild (*.py).
 
45
     * 
 
46
     * Subclasses should only reimplement visitChanged, visitAdded and visitRemoved. This method will not be called 
 
47
     * in the structure provided by pydev.
 
48
     * 
 
49
     * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
 
50
     */
 
51
    public boolean visit(IResourceDelta delta) throws CoreException {
 
52
        if(delta == null){
 
53
            return true;
 
54
        }
 
55
 
 
56
        IResource resource = delta.getResource();
 
57
 
 
58
        if(resource == null){
 
59
            return true;
 
60
        }
 
61
        
 
62
        int type = resource.getType();
 
63
    
 
64
 
 
65
        //related bug https://sourceforge.net/tracker/index.php?func=detail&aid=1238850&group_id=85796&atid=577329
 
66
        
 
67
        //the team-support plugins of eclipse use the IResource
 
68
        //method setTeamPrivateMember to indicate resources
 
69
        //that are only in the project for the team-stuff (e.g. .svn or
 
70
        //.cvs or _darcs directories).
 
71
        if (resource.isTeamPrivateMember()){
 
72
            return true;
 
73
        }
 
74
        
 
75
        if (type == IResource.FOLDER) {
 
76
            switch (delta.getKind()) {
 
77
                case IResourceDelta.REMOVED:
 
78
                    visitRemovedResource(resource, null, monitor);
 
79
                    break;
 
80
                //for folders, we don't have to do anything if added or changed (we just treat their children, that should
 
81
                //resolve for modules -- we do, however have to treat __init__.py differently).
 
82
            }
 
83
        }
 
84
        
 
85
        
 
86
        if (type == IResource.FILE) {
 
87
            String ext = resource.getFileExtension();
 
88
            if(ext == null){ //resource.getFileExtension() may return null if it has none.
 
89
                return true;
 
90
            }
 
91
            
 
92
            //only analyze projects with the python nature...
 
93
            IProject project = resource.getProject();
 
94
            PythonNature nature = PythonNature.getPythonNature(project);
 
95
            
 
96
            if(project != null && nature != null){
 
97
                //we just want to make the visit if it is a valid python file and it is in the pythonpath
 
98
                if (PythonPathHelper.isValidSourceFile("."+ext)) {
 
99
                    
 
100
                    boolean isAddOrChange = false;
 
101
                    isAddOrChange = chooseVisit(delta, resource, isAddOrChange);
 
102
 
 
103
                    if(isAddOrChange){
 
104
                        //communicate the progress
 
105
                        currentResourcesVisited++;
 
106
                        PyDevBuilder.communicateProgress(monitor, totalResources, currentResourcesVisited, resource, this);
 
107
                    }
 
108
                }else if(ext.equals("pyc")){
 
109
                    try {
 
110
                        //we do that because we may have an added .pyc without a correspondent .py file
 
111
                        new PycRemoverBuilderVisitor().visitAddedResource(resource, null, monitor); //doc is not used in pyc remover
 
112
                    } catch (Exception e) {
 
113
                        Log.log(e);
 
114
                    }
 
115
                }
 
116
 
 
117
            }
 
118
        }
 
119
        
 
120
        return true;
 
121
    }
 
122
    
 
123
        protected boolean chooseVisit(IResourceDelta delta, IResource resource, boolean isAddOrChange) {
 
124
                switch (delta.getKind()) {
 
125
                    case IResourceDelta.ADDED :
 
126
                        visitAddedResource(resource, REF.getDocFromResource(resource), monitor);
 
127
                        isAddOrChange = true;
 
128
                        break;
 
129
                    case IResourceDelta.CHANGED:
 
130
                        visitChangedResource(resource, REF.getDocFromResource(resource), monitor);
 
131
                        isAddOrChange = true;
 
132
                        break;
 
133
                    case IResourceDelta.REMOVED:
 
134
                        visitRemovedResource(resource, null, monitor);
 
135
                        break;
 
136
                }
 
137
                return isAddOrChange;
 
138
        }
 
139
 
 
140
}