DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 12.7 Responding to User Actions in a Plug-in

12.7.1 Problem

You want to let your plug-in respond to user actions in the Eclipse IDE.

12.7.2 Solution

Create an action set, and support it in your plug-in's code. You can do this with the New Extension wizard.

12.7.3 Discussion

An action represents an action the user can perform; for example, you can connect actions to elements such as menus and toolbars. To specify what happens when an action is activated, you extend the Action class. After you've created an action, you can use it in a variety of places, such as menu item selections or toolbar button clicks; the same action object will make the same thing happen in either case. An action set is, as the name implies, a set of actions. You connect actions to items such as menus and toolbars using an action set.

To develop action sets, you use extension points. An extension point lets one plug-in build on what another plug-in exports. Action sets such as those that enable you to implement menu and toolbar actions are extensions of the org.eclipse.ui.actionSets extension point.

Plug-ins can use only those classes that are exported by other plug-ins, which makes extension points especially important. Much support for custom plug-ins is already built into several standard plug-ins that come with Eclipse. To let a plug-in use your Java code, you can put that code in a .jar file; then you can wrap the .jar file inside a plug-in so that other plug-ins can access your code.


To create an action set which ties a menu and toolbar button to an action, click the Extension tab in the MenuPlugInFromScratch project's plug-in manifest editor, and click Add to open the New Extension wizard shown in Figure 12-15. Select Generic Wizards in the left box, select Schema-based Extension in the right box, and click Next.

Figure 12-15. Using the New Extension wizard
figs/ecb_1215.gif


The next dialog enables you to select the extension point on which you want to build, as shown in Figure 12-16. Because we want to create an action set to support toolbar buttons and menu items, we'll use the org.eclipse.ui.actionSets extension point. Select it, as shown in the figure, and click Finish.

Figure 12-16. Selecting an extension point
figs/ecb_1216.gif


When you click Finish, an entry for the org.eclipse.ui.actionSets extension point is added to the Extensions pane in the Eclipse manifest editor, as shown in Figure 12-17. To create a new action set, right-click org.eclipse.ui.actionSets in the All Extensions box and select New actionSet, creating a new action set, as shown in the figure.

Figure 12-17. A new action set
figs/ecb_1217.gif


To set the properties of this new action set, select it and, in the Properties view at the bottom of the Eclipse window, set the label property to Action Set 1 and the visible property to true, as shown in Figure 12-18.

Figure 12-18. Configuring the action set
figs/ecb_1218.gif


That creates the new action set.

    Previous Section  < Day Day Up >  Next Section