KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import org.eclipse.core.resources.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.team.core.Team;
17 import org.eclipse.team.internal.ccvs.core.*;
18 import org.eclipse.team.internal.ccvs.core.client.Session;
19 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
20 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
21 import org.eclipse.team.internal.ccvs.core.util.Util;
22
23
24 /**
25  * Represents handles to CVS resource on the local file system. Synchronization
26  * information is taken from the CVS subdirectories.
27  *
28  * @see LocalFolder
29  * @see LocalFile
30  */

31 abstract class EclipseResource implements ICVSResource, Comparable JavaDoc {
32
33      // The separator that must be used when creating CVS resource paths. Never use
34
// the platform default separator since it is not compatible with CVS resources.
35
protected static final String JavaDoc SEPARATOR = Session.SERVER_SEPARATOR;
36     protected static final String JavaDoc CURRENT_LOCAL_FOLDER = Session.CURRENT_LOCAL_FOLDER;
37         
38     /*
39      * The local resource represented by this handle
40      */

41     IResource resource;
42     
43     /*
44      * Creates a CVS handle to the provided resource
45      */

46     protected EclipseResource(IResource resource) {
47         Assert.isNotNull(resource);
48         this.resource = resource;
49     }
50     
51     /*
52      * Get the extention of the path of resource relative to the path of root
53      *
54      * @throws CVSException if root is not a root-folder of resource
55      */

56     public String JavaDoc getRelativePath(ICVSFolder root) throws CVSException {
57         try {
58             EclipseResource rootFolder;
59             String JavaDoc result;
60             rootFolder = (EclipseResource)root;
61             result = Util.getRelativePath(rootFolder.getPath(), getPath());
62             if (result.length() == 0) return CURRENT_LOCAL_FOLDER;
63             return result;
64         } catch (ClassCastException JavaDoc e) {
65             IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, CVSMessages.EclipseResource_invalidResourceClass, e, root);
66             throw new CVSException(status);
67         }
68     }
69
70     /*
71      * @see ICVSResource#exists()
72      */

73     public boolean exists() {
74         return resource.exists();
75     }
76
77     /*
78      * Returns the parent folder of this resource of <code>null</code> if resource
79      * the resource.
80      *
81      * @see ICVSResource#getParent()
82      */

83     public ICVSFolder getParent() {
84         IContainer parent = resource.getParent();
85         if (parent==null) {
86             return null;
87         }
88         return new EclipseFolder(parent);
89     }
90
91     /*
92      * @see ICVSResource#getName()
93      */

94     public String JavaDoc getName() {
95         return resource.getName();
96     }
97
98     /*
99      * @see ICVSResource#isIgnored()
100      */

101     public boolean isIgnored() throws CVSException {
102         // a managed resource is never ignored
103
if(isManaged() || resource.getType()==IResource.ROOT || resource.getType()==IResource.PROJECT) {
104             return false;
105         }
106         
107         // If the resource is a derived or linked resource, it is ignored
108
if (resource.isDerived() || resource.isLinked()) {
109             return true;
110         }
111         
112         // always ignore CVS
113
String JavaDoc name = getName();
114         if (name.equals("CVS")) return true; //$NON-NLS-1$
115

116         // check the global ignores from Team
117
if (Team.isIgnoredHint(resource)) return true;
118         
119         // check ignore patterns from the .cvsignore file.
120
if(EclipseSynchronizer.getInstance().isIgnored(resource)) {
121             return true;
122         }
123         
124         // check the parent, if the parent is ignored or mapped to CVSROOT/Emptydir
125
// then this resource is ignored also
126
ICVSFolder parent = getParent();
127         if(parent==null) return false;
128         if (parent.isIgnored()) return true;
129         FolderSyncInfo info = parent.getFolderSyncInfo();
130         if (info == null) return false;
131         return info.isVirtualDirectory();
132     }
133     
134     /*
135      * @see ICVSResource#setIgnoredAs(String)
136      */

137     public void setIgnoredAs(final String JavaDoc pattern) throws CVSException {
138         run(new ICVSRunnable() {
139             public void run(IProgressMonitor monitor) throws CVSException {
140                 EclipseSynchronizer.getInstance().addIgnored(resource.getParent(), pattern);
141             }
142         }, null);
143     }
144
145     /*
146      * @see ICVSResource#isManaged()
147      */

148     public boolean isManaged() throws CVSException {
149         return isManaged(getSyncBytes());
150     }
151     
152     /*
153      * Helper method that captures the sematics of isManaged given a ResourceSyncInfo
154      */

155     public boolean isManaged(byte[] syncBytes) {
156         return syncBytes != null;
157     }
158     
159     /**
160      * Two ManagedResources are equal, if there cvsResources are
161      * equal (and that is, if the point to the same file)
162      */

163     public boolean equals(Object JavaDoc obj) {
164         
165         if (!(obj instanceof EclipseResource)) {
166             return false;
167         } else {
168             return getPath().equals(((EclipseResource) obj).getPath());
169         }
170     }
171             
172     /*
173      * @see ICVSResource#getPath()
174      */

175     public String JavaDoc getPath() {
176         return resource.getFullPath().toString();
177     }
178     
179     /*
180      * @see ICVSResource#isFolder()
181      */

182     public boolean isFolder() {
183         return false;
184     }
185     
186     /*
187      * @see org.eclipse.team.internal.ccvs.core.ICVSFile#getSyncBytes()
188      */

189     public byte[] getSyncBytes() throws CVSException {
190         return EclipseSynchronizer.getInstance().getSyncBytes(getIResource());
191     }
192     
193     /*
194      * @see org.eclipse.team.internal.ccvs.core.ICVSFile#setSyncBytes(byte[])
195      */

196     public void setSyncBytes(byte[] syncBytes) throws CVSException {
197         if (getParent().isCVSFolder()) {
198             EclipseSynchronizer.getInstance().setSyncBytes(getIResource(), syncBytes);
199         }
200     }
201     
202     /*
203      * @see ICVSResource#getSyncInfo()
204      */

205     public ResourceSyncInfo getSyncInfo() throws CVSException {
206         return EclipseSynchronizer.getInstance().getResourceSync(resource);
207     }
208     
209     /*
210      * Implement the hashcode on the underlying strings, like it is done in the equals.
211      */

212     public int hashCode() {
213         return getPath().hashCode();
214     }
215     
216     /*
217      * Give the pathname back
218      */

219     public String JavaDoc toString() {
220         return getPath();
221     }
222     
223     /*
224      * @see ICVSResource#unmanage()
225      */

226     public void unmanage(IProgressMonitor monitor) throws CVSException {
227         EclipseSynchronizer.getInstance().deleteResourceSync(resource);
228     }
229     
230     /*
231      * @see Comparable#compareTo(Object)
232      */

233     public int compareTo(Object JavaDoc arg0) {
234         EclipseResource other = (EclipseResource)arg0;
235         return resource.getFullPath().toString().compareTo(other.resource.getFullPath().toString());
236     }
237
238     /**
239      * @see org.eclipse.team.internal.ccvs.core.ICVSResource#getIResource()
240      */

241     public IResource getIResource() {
242         return resource;
243     }
244
245     /**
246      * Called by a resource change listener when a resource is changed or added. This allows
247      * CVS resources to adjust any internal state based on the change.
248      *
249      * @param forAddition modification is an addition
250      * @throws CVSException
251      */

252     public abstract void handleModification(boolean forAddition) throws CVSException;
253     
254     public void run(final ICVSRunnable job, IProgressMonitor monitor) throws CVSException {
255         final CVSException[] error = new CVSException[1];
256         try {
257             // Do not use a scheduling rule in the workspace run since one
258
// will be obtained by the EclipseSynchronizer
259
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
260                 public void run(IProgressMonitor monitor) throws CoreException {
261                     try {
262                         EclipseSynchronizer.getInstance().run(getIResource(), job, monitor);
263                     } catch(CVSException e) {
264                         error[0] = e;
265                     }
266                 }
267             }, null /* no rule */, 0, monitor);
268         } catch(CoreException e) {
269             throw CVSException.wrapException(e);
270         }
271         if(error[0]!=null) {
272             throw error[0];
273         }
274     }
275 }
276
Popular Tags