KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > variants > ResourceVariantTree


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.core.variants;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IWorkspaceRunnable;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.team.core.TeamException;
18
19 /**
20  * Provides caching for a {@link AbstractResourceVariantTree} using a
21  * {@link ResourceVariantByteStore}.
22  *
23  * @see IResourceVariantTree
24  * @see AbstractResourceVariantTree
25  * @see ResourceVariantByteStore
26  * @since 3.0
27  */

28 public abstract class ResourceVariantTree extends AbstractResourceVariantTree {
29     
30     private ResourceVariantByteStore store;
31
32     /**
33      * Create a resource variant tree that uses the provided byte store to
34      * cache the resource variant bytes.
35      * @param store the resource variant byte store used to cache resource variants
36      */

37     protected ResourceVariantTree(ResourceVariantByteStore store) {
38         this.store = store;
39     }
40     
41     /* (non-Javadoc)
42      * @see org.eclipse.team.core.variants.IResourceVariantTree#members(org.eclipse.core.resources.IResource)
43      */

44     public IResource[] members(IResource resource) throws TeamException {
45         return getByteStore().members(resource);
46     }
47     
48     /* (non-Javadoc)
49      * @see org.eclipse.team.core.variants.IResourceVariantTree#hasResourceVariant(org.eclipse.core.resources.IResource)
50      */

51     public boolean hasResourceVariant(IResource resource) throws TeamException {
52         return getByteStore().getBytes(resource) != null;
53     }
54     
55     /* (non-Javadoc)
56      * @see org.eclipse.team.core.variants.IResourceVariantTree#flushVariants(org.eclipse.core.resources.IResource, int)
57      */

58     public void flushVariants(IResource resource, int depth) throws TeamException {
59         getByteStore().flushBytes(resource, depth);
60     }
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.team.core.variants.AbstractResourceVariantTree#setVariant(org.eclipse.core.resources.IResource, org.eclipse.team.core.variants.IResourceVariant)
64      */

65     protected boolean setVariant(IResource local, IResourceVariant remote) throws TeamException {
66         ResourceVariantByteStore cache = getByteStore();
67         byte[] newRemoteBytes = getBytes(local, remote);
68         boolean changed;
69         if (newRemoteBytes == null) {
70             changed = cache.deleteBytes(local);
71         } else {
72             changed = cache.setBytes(local, newRemoteBytes);
73         }
74         return changed;
75     }
76     
77     /**
78      * Get the byte store that is used to cache the serialization bytes
79      * for the resource variants of this tree. A byte store is used
80      * to reduce the memory footprint of the tree.
81      * <p>
82      * This method is not intended to be overridden by subclasses.
83      *
84      * @return the resource variant tree that is being refreshed.
85      */

86     protected ResourceVariantByteStore getByteStore() {
87         return store;
88     }
89     
90     /**
91      * Get the bytes to be stored in the <code>ResourceVariantByteStore</code>
92      * from the given resource variant. By default, the <code>IResourceVariant#asBytes()</code>
93      * method is used to get the bytes.
94      * @param local the local resource
95      * @param remote the corresponding resource variant handle
96      * @return the bytes for the resource variant.
97      */

98     protected byte[] getBytes(IResource local, IResourceVariant remote) throws TeamException {
99         if (remote == null) return null;
100         return remote.asBytes();
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.team.core.variants.AbstractResourceVariantTree#collectChanges(org.eclipse.core.resources.IResource, org.eclipse.team.core.variants.IResourceVariant, int, org.eclipse.core.runtime.IProgressMonitor)
105      */

106     protected IResource[] collectChanges(final IResource local,
107             final IResourceVariant remote, final int depth, IProgressMonitor monitor)
108             throws TeamException {
109         final IResource[][] resources = new IResource[][] { null };
110         getByteStore().run(local, new IWorkspaceRunnable() {
111             public void run(IProgressMonitor monitor) throws CoreException {
112                 resources[0] = ResourceVariantTree.super.collectChanges(local, remote, depth, monitor);
113             }
114         }, monitor);
115         return resources[0];
116     }
117 }
118
Popular Tags