KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > ResourceEditionNode


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;
12
13  
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import org.eclipse.compare.CompareUI;
18 import org.eclipse.compare.IEncodedStreamContentAccessor;
19 import org.eclipse.compare.ITypedElement;
20 import org.eclipse.compare.structuremergeviewer.IStructureComparator;
21 import org.eclipse.core.resources.IEncodedStorage;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IStorage;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.team.core.TeamException;
30 import org.eclipse.team.core.variants.IResourceVariant;
31 import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource;
32
33 /**
34  * A class for comparing ICVSRemoteResource objects
35  */

36 public class ResourceEditionNode implements IStructureComparator, ITypedElement, IEncodedStreamContentAccessor {
37     private ICVSRemoteResource resource;
38     private ResourceEditionNode[] children;
39     
40     /**
41      * Creates a new ResourceEditionNode on the given resource edition.
42      */

43     public ResourceEditionNode(ICVSRemoteResource resourceEdition) {
44         this.resource = resourceEdition;
45     }
46         
47     /**
48      * Returns true if both resources names are identical.
49      * The content is not considered.
50      * @see IComparator#equals
51      */

52     public boolean equals(Object JavaDoc other) {
53         if (other instanceof ITypedElement) {
54             String JavaDoc otherName = ((ITypedElement)other).getName();
55             return getName().equals(otherName);
56         }
57         return super.equals(other);
58     }
59     
60     /**
61      * Enumerate children of this node (if any).
62      */

63     public Object JavaDoc[] getChildren() {
64         if (children == null) {
65             children = new ResourceEditionNode[0];
66             if (resource != null) {
67                 try {
68                     CVSUIPlugin.runWithProgress(null, true /*cancelable*/, new IRunnableWithProgress() {
69                         public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
70                             try {
71                                 ICVSRemoteResource[] members = resource.members(monitor);
72                                 children = new ResourceEditionNode[members.length];
73                                 for (int i = 0; i < members.length; i++) {
74                                     children[i] = new ResourceEditionNode(members[i]);
75                                 }
76                             } catch (TeamException e) {
77                                 throw new InvocationTargetException JavaDoc(e);
78                             }
79                         }
80                     });
81                 } catch (InterruptedException JavaDoc e) {
82                     // operation canceled
83
} catch (InvocationTargetException JavaDoc e) {
84                     Throwable JavaDoc t = e.getTargetException();
85                     if (t instanceof TeamException) {
86                         CVSUIPlugin.log(((TeamException) t));
87                     }
88                 }
89             }
90         }
91         return children;
92     }
93
94     /**
95      * @see IStreamContentAccessor#getContents()
96      */

97     public InputStream JavaDoc getContents() throws CoreException {
98         IStorage storage = getStorage();
99         if (storage != null) {
100             return storage.getContents();
101         }
102         return new ByteArrayInputStream JavaDoc(new byte[0]);
103     }
104     
105     public Image getImage() {
106         return CompareUI.getImage(resource);
107     }
108     
109     /**
110      * Returns the name of this node.
111      */

112     public String JavaDoc getName() {
113         return resource == null ? "" : resource.getName(); //$NON-NLS-1$
114
}
115     
116     public ICVSRemoteResource getRemoteResource() {
117         return resource;
118     }
119     
120     /**
121      * Returns the comparison type for this node.
122      */

123     public String JavaDoc getType() {
124         if (resource == null) {
125             return UNKNOWN_TYPE;
126         }
127         if (resource.isContainer()) {
128             return FOLDER_TYPE;
129         }
130         String JavaDoc name = resource.getName();
131         name = name.substring(name.lastIndexOf('.') + 1);
132         return name.length() == 0 ? UNKNOWN_TYPE : name;
133     }
134     
135     /**
136      * @see IComparator#equals
137      */

138     public int hashCode() {
139         return getName().hashCode();
140     }
141
142     /* (non-Javadoc)
143      * @see org.eclipse.compare.IEncodedStreamContentAccessor#getCharset()
144      */

145     public String JavaDoc getCharset() throws CoreException {
146         // Use the local file encoding if there is one
147
IResource local = resource.getIResource();
148         if (local != null && local.getType() == IResource.FILE) {
149             return ((IFile)local).getCharset();
150         }
151         // See if the remote file has an encoding
152
IStorage storage = getStorage();
153         if (storage instanceof IEncodedStorage) {
154             String JavaDoc charset = ((IEncodedStorage)storage).getCharset();
155             if (charset != null) {
156                 return charset;
157             }
158         }
159         return null;
160     }
161     
162     private IStorage getStorage() throws TeamException {
163         if (resource == null) {
164             return null;
165         }
166         final IStorage[] holder = new IStorage[1];
167         try {
168             CVSUIPlugin.runWithProgress(null, true /*cancelable*/, new IRunnableWithProgress() {
169                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
170                     try {
171                         holder[0] = ((IResourceVariant)resource).getStorage(monitor);
172                     } catch (TeamException e) {
173                         throw new InvocationTargetException JavaDoc(e);
174                     }
175                 }
176             });
177         } catch (InvocationTargetException JavaDoc e) {
178             throw TeamException.asTeamException(e);
179         } catch (InterruptedException JavaDoc e) {
180             // Shouldn't happen. Ignore
181
}
182         return holder[0];
183     }
184 }
185
Popular Tags