KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.operations;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.mapping.ResourceMapping;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.team.internal.ccvs.core.*;
25 import org.eclipse.team.internal.ccvs.core.client.*;
26 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
27 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
28 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
29 import org.eclipse.team.internal.ccvs.ui.Policy;
30 import org.eclipse.team.internal.ccvs.ui.actions.TagAction;
31 import org.eclipse.team.internal.ccvs.ui.tags.TagSource;
32 import org.eclipse.ui.IWorkbenchPart;
33
34 public class TagOperation extends RepositoryProviderOperation implements ITagOperation {
35
36     private Set JavaDoc localOptions = new HashSet JavaDoc();
37     private CVSTag tag;
38
39     public TagOperation(IWorkbenchPart part, ResourceMapping[] mappers) {
40         super(part, mappers);
41     }
42
43     public CVSTag getTag() {
44         return tag;
45     }
46
47     public void setTag(CVSTag tag) {
48         this.tag = tag;
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#execute(org.eclipse.team.internal.ccvs.core.CVSTeamProvider, org.eclipse.core.resources.IResource[], org.eclipse.core.runtime.IProgressMonitor)
53      */

54     protected void execute(CVSTeamProvider provider, IResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
55         IStatus status = tag(provider, resources, recurse, monitor);
56         collectStatus(status);
57     }
58
59     /**
60      * Override to dislay the number of tag operations that succeeded
61      */

62     protected String JavaDoc getErrorMessage(IStatus[] problems, int operationCount) {
63         // We accumulated 1 status per resource above.
64
if(operationCount == 1) {
65             return CVSUIMessages.TagAction_tagProblemsMessage;
66         } else {
67             return NLS.bind(CVSUIMessages.TagAction_tagProblemsMessageMultiple, new String JavaDoc[] { Integer.toString(operationCount - problems.length), Integer.toString(problems.length) });
68         }
69     }
70     
71     /**
72      * Tag the resources in the CVS repository with the given tag.
73      *
74      * The returned IStatus will be a status containing any errors or warnings.
75      * If the returned IStatus is a multi-status, the code indicates the severity.
76      * Possible codes are:
77      * CVSStatus.OK - Nothing to report
78      * CVSStatus.SERVER_ERROR - The server reported an error
79      * any other code - warning messages received from the server
80      * @param recurse
81      */

82     public IStatus tag(CVSTeamProvider provider, IResource[] resources, boolean recurse, IProgressMonitor progress) throws CVSException {
83                         
84         LocalOption[] commandOptions = (LocalOption[])localOptions.toArray(new LocalOption[localOptions.size()]);
85         if (recurse) {
86             commandOptions = Command.DO_NOT_RECURSE.removeFrom(commandOptions);
87         } else {
88             commandOptions = Command.RECURSE.removeFrom(commandOptions);
89             commandOptions = Command.DO_NOT_RECURSE.addTo(commandOptions);
90         }
91                 
92         // Build the arguments list
93
String JavaDoc[] arguments = getStringArguments(resources);
94
95         // Execute the command
96
CVSWorkspaceRoot root = provider.getCVSWorkspaceRoot();
97         Session s = new Session(root.getRemoteLocation(), root.getLocalRoot());
98         progress.beginTask(null, 100);
99         try {
100             // Opening the session takes 20% of the time
101
s.open(Policy.subMonitorFor(progress, 20), true /* open for modification */);
102             return Command.TAG.execute(s,
103                 Command.NO_GLOBAL_OPTIONS,
104                 commandOptions,
105                 tag,
106                 arguments,
107                 null,
108                 Policy.subMonitorFor(progress, 80));
109         } finally {
110             s.close();
111             progress.done();
112         }
113     }
114     
115     public void addLocalOption(LocalOption option) {
116         localOptions.add(option);
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.team.internal.ccvs.ui.operations.ITagOperation#moveTag()
121      */

122     public void moveTag() {
123         addLocalOption(Tag.FORCE_REASSIGNMENT);
124     }
125     
126     /* (non-Javadoc)
127      * @see org.eclipse.team.internal.ccvs.ui.operations.ITagOperation#recurse()
128      */

129     public void doNotRecurse() {
130         addLocalOption(Command.DO_NOT_RECURSE);
131     }
132
133     protected String JavaDoc getTaskName() {
134         return CVSUIMessages.TagFromWorkspace_taskName;
135     }
136     
137     /* (non-Javadoc)
138      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#getTaskName(org.eclipse.team.internal.ccvs.core.CVSTeamProvider)
139      */

140     protected String JavaDoc getTaskName(CVSTeamProvider provider) {
141         return NLS.bind(CVSUIMessages.TagOperation_0, new String JavaDoc[] { provider.getProject().getName() });
142     }
143     
144     /* (non-Javadoc)
145      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
146      */

147     public void execute(IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
148         super.execute(monitor);
149         if (!errorsOccurred()) {
150             try {
151                 TagAction.broadcastTagChange(getCVSResources(), getTag());
152             } catch (InvocationTargetException JavaDoc e) {
153                 throw CVSException.wrapException(e);
154             }
155         }
156     }
157
158     private ICVSResource[] getCVSResources() {
159         IResource[] resources = getTraversalRoots();
160         ICVSResource[] cvsResources = new ICVSResource[resources.length];
161         for (int i = 0; i < resources.length; i++) {
162             cvsResources[i] = CVSWorkspaceRoot.getCVSResourceFor(resources[i]);
163         }
164         return cvsResources;
165     }
166
167     /* (non-Javadoc)
168      * @see org.eclipse.team.internal.ccvs.ui.operations.ITagOperation#getTagSource()
169      */

170     public TagSource getTagSource() {
171        return TagSource.create(getProjects());
172     }
173
174     private IProject[] getProjects() {
175         ResourceMapping[] mappings = getSelectedMappings();
176         Set JavaDoc projects = new HashSet JavaDoc();
177         for (int i = 0; i < mappings.length; i++) {
178             ResourceMapping mapping = mappings[i];
179             projects.addAll(Arrays.asList(mapping.getProjects()));
180         }
181         return (IProject[]) projects.toArray(new IProject[projects.size()]);
182     }
183
184     protected boolean isReportableError(IStatus status) {
185         return super.isReportableError(status)
186             || status.getCode() == CVSStatus.TAG_ALREADY_EXISTS;
187     }
188
189     /* (non-Javadoc)
190      * @see org.eclipse.team.internal.ccvs.ui.operations.ITagOperation#isEmpty()
191      */

192     public boolean isEmpty() {
193         return getSelectedMappings().length == 0;
194     }
195 }
196
Popular Tags