Beruflich Dokumente
Kultur Dokumente
Reproduction and/or distribution in whole or in part in electronic, paper or other forms without written permission is prohibited.
Reprinted for Balaji Nallathambi, Verizon Balaji@verizon.com Reprinted with permission as a subscription benefit of Books24x7, http://www.books24x7.com/
Table of Contents
Point 25: Creating Plugins using the Eclipse PDE......................................................................1 Introducing the Eclipse PDE ............................................................................................................2 Understanding the PDE Workbench.......................................................................................2 Understanding the Plugin Manifest Editor............................................................................2 . Understanding the Plugin.xml File..........................................................................................7 Creating a JDBCView Plugin Project............................................................................................8 Adding References to the JDBCView Plugin Project..........................................................12 Creating the JdbcView Class................................................................................................15 Configuring the Plugin.xml File for the JDBCView Plugin ...................................................20 Running the JDBCView Plugin...........................................................................................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 Plugin.....................................................................................29 Related Topics................................................................................................................................33
Note
To learn more about the Eclipse workbench, see the Understanding Eclipse IDE ReferencePoint.
The Eclipse PDE enables you to create plugin projects. A plugin project contains folders and files, such as manifest file, Java code library, and properties files.
Figure 2251: The Overview Page of the Plugin Manifest Editor The Overview page displays the basic plugin information, such as plugin identifier, plugin name, version, and provider name. You can edit this information in the General Information section. You can only view the contents of other sections in the Overview page. The Dependencies page allows you to select a list of plugins that are required for your plugin to work. Figure 2252 shows the Dependencies page:
Figure 2252: The Dependencies Page of the Plugin Manifest Editor The Runtime Information page helps you to add runtime libraries to your plugin. This page allows you to include library content, specify library type, and library exporting rules. Figure 2253 shows the Runtime Information page:
Figure 2253: The Runtime Information Page of the Plugin Manifest Editor You can use the Extensions page to add extensions to your plugin. You can browse and edit plugin extensions using the Extensions page. The extensions serve as a key mechanism to extend the behavior of a plugin. Figure 2254 shows the Extensions page:
Figure 2254: The Extensions Page of the Plugin Manifest Editor The Extension Points page allows you to define the extension points for a plugin. Other plugins can use this extension point to extend their operation. Figure 2255 shows the Extension Points page:
Figure 2255: The Extension Points Page of the Plugin Manifest Editor
Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited
The plugin Manifest Editor provides a Source page to edit the plugin code. Figure 2256 shows the Source page:
Figure 2257: The New Project Dialog Box 3. Click the Next button. The Plugin Project Name page of the New Plugin Project dialog box appears. 4. Specify a name for the project as JDBCView in the Project name text box.
Note Do not use spaces and special characters in the project name.
5. Clear the Use default check box and specify the directory where you want to save the project, as shown in Figure 2258:
Figure 2258: The Plugin Project Name Page of the New Plugin Project Dialog Box 6. Click the Next button. The Plugin Project Structure page appears. 7. Select the Create a Java project option and provide a value for output folder, plugin runtime library name and source folder, as shown in Figure 2259:
10
Figure 2259: The Plugin Project Structure Page of the New Plugin Project Dialog Box 8. Click the Next button. The Plugin Code Generators page appears to define the plugin code generation options, as shown in Figure 22510:
11
Figure 22510: The Plugin Code Generators Page of the New Plugin Project Dialog Box 9. Click the Finish button to create the project. The Plugin project is created by the Eclipse PDE with default values, as shown in Figure 22511:
Figure 22511: The Package Explorer with the Default Set of Configuration
12
Figure 22512: The Import Wizard 3. Click the Next button. The Import External Plugins page of the Import Plugins and Fragments dialog box appears, as shown in Figure 22513:
13
Figure 22513: The Import Plugins and Fragments Dialog Box 4. Click the Next button. The Selection page of the Import Plugins and Fragments dialog box appears. 5. Select org.eclipse.ui and org.eclipse.core.boot packages and other dependent packages. Figure 22514 shows the packages from which you can select:
14
Figure 22514: The Selection Page of the Import Plugins and Fragments Dialog Box 6. Click Finish. The Package Explorer appears, as shown in Figure 22515:
15
16
Figure 22516: The New Java Class Dialog Box 2. Click the Browse button to select superclass for the JdbcView class. The Superclass Selection window appears, as shown in Figure 22517:
17
Figure 22517: The Superclass Selection Window 3. The JdbcView class should extend the ViewPart class to implement a view. As a result, you need to select the ViewPart class as superclass and click OK. The New Java Class creation dialog box appears with the superclass name, as shown in Figure 22518:
18
Figure 22518: The New Java Class Dialog Box for the JdbcView Class 4. Click Finish button. The Eclipse PDE generates code for the JdbcView class, which appears in the Source page of the plugin Manifest Editor.
Listing 2251 shows the JdbcView class with the generated code:
Listing 2251: 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 { /* (nonJavadoc) * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) */ public void createPartControl(Composite parent) { // TODO Autogenerated method stub }
Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited
19
/* (nonJavadoc) * @see org.eclipse.ui.IWorkbenchPart#setFocus() */ public void setFocus() { // TODO Autogenerated method stub } }
In the above code, by default the ViewPart class inherits the methods createPartControl() and the setFocus() from the IViewPart class, which defines the interface for views. You can write the implementation of the JdbcView class by including the logic for these methods. Listing 2252 shows the modified version of the JdbcView class:
Listing 2252: 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 { /* (nonJavadoc) * @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 Autogenerated constructor stub } public void createPartControl(Composite parent) { // TODO Autogenerated 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");
Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited
20
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); } /* (nonJavadoc) * @see org.eclipse.ui.IWorkbenchPart#setFocus() */ public void setFocus() { // TODO Autogenerated method stub } public void widgetSelected(SelectionEvent e) { } }
In the above code, you modify the method createPartControl() to add controls, such as Label, and TextField to accept the values, such as database URL, user name and password to connect to the database.
The above code contains the plugin id, plugin name, and the runtime library name configured in it. You can use the extension tag to specify the JdbcView class in the plugin.xml file.
Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited
21
Listing 2254 shows the plugin.xml file with the JdbcView class configured in it: Listing 2254: Configured plugin.xml File for the JdbcView Class
<?xml version="1.0" encoding="UTF8"?> <plugin id="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"/> <view class="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 plugin is an extension of org.eclipse.ui.views. The View tag specifies the name of the view and the class name of the view.
22
2. Click Runtime Workbench, a new instance of Eclipse opens, as shown in Figure 22520:
Figure 22520: The New Eclipse Window 3. Select Window>Customize Perspective, the Customize Perspective dialog box appears. 4. Expand the Window>Show View option and select the JDBC View option in the Customize Perspective dialog box, as shown in Figure 22521:
23
5. Click OK. The Eclipse window appears. Select the menu option Window> Show View>JDBC View option, as shown in Figure 22522:
Figure 22522: The Eclipse Window with the JDBC View Selected 6. Place the JDBC View in the Workspace, as shown in Figure 22523:
25
Figure 22524: The Package Explorer with the Default Set of Configuration for the Toolbar Project 8. You can add the org.eclipse.ui, the org.eclipse.core.boot packages, and the dependencies to the Toolbar project to display the button in the toolbar.
26
Figure 22525: The Implemented Interfaces Selection Window 3. Select the IWorkbenchWindowActionDelegate interface and click OK. 4. Select the Constructors from superclass and Inherited abstract methods checkbox options in the New Java Class dialog box, as shown in Figure 22526:
27
Figure 22526: The New Java Class Dialog Box for the Toolbar Project 5. Click Finish to create the TestToolAction class. The Eclipse PDE generates code for the TestToolAction class that appears in the source page of the plugin Manifest Editor.
You can add the default constructor and the inherited abstract methods, after creating the TestToolAction class. Listing 2255 shows the constructors and the inherited abstract methods included while creating the TestToolAction class:
Listing 2255: 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 */
Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited
28
public class TestToolAction implements IWorkbenchWindowActionDelegate { /** * */ public TestToolAction() { super(); // TODO Autogenerated constructor stub } /* (nonJavadoc) * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() */ public void dispose() { // TODO Autogenerated method stub } /* (nonJavadoc) * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWin */ public void init(IWorkbenchWindow window) { // TODO Autogenerated method stub } /* (nonJavadoc) * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ public void run(IAction action) { // TODO Autogenerated method stub } /* (nonJavadoc) * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IActio org.eclipse.jface.viewers.ISelection) */ public void selectionChanged(IAction action, ISelection selection) { // TODO Autogenerated method stub } }
In the above code, modify the run method to include the following line of code that displays the test message:
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.
The above code shows the plugin id as com.test.toolbar, name as TestTool, and the runtime library name as toolbar.jar. You need to configure the plugin to add the button in the toolbar. The extension tag provides configuration information about plugin.
Reprinted for v697039, Verizon SkillSoft, SkillSoft Corporation (c) 2002, Copying Prohibited
29
Listing 2257 shows the plugin.xml configuration file that helps to display the button in the toolbar: Listing 2257: Configured plugin.xml File for the Toolbar Project
<?xml version="1.0" encoding="UTF8"?> <plugin id="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"> <actionSet id="com.test.toolbar.actionSet" label="TestToolButton"> <action id="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 toolbar path. After configuring the plugin.xml file, you can test the plugin by running it in the workbench.
30
Figure 22527: The Customize Perspective Dialog Box 4. Click OK to save the changes. The toolbar in the workbench contains a red button, as shown in Figure 22528:
31
Figure 22528: The Eclipse Window with Red Button Displayed 5. Click the red button in the toolbar, a dialog box appears, as shown in Figure 22529:
32
Figure 22529: A Dialog Box with Test Message 6. Click OK to close the dialog box.
The Eclipse PDE places the TestToolButton in the toolbar and adds the action to work accordingly.
Related Topics
For related information on this topic, you can refer to: Understanding Eclipse IDE Using the Java plugin Understanding Classes and Objects in Java