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/
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
All rights reserved. Reproduction and/or distribution in whole or inpart in electronic, paper or other forms without written permission
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
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.
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:
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:
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:
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:
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
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.
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.
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:
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:
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:
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:
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 }
/* (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");
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
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.
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:
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:
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:
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>Preferences>Java>Code Generation>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>Preferences>Java>Code Generation>Code and Comments */
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
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.
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.