1 11 package org.eclipse.jdt.internal.ui.compare; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.InputStream ; 15 16 import org.eclipse.core.runtime.CoreException; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IFileState; 20 import org.eclipse.core.resources.IResource; 21 22 import org.eclipse.swt.graphics.Image; 23 24 import org.eclipse.jface.text.IDocument; 25 26 import org.eclipse.compare.HistoryItem; 27 import org.eclipse.compare.IEncodedStreamContentAccessor; 28 import org.eclipse.compare.IResourceProvider; 29 import org.eclipse.compare.ITypedElement; 30 import org.eclipse.compare.ResourceNode; 31 import org.eclipse.compare.structuremergeviewer.DocumentRangeNode; 32 33 import org.eclipse.jdt.core.dom.ASTNode; 34 35 import org.eclipse.jdt.internal.ui.JavaPlugin; 36 37 41 class JavaTextBufferNode implements ITypedElement, IEncodedStreamContentAccessor, IResourceProvider { 42 43 private IFile fFile; 44 private IDocument fDocument; 45 private boolean fInEditor; 46 47 JavaTextBufferNode(IFile file, IDocument document, boolean inEditor) { 48 fFile= file; 49 fDocument= document; 50 fInEditor= inEditor; 51 } 52 53 public String getName() { 54 if (fInEditor) 55 return CompareMessages.Editor_Buffer; 56 return CompareMessages.Workspace_File; 57 } 58 59 public String getType() { 60 return "java"; } 62 63 public Image getImage() { 64 return null; 65 } 66 67 public InputStream getContents() { 68 return new ByteArrayInputStream (JavaCompareUtilities.getBytes(fDocument.get(), "UTF-16")); } 70 71 public String getCharset() { 72 return "UTF-16"; } 74 75 public IResource getResource() { 76 return fFile; 77 } 78 79 static final ITypedElement[] buildEditions(ITypedElement target, IFile file) { 80 81 IFileState[] states= null; 83 try { 85 states= file.getHistory(null); 86 } catch (CoreException ex) { 87 JavaPlugin.log(ex); 88 } 89 90 int count= 1; 91 if (states != null) 92 count+= states.length; 93 94 ITypedElement[] editions= new ITypedElement[count]; 95 editions[0]= new ResourceNode(file); 96 if (states != null) 97 for (int i= 0; i < states.length; i++) 98 editions[i+1]= new HistoryItem(target, states[i]); 99 return editions; 100 } 101 102 106 static final int getPlaceHolderType(ITypedElement element) { 107 108 if (element instanceof DocumentRangeNode) { 109 JavaNode jn= (JavaNode) element; 110 switch (jn.getTypeCode()) { 111 112 case JavaNode.PACKAGE: 113 return ASTNode.PACKAGE_DECLARATION; 114 115 case JavaNode.CLASS: 116 case JavaNode.INTERFACE: 117 return ASTNode.TYPE_DECLARATION; 118 119 case JavaNode.ENUM: 120 return ASTNode.ENUM_DECLARATION; 121 122 case JavaNode.ANNOTATION: 123 return ASTNode.ANNOTATION_TYPE_DECLARATION; 124 125 case JavaNode.CONSTRUCTOR: 126 case JavaNode.METHOD: 127 return ASTNode.METHOD_DECLARATION; 128 129 case JavaNode.FIELD: 130 return ASTNode.FIELD_DECLARATION; 131 132 case JavaNode.INIT: 133 return ASTNode.INITIALIZER; 134 135 case JavaNode.IMPORT: 136 case JavaNode.IMPORT_CONTAINER: 137 return ASTNode.IMPORT_DECLARATION; 138 139 case JavaNode.CU: 140 return ASTNode.COMPILATION_UNIT; 141 } 142 } 143 return -1; 144 } 145 } 146 | Popular Tags |