12.10. Coding a Plug-in Action
Problem
You have an action, and you need to make it actually do something.
Solution
Edit its .java
file, and implement the support
you need.
Discussion
Continuing the example developed over the previous three recipes,
open Action1.java
now if it’s
not already open. This file contains plenty of
TODO
s, such as adding code to the
action’s constructor to customize it, which
we’ll ignore in this example. Here,
we’ll display a message box when the user selects
our menu item or clicks our toolbar button, as the plug-in developed
earlier in this chapter did.
To do that, we’ll need an object that implements the
Workbench window interface, IWorkbenchWindow
. The
object we need is passed to the init
method in
Action1.java
, so we’ll begin by
creating a private
class variable,
window
, to hold it:
public class Action1 implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
.
.
.
Then we’ll store the Workbench window passed to us
in the init
method in this variable like so:
public void init(IWorkbenchWindow window) {
this.window = window;
}
We can use this variable and the openInformation
method of the MessageDialog
class to display a
message box with the message This
plug-in
is
functional
. in the action’s
run
method. To use the
MessageDialog
class, we first import it and then
call it in the run
method:
import org.eclipse.jface.dialogs.MessageDialog; . . . public void run(IAction action) { MessageDialog.openInformation( window.getShell( ), "New Plug-in" ...
Get Eclipse Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.