KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > filesystem > RLogTreeBuilder


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

14 package org.eclipse.team.internal.ccvs.core.filesystem;
15
16 import java.util.HashMap JavaDoc;
17
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.team.internal.ccvs.core.CVSException;
21 import org.eclipse.team.internal.ccvs.core.CVSTag;
22 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile;
23 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder;
24 import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource;
25 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
26 import org.eclipse.team.internal.ccvs.core.ICVSResource;
27 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolderTree;
28
29 class RLogTreeBuilder {
30
31     private ICVSRepositoryLocation location;
32     private RemoteFolderTree tree;
33     private CVSTag tag;
34     private HashMap JavaDoc folderMap;
35     private HashMap JavaDoc logMap;
36     private LogEntryCache cache;
37
38     public RLogTreeBuilder(ICVSRepositoryLocation location, CVSTag tag, LogEntryCache cache) {
39         this.tag = tag;
40         this.location = location;
41         this.cache = cache;
42         reset();
43     }
44
45     public RemoteFolderTree getTree() {
46         return tree;
47     }
48
49     /**
50      * Reset the builder to prepare for a new build
51      */

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

62     public void newFile(IPath remoteFilePath, ICVSRemoteFile remoteFile) {
63         try {
64             addFile(tree, tag, remoteFile, remoteFilePath);
65         } catch (CVSException 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         //get the log entry info for this file and save it
76
logMap.put(resource, cache.getLogEntry(resource));
77
78         ICVSRemoteResource[] children = tree.getChildren();
79         ICVSRemoteResource[] newChildren;
80         if (children == null) {
81             newChildren = new ICVSRemoteResource[] {resource};
82         } else {
83             newChildren = new ICVSRemoteResource[children.length + 1];
84             System.arraycopy(children, 0, newChildren, 0, children.length);
85             newChildren[children.length] = resource;
86         }
87         tree.setChildren(newChildren);
88     }
89
90     /*
91      * Get the folder at the given path in the given tree, creating any missing folders as needed.
92      */

93     private ICVSRemoteFolder getFolder(RemoteFolderTree tree, CVSTag tag, IPath remoteFolderPath, IPath parentPath) throws CVSException {
94         if (remoteFolderPath.segmentCount() == 0)
95             return tree;
96         String JavaDoc name = remoteFolderPath.segment(0);
97         ICVSResource child;
98         IPath childPath = parentPath.append(name);
99         if (tree.childExists(name)) {
100             child = tree.getChild(name);
101         } else {
102             child = new RemoteFolderTree(tree, tree.getRepository(), childPath.toString(), tag);
103             //Save this folder in hash map
104
folderMap.put(childPath.toString(), child);
105             ((RemoteFolderTree) child).setChildren(new ICVSRemoteResource[0]);
106             addChild(tree, (ICVSRemoteResource) child);
107         }
108         return getFolder((RemoteFolderTree) child, tag, remoteFolderPath.removeFirstSegments(1), childPath);
109     }
110
111     public HashMap JavaDoc getFolderMap() {
112         return folderMap;
113     }
114
115     public HashMap JavaDoc getLogMap() {
116         return logMap;
117     }
118 }
119
Popular Tags