001: /*******************************************************************************
002: * Copyright (c) 2004 Andrei Loskutov.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the BSD License
005: * which accompanies this distribution, and is available at
006: * http://www.opensource.org/licenses/bsd-license.php
007: * Contributor: Andrei Loskutov - initial API and implementation
008: *******************************************************************************/package de.loskutov.bco.ui.actions;
009:
010: import java.util.ArrayList;
011: import java.util.BitSet;
012: import java.util.Iterator;
013:
014: import org.eclipse.compare.CompareUI;
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.jdt.core.IClassFile;
018: import org.eclipse.jdt.core.ICompilationUnit;
019: import org.eclipse.jdt.core.IJavaElement;
020: import org.eclipse.jdt.core.JavaCore;
021: import org.eclipse.jface.action.IAction;
022: import org.eclipse.jface.preference.IPreferenceStore;
023: import org.eclipse.jface.viewers.ISelection;
024: import org.eclipse.jface.viewers.IStructuredSelection;
025: import org.eclipse.swt.widgets.Shell;
026: import org.eclipse.ui.IObjectActionDelegate;
027: import org.eclipse.ui.IWorkbenchPart;
028:
029: import de.loskutov.bco.BytecodeOutlinePlugin;
030: import de.loskutov.bco.compare.BytecodeCompare;
031: import de.loskutov.bco.compare.TypedElement;
032: import de.loskutov.bco.preferences.BCOConstants;
033: import de.loskutov.bco.ui.JdtUtils;
034:
035: /**
036: * @author Andrei
037: */
038: public abstract class BytecodeAction implements IObjectActionDelegate {
039: protected IStructuredSelection selection;
040: protected Shell shell;
041:
042: /**
043: * (non-Javadoc)
044: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
045: * org.eclipse.jface.viewers.ISelection)
046: */
047: public void selectionChanged(IAction action, ISelection newSelection) {
048: if (newSelection instanceof IStructuredSelection) {
049: this .selection = (IStructuredSelection) newSelection;
050: }
051: }
052:
053: /**
054: * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
055: */
056: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
057: this .shell = targetPart.getSite().getShell();
058: }
059:
060: /**
061: * @see de.loskutov.branchview.connection.Command#exec(java.lang.Object)
062: */
063: protected void exec(IJavaElement element1, IJavaElement element2)
064: throws Exception {
065: final BitSet modes = getModes();
066: CompareUI.openCompareEditor(new BytecodeCompare(
067: createTypedElement(element1, modes),
068: createTypedElement(element2, modes)));
069: }
070:
071: protected TypedElement createTypedElement(IJavaElement javaElement,
072: BitSet modes) {
073: String name;
074: IClassFile classFile = (IClassFile) javaElement
075: .getAncestor(IJavaElement.CLASS_FILE);
076: // existing read-only class files
077: if (classFile != null) {
078: name = classFile.getPath().toOSString();
079: if (!name.endsWith(".class")) { //$NON-NLS-1$
080: name += '/' + JdtUtils.getFullBytecodeName(classFile);
081: }
082: } else {
083: // usual eclipse - generated bytecode
084: name = JdtUtils.getByteCodePath(javaElement);
085: }
086: String methodName = null;
087: if (javaElement.getElementType() == IJavaElement.METHOD
088: || javaElement.getElementType() == IJavaElement.INITIALIZER) {
089: methodName = JdtUtils.getMethodSignature(javaElement);
090: if (methodName != null) {
091: name += ":" + methodName;
092: }
093: }
094: return new TypedElement(name, methodName,
095: TypedElement.TYPE_BYTECODE, javaElement, modes);
096: }
097:
098: private BitSet getModes() {
099: IPreferenceStore store = BytecodeOutlinePlugin.getDefault()
100: .getPreferenceStore();
101: BitSet modes = new BitSet();
102: modes.set(BCOConstants.F_LINK_VIEW_TO_EDITOR, store
103: .getBoolean(BCOConstants.LINK_VIEW_TO_EDITOR));
104: modes.set(BCOConstants.F_SHOW_ONLY_SELECTED_ELEMENT, store
105: .getBoolean(BCOConstants.SHOW_ONLY_SELECTED_ELEMENT));
106: modes.set(BCOConstants.F_SHOW_RAW_BYTECODE, store
107: .getBoolean(BCOConstants.SHOW_RAW_BYTECODE));
108: modes.set(BCOConstants.F_SHOW_LINE_INFO, store
109: .getBoolean(BCOConstants.DIFF_SHOW_LINE_INFO));
110: modes.set(BCOConstants.F_SHOW_VARIABLES, store
111: .getBoolean(BCOConstants.DIFF_SHOW_VARIABLES));
112: modes.set(BCOConstants.F_SHOW_ASMIFIER_CODE, store
113: .getBoolean(BCOConstants.DIFF_SHOW_ASMIFIER_CODE));
114: modes.set(BCOConstants.F_SHOW_ANALYZER, store
115: .getBoolean(BCOConstants.SHOW_ANALYZER));
116: modes.set(BCOConstants.F_SHOW_STACKMAP, store
117: .getBoolean(BCOConstants.DIFF_SHOW_STACKMAP));
118: modes.set(BCOConstants.F_EXPAND_STACKMAP, store
119: .getBoolean(BCOConstants.DIFF_EXPAND_STACKMAP));
120: return modes;
121: }
122:
123: protected IJavaElement[] getSelectedResources() {
124: ArrayList resources = null;
125: if (!selection.isEmpty()) {
126: resources = new ArrayList();
127: for (Iterator elements = selection.iterator(); elements
128: .hasNext();) {
129: Object next = elements.next();
130: if (next instanceof IFile) {
131: resources.add(JavaCore.create((IFile) next));
132: continue;
133: }
134: if (next instanceof IJavaElement) {
135: resources.add(next);
136: continue;
137: } else if (next instanceof IAdaptable) {
138: IAdaptable a = (IAdaptable) next;
139: Object adapter = a.getAdapter(IFile.class);
140: if (adapter instanceof IFile) {
141: resources.add(JavaCore.create((IFile) adapter));
142: continue;
143: }
144: adapter = a.getAdapter(ICompilationUnit.class);
145: if (adapter instanceof ICompilationUnit) {
146: resources.add(adapter);
147: continue;
148: }
149:
150: adapter = a.getAdapter(IClassFile.class);
151: if (adapter instanceof IClassFile) {
152: resources.add(adapter);
153: continue;
154: }
155: }
156: }
157: }
158:
159: if (resources != null && !resources.isEmpty()) {
160: return (IJavaElement[]) resources
161: .toArray(new IJavaElement[resources.size()]);
162: }
163:
164: return new IJavaElement[0];
165: }
166: }
|