KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > resources > FileModificationManager


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.team.internal.ccvs.core.resources;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.QualifiedName;
19 import org.eclipse.team.core.RepositoryProvider;
20 import org.eclipse.team.internal.ccvs.core.*;
21 import org.eclipse.team.internal.ccvs.core.util.ResourceStateChangeListeners;
22
23 /**
24  * This class performs several functions related to determining the modified
25  * status of files under CVS control. First, it listens for change delta's for
26  * files and brodcasts them to all listeners. It also registers as a save
27  * participant so that deltas generated before the plugin are loaded are not
28  * missed. Secondly, it listens for CVS resource state change events and uses
29  * these to properly mark files and folders as modified.
30  */

31 public class FileModificationManager implements IResourceChangeListener {
32     
33     private static final QualifiedName UPDATE_TIMESTAMP = new QualifiedName(CVSProviderPlugin.ID, "update-timestamp"); //$NON-NLS-1$
34

35     /* private */Set JavaDoc modifiedResources = new HashSet JavaDoc();
36
37     // consider the following changes types and ignore the others (e.g. marker and description changes are ignored)
38
protected int INTERESTING_CHANGES = IResourceDelta.CONTENT |
39                                                                     IResourceDelta.MOVED_FROM |
40                                                                     IResourceDelta.MOVED_TO |
41                                                                     IResourceDelta.OPEN |
42                                                                     IResourceDelta.REPLACED |
43                                                                     IResourceDelta.TYPE;
44
45     /**
46      * Listen for file modifications and fire modification state changes
47      *
48      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
49      */

50     public void resourceChanged(IResourceChangeEvent event) {
51         try {
52             event.getDelta().accept(new IResourceDeltaVisitor() {
53                 public boolean visit(IResourceDelta delta) {
54                     IResource resource = delta.getResource();
55                     
56                     if (resource.getType()==IResource.PROJECT) {
57                         IProject project = (IProject)resource;
58                         if (!project.isAccessible()) {
59                             return false;
60                         }
61                         if ((delta.getFlags() & IResourceDelta.OPEN) != 0) {
62                             return false;
63                         }
64                         if (RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId()) == null) {
65                             return false;
66                         }
67                     }
68                     
69                     if (resource.getType()==IResource.FILE && delta.getKind() == IResourceDelta.CHANGED && resource.exists()) {
70                         int flags = delta.getFlags();
71                         if((flags & INTERESTING_CHANGES) != 0) {
72                             resourceChanged(resource, false);
73                         }
74                     } else if (delta.getKind() == IResourceDelta.ADDED) {
75                         resourceChanged(resource, true);
76                     } else if (delta.getKind() == IResourceDelta.REMOVED) {
77                         try {
78                             EclipseSynchronizer.getInstance().handleDeleted(resource);
79                         } catch (CVSException e) {
80                             CVSProviderPlugin.log(e);
81                         }
82                         modifiedResources.add(resource);
83                     }
84
85                     return true;
86                 }
87             });
88             if (!modifiedResources.isEmpty()) {
89                 ResourceStateChangeListeners.getListener().resourceModified(
90                     (IResource[])modifiedResources.toArray(new IResource[modifiedResources.size()]));
91                 modifiedResources.clear();
92             }
93         } catch (CoreException e) {
94             CVSProviderPlugin.log(e);
95         }
96
97     }
98
99     /**
100      * Method updated flags the objetc as having been modfied by the updated
101      * handler. This flag is read during the resource delta to determine whether
102      * the modification made the file dirty or not.
103      *
104      * @param mFile
105      */

106     public void updated(ICVSFile mFile) {
107         try {
108             if (mFile instanceof EclipseFile) {
109                 IFile file = (IFile)mFile.getIResource();
110                 file.setSessionProperty(UPDATE_TIMESTAMP, new Long JavaDoc(file.getModificationStamp()));
111             }
112         } catch (CoreException e) {
113             CVSProviderPlugin.log(e);
114         }
115     }
116     
117     /*
118      * Handle added and changed resources by signaling the change to the corresponding
119      * CVS resource and recording the change for broadcast to interested listeners.
120      */

121     /* private */void resourceChanged(IResource resource, boolean addition) {
122         if (isCleanUpdate(resource)) return;
123         try {
124             EclipseResource cvsResource = (EclipseResource)CVSWorkspaceRoot.getCVSResourceFor(resource);
125             if (!cvsResource.isIgnored()) {
126                 cvsResource.handleModification(addition);
127                 modifiedResources.add(resource);
128             }
129             // see bug 170743
130
// ignored .cvsignore should always be clean and do not affect the path
131
if(cvsResource.getName().equals(".cvsignore") && cvsResource.isIgnored()){ //$NON-NLS-1$
132
EclipseSynchronizer.getInstance().setModified((EclipseFile) cvsResource, ICVSFile.CLEAN);
133                 modifiedResources.add(resource);
134             }
135         } catch (CVSException e) {
136             // Log the exception and continue
137
CVSProviderPlugin.log(e);
138         }
139     }
140
141     /**
142      * If the file was the result of a clean update, the cached timestamp will
143      * be removed.
144      *
145      * @param resource
146      * @return boolean
147      */

148     private boolean isCleanUpdate(IResource resource) {
149         if(resource.getType() != IResource.FILE) return false;
150         long modStamp = resource.getModificationStamp();
151         Long JavaDoc whenWeWrote;
152         try {
153             whenWeWrote = (Long JavaDoc)resource.getSessionProperty(UPDATE_TIMESTAMP);
154             resource.setSessionProperty(UPDATE_TIMESTAMP, null);
155         } catch(CoreException e) {
156             CVSProviderPlugin.log(e);
157             whenWeWrote = null;
158         }
159         return (whenWeWrote!=null && whenWeWrote.longValue() == modStamp);
160     }
161 }
162
163
Popular Tags