PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.team.cvs.ui.source_3.3.400.I20110510-0800/org/eclipse/team/internal/ccvs/ui/CVSProjectSetSerializer.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 220 lines | 180 code | 12 blank | 28 comment | 36 complexity | 51ff8b32a01bd1571ee0017a1b5cd427 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2005 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;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.StringTokenizer;
  16. import org.eclipse.core.resources.IProject;
  17. import org.eclipse.core.resources.ResourcesPlugin;
  18. import org.eclipse.core.runtime.IProgressMonitor;
  19. import org.eclipse.core.runtime.SubProgressMonitor;
  20. import org.eclipse.jface.dialogs.IDialogConstants;
  21. import org.eclipse.jface.dialogs.MessageDialog;
  22. import org.eclipse.osgi.util.NLS;
  23. import org.eclipse.swt.widgets.Shell;
  24. import org.eclipse.team.core.IProjectSetSerializer;
  25. import org.eclipse.team.core.RepositoryProvider;
  26. import org.eclipse.team.core.TeamException;
  27. import org.eclipse.team.internal.ccvs.core.CVSException;
  28. import org.eclipse.team.internal.ccvs.core.CVSTag;
  29. import org.eclipse.team.internal.ccvs.core.CVSTeamProvider;
  30. import org.eclipse.team.internal.ccvs.core.ICVSFolder;
  31. import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder;
  32. import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
  33. import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
  34. import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
  35. import org.eclipse.team.internal.ccvs.core.resources.RemoteFolder;
  36. import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
  37. import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
  38. import org.eclipse.team.internal.ccvs.ui.operations.CheckoutSingleProjectOperation;
  39. import org.eclipse.ui.actions.WorkspaceModifyOperation;
  40. public class CVSProjectSetSerializer implements IProjectSetSerializer {
  41. /**
  42. * @see IProjectSetSerializer#asReference(IProject[])
  43. *
  44. * "1.0,repoLocation,module,projectName[,tag]"
  45. */
  46. public String[] asReference(IProject[] providerProjects, Object context, IProgressMonitor monitor) throws TeamException {
  47. String[] result = new String[providerProjects.length];
  48. for (int i = 0; i < providerProjects.length; i++) {
  49. StringBuffer buffer = new StringBuffer();
  50. buffer.append("1.0,"); //$NON-NLS-1$
  51. IProject project = providerProjects[i];
  52. CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(project);
  53. CVSWorkspaceRoot root = provider.getCVSWorkspaceRoot();
  54. CVSRepositoryLocation location = CVSRepositoryLocation.fromString(root.getRemoteLocation().getLocation(false));
  55. location.setUserMuteable(true);
  56. String repoLocation = location.getLocation(false);
  57. buffer.append(repoLocation);
  58. buffer.append(","); //$NON-NLS-1$
  59. ICVSFolder folder = root.getLocalRoot();
  60. FolderSyncInfo syncInfo = folder.getFolderSyncInfo();
  61. String module = syncInfo.getRepository();
  62. buffer.append(module);
  63. buffer.append(","); //$NON-NLS-1$
  64. String projectName = folder.getName();
  65. buffer.append(projectName);
  66. CVSTag tag = syncInfo.getTag();
  67. if (tag != null) {
  68. if (tag.getType() != CVSTag.DATE) {
  69. buffer.append(","); //$NON-NLS-1$
  70. String tagName = tag.getName();
  71. buffer.append(tagName);
  72. }
  73. }
  74. result[i] = buffer.toString();
  75. }
  76. return result;
  77. }
  78. /**
  79. * @see IProjectSetSerializer#addToWorkspace(String[])
  80. */
  81. public IProject[] addToWorkspace(String[] referenceStrings, String filename, Object context, IProgressMonitor monitor) throws TeamException {
  82. final int size = referenceStrings.length;
  83. final IProject[] projects = new IProject[size];
  84. final ICVSRepositoryLocation[] locations = new ICVSRepositoryLocation[size];
  85. final String[] modules = new String[size];
  86. final CVSTag[] tags = new CVSTag[size];
  87. for (int i = 0; i < size; i++) {
  88. StringTokenizer tokenizer = new StringTokenizer(referenceStrings[i], ","); //$NON-NLS-1$
  89. String version = tokenizer.nextToken();
  90. if (!version.equals("1.0")) { //$NON-NLS-1$
  91. // Bail out, this is a newer version
  92. return null;
  93. }
  94. String repo = tokenizer.nextToken();
  95. locations[i] = getLocationFromString(repo);
  96. modules[i] = tokenizer.nextToken();
  97. String projectName = tokenizer.nextToken();
  98. projects[i] = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
  99. if (tokenizer.hasMoreTokens()) {
  100. String tagName = tokenizer.nextToken();
  101. tags[i] = new CVSTag(tagName, CVSTag.BRANCH);
  102. }
  103. }
  104. // Check if any projects will be overwritten, and warn the user.
  105. boolean yesToAll = false;
  106. int action;
  107. final int[] num = new int[] {size};
  108. for (int i = 0; i < size; i++) {
  109. Shell shell = null;
  110. IProject project = projects[i];
  111. if (project.exists()) {
  112. if (shell == null) {
  113. if (context instanceof Shell) {
  114. shell = (Shell)context;
  115. } else {
  116. return null;
  117. }
  118. }
  119. action = confirmOverwrite(project, yesToAll, shell);
  120. yesToAll = action == 2;
  121. // message dialog
  122. switch (action) {
  123. // no
  124. case 1:
  125. // Remove it from the set
  126. locations[i] = null;
  127. num[0]--;
  128. break;
  129. // yes to all
  130. case 2:
  131. // yes
  132. case 0:
  133. break;
  134. // cancel
  135. case 3:
  136. default:
  137. return null;
  138. }
  139. }
  140. }
  141. WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
  142. public void execute(IProgressMonitor monitor) throws InterruptedException, InvocationTargetException {
  143. monitor.beginTask("", 1000 * num[0]); //$NON-NLS-1$
  144. try {
  145. for (int i = 0; i < size; i++) {
  146. if (locations[i] != null) {
  147. ICVSRemoteFolder remote = new RemoteFolder(null, locations[i], modules[i], tags[i]);
  148. new CheckoutSingleProjectOperation(null /* no part */, remote, projects[i], null /* location */, true)
  149. .run(new SubProgressMonitor(monitor, 1000));
  150. }
  151. }
  152. } finally {
  153. monitor.done();
  154. }
  155. }
  156. };
  157. try {
  158. op.run(monitor);
  159. } catch (InterruptedException e) {
  160. } catch (InvocationTargetException e) {
  161. Throwable t = e.getTargetException();
  162. if (t instanceof TeamException) {
  163. throw (TeamException)t;
  164. }
  165. }
  166. List result = new ArrayList();
  167. for (int i = 0; i < projects.length; i++) {
  168. if (projects[i] != null) result.add(projects[i]);
  169. }
  170. return (IProject[])result.toArray(new IProject[result.size()]);
  171. }
  172. private ICVSRepositoryLocation getLocationFromString(String repo) throws CVSException {
  173. // create the new location
  174. ICVSRepositoryLocation newLocation = CVSRepositoryLocation.fromString(repo);
  175. if (newLocation.getUsername() == null || newLocation.getUsername().length() == 0) {
  176. // look for an existing location that matched
  177. ICVSRepositoryLocation[] locations = KnownRepositories.getInstance().getRepositories();
  178. for (int i = 0; i < locations.length; i++) {
  179. ICVSRepositoryLocation location = locations[i];
  180. if (location.getMethod() == newLocation.getMethod()
  181. && location.getHost().equals(newLocation.getHost())
  182. && location.getPort() == newLocation.getPort()
  183. && location.getRootDirectory().equals(newLocation.getRootDirectory()))
  184. return location;
  185. }
  186. }
  187. return newLocation;
  188. }
  189. private int confirmOverwrite(IProject project, boolean yesToAll, Shell shell) {
  190. if (yesToAll) return 2;
  191. if (!project.exists()) return 0;
  192. final MessageDialog dialog =
  193. 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, //
  194. new String[] {
  195. IDialogConstants.YES_LABEL,
  196. IDialogConstants.NO_LABEL,
  197. IDialogConstants.YES_TO_ALL_LABEL,
  198. IDialogConstants.CANCEL_LABEL},
  199. 0);
  200. final int[] result = new int[1];
  201. shell.getDisplay().syncExec(new Runnable() {
  202. public void run() {
  203. result[0] = dialog.open();
  204. }
  205. });
  206. return result[0];
  207. }
  208. }