creating plug-ins using the eclipse pde.pdf

35
All rights reserved. Reproduction and/or distribution in whole or in part in electronic, paper or other forms without written permission is prohibited. Java ReferencePoint Suite SkillSoft Corporation. (c) 2002. Copying Prohibited. Reprinted for Balaji Nallathambi, Verizon [email protected] Reprinted with permission as a subscription benefit of Books24x7, http://www.books24x7.com/

Upload: swaminathan-ramasubramanian

Post on 16-Apr-2015

34 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Creating Plug-ins using the Eclipse PDE.pdf

All rights reserved. Reproduction and/or distribution in whole or inpart in electronic, paper or other forms without written permission

is prohibited.

Java ReferencePoint SuiteSkillSoft Corporation. (c) 2002. Copying Prohibited.

Reprinted for Balaji Nallathambi, [email protected]

Reprinted with permission as a subscription benefit of Books24x7,http://www.books24x7.com/

Page 2: Creating Plug-ins using the Eclipse PDE.pdf

Table of Contents Point 25: Creating Plug−ins using the Eclipse PDE......................................................................1

Introducing the Eclipse PDE............................................................................................................2 Understanding the PDE Workbench.......................................................................................2 Understanding the Plug−in Manifest Editor.............................................................................2 Understanding the Plugin.xml File..........................................................................................7

Creating a JDBCView Plug−in Project ............................................................................................8 Adding References to the JDBCView Plug−in Project..........................................................12 Creating the JdbcView Class................................................................................................15 Configuring the Plugin.xml File for the JDBCView Plug−in...................................................20 Running the JDBCView Plug−in...........................................................................................21

Creating a Button in Toolbar .........................................................................................................24 Creating the TestTool Class.................................................................................................25 Configuring the plugin.xml File for the Toolbar Project.........................................................28 Running the Toolbar Button Plug−in.....................................................................................29

Related Topics ................................................................................................................................33

i

Page 3: Creating Plug-ins using the Eclipse PDE.pdf

Point 25: Creating Plug−ins using the Eclipse PDEP. SuryaThe Plug−in Development Environment (PDE) in Eclipse is a toolset, which allows you to create,test, debug, and deploy plug−ins. You can deploy these plug−ins in the Eclipse platform to extendthe functionality of the platform. To create and deploy a plug−in, you need to create a manifest file,write Java source code, compile the code into a library, and test and package the plug−in.

This ReferencePoint introduces the Eclipse PDE. It also describes how to create a view and abutton plug−in in the Eclipse PDE.

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 4: Creating Plug-ins using the Eclipse PDE.pdf

Introducing the Eclipse PDEThe Eclipse PDE workbench integrates itself into the Eclipse workbench by providing tools, such asnew perspectives, wizards and editors. As a result, you need not launch the Eclipse PDEseparately. These tools enable you to develop and test plug−ins inside the Eclipse workbench.

Note To learn more about the Eclipse workbench, see the Understanding Eclipse IDEReferencePoint.

The Eclipse PDE enables you to create plug−in projects. A plug−in project contains folders andfiles, such as manifest file, Java code library, and properties files.

Understanding the PDE Workbench

The Eclipse PDE provides two instances of the Eclipse workbench, host instance, and runtimeinstance. You can use these workbench instances to develop, test, debug, and deploy the plug−insthat you build in your project. You can use the Eclipse PDE host workbench instance to developplug−ins. The host workbench instance allows you to use plug−ins that are available in the EclipseIDE.

The Eclipse PDE provides a launcher to test your plug−ins. The launcher is a runtime workbenchinstance that runs independent of the Eclipse host workbench. The runtime workbench instanceintegrates the plug−ins you need to test with the features of the Eclipse PDE workbench allowingyou to test the plug−ins in a real−time environment.

Understanding the Plug−in Manifest Editor

The plug−in Manifest Editor is a multi−page UI editor in the Eclipse PDE. You can view the plug−inManifest Editor after creating a plug−in project. The plug−in Manifest Editor generates theplugin.xml file also called plug−in manifest file, which describes its content to the Eclipse runtime.When you select the plugin.xml file, you can view the plug−in Manifest Editor, which consists ofpages, such as Overview, Dependencies, Runtime, Extensions, Extension Points, and Source.

The Overview page of the plug−in Manifest Editor provides you an outline view of the subsequentpages, as shown in Figure 2−25−1:

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 5: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−1: The Overview Page of the Plug−in Manifest EditorThe Overview page displays the basic plug−in information, such as plug−in identifier, plug−in name,version, and provider name. You can edit this information in the General Information section. Youcan only view the contents of other sections in the Overview page.

The Dependencies page allows you to select a list of plug−ins that are required for your plug−in towork. Figure 2−25−2 shows the Dependencies page:

Java ReferencePoint Suite 3

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 6: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−2: The Dependencies Page of the Plug−in Manifest EditorThe Run−time Information page helps you to add runtime libraries to your plug−in. This page allowsyou to include library content, specify library type, and library exporting rules. Figure 2−25−3 showsthe Run−time Information page:

Java ReferencePoint Suite 4

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 7: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−3: The Run−time Information Page of the Plug−in Manifest EditorYou can use the Extensions page to add extensions to your plug−in. You can browse and editplug−in extensions using the Extensions page. The extensions serve as a key mechanism to extendthe behavior of a plug−in. Figure 2−25−4 shows the Extensions page:

Java ReferencePoint Suite 5

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 8: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−4: The Extensions Page of the Plug−in Manifest EditorThe Extension Points page allows you to define the extension points for a plug−in. Other plug−inscan use this extension point to extend their operation. Figure 2−25−5 shows the Extension Pointspage:

Figure 2−25−5: The Extension Points Page of the Plug−in Manifest Editor

Java ReferencePoint Suite 6

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 9: Creating Plug-ins using the Eclipse PDE.pdf

The plug−in Manifest Editor provides a Source page to edit the plug−in code. Figure 2−25−6 showsthe Source page:

Figure 2−25−6: The Source Page of the Plug−in Manifest EditorUnderstanding the Plugin.xml File

Each plug−in contains a plug−in manifest file or plugin.xml file in the Eclipse workbench. Theplugin.xml file describes the properties of plug−in, such as Plug−in name, Plug−in version, Plug−inruntime JAR name, dependencies, and extensions to the Eclipse runtime.

You can configure the information in the plugin.xml file using the Eclipse PDE. The plugin.xml filedescribes the plug−in including the location of code and the extensions it is using.

Java ReferencePoint Suite 7

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 10: Creating Plug-ins using the Eclipse PDE.pdf

Creating a JDBCView Plug−in ProjectYou can create a plug−in project using the File menu or the New Wizard button on the toolbarlocated in the Eclipse workbench. For example, you can create a JDBCView plug−in, which displaysa user interface in the workbench. This interface is a view in the workbench that accepts values,such as JDBC driver, URL, user name, and password. You can extend this user interface to performother functions, such as connecting to a database. To create the JDBCView plug−in:

Select File−>New−>Project. The New Project dialog box appears.1.

Select the Plug−in Development option in the left pane of the New Project dialog box andPlug−in Project in the right pane, as shown in Figure 2−25−7:

Figure 2−25−7: The New Project Dialog Box

2.

Click the Next button. The Plug−in Project Name page of the New Plug−in Project dialog boxappears.

3.

Specify a name for the project as JDBCView in the Project name text box.4.

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 11: Creating Plug-ins using the Eclipse PDE.pdf

Note Do not use spaces and special characters in the project name.

Clear the Use default check box and specify the directory where you want to save theproject, as shown in Figure 2−25−8:

Figure 2−25−8: The Plug−in Project Name Page of the New Plug−in Project Dialog Box

5.

Click the Next button. The Plug−in Project Structure page appears.6.

Select the Create a Java project option and provide a value for output folder, plug−in runtimelibrary name and source folder, as shown in Figure 2−25−9:

7.

Java ReferencePoint Suite 9

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 12: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−9: The Plug−in Project Structure Page of the New Plug−in Project Dialog Box

Click the Next button. The Plug−in Code Generators page appears to define the plug−incode generation options, as shown in Figure 2−25−10:

8.

Java ReferencePoint Suite 10

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 13: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−10: The Plug−in Code Generators Page of the New Plug−in Project DialogBox

Click the Finish button to create the project. The Plug−in project is created by the EclipsePDE with default values, as shown in Figure 2−25−11:

Figure 2−25−11: The Package Explorer with the Default Set of Configuration

9.

Java ReferencePoint Suite 11

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 14: Creating Plug-ins using the Eclipse PDE.pdf

Adding References to the JDBCView Plug−in Project

The user interface components used in this example extend the Standard Widget Toolkit (SWT)user interface API�s that are available in the Eclipse project. You can import the org.eclipse.ui andthe org.eclipse.core.boot packages, and other dependent packages, such asorg.eclipse.core.resources and org.eclipse.help to the JDBCView plug−in project. These packagesprovide the basic support for launching the Eclipse platform. To import the references:

Select File−>Import. The Import wizard appears.1.

Select the External Plug−ins and Fragments from the available import source options, asshown in Figure 2−25−12:

Figure 2−25−12: The Import Wizard

2.

Click the Next button. The Import External Plug−ins page of the Import Plug−ins andFragments dialog box appears, as shown in Figure 2−25−13:

3.

Java ReferencePoint Suite 12

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 15: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−13: The Import Plug−ins and Fragments Dialog Box

Click the Next button. The Selection page of the Import Plug−ins and Fragments dialog boxappears.

4.

Select org.eclipse.ui and org.eclipse.core.boot packages and other dependent packages.Figure 2−25−14 shows the packages from which you can select:

5.

Java ReferencePoint Suite 13

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 16: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−14: The Selection Page of the Import Plug−ins and Fragments Dialog Box

Click Finish. The Package Explorer appears, as shown in Figure 2−25−15:6.

Java ReferencePoint Suite 14

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 17: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−15: The Package Explorer Dialog Box

Creating the JdbcView Class

You need to create the JdbcView class to display the fields, such as JDBC driver, URL, user nameand password, which accept data from the end user. You can create the JdbcView class byextending the existing ViewPart class of the Eclipse PDE. To create the JdbcView class:

Right click the src folder in the JDBCView project and select New −>Class option. The NewJava Class dialog box appears. Type the class name as JdbcView in the Name field of theJava Class page, as shown in Figure 2−25−16:

1.

Java ReferencePoint Suite 15

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 18: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−16: The New Java Class Dialog Box

Click the Browse button to select superclass for the JdbcView class. The SuperclassSelection window appears, as shown in Figure 2−25−17:

2.

Java ReferencePoint Suite 16

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 19: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−17: The Superclass Selection Window

The JdbcView class should extend the ViewPart class to implement a view. As a result, youneed to select the ViewPart class as superclass and click OK. The New Java Class creationdialog box appears with the superclass name, as shown in Figure 2−25−18:

3.

Java ReferencePoint Suite 17

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 20: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−18: The New Java Class Dialog Box for the JdbcView Class

Click Finish button. The Eclipse PDE generates code for the JdbcView class, which appearsin the Source page of the plug−in Manifest Editor.

4.

Listing 2−25−1 shows the JdbcView class with the generated code:

Listing 2−25−1: The JdbcView Class

package jdbcview;import org.eclipse.swt.widgets.Composite;import org.eclipse.ui.part.ViewPart;/*** @author Xyz** To change the template for this generated type comment go to* Window>Preferences>Java>Code Generation>Code and Comments*/public class JdbcView extends ViewPart { /* (non−Javadoc) * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) */ public void createPartControl(Composite parent) { // TODO Auto−generated method stub }

Java ReferencePoint Suite 18

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 21: Creating Plug-ins using the Eclipse PDE.pdf

/* (non−Javadoc) * @see org.eclipse.ui.IWorkbenchPart#setFocus() */ public void setFocus() { // TODO Auto−generated method stub }}

In the above code, by default the ViewPart class inherits the methods createPartControl() and thesetFocus() from the IViewPart class, which defines the interface for views. You can write theimplementation of the JdbcView class by including the logic for these methods.

Listing 2−25−2 shows the modified version of the JdbcView class:

Listing 2−25−2: The Modified JdbcView Class

/* * Created on Aug 29, 2003 * This class helps in creating a new view that help * in configuring the JDBC properties. This Class is * an example for creating the User Interface only. * You can extend the functionality according to your * requirements. * */package jdbcview;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Text;import org.eclipse.ui.part.ViewPart;/** * @author Xyz * * This class helps in displaying the view containing the labels * and text fields that accept JDBC properties. * */public class JdbcView extends ViewPart { /* (non−Javadoc) * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) */ Text driverTxt, urlTxt, userTxt, passTxt; Label driver,url,userName,password; /** * */ public JdbcView() { super(); // TODO Auto−generated constructor stub } public void createPartControl(Composite parent) { // TODO Auto−generated method stub GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 2; parent.setLayout(gridLayout); driver = new Label(parent, SWT.NONE); driver.setText("JDBC Driver"); driverTxt = new Text(parent,SWT.BORDER); url = new Label(parent, SWT.NONE); url.setText("URL"); urlTxt = new Text(parent,SWT.BORDER); userName = new Label(parent, SWT.NONE); userName.setText("User Name");

Java ReferencePoint Suite 19

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 22: Creating Plug-ins using the Eclipse PDE.pdf

userTxt = new Text(parent,SWT.BORDER); password = new Label(parent, SWT.NONE); password.setText("Password"); passTxt = new Text(parent,SWT.BORDER); passTxt.setEchoChar('#'); GridData gridData = new GridData(); gridData.widthHint = 100; driver.setLayoutData(gridData); gridData = new GridData(); gridData.widthHint = 100; url.setLayoutData(gridData); gridData = new GridData(); gridData.widthHint = 100; userName.setLayoutData(gridData); gridData = new GridData(); gridData.widthHint = 100; password.setLayoutData(gridData); GridData gridData2 = new GridData(GridData.FILL_VERTICAL); gridData2.widthHint = 100; driverTxt.setLayoutData(gridData2); gridData2 = new GridData(GridData.FILL_VERTICAL); gridData2.widthHint = 100; urlTxt.setLayoutData(gridData2); gridData2 = new GridData(GridData.FILL_VERTICAL); gridData2.widthHint = 100; userTxt.setLayoutData(gridData2); gridData2 = new GridData(GridData.FILL_VERTICAL); gridData2.widthHint = 100; passTxt.setLayoutData(gridData2); } /* (non−Javadoc) * @see org.eclipse.ui.IWorkbenchPart#setFocus() */ public void setFocus() { // TODO Auto−generated method stub } public void widgetSelected(SelectionEvent e) { }}

In the above code, you modify the method createPartControl() to add controls, such as Label, andTextField to accept the values, such as database URL, user name and password to connect to thedatabase.

Configuring the Plugin.xml File for the JDBCView Plug−in

You need to configure the plugin.xml file, after creating the JdbcView class. By default, theplugin.xml file would contain the information provided in the Listing 2−25−3:

Listing 2−25−3: The plugin.xml File for the JdbcView Class

<?xml version="1.0" encoding="UTF−8"?><pluginid="JDBCView"name="JDBCView"version="1.0.0"><runtime><library name="JDBCView.jar"/></runtime></plugin>

The above code contains the plug−in id, plug−in name, and the runtime library name configured init. You can use the extension tag to specify the JdbcView class in the plugin.xml file.

Java ReferencePoint Suite 20

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 23: Creating Plug-ins using the Eclipse PDE.pdf

Listing 2−25−4 shows the plugin.xml file with the JdbcView class configured in it:

Listing 2−25−4: Configured plugin.xml File for the JdbcView Class

<?xml version="1.0" encoding="UTF−8"?><pluginid="JDBCView"name="JDBCView"version="1.0.0"><runtime><library name="JDBCView.jar"/></runtime><requires><import plugin="org.eclipse.ui"/><import plugin="org.eclipse.core.runtime"/></requires><extension point="org.eclipse.ui.views"><category name="JDBC Views" id="jdbc.views"/><viewclass="jdbcview.JdbcView"category="jdbc.views"name ="JDBC View"id ="custom.jdbc.views"/></extension></plugin>

The above code contains the extension tag, which specifies that the plug−in is an extension oforg.eclipse.ui.views. The View tag specifies the name of the view and the class name of the view.

Running the JDBCView Plug−in

You can invoke the JDBCView plug−in from the Eclipse PDE and run on a separate Eclipseinstance. To run the JDBCView plug−in:

Click the Run button on the toolbar and select the Run As option, as shown in Figure2−25−19:

Figure 2−25−19: The Runtime Workbench Option

1.

Java ReferencePoint Suite 21

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 24: Creating Plug-ins using the Eclipse PDE.pdf

Click Run−time Workbench, a new instance of Eclipse opens, as shown in Figure 2−25−20:

Figure 2−25−20: The New Eclipse Window

2.

Select Window−>Customize Perspective, the Customize Perspective dialog box appears.3.

Expand the Window−>Show View option and select the JDBC View option in the CustomizePerspective dialog box, as shown in Figure 2−25−21:

Figure 2−25−21: JDBC View Option

4.

Java ReferencePoint Suite 22

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 25: Creating Plug-ins using the Eclipse PDE.pdf

Click OK. The Eclipse window appears. Select the menu option Window−> ShowView−>JDBC View option, as shown in Figure 2−25−22:

Figure 2−25−22: The Eclipse Window with the JDBC View Selected

5.

Place the JDBC View in the Workspace, as shown in Figure 2−25−23:

Figure 2−25−23: The Eclipse Window with the JDBC View

6.

Java ReferencePoint Suite 23

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 26: Creating Plug-ins using the Eclipse PDE.pdf

Creating a Button in ToolbarYou can create a new button as a plug−in project and add this button to the toolbar. You can assignany action to the click event of this button. To create a Toolbar project:

Select the Plug−in Project in the New Plug−in Project dialog box. Click the Next button. ThePlug−in Project page of the New Plug−in Project dialog box appears, as shown in the Figure2−25−8.

1.

Specify a name for the project as com.test.toolbar in the Project name text box.2.

Clear the Use default check box and click the Browse button to select the folder where youwant to save the project.

3.

Click the Next button. The Plug−in Project Structure page of the New Plug−in Projectappears, as shown in the Figure 2−25−9.

4.

Select the Create a Java project option and provide a value for output folder, plug−in runtimelibrary name, and source folder.

5.

Click the Next button. The Plug−in Code Generators page of the New Plug−in Project dialogbox appears, as shown in the Figure 2−25−10.

6.

Click the Finish button to create the Toolbar project. The Eclipse PDE creates the Toolbarproject with default values, as shown in Figure 2−25−24:

7.

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 27: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−24: The Package Explorer with the Default Set of Configuration for the ToolbarProject

You can add the org.eclipse.ui, the org.eclipse.core.boot packages, and the dependenciesto the Toolbar project to display the button in the toolbar.

8.

Creating the TestTool Class

You can enable the button created in the toolbar to handle events. The click event of the buttonshould invoke an action. For this you can create a TestToolAction class, which implements theIWorkbenchWindowActionDelegate interface. The TestToolAction class should override themethods of this interface. When the end user clicks the button, a popup message is displayed to theend user using the MessageDialog class. To create a TestToolAction class:

Right click the src folder in the project com.test.toolbar and select New −> Class option. TheNew Java Class dialog appears.

1.

Provide the class name as TestToolAction and click the Add button. The ImplementedInterfaces Selection window for TestToolAction class appears, as shown in Figure 2−25−25:

2.

Java ReferencePoint Suite 25

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 28: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−25: The Implemented Interfaces Selection Window

Select the IWorkbenchWindowActionDelegate interface and click OK.3.

Select the Constructors from superclass and Inherited abstract methods checkbox options inthe New Java Class dialog box, as shown in Figure 2−25−26:

4.

Java ReferencePoint Suite 26

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 29: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−26: The New Java Class Dialog Box for the Toolbar Project

Click Finish to create the TestToolAction class. The Eclipse PDE generates code for theTestToolAction class that appears in the source page of the plug−in Manifest Editor.

5.

You can add the default constructor and the inherited abstract methods, after creating theTestToolAction class.

Listing 2−25−5 shows the constructors and the inherited abstract methods included while creatingthe TestToolAction class:

Listing 2−25−5: The TestToolAction Class

/* * Created on Sep 4, 2004 * * To change the template for this generated file go to * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments */package com.test.toolbar;import org.eclipse.jface.action.IAction;import org.eclipse.jface.viewers.ISelection;import org.eclipse.ui.IWorkbenchWindow;import org.eclipse.ui.IWorkbenchWindowActionDelegate;/** * @author xyz * * To change the template for this generated type comment go to * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments */

Java ReferencePoint Suite 27

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 30: Creating Plug-ins using the Eclipse PDE.pdf

public class TestToolAction implements IWorkbenchWindowActionDelegate { /** * */ public TestToolAction() { super(); // TODO Auto−generated constructor stub } /* (non−Javadoc) * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() */ public void dispose() { // TODO Auto−generated method stub } /* (non−Javadoc) * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow) */ public void init(IWorkbenchWindow window) { // TODO Auto−generated method stub } /* (non−Javadoc) * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ public void run(IAction action) { // TODO Auto−generated method stub } /* (non−Javadoc) * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) */ public void selectionChanged(IAction action, ISelection selection) { // TODO Auto−generated method stub }}

In the above code, modify the run method to include the following line of code that displays the testmessage:

MessageDialog.openInformation(null, null,"Test ToolButton");

When you click the button in the toolbar, the screen displays this message. After modifying the run()method, it is necessary to update the plugin.xml file for the toolbar button.

Configuring the plugin.xml File for the Toolbar Project

You need to configure the plugin.xml file, after creating the Toolbar project to display the button inthe toolbar. By default, the plugin.xml file would contain the information provided in the Listing2−25−6:

Listing 2−25−6: The plugin.xml File for the Toolbar Project

<?xml version="1.0" encoding="UTF−8"?><pluginid="com.test.toolbar"name="TestTool"version="1.0.0"><runtime><library name="toolbar.jar"/></runtime></plugin>

The above code shows the plug−in id as com.test.toolbar, name as TestTool, and the runtimelibrary name as toolbar.jar. You need to configure the plug−in to add the button in the toolbar. Theextension tag provides configuration information about plug−in.

Java ReferencePoint Suite 28

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 31: Creating Plug-ins using the Eclipse PDE.pdf

Listing 2−25−7 shows the plugin.xml configuration file that helps to display the button in the toolbar:

Listing 2−25−7: Configured plugin.xml File for the Toolbar Project

<?xml version="1.0" encoding="UTF−8"?><pluginid="com.test.toolbar"name="TestTool"version="1.0.0"><runtime><library name="toolbar.jar"/></runtime><requires><import plugin="org.eclipse.ui"/></requires><extension point="org.eclipse.ui.actionSets"><actionSetid="com.test.toolbar.actionSet"label="TestToolButton"><actionid="com.test.toolbar.TestToolAction"label="TestTool"toolbarPath="TestToolGroup"class="com.test.toolbar.TestToolAction"></action></actionSet></extension></plugin>

In the above code, the extension tag specifies the action name, the button name, and the toolbarpath. After configuring the plugin.xml file, you can test the plug−in by running it in the workbench.

Running the Toolbar Button Plug−in

You can invoke the Toolbar button plug−in from the Eclipse PDE that run on a separate Eclipseinstance. To run the Toolbar button plug−in:

Click the Run option from the toolbar and select the Run As−> Run−time Workbench option.A new workbench appears.

1.

Select Window−>Customize Perspective option, the Customize Perspective windowappears.

2.

Expand the Other option and select the TestToolButton option, as shown in Figure 2−25−27:3.

Java ReferencePoint Suite 29

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 32: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−27: The Customize Perspective Dialog Box

Click OK to save the changes. The toolbar in the workbench contains a red button, as shownin Figure 2−25−28:

4.

Java ReferencePoint Suite 30

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 33: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−28: The Eclipse Window with Red Button Displayed

Click the red button in the toolbar, a dialog box appears, as shown in Figure 2−25−29:5.

Java ReferencePoint Suite 31

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 34: Creating Plug-ins using the Eclipse PDE.pdf

Figure 2−25−29: A Dialog Box with Test Message

Click OK to close the dialog box.6.

The Eclipse PDE places the TestToolButton in the toolbar and adds the action to work accordingly.

Java ReferencePoint Suite 32

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited

Page 35: Creating Plug-ins using the Eclipse PDE.pdf

Related TopicsFor related information on this topic, you can refer to:

Understanding Eclipse IDE•

Using the Java plug−in•

Understanding Classes and Objects in Java•

Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited