KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > model > RemoteContentProvider


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.model;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.jface.viewers.AbstractTreeViewer;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.team.internal.ccvs.core.*;
18 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolderTree;
19 import org.eclipse.team.internal.ccvs.core.resources.RemoteResource;
20 import org.eclipse.team.internal.ccvs.ui.repo.RepositoryRoot;
21 import org.eclipse.ui.IWorkingSet;
22 import org.eclipse.ui.model.WorkbenchContentProvider;
23 import org.eclipse.ui.progress.DeferredTreeContentManager;
24
25 /**
26  * Extension to the generic workbench content provider mechanism
27  * to lazily determine whether an element has children. That is,
28  * children for an element aren't fetched until the user clicks
29  * on the tree expansion box.
30  */

31 public class RemoteContentProvider extends WorkbenchContentProvider {
32
33     IWorkingSet workingSet;
34     DeferredTreeContentManager manager;
35
36     HashMap JavaDoc cachedTrees;
37     
38     public RemoteContentProvider(){
39         cachedTrees = new HashMap JavaDoc();
40     }
41     
42     /* (non-Javadoc)
43      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
44      */

45     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
46         if (viewer instanceof AbstractTreeViewer) {
47             manager = new DeferredTreeContentManager(this, (AbstractTreeViewer) viewer);
48         }
49         super.inputChanged(viewer, oldInput, newInput);
50     }
51
52     public boolean hasChildren(Object JavaDoc element) {
53         // the + box will always appear, but then disappear
54
// if not needed after you first click on it.
55
if (element instanceof ICVSRemoteResource) {
56             if (element instanceof ICVSRemoteFolder) {
57                 return ((ICVSRemoteFolder) element).isExpandable();
58             }
59             return ((ICVSRemoteResource) element).isContainer();
60         } else if (element instanceof CVSResourceElement) {
61             ICVSResource r = ((CVSResourceElement) element).getCVSResource();
62             if (r instanceof RemoteResource) {
63                 return r.isFolder();
64             }
65         } else if (element instanceof VersionCategory) {
66             return true;
67         } else if (element instanceof BranchCategory) {
68             return true;
69         } else if (element instanceof CVSTagElement) {
70             return true;
71         } else if (element instanceof RemoteModule) {
72             return true;
73         }
74         if (manager != null) {
75             if (manager.isDeferredAdapter(element))
76                 return manager.mayHaveChildren(element);
77         }
78
79         return super.hasChildren(element);
80     }
81
82     /**
83      * Sets the workingSet.
84      * @param workingSet The workingSet to set
85      */

86     public void setWorkingSet(IWorkingSet workingSet) {
87         this.workingSet = workingSet;
88     }
89
90     /**
91      * Returns the workingSet.
92      * @return IWorkingSet
93      */

94     public IWorkingSet getWorkingSet() {
95         return workingSet;
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.ui.model.WorkbenchContentProvider#getChildren(java.lang.Object)
100      */

101     public Object JavaDoc[] getChildren(Object JavaDoc element) {
102         //check to see if we already have the children cached in the tree map
103
Object JavaDoc tree = cachedTrees.get(element);
104         if (tree != null) {
105             return ((RemoteFolderTree) tree).getChildren();
106         }
107         
108         if (manager != null) {
109             Object JavaDoc[] children = manager.getChildren(element);
110             if (children != null) {
111                 // This will be a placeholder to indicate
112
// that the real children are being fetched
113
return children;
114             }
115         }
116         Object JavaDoc[] children = super.getChildren(element);
117         for (int i = 0; i < children.length; i++) {
118             Object JavaDoc object = children[i];
119             if (object instanceof CVSModelElement)
120                 ((CVSModelElement)object).setWorkingSet(getWorkingSet());
121         }
122         return children;
123     }
124
125     public void cancelJobs(RepositoryRoot[] roots) {
126         if (manager != null) {
127             for (int i = 0; i < roots.length; i++) {
128                 RepositoryRoot root = roots[i];
129                 cancelJobs(root.getRoot());
130             }
131         }
132     }
133     
134     /**
135      * Cancel any jobs that are fetching content from the given location.
136      * @param location
137      */

138     public void cancelJobs(ICVSRepositoryLocation location) {
139         if (manager != null) {
140             manager.cancel(location);
141         }
142     }
143     
144     /**
145      * Adds a remote folder tree to the cache
146      * @param project
147      *
148      */

149     public void addCachedTree(ICVSRemoteFolder project, RemoteFolderTree tree){
150         cachedTrees.put(project, tree);
151     }
152     
153     public void purgeCache(){
154         cachedTrees.clear();
155     }
156     
157     
158 }
159
Popular Tags