KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.viewers.AbstractTreeViewer;
17 import org.eclipse.jface.viewers.TreeViewer;
18 import org.eclipse.team.core.TeamException;
19 import org.eclipse.team.internal.ccvs.core.*;
20 import org.eclipse.team.internal.ccvs.core.client.listeners.LogEntry;
21 import org.eclipse.team.internal.ccvs.core.client.listeners.LogListener;
22 import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
23 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolderTree;
24 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
25 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
26 import org.eclipse.team.internal.ccvs.ui.model.RemoteContentProvider;
27 import org.eclipse.team.internal.ccvs.ui.operations.RemoteLogOperation.LogEntryCache;
28 import org.eclipse.team.internal.ccvs.ui.repo.RepositoriesView;
29 import org.eclipse.team.internal.ui.Utils;
30 import org.eclipse.ui.IWorkbenchPart;
31
32 public class FetchAllMembersOperation extends RemoteOperation {
33    
34     class RLogTreeBuilder {
35             
36             private ICVSRepositoryLocation location;
37             private RemoteFolderTree tree;
38             private CVSTag tag;
39
40             public RLogTreeBuilder(ICVSRepositoryLocation location, CVSTag tag) {
41                 this.tag = tag;
42                 this.location = location;
43                 reset();
44             }
45             
46             public RemoteFolderTree getTree() {
47                 return tree;
48             }
49            
50             /**
51              * Reset the builder to prepare for a new build
52              */

53             public void reset() {
54                 tree = new RemoteFolderTree(null, location, ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME, tag);
55                 tree.setChildren(new ICVSRemoteResource[0]);
56             }
57             
58             /* (non-Javadoc)
59              * @see org.eclipse.team.internal.ccvs.core.client.listeners.RDiffSummaryListener.IFileDiffListener#newFile(java.lang.String, java.lang.String)
60              */

61             public void newFile(IPath remoteFilePath, ICVSRemoteFile remoteFile) {
62                 try {
63                     addFile(tree,tag,remoteFile, remoteFilePath);
64                 } catch (CVSException e) {
65                     CVSUIPlugin.log(e);
66                 }
67             }
68             
69             private void addFile(RemoteFolderTree tree, CVSTag tag, ICVSRemoteFile file,IPath filePath) throws CVSException {
70                 RemoteFolderTree parent = (RemoteFolderTree)getFolder(tree, tag, filePath.removeLastSegments(1), Path.EMPTY);
71                 addChild(parent, file);
72             }
73             
74             private void addChild(RemoteFolderTree tree, ICVSRemoteResource resource) {
75                 ICVSRemoteResource[] children = tree.getChildren();
76                 ICVSRemoteResource[] newChildren;
77                 if (children == null) {
78                     newChildren = new ICVSRemoteResource[] { resource };
79                 } else {
80                     newChildren = new ICVSRemoteResource[children.length + 1];
81                     System.arraycopy(children, 0, newChildren, 0, children.length);
82                     newChildren[children.length] = resource;
83                 }
84                 tree.setChildren(newChildren);
85             }
86             
87             /*
88              * Get the folder at the given path in the given tree, creating any missing folders as needed.
89              */

90             private ICVSRemoteFolder getFolder(RemoteFolderTree tree, CVSTag tag, IPath remoteFolderPath, IPath parentPath) throws CVSException {
91                 if (remoteFolderPath.segmentCount() == 0) return tree;
92                 String JavaDoc name = remoteFolderPath.segment(0);
93                 ICVSResource child;
94                 IPath childPath = parentPath.append(name);
95                 if (tree.childExists(name)) {
96                     child = tree.getChild(name);
97                 } else {
98                     child = new RemoteFolderTree(tree, tree.getRepository(), childPath.toString(), tag);
99                     ((RemoteFolderTree)child).setChildren(new ICVSRemoteResource[0]);
100                     addChild(tree, (ICVSRemoteResource)child);
101                 }
102                 return getFolder((RemoteFolderTree)child, tag, remoteFolderPath.removeFirstSegments(1), childPath);
103             }
104     }
105     
106     static final String JavaDoc DEAD_STATE = "dead"; //$NON-NLS-1$
107
ICVSRepositoryLocation repoLocation;
108     
109     public FetchAllMembersOperation(IWorkbenchPart part, ICVSRemoteResource[] folders, ICVSRepositoryLocation repoLocation) {
110         super(part, folders);
111         this.repoLocation = repoLocation;
112     }
113
114     protected void execute(IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
115         //expand each folder selected in the tree
116
ICVSRemoteResource[] restest = getRemoteResources();
117             ICVSRemoteFolder testfolder = (ICVSRemoteFolder) restest[0];
118             CVSTag tag = testfolder.getTag();
119             if (tag == null)
120                 tag = CVSTag.DEFAULT;
121             LogEntryCache cache = new LogEntryCache();
122
123             RemoteLogOperation operation = new RemoteLogOperation(getPart(), getRemoteResources(), tag, null, cache);
124             try {
125                 operation.run(monitor);
126                 ICVSRemoteResource[] remoteRes = getRemoteResources();
127                 final ICVSRemoteFolder project = (ICVSRemoteFolder) remoteRes[0];
128                 //Get the entry paths
129
String JavaDoc[] entry = cache.getCachedFilePaths();
130                 //Strip repo + project info from entries
131
RLogTreeBuilder treeBuilder = new RLogTreeBuilder(project.getRepository(),tag);
132                 for (int i = 0; i < entry.length; i++) {
133                     ILogEntry[] logEntry = cache.getLogEntries(entry[i]);
134                     
135                     //might not have state if this a branch entry
136
if (logEntry[0].getState() != null &&
137                         logEntry[0].getState().equals(DEAD_STATE))
138                         continue;
139                     
140                     
141                     ICVSRemoteFile remoteFile = logEntry[0].getRemoteFile();
142                     //if the current folder tag is a branch tag, we need to take the extra step
143
//of making sure that the file's revision number has been set appropriately
144
if (tag.getType() == CVSTag.BRANCH &&
145                         remoteFile.getRevision().equals(LogListener.BRANCH_REVISION))
146                         verifyRevision(tag, logEntry[0], remoteFile);
147                     
148                     IPath logPath = new Path(null,remoteFile.getRepositoryRelativePath());
149                     if (logPath.segmentCount()>0)
150                         logPath = logPath.removeFirstSegments(1);
151                 
152                     treeBuilder.newFile(logPath, remoteFile);
153                 }
154             
155                 RemoteFolderTree remoteTree = treeBuilder.getTree();
156                 IWorkbenchPart part = this.getPart();
157                 if (part instanceof RepositoriesView ){
158                     final RepositoriesView repView = (RepositoriesView) part;
159                     RemoteContentProvider prov = repView.getContentProvider();
160                     prov.addCachedTree(project, remoteTree);
161                     final TreeViewer tree = repView.getViewer();
162                 
163                     Utils.asyncExec( new Runnable JavaDoc() {
164                         public void run() {
165                             tree.expandToLevel(project, AbstractTreeViewer.ALL_LEVELS);
166                         }
167                     }, repView.getViewer());
168                 }
169                 
170             } catch (InvocationTargetException JavaDoc e) {
171                 throw CVSException.wrapException(e);
172             } catch (InterruptedException JavaDoc e) {
173                 // Ignore;
174
} catch (TeamException e){
175                 throw CVSException.wrapException(e);
176             }
177     
178     }
179
180     private void verifyRevision(CVSTag tag, ILogEntry entry, ICVSRemoteFile remoteFile) throws CVSException {
181         if (entry instanceof LogEntry){
182             LogEntry logEntry = (LogEntry) entry;
183             String JavaDoc[] allBranchRevisions = logEntry.getBranchRevisions();
184             CVSTag[] allCVSTags = entry.getTags();
185             for (int i = 0; i < allCVSTags.length; i++) {
186                 if (allCVSTags[i].equals(tag)){
187                     //get the revision number stored for this tag
188
((RemoteFile) remoteFile).setRevision(allBranchRevisions[i]);
189                     break;
190                 }
191             }
192         }
193     }
194     
195     protected String JavaDoc getTaskName() {
196         return CVSUIMessages.FetchAllMembersOperation_0;
197     }
198
199
200 }
201
Popular Tags