1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.InputStream ; 16 import java.lang.reflect.InvocationTargetException ; 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 36 public class ResourceEditionNode implements IStructureComparator, ITypedElement, IEncodedStreamContentAccessor { 37 private ICVSRemoteResource resource; 38 private ResourceEditionNode[] children; 39 40 43 public ResourceEditionNode(ICVSRemoteResource resourceEdition) { 44 this.resource = resourceEdition; 45 } 46 47 52 public boolean equals(Object other) { 53 if (other instanceof ITypedElement) { 54 String otherName = ((ITypedElement)other).getName(); 55 return getName().equals(otherName); 56 } 57 return super.equals(other); 58 } 59 60 63 public Object [] getChildren() { 64 if (children == null) { 65 children = new ResourceEditionNode[0]; 66 if (resource != null) { 67 try { 68 CVSUIPlugin.runWithProgress(null, true , new IRunnableWithProgress() { 69 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 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 (e); 78 } 79 } 80 }); 81 } catch (InterruptedException e) { 82 } catch (InvocationTargetException e) { 84 Throwable t = e.getTargetException(); 85 if (t instanceof TeamException) { 86 CVSUIPlugin.log(((TeamException) t)); 87 } 88 } 89 } 90 } 91 return children; 92 } 93 94 97 public InputStream getContents() throws CoreException { 98 IStorage storage = getStorage(); 99 if (storage != null) { 100 return storage.getContents(); 101 } 102 return new ByteArrayInputStream (new byte[0]); 103 } 104 105 public Image getImage() { 106 return CompareUI.getImage(resource); 107 } 108 109 112 public String getName() { 113 return resource == null ? "" : resource.getName(); } 115 116 public ICVSRemoteResource getRemoteResource() { 117 return resource; 118 } 119 120 123 public String getType() { 124 if (resource == null) { 125 return UNKNOWN_TYPE; 126 } 127 if (resource.isContainer()) { 128 return FOLDER_TYPE; 129 } 130 String name = resource.getName(); 131 name = name.substring(name.lastIndexOf('.') + 1); 132 return name.length() == 0 ? UNKNOWN_TYPE : name; 133 } 134 135 138 public int hashCode() { 139 return getName().hashCode(); 140 } 141 142 145 public String getCharset() throws CoreException { 146 IResource local = resource.getIResource(); 148 if (local != null && local.getType() == IResource.FILE) { 149 return ((IFile)local).getCharset(); 150 } 151 IStorage storage = getStorage(); 153 if (storage instanceof IEncodedStorage) { 154 String 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 , new IRunnableWithProgress() { 169 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 170 try { 171 holder[0] = ((IResourceVariant)resource).getStorage(monitor); 172 } catch (TeamException e) { 173 throw new InvocationTargetException (e); 174 } 175 } 176 }); 177 } catch (InvocationTargetException e) { 178 throw TeamException.asTeamException(e); 179 } catch (InterruptedException e) { 180 } 182 return holder[0]; 183 } 184 } 185 | Popular Tags |