1 11 package org.eclipse.jdt.internal.ui.compare; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.InputStream ; 15 import java.io.UnsupportedEncodingException ; 16 import java.util.ResourceBundle ; 17 18 import org.eclipse.core.runtime.CoreException; 19 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 28 import org.eclipse.ui.IActionDelegate; 29 30 import org.eclipse.compare.IEncodedStreamContentAccessor; 31 import org.eclipse.compare.ITypedElement; 32 import org.eclipse.compare.structuremergeviewer.DiffNode; 33 34 import org.eclipse.jdt.core.IJavaElement; 35 import org.eclipse.jdt.core.ISourceRange; 36 import org.eclipse.jdt.core.ISourceReference; 37 import org.eclipse.jdt.core.JavaModelException; 38 39 import org.eclipse.jdt.internal.corext.util.Messages; 40 41 import org.eclipse.jdt.ui.JavaElementLabelProvider; 42 43 import org.eclipse.jdt.internal.ui.JavaPlugin; 44 45 public class JavaCompareAction implements IActionDelegate { 46 47 class TypedElement implements ITypedElement, IEncodedStreamContentAccessor { 48 49 private ISourceReference fSource; 50 private String fContents; 51 52 TypedElement(ISourceReference s, String contents) { 53 fSource= s; 54 fContents= contents; 55 } 56 57 public String getName() { 58 return fJavaElementLabelProvider.getText(fSource); 59 } 60 61 public String getType() { 62 return "JAVA"; } 64 65 public Image getImage() { 66 return fJavaElementLabelProvider.getImage(fSource); 67 } 68 69 public InputStream getContents() throws CoreException { 70 byte[] bytes; 71 try { 72 bytes= fContents.getBytes("UTF-16"); } catch (UnsupportedEncodingException e) { 74 bytes= fContents.getBytes(); 75 } 76 return new ByteArrayInputStream (bytes); 77 } 78 79 public String getCharset() { 80 return "UTF-16"; } 82 } 83 84 private static final String BUNDLE_NAME= "org.eclipse.jdt.internal.ui.compare.CompareAction"; 86 private ISourceReference fLeft; 87 private ISourceReference fRight; 88 89 private JavaElementLabelProvider fJavaElementLabelProvider; 90 91 92 public void selectionChanged(IAction action, ISelection selection) { 93 action.setEnabled(isEnabled(selection)); 94 } 95 96 public void run(IAction action) { 97 Shell shell= JavaPlugin.getActiveWorkbenchShell(); 98 ResourceBundle bundle= ResourceBundle.getBundle(BUNDLE_NAME); 99 CompareDialog d= new CompareDialog(shell, bundle); 100 101 102 String left= null; 103 String right= null; 104 105 try { 106 left= getExtendedSource(fLeft); 107 } catch (JavaModelException ex) { 108 JavaPlugin.log(ex); 109 } 110 111 try { 112 right= getExtendedSource(fRight); 113 } catch (JavaModelException ex) { 114 JavaPlugin.log(ex); 115 } 116 117 fJavaElementLabelProvider= new JavaElementLabelProvider( 118 JavaElementLabelProvider.SHOW_PARAMETERS | 119 JavaElementLabelProvider.SHOW_OVERLAY_ICONS | 120 JavaElementLabelProvider.SHOW_ROOT); 121 122 if (left == null || right == null) { 123 String errorTitle= JavaCompareUtilities.getString(bundle, "errorTitle"); String errorFormat= JavaCompareUtilities.getString(bundle, "errorFormat"); 126 Object element= null; 127 if (left == null) 128 element= fLeft; 129 else 130 element= fRight; 131 132 String message= Messages.format(errorFormat, new String [] { fJavaElementLabelProvider.getText(element) } ); 133 134 MessageDialog.openError(shell, errorTitle, message); 135 return; 136 } 137 138 d.compare(new DiffNode(new TypedElement(fLeft, left), new TypedElement(fRight, right))); 139 140 fJavaElementLabelProvider.dispose(); 141 fJavaElementLabelProvider= null; 142 } 143 144 protected boolean isEnabled(ISelection selection) { 145 if (selection instanceof IStructuredSelection) { 146 Object [] sel= ((IStructuredSelection) selection).toArray(); 147 if (sel.length == 2) { 148 for (int i= 0; i < 2; i++) { 149 Object o= sel[i]; 150 if (!(o instanceof ISourceReference)) 151 return false; 152 } 153 fLeft= (ISourceReference) sel[0]; 154 fRight= (ISourceReference) sel[1]; 155 return true; 156 } 157 } 158 return false; 159 } 160 161 private String getExtendedSource(ISourceReference ref) throws JavaModelException { 162 163 if (ref instanceof IJavaElement) { 165 IJavaElement parent= ((IJavaElement) ref).getParent(); 166 if (parent instanceof ISourceReference) { 167 ISourceReference sr= (ISourceReference) parent; 168 String parentContent= sr.getSource(); 169 if (parentContent != null) { 170 ISourceRange parentRange= sr.getSourceRange(); 171 ISourceRange childRange= ref.getSourceRange(); 172 173 int start= childRange.getOffset() - parentRange.getOffset(); 174 int end= start + childRange.getLength(); 175 176 while (start > 0) { 178 char c= parentContent.charAt(start-1); 179 if (c == '\n' || c == '\r') 180 break; 181 start--; 182 } 183 184 return parentContent.substring(start, end); 185 } 186 } 187 } 188 189 return ref.getSource(); 190 } 191 } 192 | Popular Tags |