1 9 package de.loskutov.bco.ui.actions; 10 11 import java.util.ArrayList ; 12 import java.util.BitSet ; 13 import java.util.Iterator ; 14 15 import org.eclipse.compare.CompareUI; 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.runtime.IAdaptable; 18 import org.eclipse.jdt.core.IClassFile; 19 import org.eclipse.jdt.core.ICompilationUnit; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.JavaCore; 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.preference.IPreferenceStore; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.swt.widgets.Shell; 27 import org.eclipse.ui.IObjectActionDelegate; 28 import org.eclipse.ui.IWorkbenchPart; 29 30 import de.loskutov.bco.BytecodeOutlinePlugin; 31 import de.loskutov.bco.compare.BytecodeCompare; 32 import de.loskutov.bco.compare.TypedElement; 33 import de.loskutov.bco.preferences.BCOConstants; 34 import de.loskutov.bco.ui.JdtUtils; 35 36 39 public abstract class BytecodeAction implements IObjectActionDelegate { 40 protected IStructuredSelection selection; 41 protected Shell shell; 42 43 48 public void selectionChanged(IAction action, ISelection newSelection) { 49 if (newSelection instanceof IStructuredSelection) { 50 this.selection = (IStructuredSelection) newSelection; 51 } 52 } 53 54 57 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 58 this.shell = targetPart.getSite().getShell(); 59 } 60 61 64 protected void exec(IJavaElement element1, IJavaElement element2) throws Exception { 65 final BitSet modes = getModes(); 66 CompareUI.openCompareEditor(new BytecodeCompare( 67 createTypedElement(element1, modes), 68 createTypedElement(element2, modes))); 69 } 70 71 protected TypedElement createTypedElement(IJavaElement javaElement, BitSet modes) { 72 String name; 73 IClassFile classFile = (IClassFile) javaElement 74 .getAncestor(IJavaElement.CLASS_FILE); 75 if (classFile != null) { 77 name = classFile.getPath().toOSString(); 78 if (!name.endsWith(".class")) { name += '/' + JdtUtils.getFullBytecodeName(classFile); 80 } 81 } else { 82 name = JdtUtils.getByteCodePath(javaElement); 84 } 85 String methodName = null; 86 if(javaElement.getElementType() == IJavaElement.METHOD || 87 javaElement.getElementType() == IJavaElement.INITIALIZER){ 88 methodName = JdtUtils.getMethodSignature(javaElement); 89 if(methodName != null){ 90 name += ":" + methodName; 91 } 92 } 93 return new TypedElement(name, methodName, TypedElement.TYPE_BYTECODE, javaElement, modes); 94 } 95 96 private BitSet getModes() { 97 IPreferenceStore store = BytecodeOutlinePlugin.getDefault().getPreferenceStore(); 98 BitSet modes = new BitSet (); 99 modes.set(BCOConstants.F_LINK_VIEW_TO_EDITOR, store.getBoolean(BCOConstants.LINK_VIEW_TO_EDITOR)); 100 modes.set(BCOConstants.F_SHOW_ONLY_SELECTED_ELEMENT, store.getBoolean(BCOConstants.SHOW_ONLY_SELECTED_ELEMENT)); 101 modes.set(BCOConstants.F_SHOW_RAW_BYTECODE, store.getBoolean(BCOConstants.SHOW_RAW_BYTECODE)); 102 modes.set(BCOConstants.F_SHOW_LINE_INFO, store.getBoolean(BCOConstants.SHOW_LINE_INFO)); 103 modes.set(BCOConstants.F_SHOW_VARIABLES, store.getBoolean(BCOConstants.SHOW_VARIABLES)); 104 modes.set(BCOConstants.F_SHOW_ASMIFIER_CODE, store.getBoolean(BCOConstants.SHOW_ASMIFIER_CODE)); 105 modes.set(BCOConstants.F_SHOW_ANALYZER, store.getBoolean(BCOConstants.SHOW_ANALYZER)); 106 modes.set(BCOConstants.F_SHOW_STACKMAP, store.getBoolean(BCOConstants.SHOW_STACKMAP)); 107 modes.set(BCOConstants.F_EXPAND_STACKMAP, store.getBoolean(BCOConstants.EXPAND_STACKMAP)); 108 return modes; 109 } 110 111 protected IJavaElement[] getSelectedResources() { 112 ArrayList resources = null; 113 if (!selection.isEmpty()) { 114 resources = new ArrayList (); 115 for (Iterator elements = selection.iterator(); elements.hasNext();) { 116 Object next = elements.next(); 117 if (next instanceof IFile) { 118 resources.add(JavaCore.create((IFile)next)); 119 continue; 120 } if (next instanceof IJavaElement) { 121 resources.add(next); 122 continue; 123 } else if (next instanceof IAdaptable) { 124 IAdaptable a = (IAdaptable) next; 125 Object adapter = a.getAdapter(IFile.class); 126 if (adapter instanceof IFile) { 127 resources.add(JavaCore.create((IFile)adapter)); 128 continue; 129 } 130 adapter = a.getAdapter(ICompilationUnit.class); 131 if (adapter instanceof ICompilationUnit) { 132 resources.add(adapter); 133 continue; 134 } 135 136 adapter = a.getAdapter(IClassFile.class); 137 if (adapter instanceof IClassFile) { 138 resources.add(adapter); 139 continue; 140 } 141 } 142 } 143 } 144 145 if (resources != null && !resources.isEmpty()) { 146 return (IJavaElement[]) resources.toArray(new IJavaElement[resources 147 .size()]); 148 } 149 150 return new IJavaElement[0]; 151 } 152 } 153 | Popular Tags |