KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > ResourceDeltaVisitor


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso;
5
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.core.resources.IResource;
9 import org.eclipse.core.resources.IResourceDelta;
10 import org.eclipse.core.resources.IResourceDeltaVisitor;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.core.runtime.jobs.Job;
16
17 /**
18  * Invoked when a project resource change occurs. First test to see if
19  * the config file content changed and, if so, clear the config session
20  * information. Next check if a module has been compiled and, if so, inspect
21  * the module for terracotta artifacts.
22  *
23  * @see org.eclipse.core.resources.IResourceDeltaVisitor
24  * @see org.eclipse.ui.IWorkbench.addResourceChangeListener
25  * @see TcPlugin.ResourceListener
26  */

27
28 public class ResourceDeltaVisitor implements IResourceDeltaVisitor {
29   private static final boolean debug =
30     Boolean.getBoolean("ResourceDeltaVisitor.debug");
31   
32   public boolean visit(IResourceDelta delta) {
33     final TcPlugin plugin = TcPlugin.getDefault();
34     int kind = delta.getKind();
35     int flags = delta.getFlags();
36     IResource res = delta.getResource();
37     final IProject project = res.getProject();
38     
39     if(debug) {
40       dump(delta);
41     }
42     
43     if(!plugin.hasTerracottaNature(project)) {
44       return true;
45     }
46     
47     switch(kind) {
48       case IResourceDelta.CHANGED: {
49         if((flags & IResourceDelta.CONTENT) != 0) {
50           if(res instanceof IFile) {
51             IFile file = (IFile)res;
52             IPath path = file.getLocation();
53             String JavaDoc ext = path.getFileExtension();
54             
55             if(ext.equals("xml")) {
56               if((flags & IResourceDelta.MARKERS) != 0) {
57                 return false;
58               }
59               if(plugin.getConfigurationFile(project).equals(res) &&
60                  plugin.getConfigurationEditor(project) == null)
61               {
62                 try {
63                   new Job("Reloading DSO Configuration") {
64                     public IStatus run(IProgressMonitor monitor) {
65                       plugin.reloadConfiguration(project);
66                       return Status.OK_STATUS;
67                     }
68                   }.schedule();
69                 } catch(Exception JavaDoc e) {/**/}
70                 return false;
71               }
72             }
73           }
74         }
75         break;
76       }
77       case IResourceDelta.ADDED: {
78         if((flags & IResourceDelta.MOVED_FROM) != 0) {
79           if(res instanceof IFile) {
80             plugin.fileMoved((IFile)res, delta.getMovedFromPath());
81           }
82         }
83         else if(res instanceof IProject) {
84           IProject aProject = (IProject)res;
85           
86           if(plugin.getConfigurationFile(aProject) == null) {
87             plugin.staleProjectAdded(aProject);
88           }
89         }
90         break;
91       }
92       case IResourceDelta.REMOVED: {
93         if((flags & IResourceDelta.MOVED_TO) == 0) {
94           if(res instanceof IFile) {
95             plugin.fileRemoved((IFile)res);
96           }
97         }
98         break;
99       }
100     }
101     
102     return true;
103   }
104   
105   private void dump(IResourceDelta delta) {
106     int kind = delta.getKind();
107     int flags = delta.getFlags();
108     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
109     
110     sb.append(delta.getResource().getFullPath());
111     
112     switch(kind) {
113       case IResourceDelta.NO_CHANGE:
114         sb.append(" NO_CHANGE");
115         break;
116       case IResourceDelta.ADDED:
117         sb.append(" ADDED");
118         break;
119       case IResourceDelta.REMOVED:
120         sb.append(" REMOVED");
121         break;
122       case IResourceDelta.CHANGED:
123         sb.append(" CHANGED");
124         break;
125       case IResourceDelta.ADDED_PHANTOM:
126         sb.append(" ADDED_PHANTOM");
127         break;
128       case IResourceDelta.REMOVED_PHANTOM:
129         sb.append(" REMOVED_PHANTOM");
130         break;
131     }
132     
133     if((flags & IResourceDelta.CONTENT) != 0) {
134       sb.append(" CONTENT");
135     }
136     if((flags & IResourceDelta.MOVED_FROM) != 0) {
137       sb.append(" MOVED_FROM");
138     }
139     if((flags & IResourceDelta.MOVED_TO) != 0) {
140       sb.append(" MOVED_TO");
141     }
142     if((flags & IResourceDelta.OPEN) != 0) {
143       sb.append(" OPEN");
144     }
145     if((flags & IResourceDelta.TYPE) != 0) {
146       sb.append(" TYPE");
147     }
148     if((flags & IResourceDelta.SYNC) != 0) {
149       sb.append(" SYNC");
150     }
151     if((flags & IResourceDelta.MARKERS) != 0) {
152       sb.append(" MARKERS");
153     }
154     if((flags & IResourceDelta.REPLACED) != 0) {
155       sb.append(" REPLACED");
156     }
157     if((flags & IResourceDelta.DESCRIPTION) != 0) {
158       sb.append(" DESCRIPTION");
159     }
160     if((flags & IResourceDelta.ENCODING) != 0) {
161       sb.append(" ENCODING");
162     }
163     
164     System.out.println(sb.toString());
165   }
166 }
167
Popular Tags