1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 import java.util.StringTokenizer ; 17 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.SubProgressMonitor; 22 import org.eclipse.jface.dialogs.IDialogConstants; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.swt.widgets.Shell; 26 import org.eclipse.team.core.IProjectSetSerializer; 27 import org.eclipse.team.core.RepositoryProvider; 28 import org.eclipse.team.core.TeamException; 29 import org.eclipse.team.internal.ccvs.core.CVSException; 30 import org.eclipse.team.internal.ccvs.core.CVSTag; 31 import org.eclipse.team.internal.ccvs.core.CVSTeamProvider; 32 import org.eclipse.team.internal.ccvs.core.ICVSFolder; 33 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder; 34 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation; 35 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 36 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 37 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolder; 38 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; 39 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; 40 import org.eclipse.team.internal.ccvs.ui.operations.CheckoutSingleProjectOperation; 41 import org.eclipse.ui.actions.WorkspaceModifyOperation; 42 43 public class CVSProjectSetSerializer implements IProjectSetSerializer { 44 45 50 public String [] asReference(IProject[] providerProjects, Object context, IProgressMonitor monitor) throws TeamException { 51 String [] result = new String [providerProjects.length]; 52 for (int i = 0; i < providerProjects.length; i++) { 53 StringBuffer buffer = new StringBuffer (); 54 buffer.append("1.0,"); 56 IProject project = providerProjects[i]; 57 CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(project); 58 CVSWorkspaceRoot root = provider.getCVSWorkspaceRoot(); 59 CVSRepositoryLocation location = CVSRepositoryLocation.fromString(root.getRemoteLocation().getLocation(false)); 60 location.setUserMuteable(true); 61 String repoLocation = location.getLocation(false); 62 buffer.append(repoLocation); 63 buffer.append(","); 65 ICVSFolder folder = root.getLocalRoot(); 66 FolderSyncInfo syncInfo = folder.getFolderSyncInfo(); 67 String module = syncInfo.getRepository(); 68 buffer.append(module); 69 buffer.append(","); 71 String projectName = folder.getName(); 72 buffer.append(projectName); 73 CVSTag tag = syncInfo.getTag(); 74 if (tag != null) { 75 if (tag.getType() != CVSTag.DATE) { 76 buffer.append(","); String tagName = tag.getName(); 78 buffer.append(tagName); 79 } 80 } 81 result[i] = buffer.toString(); 82 } 83 return result; 84 } 85 86 89 public IProject[] addToWorkspace(String [] referenceStrings, String filename, Object context, IProgressMonitor monitor) throws TeamException { 90 final int size = referenceStrings.length; 91 final IProject[] projects = new IProject[size]; 92 final ICVSRepositoryLocation[] locations = new ICVSRepositoryLocation[size]; 93 final String [] modules = new String [size]; 94 final CVSTag[] tags = new CVSTag[size]; 95 96 for (int i = 0; i < size; i++) { 97 StringTokenizer tokenizer = new StringTokenizer (referenceStrings[i], ","); String version = tokenizer.nextToken(); 99 if (!version.equals("1.0")) { return null; 102 } 103 String repo = tokenizer.nextToken(); 104 locations[i] = getLocationFromString(repo); 105 modules[i] = tokenizer.nextToken(); 106 String projectName = tokenizer.nextToken(); 107 projects[i] = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); 108 if (tokenizer.hasMoreTokens()) { 109 String tagName = tokenizer.nextToken(); 110 tags[i] = new CVSTag(tagName, CVSTag.BRANCH); 111 } 112 } 113 boolean yesToAll = false; 115 int action; 116 final int[] num = new int[] {size}; 117 for (int i = 0; i < size; i++) { 118 Shell shell = null; 119 IProject project = projects[i]; 120 if (project.exists()) { 121 if (shell == null) { 122 if (context instanceof Shell) { 123 shell = (Shell)context; 124 } else { 125 return null; 126 } 127 } 128 action = confirmOverwrite(project, yesToAll, shell); 129 yesToAll = action == 2; 130 131 switch (action) { 133 case 1: 135 locations[i] = null; 137 num[0]--; 138 break; 139 case 2: 141 case 0: 143 break; 144 case 3: 146 default: 147 return null; 148 } 149 } 150 } 151 WorkspaceModifyOperation op = new WorkspaceModifyOperation() { 152 public void execute(IProgressMonitor monitor) throws InterruptedException , InvocationTargetException { 153 monitor.beginTask("", 1000 * num[0]); try { 155 for (int i = 0; i < size; i++) { 156 if (locations[i] != null) { 157 ICVSRemoteFolder remote = new RemoteFolder(null, locations[i], modules[i], tags[i]); 158 new CheckoutSingleProjectOperation(null , remote, projects[i], null , true) 159 .run(new SubProgressMonitor(monitor, 1000)); 160 } 161 } 162 } finally { 163 monitor.done(); 164 } 165 } 166 }; 167 try { 168 op.run(monitor); 169 } catch (InterruptedException e) { 170 } catch (InvocationTargetException e) { 171 Throwable t = e.getTargetException(); 172 if (t instanceof TeamException) { 173 throw (TeamException)t; 174 } 175 } 176 List result = new ArrayList (); 177 for (int i = 0; i < projects.length; i++) { 178 if (projects[i] != null) result.add(projects[i]); 179 } 180 return (IProject[])result.toArray(new IProject[result.size()]); 181 } 182 183 private ICVSRepositoryLocation getLocationFromString(String repo) throws CVSException { 184 ICVSRepositoryLocation newLocation = CVSRepositoryLocation.fromString(repo); 186 if (newLocation.getUsername() == null || newLocation.getUsername().length() == 0) { 187 ICVSRepositoryLocation[] locations = KnownRepositories.getInstance().getRepositories(); 189 for (int i = 0; i < locations.length; i++) { 190 ICVSRepositoryLocation location = locations[i]; 191 if (location.getMethod() == newLocation.getMethod() 192 && location.getHost().equals(newLocation.getHost()) 193 && location.getPort() == newLocation.getPort() 194 && location.getRootDirectory().equals(newLocation.getRootDirectory())) 195 return location; 196 } 197 } 198 return newLocation; 199 } 200 201 private int confirmOverwrite(IProject project, boolean yesToAll, Shell shell) { 202 if (yesToAll) return 2; 203 if (!project.exists()) return 0; 204 final MessageDialog dialog = 205 new MessageDialog(shell, CVSUIMessages.CVSProjectSetSerializer_Confirm_Overwrite_Project_8, null, NLS.bind(CVSUIMessages.CVSProjectSetSerializer_The_project__0__already_exists__Do_you_wish_to_overwrite_it__9, new String [] { project.getName() }), MessageDialog.QUESTION, new String [] { 207 IDialogConstants.YES_LABEL, 208 IDialogConstants.NO_LABEL, 209 IDialogConstants.YES_TO_ALL_LABEL, 210 IDialogConstants.CANCEL_LABEL}, 211 0); 212 final int[] result = new int[1]; 213 shell.getDisplay().syncExec(new Runnable () { 214 public void run() { 215 result[0] = dialog.open(); 216 } 217 }); 218 return result[0]; 219 } 220 } 221 | Popular Tags |