KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > CVSProjectSetSerializer


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
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
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     /**
46      * @see IProjectSetSerializer#asReference(IProject[])
47      *
48      * "1.0,repoLocation,module,projectName[,tag]"
49      */

50     public String JavaDoc[] asReference(IProject[] providerProjects, Object JavaDoc context, IProgressMonitor monitor) throws TeamException {
51         String JavaDoc[] result = new String JavaDoc[providerProjects.length];
52         for (int i = 0; i < providerProjects.length; i++) {
53             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
54             buffer.append("1.0,"); //$NON-NLS-1$
55

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 JavaDoc repoLocation = location.getLocation(false);
62             buffer.append(repoLocation);
63             buffer.append(","); //$NON-NLS-1$
64

65             ICVSFolder folder = root.getLocalRoot();
66             FolderSyncInfo syncInfo = folder.getFolderSyncInfo();
67             String JavaDoc module = syncInfo.getRepository();
68             buffer.append(module);
69             buffer.append(","); //$NON-NLS-1$
70

71             String JavaDoc 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(","); //$NON-NLS-1$
77
String JavaDoc tagName = tag.getName();
78                     buffer.append(tagName);
79                 }
80             }
81             result[i] = buffer.toString();
82         }
83         return result;
84     }
85
86     /**
87      * @see IProjectSetSerializer#addToWorkspace(String[])
88      */

89     public IProject[] addToWorkspace(String JavaDoc[] referenceStrings, String JavaDoc filename, Object JavaDoc 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 JavaDoc[] modules = new String JavaDoc[size];
94         final CVSTag[] tags = new CVSTag[size];
95         
96         for (int i = 0; i < size; i++) {
97             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(referenceStrings[i], ","); //$NON-NLS-1$
98
String JavaDoc version = tokenizer.nextToken();
99             if (!version.equals("1.0")) { //$NON-NLS-1$
100
// Bail out, this is a newer version
101
return null;
102             }
103             String JavaDoc repo = tokenizer.nextToken();
104             locations[i] = getLocationFromString(repo);
105             modules[i] = tokenizer.nextToken();
106             String JavaDoc projectName = tokenizer.nextToken();
107             projects[i] = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
108             if (tokenizer.hasMoreTokens()) {
109                 String JavaDoc tagName = tokenizer.nextToken();
110                 tags[i] = new CVSTag(tagName, CVSTag.BRANCH);
111             }
112         }
113         // Check if any projects will be overwritten, and warn the user.
114
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                 // message dialog
132
switch (action) {
133                         // no
134
case 1:
135                             // Remove it from the set
136
locations[i] = null;
137                             num[0]--;
138                             break;
139                         // yes to all
140
case 2:
141                         // yes
142
case 0:
143                             break;
144                         // cancel
145
case 3:
146                         default:
147                             return null;
148                     }
149             }
150         }
151         WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
152             public void execute(IProgressMonitor monitor) throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
153                 monitor.beginTask("", 1000 * num[0]); //$NON-NLS-1$
154
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 /* no part */, remote, projects[i], null /* location */, 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 JavaDoc e) {
170         } catch (InvocationTargetException JavaDoc e) {
171             Throwable JavaDoc t = e.getTargetException();
172             if (t instanceof TeamException) {
173                 throw (TeamException)t;
174             }
175         }
176         List JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc repo) throws CVSException {
184         // create the new location
185
ICVSRepositoryLocation newLocation = CVSRepositoryLocation.fromString(repo);
186         if (newLocation.getUsername() == null || newLocation.getUsername().length() == 0) {
187             // look for an existing location that matched
188
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 JavaDoc[] { project.getName() }), MessageDialog.QUESTION, //
206
new String JavaDoc[] {
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 JavaDoc() {
214             public void run() {
215                 result[0] = dialog.open();
216             }
217         });
218         return result[0];
219     }
220 }
221
Popular Tags