1 11 package org.eclipse.compare; 12 13 import java.io.*; 14 import java.util.ArrayList ; 15 16 import org.eclipse.compare.internal.Utilities; 17 import org.eclipse.compare.structuremergeviewer.IStructureComparator; 18 import org.eclipse.core.resources.*; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.swt.widgets.Shell; 22 23 33 public class ResourceNode extends BufferedContent 34 implements IEncodedStreamContentAccessor, IStructureComparator, ITypedElement, 35 IEditableContent, IModificationDate, IResourceProvider, IEditableContentExtension { 36 37 private IResource fResource; 38 private ArrayList fChildren; 39 40 41 46 public ResourceNode(IResource resource) { 47 fResource= resource; 48 Assert.isNotNull(resource); 49 } 50 51 56 public IResource getResource() { 57 return fResource; 58 } 59 60 63 public InputStream getContents() throws CoreException { 64 if (fResource instanceof IStorage) 65 return super.getContents(); 66 return null; 67 } 68 69 72 public long getModificationDate() { 73 return fResource.getLocalTimeStamp(); 74 } 75 76 79 public String getName() { 80 if (fResource != null) 81 return fResource.getName(); 82 return null; 83 } 84 85 88 public String getType() { 89 if (fResource instanceof IContainer) 90 return ITypedElement.FOLDER_TYPE; 91 if (fResource != null) { 92 String s= fResource.getFileExtension(); 93 if (s != null) 94 return s; 95 } 96 return ITypedElement.UNKNOWN_TYPE; 97 } 98 99 102 public Image getImage() { 103 return CompareUI.getImage(fResource); 104 } 105 106 110 public boolean equals(Object other) { 111 if (other instanceof ITypedElement) { 112 String otherName= ((ITypedElement)other).getName(); 113 return getName().equals(otherName); 114 } 115 return super.equals(other); 116 } 117 118 122 public int hashCode() { 123 return getName().hashCode(); 124 } 125 126 129 public Object [] getChildren() { 130 if (fChildren == null) { 131 fChildren= new ArrayList (); 132 if (fResource instanceof IContainer) { 133 try { 134 IResource members[]= ((IContainer)fResource).members(); 135 for (int i= 0; i < members.length; i++) { 136 IStructureComparator child= createChild(members[i]); 137 if (child != null) 138 fChildren.add(child); 139 } 140 } catch (CoreException ex) { 141 } 143 } 144 } 145 return fChildren.toArray(); 146 } 147 148 158 protected IStructureComparator createChild(IResource child) { 159 return new ResourceNode(child); 160 } 161 162 169 protected InputStream createStream() throws CoreException { 170 if (fResource instanceof IStorage) { 171 InputStream is= null; 172 IStorage storage= (IStorage) fResource; 173 try { 174 is= storage.getContents(); 175 } catch (CoreException e) { 176 if (e.getStatus().getCode() == IResourceStatus.OUT_OF_SYNC_LOCAL) { 177 fResource.refreshLocal(IResource.DEPTH_INFINITE, null); 178 is= storage.getContents(); 179 } else 180 throw e; 181 } 182 if (is != null) 183 return new BufferedInputStream(is); 184 } 185 return null; 186 } 187 188 191 public boolean isEditable() { 192 return true; 193 } 194 195 198 public ITypedElement replace(ITypedElement child, ITypedElement other) { 199 return child; 200 } 201 202 205 public String getCharset() { 206 return Utilities.getCharset(fResource); 207 } 208 209 212 public boolean isReadOnly() { 213 if (fResource.getType() == IResource.FILE) { 214 ResourceAttributes attrs = fResource.getResourceAttributes(); 215 if (attrs != null) { 216 return attrs.isReadOnly(); 217 } 218 } 219 return false; 220 } 221 222 225 public IStatus validateEdit(Shell shell) { 226 if (isReadOnly()) 227 return ResourcesPlugin.getWorkspace().validateEdit(new IFile[] { (IFile)fResource}, shell); 228 return Status.OK_STATUS; 229 } 230 } 231 232 | Popular Tags |