KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > operations > ProjectMetaFileOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * Philippe Ombredanne - bug 84808
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.ui.operations;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.team.core.TeamException;
22 import org.eclipse.team.internal.ccvs.core.*;
23 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolder;
24 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
25 import org.eclipse.team.internal.ccvs.ui.Policy;
26 import org.eclipse.ui.IWorkbenchPart;
27
28 /**
29  * Operation which checks for the existence of the .project file
30  * in a remote folder, or to retrieve the project name for one or more
31  * folders based on what is in the .project file.
32  *
33  * To check for meta file exitence, the operation can be run
34  * by executing the operation and then checking <code>metaFileExists</code>
35  * Use the retrieveContent as false in the constructor, to avoid the
36  * overhead of retrieving the file content too.
37  *
38  * To update the folders with project names, the operation can be run
39  * by calling the static method <code>updateFoldersWithProjectName</code>
40  * or by executing the operation and then checking <code>getUpdatedFolders</code>
41  * to retrieve updated folders.
42  * Use the retrieveContent as true in the constructor to retrieve the content.
43  *
44  * The <code>metaFileExists</code> flag is always updated regardless of the
45  * retrieveContent constructor argument value
46  */

47 public class ProjectMetaFileOperation extends CVSOperation {
48
49     private ICVSRemoteFolder[] remoteFolders;
50     private boolean metaFileExists;
51     private boolean retrieveContent;
52     
53     /*
54      * Update a list of folders with their project names
55      * for those folders that have one.
56      */

57     public static ICVSRemoteFolder[] updateFoldersWithProjectName(IWorkbenchPart part, ICVSRemoteFolder[] folders)
58             throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
59         ProjectMetaFileOperation op = new ProjectMetaFileOperation(part, folders, true /*retrieve metafile content*/);
60         op.run();
61         return op.getUpdatedFolders();
62     }
63
64     public ProjectMetaFileOperation(IWorkbenchPart part, ICVSRemoteFolder[] remoteFolders, boolean retrieveContent) {
65         super(part);
66         this.remoteFolders = remoteFolders;
67         this.retrieveContent = retrieveContent;
68     }
69     
70     /*
71      * Update the folders with a project name if the provided remote folder contains a non empty project name
72      * in its meta-file (i.e. .project file)
73      * Set the metafile existence to true as needed
74      */

75     private void checkForMetafileAndUpdateFoldersWithRemoteProjectName(ICVSRemoteFolder[] folders, IProgressMonitor monitor) throws CVSException {
76         metaFileExists = false;
77         monitor.beginTask(null, folders.length*100);
78         for (int i = 0; i < folders.length; i++) {
79             // make a copy of the folder so that we will not effect the original
80
// folder when we refetch the members
81
// TODO: this is a strange thing to need to do. We should fix this.
82
ICVSRemoteFolder folder = (ICVSRemoteFolder) folders[i].forTag(folders[i].getTag());
83         
84             try {
85                 folder.members(Policy.subMonitorFor(monitor, 50));
86             } catch (TeamException e) {
87                 throw CVSException.wrapException(e);
88             }
89             // Check for the existance of the .project file
90
// and attempt to create an IProjectDescription of it
91
// and extract the project name
92
InputStream JavaDoc in = null;
93             try {
94                 ICVSRemoteFile remote = (ICVSRemoteFile) folder.getFile(".project"); //$NON-NLS-1$
95
//if we have gone so far, then a metafile exists.
96
metaFileExists = true;
97                 // retrieve the file content optionally, if requested
98
if (retrieveContent && folder instanceof RemoteFolder) {
99                     RemoteFolder rf = (RemoteFolder) folder;
100                     
101                     //load the project description from the retrieved metafile
102
in = remote.getContents(Policy.subMonitorFor(monitor, 50));
103                     if (in == null || monitor.isCanceled()) {
104                         break;
105                     }
106                     IWorkspace workspace = ResourcesPlugin.getWorkspace();
107                     IProjectDescription projectDesc = workspace.loadProjectDescription(in);
108                     
109                     //clone the remote folder into a remote project folder
110
//set the project name
111
RemoteProjectFolder rpf = new RemoteProjectFolder(rf, projectDesc.getName());
112                     // ... and update back our folder
113
folders[i] = rpf;
114                 }
115             } catch (TeamException e) {
116                 // We couldn't retrieve the project meta file so assume it doesn't
117
// exist
118
} catch (CoreException e) {
119                 // We couldn't read the project description, so assume the
120
// metafile is not a metafile, or is incorrect
121
// which is as if it does not exist
122
} finally {
123                 try {
124                     if (in != null) {
125                         in.close();
126                     }
127                 } catch (IOException JavaDoc e) {
128                     // ignore : we cannot read the file, so it's like it is not there
129
}
130             }
131         }
132         monitor.done();
133     }
134
135     
136     /* (non-Javadoc)
137      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
138      */

139     public void execute(IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
140         checkForMetafileAndUpdateFoldersWithRemoteProjectName(remoteFolders, monitor);
141     }
142     
143     /**
144      * Return true if the meta file exists remotely. This method should only be invoked
145      * after the operation has been executed;
146      * @return
147      */

148     public boolean metaFileExists() {
149         return metaFileExists;
150     }
151
152     /**
153      * @return the updated folders with project name from the remote project meta
154      * information if the .project file was properly retrieved or the
155      * unmodified folders if retrieval failed. This method should only be
156      * invoked after the operation has been executed;
157      */

158     public ICVSRemoteFolder[] getUpdatedFolders() {
159         return remoteFolders;
160     }
161
162     /* (non-Javadoc)
163      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
164      */

165     protected String JavaDoc getTaskName() {
166         return CVSUIMessages.ProjectMetaFile_taskName;
167     }
168     
169     /* (non-Javadoc)
170      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#canRunAsJob()
171      */

172     public boolean canRunAsJob() {
173         // This operation should never be run in the background.
174
return false;
175     }
176 }
177
Popular Tags