KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > editors > BytecodeActionBarContributor


1 package de.loskutov.bco.editors;
2
3 import org.eclipse.jdt.internal.ui.javaeditor.ClassFileEditorActionContributor;
4 import org.eclipse.jface.action.Action;
5 import org.eclipse.jface.action.IMenuManager;
6 import org.eclipse.jface.action.IToolBarManager;
7 import org.eclipse.jface.resource.ImageDescriptor;
8 import org.eclipse.jface.text.ITextSelection;
9 import org.eclipse.jface.viewers.ISelection;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.ui.IEditorPart;
12 import org.eclipse.ui.IWorkbenchActionConstants;
13 import org.eclipse.ui.plugin.AbstractUIPlugin;
14
15 import de.loskutov.bco.BytecodeOutlinePlugin;
16 import de.loskutov.bco.preferences.BCOConstants;
17
18 /**
19  * Adds "Show Bytecode" action to tool/menu bars
20  * @author V. Grishchenko, Eugene Kuleshov, Andrei
21  */

22 public class BytecodeActionBarContributor
23     extends
24         ClassFileEditorActionContributor {
25
26     BytecodeClassFileEditor editor;
27     protected ShowBytecodeAction refreshAction;
28     protected ToggleRawBytecodeAction toggleRawBytecodeAction;
29
30     public BytecodeActionBarContributor() {
31         super();
32         String JavaDoc symbolicName = BytecodeOutlinePlugin.getDefault().getBundle()
33             .getSymbolicName();
34         ImageDescriptor actionIcon = AbstractUIPlugin
35             .imageDescriptorFromPlugin(symbolicName, "icons/bytecodeview.gif");
36
37         refreshAction = new ShowBytecodeAction(actionIcon);
38
39         actionIcon = AbstractUIPlugin.imageDescriptorFromPlugin(
40             symbolicName, "icons/raw_mode.gif");
41         toggleRawBytecodeAction = new ToggleRawBytecodeAction(actionIcon);
42
43     }
44
45     public void contributeToToolBar(IToolBarManager toolBarManager) {
46         super.contributeToToolBar(toolBarManager);
47         toolBarManager.add(refreshAction);
48         toolBarManager.add(toggleRawBytecodeAction);
49         // toolBarManager.add(new Separator(JadclipsePlugin.PID_JADCLIPSE));
50
// toolBarManager.appendToGroup(JadclipsePlugin.PID_JADCLIPSE, dAction);
51
}
52
53     public void contributeToMenu(IMenuManager menu) {
54         super.contributeToMenu(menu);
55         IMenuManager edit = menu
56             .findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
57         if (edit != null) {
58             edit.add(refreshAction);
59             edit.add(toggleRawBytecodeAction);
60         }
61     }
62
63     public void setActiveEditor(IEditorPart targetEditor) {
64         if (targetEditor instanceof BytecodeClassFileEditor) {
65             editor = (BytecodeClassFileEditor) targetEditor;
66             refreshAction.setEnabled(editor.hasMappedSource());
67             refreshAction.setChecked(editor.isDecompiled());
68             toggleRawBytecodeAction.setEnabled(editor.isDecompiled());
69             toggleRawBytecodeAction.setChecked(editor
70                 .getDecompilerFlag(BCOConstants.F_SHOW_RAW_BYTECODE));
71         } else {
72             refreshAction.setEnabled(false);
73             toggleRawBytecodeAction.setEnabled(false);
74             editor = null;
75         }
76         super.setActiveEditor(targetEditor);
77     }
78
79     private class ShowBytecodeAction extends Action {
80
81         protected ShowBytecodeAction(ImageDescriptor actionIcon) {
82             super("Show Bytecode@Ctrl+Shift+B", SWT.TOGGLE);
83             setImageDescriptor(actionIcon);
84             setToolTipText("Show Bytecode");
85             setAccelerator(SWT.CTRL | SWT.SHIFT | 'B');
86         }
87
88         public void run() {
89             if (editor != null) {
90                 ISelection selection = editor.getSelectionProvider()
91                     .getSelection();
92                 boolean showBytecode = isChecked();
93                 if (editor.isDecompiled() != showBytecode) {
94
95                     editor.doSetInput(true, !showBytecode);
96
97                     if (selection instanceof ITextSelection) {
98                         ITextSelection textSelection = (ITextSelection) selection;
99                         textSelection = editor.convertSelection(
100                             textSelection, showBytecode);
101                         editor.getSelectionProvider().setSelection(
102                             textSelection);
103                     }
104                 }
105                 toggleRawBytecodeAction.setEnabled(editor.isDecompiled());
106             }
107         }
108     }
109
110     private class ToggleRawBytecodeAction extends Action {
111
112         protected ToggleRawBytecodeAction(ImageDescriptor actionIcon) {
113             super("Show Internal Types", SWT.TOGGLE);
114             setImageDescriptor(actionIcon);
115             setToolTipText("Show Internal Types");
116         }
117
118         public void run() {
119             if (editor != null) {
120                 editor.setDecompilerFlag(
121                     BCOConstants.F_SHOW_RAW_BYTECODE, isChecked());
122                 ISelection selection = editor.getSelectionProvider()
123                     .getSelection();
124
125                 // we convert selection first to source line bacause bytecode lines could
126
// not match for different bytecode view modes.
127
int sourceLine = 0;
128                 if (selection instanceof ITextSelection) {
129                     sourceLine = editor.getSourceLine((ITextSelection) selection);
130                 }
131
132                 editor.doSetInput(true, false);
133
134                 if (selection instanceof ITextSelection) {
135                     ITextSelection textSelection = editor.convertLine(sourceLine);
136                     if(textSelection != null) {
137                         editor.getSelectionProvider().setSelection(textSelection);
138                     }
139                 }
140             }
141         }
142     }
143
144 }
145
Popular Tags