Recipe 12.10 Coding a Plug-in Action

     

12.10.1 Problem

You have an action, and you need to make it actually do something.

12.10.2 Solution

Edit its .java file, and implement the support you need.

12.10.3 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"  ,     "  This plug-in is functional.");  } 

That finishes the plug-in. Now save all files, and rebuild the project. To test this new plug-in, launch the Run-time Workbench.

Note that there's one more step to load the plug-in, which wasn't necessary with the plug-in developed using a wizard in the earlier part of this chapter. We have to explicitly add the new plug-in to the current perspective to see it, so select Window Customize Perspective in the Run-time Workbench, select the box next to Action Set 1, as shown in Figure 12-22, and click OK.

Figure 12-22. Customizing the perspective
figs/ecb_1222.gif

You should now see the default square button icon that Eclipse uses for toolbar buttons and the new menu, Menu 1 , as shown in Figure 12-23.

Figure 12-23. A new plug-in at work
figs/ecb_1223.gif

Select New Menu Action 1, or click the New button. You should see the New Plug-in message box, as shown in Figure 12-24. Congratulations, you're an Eclipse plug-in developer.

Figure 12-24. The message from the new plug-in
figs/ecb_1224.gif

Having to customize the perspective every time you launch a new plug-in using Window Customize Perspective is a little cumbersome. Take a look at the next recipe to see how to get around it.

12.10.4 See Also

Recipe 12.7 on responding to a user's action; Recipe 12.9 on creating Action sets; Recipe 12.11 on adding a plug-in to a perspective.



Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net