Commit 39a054bc authored by andrei's avatar andrei

Added "attach source" button to the editor toolbar.

Removed some meaningless contributions from plugin.xml.
parent efe0cf86
......@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bytecode Outline Plug-in
Bundle-SymbolicName: de.loskutov.BytecodeOutline;singleton:=true
Bundle-Version: 2.2.10
Bundle-Version: 2.2.11
Bundle-ClassPath: .,
lib/asm-3.2.jar,
lib/asm-analysis-3.2.jar,
......
......@@ -114,7 +114,9 @@ public class BytecodeOutlinePlugin extends AbstractUIPlugin {
if (messageID != null) {
message = getResourceString(messageID);
}
message = message + " " + error.getMessage();//$NON-NLS-1$
if (error != null) {
message += " " + error.getMessage();
}
MessageDialog.openError(
shell, getResourceString("BytecodeOutline.Title"), //$NON-NLS-1$
message);
......
package de.loskutov.bco.editors;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ClasspathContainerInitializer;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.ui.javaeditor.ClassFileEditorActionContributor;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
......@@ -8,6 +22,8 @@ import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.plugin.AbstractUIPlugin;
......@@ -26,6 +42,7 @@ public class BytecodeActionBarContributor
BytecodeClassFileEditor editor;
protected ShowBytecodeAction refreshAction;
protected ToggleRawBytecodeAction toggleRawBytecodeAction;
private final AttachSourceAction attachAction;
public BytecodeActionBarContributor() {
super();
......@@ -40,12 +57,16 @@ public class BytecodeActionBarContributor
symbolicName, "icons/raw_mode.gif");
toggleRawBytecodeAction = new ToggleRawBytecodeAction(actionIcon);
actionIcon = AbstractUIPlugin.imageDescriptorFromPlugin(
symbolicName, "icons/source.gif");
attachAction = new AttachSourceAction(actionIcon);
}
public void contributeToToolBar(IToolBarManager toolBarManager) {
super.contributeToToolBar(toolBarManager);
toolBarManager.add(refreshAction);
toolBarManager.add(toggleRawBytecodeAction);
toolBarManager.add(attachAction);
// toolBarManager.add(new Separator(JadclipsePlugin.PID_JADCLIPSE));
// toolBarManager.appendToGroup(JadclipsePlugin.PID_JADCLIPSE, dAction);
}
......@@ -57,6 +78,7 @@ public class BytecodeActionBarContributor
if (edit != null) {
edit.add(refreshAction);
edit.add(toggleRawBytecodeAction);
edit.add(attachAction);
}
}
......@@ -68,9 +90,11 @@ public class BytecodeActionBarContributor
toggleRawBytecodeAction.setEnabled(editor.isDecompiled());
toggleRawBytecodeAction.setChecked(editor
.getDecompilerFlag(BCOConstants.F_SHOW_RAW_BYTECODE));
attachAction.setEnabled(editor.isSourceAttachmentPossible());
} else {
refreshAction.setEnabled(false);
toggleRawBytecodeAction.setEnabled(false);
attachAction.setEnabled(false);
editor = null;
}
super.setActiveEditor(targetEditor);
......@@ -107,6 +131,73 @@ public class BytecodeActionBarContributor
}
}
private class AttachSourceAction extends Action {
protected AttachSourceAction(ImageDescriptor actionIcon) {
super("Attach Source...", SWT.NONE);
setImageDescriptor(actionIcon);
setToolTipText("Attach Source...");
}
public void run() {
if (editor == null) {
return;
}
IPackageFragmentRoot root = editor.getPackageFragmentRoot(editor.getClassFile());
try {
IClasspathEntry entry = root.getRawClasspathEntry();
IPath containerPath = null;
IJavaProject javaProject = root.getJavaProject();
if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
containerPath = entry.getPath();
IClasspathContainer container = JavaCore
.getClasspathContainer(containerPath, javaProject);
if(!isSourceAttachmentPossible(containerPath, javaProject)){
editor.setSourceAttachmentPossible(false);
attachAction.setEnabled(false);
BytecodeOutlinePlugin
.error("Unable to configure source attachment:\n"
+ "classpath entry '" + containerPath +
"'\nis either read-only "
+ "or source attachment is not supported...", null);
return;
}
entry = JavaModelUtil.findEntryInContainer(container, root
.getPath());
}
Shell shell = Display.getDefault().getActiveShell();
IClasspathEntry cpe = BuildPathDialogAccess
.configureSourceAttachment(shell, entry);
if (cpe == null) {
return;
}
String[] changedAttributes = {CPListElement.SOURCEATTACHMENT};
BuildPathSupport.modifyClasspathEntry(
shell, cpe, changedAttributes, javaProject, containerPath,
new NullProgressMonitor());
} catch (CoreException e) {
BytecodeOutlinePlugin.error(
"Unable to configure source attachment", e);
}
}
private boolean isSourceAttachmentPossible(IPath containerPath,
IJavaProject javaProject) {
ClasspathContainerInitializer initializer = JavaCore
.getClasspathContainerInitializer(containerPath
.segment(0));
IStatus status = initializer.getSourceAttachmentStatus(
containerPath, javaProject);
if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED
|| status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
return false;
}
return true;
}
}
private class ToggleRawBytecodeAction extends Action {
protected ToggleRawBytecodeAction(ImageDescriptor actionIcon) {
......
......@@ -18,6 +18,7 @@ import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
......@@ -40,6 +41,7 @@ import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
......@@ -74,6 +76,7 @@ public class BytecodeClassFileEditor extends ClassFileEditor
private boolean hasMappedSource;
private boolean decompiled;
private boolean initDone;
private boolean sourceAttachmentPossible;
/**
* Constructor for JadclipseClassFileEditor.
......@@ -95,6 +98,7 @@ public class BytecodeClassFileEditor extends ClassFileEditor
decompilerFlags.set(BCOConstants.F_SHOW_LINE_INFO, true);
decompilerFlags.set(BCOConstants.F_SHOW_VARIABLES, true);
decompilerFlags.set(BCOConstants.F_SHOW_RAW_BYTECODE, false);
setSourceAttachmentPossible(true);
}
/**
......@@ -658,17 +662,25 @@ public class BytecodeClassFileEditor extends ClassFileEditor
/*
* @see ClassFileDocumentProvider.InputChangeListener#inputChanged(IClassFileEditorInput)
*/
public void inputChanged(IClassFileEditorInput input) {
fInputUpdater.post(input);
IClassFile cf = input.getClassFile();
String source;
try {
source = cf.getSource();
setDecompiled(source != null && source.startsWith(MARK));
} catch (JavaModelException e) {
BytecodeOutlinePlugin.log(e, IStatus.ERROR);
}
public void inputChanged(final IClassFileEditorInput input) {
Runnable updateInput = new Runnable() {
public void run() {
fInputUpdater.post(input);
IClassFile cf = input.getClassFile();
try {
String source = cf.getSource();
setDecompiled(source != null && source.startsWith(MARK));
} catch (JavaModelException e) {
BytecodeOutlinePlugin.log(e, IStatus.ERROR);
}
}
};
if(Display.getCurrent() == null){
Display.getDefault().asyncExec(updateInput);
} else {
updateInput.run();
}
}
/*
......@@ -809,6 +821,22 @@ public class BytecodeClassFileEditor extends ClassFileEditor
return false;
}
/**
* Returns the package fragment root of this file.
*
* @param file the class file
* @return the package fragment root of the given class file
*/
IPackageFragmentRoot getPackageFragmentRoot(IClassFile file) {
IJavaElement element= file.getParent();
while (element != null && element.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
element= element.getParent();
}
return (IPackageFragmentRoot) element;
}
private static BytecodeClassFileEditor getBytecodeEditor(IClassFile parent) {
IEditorReference[] editorReferences = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage().getEditorReferences();
......@@ -877,4 +905,11 @@ public class BytecodeClassFileEditor extends ClassFileEditor
return -1;
}
public void setSourceAttachmentPossible(boolean sourceAttachmentPossible) {
this.sourceAttachmentPossible = sourceAttachmentPossible;
}
public boolean isSourceAttachmentPossible() {
return sourceAttachmentPossible && isDecompiled() && !hasMappedSource();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment