KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > ui > actions > BytecodeAction


1 /*******************************************************************************
2  * Copyright (c) 2004 Andrei Loskutov.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the BSD License
5  * which accompanies this distribution, and is available at
6  * http://www.opensource.org/licenses/bsd-license.php
7  * Contributor: Andrei Loskutov - initial API and implementation
8  *******************************************************************************/

9 package de.loskutov.bco.ui.actions;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.BitSet JavaDoc;
13 import java.util.Iterator JavaDoc;
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 /**
37  * @author Andrei
38  */

39 public abstract class BytecodeAction implements IObjectActionDelegate {
40     protected IStructuredSelection selection;
41     protected Shell shell;
42
43     /**
44      * (non-Javadoc)
45      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
46      * org.eclipse.jface.viewers.ISelection)
47      */

48     public void selectionChanged(IAction action, ISelection newSelection) {
49         if (newSelection instanceof IStructuredSelection) {
50             this.selection = (IStructuredSelection) newSelection;
51         }
52     }
53
54     /**
55      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
56      */

57     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
58         this.shell = targetPart.getSite().getShell();
59     }
60
61     /**
62      * @see de.loskutov.branchview.connection.Command#exec(java.lang.Object)
63      */

64     protected void exec(IJavaElement element1, IJavaElement element2) throws Exception JavaDoc {
65         final BitSet JavaDoc modes = getModes();
66         CompareUI.openCompareEditor(new BytecodeCompare(
67             createTypedElement(element1, modes),
68             createTypedElement(element2, modes)));
69     }
70
71     protected TypedElement createTypedElement(IJavaElement javaElement, BitSet JavaDoc modes) {
72         String JavaDoc name;
73         IClassFile classFile = (IClassFile) javaElement
74             .getAncestor(IJavaElement.CLASS_FILE);
75         // existing read-only class files
76
if (classFile != null) {
77             name = classFile.getPath().toOSString();
78             if (!name.endsWith(".class")) { //$NON-NLS-1$
79
name += '/' + JdtUtils.getFullBytecodeName(classFile);
80             }
81         } else {
82             // usual eclipse - generated bytecode
83
name = JdtUtils.getByteCodePath(javaElement);
84         }
85         String JavaDoc 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 JavaDoc getModes() {
97         IPreferenceStore store = BytecodeOutlinePlugin.getDefault().getPreferenceStore();
98         BitSet JavaDoc modes = new BitSet JavaDoc();
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 JavaDoc resources = null;
113         if (!selection.isEmpty()) {
114             resources = new ArrayList JavaDoc();
115             for (Iterator JavaDoc elements = selection.iterator(); elements.hasNext();) {
116                 Object JavaDoc 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 JavaDoc 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