|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2004 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.ui.operations; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.io.InputStream; |
| 15 |
import java.lang.reflect.InvocationTargetException; |
| 16 |
|
| 17 |
import org.eclipse.core.resources.IProjectDescription; |
| 18 |
import org.eclipse.core.resources.IWorkspace; |
| 19 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 20 |
import org.eclipse.core.runtime.CoreException; |
| 21 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 22 |
import org.eclipse.team.core.TeamException; |
| 23 |
import org.eclipse.team.internal.ccvs.core.CVSException; |
| 24 |
import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile; |
| 25 |
import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder; |
| 26 |
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages; |
| 27 |
import org.eclipse.ui.IWorkbenchPart; |
| 28 |
|
| 29 |
/** |
| 30 |
* Operation which checks for the existence of the .project file |
| 31 |
* in a remote folder, or to retrieve the project name for one or more |
| 32 |
* folders based on what is in the .project file. |
| 33 |
* |
| 34 |
* To check for meta file exitence, the operation can be run |
| 35 |
* by executing the operation and then checking <code>metaFileExists</code> |
| 36 |
* Use the retrieveContent as false in the constructor, to avoid the |
| 37 |
* overhead of retrieving the file content too. |
| 38 |
* |
| 39 |
* To update the folders with project names, the operation can be run |
| 40 |
* by calling the static method <code>updateFoldersWithProjectName</code> |
| 41 |
* or by executing the operation and then checking <code>getUpdatedFolders</code> |
| 42 |
* to retrieve updated folders. |
| 43 |
* Use the retrieveContent as true in the constructor to retrive the content. |
| 44 |
* |
| 45 |
* The <code>metaFileExists</code> flag is always updated regardless of the |
| 46 |
* retrieveContent constructor argument value |
| 47 |
*/ |
| 48 |
public class ProjectMetaFileOperation extends CVSOperation { |
| 49 |
|
| 50 |
private ICVSRemoteFolder[] remoteFolders; |
| 51 |
private boolean metaFileExists; |
| 52 |
private boolean retrieveContent; |
| 53 |
|
| 54 |
/* |
| 55 |
* Update a list of folders with their project names |
| 56 |
* for those folders that have one. |
| 57 |
*/ |
| 58 |
public static ICVSRemoteFolder[] updateFoldersWithProjectName(IWorkbenchPart part, ICVSRemoteFolder[] folders) |
| 59 |
throws InvocationTargetException, InterruptedException { |
| 60 |
ProjectMetaFileOperation op = new ProjectMetaFileOperation(part, folders, true /*retrieve metafile content*/); |
| 61 |
op.run(); |
| 62 |
return op.getUpdatedFolders(); |
| 63 |
} |
| 64 |
|
| 65 |
public ProjectMetaFileOperation(IWorkbenchPart part, ICVSRemoteFolder[] remoteFolders, boolean retrieveContent) { |
| 66 |
super(part); |
| 67 |
this.remoteFolders = remoteFolders; |
| 68 |
this.retrieveContent = retrieveContent; |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* Update the folders with a project name if the provided remote folder contains a non empty project name |
| 73 |
* in its meta-file (i.e. .project file) |
| 74 |
* Set the metafile existence to true as needed |
| 75 |
*/ |
| 76 |
private void checkForMetafileAndUpdateFoldersWithRemoteProjectName(ICVSRemoteFolder[] folders, IProgressMonitor monitor) throws CVSException { |
| 77 |
metaFileExists = false; |
| 78 |
for (int i = 0; i < folders.length; i++) { |
| 79 |
|
| 80 |
// make a copy of the folder so that we will not effect the original |
| 81 |
// folder when we refetch the members |
| 82 |
// TODO: this is a strange thing to need to do. We should fix this. |
| 83 |
ICVSRemoteFolder folder = (ICVSRemoteFolder) folders[i].forTag(folders[i].getTag()); |
| 84 |
|
| 85 |
try { |
| 86 |
folder.members(monitor); |
| 87 |
} catch (TeamException e) { |
| 88 |
throw CVSException.wrapException(e); |
| 89 |
} |
| 90 |
// Check for the existance of the .project file |
| 91 |
// and attempt to create an IProjectDescription of it |
| 92 |
// and extract the project name |
| 93 |
InputStream in = null; |
| 94 |
try { |
| 95 |
ICVSRemoteFile remote = (ICVSRemoteFile) folder.getFile(".project"); //$NON-NLS-1$ |
| 96 |
//if we have gone so far, then a metafile exists. |
| 97 |
metaFileExists = true; |
| 98 |
// retrieve the file content optionally, if requested |
| 99 |
if (retrieveContent) { |
| 100 |
in = remote.getContents(monitor); |
| 101 |
if (in == null || monitor.isCanceled()) { |
| 102 |
break; |
| 103 |
} |
| 104 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 105 |
IProjectDescription projectDesc = workspace.loadProjectDescription(in); |
| 106 |
folder.setProjectName(projectDesc.getName()); |
| 107 |
// let's update back our folder |
| 108 |
folders[i] = folder; |
| 109 |
} |
| 110 |
} catch (TeamException e) { |
| 111 |
// We couldn't retrieve the project meta file so assume it doesn't |
| 112 |
// exist |
| 113 |
} catch (CoreException e) { |
| 114 |
// We couldn't read the project description, so assume the |
| 115 |
// metafile is not a metafile, or is incorrect |
| 116 |
// which is as if it does not exist |
| 117 |
} finally { |
| 118 |
try { |
| 119 |
if (in != null) { |
| 120 |
in.close(); |
| 121 |
} |
| 122 |
} catch (IOException e) { |
| 123 |
// ignore : we cannot read the file, so it's like it is not there |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/* (non-Javadoc) |
| 131 |
* @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#execute(org.eclipse.core.runtime.IProgressMonitor) |
| 132 |
*/ |
| 133 |
public void execute(IProgressMonitor monitor) throws CVSException, InterruptedException { |
| 134 |
checkForMetafileAndUpdateFoldersWithRemoteProjectName(remoteFolders, monitor); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Return true if the meta file exists remotely. This method should only be invoked |
| 139 |
* after the operation has been executed; |
| 140 |
* @return |
| 141 |
*/ |
| 142 |
public boolean metaFileExists() { |
| 143 |
return metaFileExists; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @return the updated folders with project name from the remote project meta |
| 148 |
* information if the .project file was properly retrieved or the |
| 149 |
* unmodified folders if retrieval failed. This method should only be |
| 150 |
* invoked after the operation has been executed; |
| 151 |
*/ |
| 152 |
public ICVSRemoteFolder[] getUpdatedFolders() { |
| 153 |
return remoteFolders; |
| 154 |
} |
| 155 |
|
| 156 |
/* (non-Javadoc) |
| 157 |
* @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName() |
| 158 |
*/ |
| 159 |
protected String getTaskName() { |
| 160 |
return CVSUIMessages.ProjectMetaFile_taskName; //$NON-NLS-1$ |
| 161 |
} |
| 162 |
|
| 163 |
/* (non-Javadoc) |
| 164 |
* @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#canRunAsJob() |
| 165 |
*/ |
| 166 |
public boolean canRunAsJob() { |
| 167 |
// This operation should never be run in the background. |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
} |