001: package de.loskutov.bco.editors;
002:
003: import org.eclipse.jdt.internal.ui.javaeditor.ClassFileEditorActionContributor;
004: import org.eclipse.jface.action.Action;
005: import org.eclipse.jface.action.IMenuManager;
006: import org.eclipse.jface.action.IToolBarManager;
007: import org.eclipse.jface.resource.ImageDescriptor;
008: import org.eclipse.jface.text.ITextSelection;
009: import org.eclipse.jface.viewers.ISelection;
010: import org.eclipse.swt.SWT;
011: import org.eclipse.ui.IEditorPart;
012: import org.eclipse.ui.IWorkbenchActionConstants;
013: import org.eclipse.ui.plugin.AbstractUIPlugin;
014:
015: import de.loskutov.bco.BytecodeOutlinePlugin;
016: import de.loskutov.bco.preferences.BCOConstants;
017:
018: /**
019: * Adds "Show Bytecode" action to tool/menu bars
020: * @author V. Grishchenko, Eugene Kuleshov, Andrei
021: */
022: public class BytecodeActionBarContributor extends
023: ClassFileEditorActionContributor {
024:
025: BytecodeClassFileEditor editor;
026: protected ShowBytecodeAction refreshAction;
027: protected ToggleRawBytecodeAction toggleRawBytecodeAction;
028:
029: public BytecodeActionBarContributor() {
030: super ();
031: String symbolicName = BytecodeOutlinePlugin.getDefault()
032: .getBundle().getSymbolicName();
033: ImageDescriptor actionIcon = AbstractUIPlugin
034: .imageDescriptorFromPlugin(symbolicName,
035: "icons/bytecodeview.gif");
036:
037: refreshAction = new ShowBytecodeAction(actionIcon);
038:
039: actionIcon = AbstractUIPlugin.imageDescriptorFromPlugin(
040: symbolicName, "icons/raw_mode.gif");
041: toggleRawBytecodeAction = new ToggleRawBytecodeAction(
042: actionIcon);
043:
044: }
045:
046: public void contributeToToolBar(IToolBarManager toolBarManager) {
047: super .contributeToToolBar(toolBarManager);
048: toolBarManager.add(refreshAction);
049: toolBarManager.add(toggleRawBytecodeAction);
050: // toolBarManager.add(new Separator(JadclipsePlugin.PID_JADCLIPSE));
051: // toolBarManager.appendToGroup(JadclipsePlugin.PID_JADCLIPSE, dAction);
052: }
053:
054: public void contributeToMenu(IMenuManager menu) {
055: super .contributeToMenu(menu);
056: IMenuManager edit = menu
057: .findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
058: if (edit != null) {
059: edit.add(refreshAction);
060: edit.add(toggleRawBytecodeAction);
061: }
062: }
063:
064: public void setActiveEditor(IEditorPart targetEditor) {
065: if (targetEditor instanceof BytecodeClassFileEditor) {
066: editor = (BytecodeClassFileEditor) targetEditor;
067: refreshAction.setEnabled(editor.hasMappedSource());
068: refreshAction.setChecked(editor.isDecompiled());
069: toggleRawBytecodeAction.setEnabled(editor.isDecompiled());
070: toggleRawBytecodeAction
071: .setChecked(editor
072: .getDecompilerFlag(BCOConstants.F_SHOW_RAW_BYTECODE));
073: } else {
074: refreshAction.setEnabled(false);
075: toggleRawBytecodeAction.setEnabled(false);
076: editor = null;
077: }
078: super .setActiveEditor(targetEditor);
079: }
080:
081: private class ShowBytecodeAction extends Action {
082:
083: protected ShowBytecodeAction(ImageDescriptor actionIcon) {
084: super ("Show Bytecode@Ctrl+Shift+B", SWT.TOGGLE);
085: setImageDescriptor(actionIcon);
086: setToolTipText("Show Bytecode");
087: setAccelerator(SWT.CTRL | SWT.SHIFT | 'B');
088: }
089:
090: public void run() {
091: if (editor != null) {
092: ISelection selection = editor.getSelectionProvider()
093: .getSelection();
094: boolean showBytecode = isChecked();
095: if (editor.isDecompiled() != showBytecode) {
096:
097: editor.doSetInput(true, !showBytecode);
098:
099: if (selection instanceof ITextSelection) {
100: ITextSelection textSelection = (ITextSelection) selection;
101: textSelection = editor.convertSelection(
102: textSelection, showBytecode);
103: editor.getSelectionProvider().setSelection(
104: textSelection);
105: }
106: }
107: toggleRawBytecodeAction.setEnabled(editor
108: .isDecompiled());
109: }
110: }
111: }
112:
113: private class ToggleRawBytecodeAction extends Action {
114:
115: protected ToggleRawBytecodeAction(ImageDescriptor actionIcon) {
116: super ("Show Internal Types", SWT.TOGGLE);
117: setImageDescriptor(actionIcon);
118: setToolTipText("Show Internal Types");
119: }
120:
121: public void run() {
122: if (editor != null) {
123: editor.setDecompilerFlag(
124: BCOConstants.F_SHOW_RAW_BYTECODE, isChecked());
125: ISelection selection = editor.getSelectionProvider()
126: .getSelection();
127:
128: // we convert selection first to source line bacause bytecode lines could
129: // not match for different bytecode view modes.
130: int sourceLine = 0;
131: if (selection instanceof ITextSelection) {
132: sourceLine = editor
133: .getSourceLine((ITextSelection) selection);
134: }
135:
136: editor.doSetInput(true, false);
137:
138: if (selection instanceof ITextSelection) {
139: ITextSelection textSelection = editor
140: .convertLine(sourceLine);
141: if (textSelection != null) {
142: editor.getSelectionProvider().setSelection(
143: textSelection);
144: }
145: }
146: }
147: }
148: }
149:
150: }
|