1) Introduction
This article will guide you in creating Eclipse Plugins using the Eclipse Plugin Development Environment (PDE). Because the Plugin Development Architecture is vast, this article will only provide brief information about the various components that fits into the PDE. It starts off in creating a Simple Plugin and they will try to explain the various pieces involved in the Plugin Development. More specifically this article will show how to create a View based Plugin, an Action based Plugin and a Preference-based Plugin.
2) Creating Views and Categories
Views occupy majority of the portion in any IDE and in Eclipse they generally falls into some Category. For example, there are Views like 'Declaration', 'Hierarchy', 'Package Explorer', and 'JavaDoc' that falls under a category called 'Java'. This can be seen by going to Window-->Show View-->Other. When you go there you can see a list of Categories like 'General', 'Ant', 'Cheat Sheats', 'CVS', 'Debug', 'Help', 'Java' and so on. These are called View Categories. Once after expanding a particular Category you can see a group of Views under that Category. So Category, in general, acts as a Container for holding logically similar type of Views.
2.1) Creating a View within a Category
Let us see how to create one such Custom Category having some Views defined within it. Let us name the category as 'Organization' having various Views like 'Employee' and 'Department'.
Follow the steps to create a new Plugin Project in Eclipse representing User-defined Views.
-
Choose File-->New-->Project, expand the Plug-in Development Folder and then choose 'Plug-in Project' and click Next.
-
For Project Name, give net.javabeat.eclipse.plugins.organization and click Next.
-
Then comes the 'New Plug-in Project' Page where you can configure the various properties of the plug-in that you are going to create. Ensure that the following values are filled in the respective columns for the 'Plug-in Properties'.
- Plug-in ID: net.javabeat.eclipse.plugins.organization
- Plugin Version: 1.0.0
- Plug-in Name: Organization Plug-in
- Plug-in Provider: javabeat.net
- Classpath: organization.jar
-
In the Plug-in Options panel, rename the Activator class to net.javabeat.eclipse.plugins.organizations.OrganizationActivator. Leave the remaining defaults and click Next.
-
Choose the Template 'Plug-in with a View' in the Available Templates and click 'Next'.
-
Ensure that the following values are given in the respective Text Fields in the Main View Settings.
- Java Package Name: net.javabeat.eclipse.plugins.countries.views
- View Class Name: OrganizationView
- View Name: Employee
- View Category ID: net.javabeat.eclipse.plugins.organization
- View Category Name: Organization
-
Un-check the 'Add the view to the resource perspective' check-box and click Next.
-
Click Next and un-check all the View Features. For this simple View, we don't necessarily need all these features.
-
When you click the Finish Button, Eclipse will prompt you to switch to 'Plug-in Development Perspective'. Check 'Remember my decision' check-box and press yes.
Browse to OrganizationView.java which is available under 'net.javabeat.eclipse.plugins.organization.views' package and search for the method 'getElements(Object)' in the 'ViewContentProvider' class. This is the method that will called to return the objects for decorating the View in consideration. By default, this method returns a String Array containing 'One', 'Two' and 'Three'. Modify the String Array such that it is returning something like return new String[] {"Harry", "David"};
To make the names more meaningful, double click the plugin.xml file and choose the plugin.xml tab in the active Editor. There you can see the View definition for the Employee. Replace the value of the attribute 'id' with 'net.javabeat.eclipse.plugins.organization.views.EmployeeView'.
To run the Organization Plugin Application that you have just developed, right-click the Project, Go to Run As and then choose Eclipse Application. Eclipse will now create a Default Work-Bench pointing to 'runtime-EclipseApplication' directory which is created in the same level as the current work-area directory. In the new Eclipse Workbench, Go to Window Menu, and then choose Show View-->Other where you can find a Category by name 'Organization'. Expand it to see the View called 'Employee' that we just created before. Click the view to see the View get attached with the existing list of Views.
2.2) Creating Multiple Views
In this section, let us expand the above example by providing support to create Multiple Views. Let us create another View called 'Department' within the Organization Category. To do this, carry out the following steps.
-
Double-click the plugin.xml and to bring the editor in front. You can see a series of tabs attached to this editor. Click on the plugin.xml tab.
-
To add another View called 'Department' under the 'Organization' Category, simply copy the view definition (given below) and paste it below the exiting View Definition, i.e Employee View, in the Xml file.
<view name="Department" icon="icons/sample.gif"
category="net.javabeat.eclipse.plugins.organization"
class="net.javabeat.eclipse.plugins.organization.views.OrganizationView"
id="net.javabeat.eclipse.plugins.organization.views.DepartmentView">
</view>
Now the plugin.xml file will look like this,
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension point="org.eclipse.ui.views">
<category name="Organization"
id="net.javabeat.eclipse.plugins.organization">
</category>
<view name="Employee" icon="icons/sample.gif"
category="net.javabeat.eclipse.plugins.organization"
class="net.javabeat.eclipse.plugins.organization.views.OrganizationView"
id="net.javabeat.eclipse.plugins.organization.views.EmployeeView">
</view>
<view name="Department" icon="icons/sample.gif"
category="net.javabeat.eclipse.plugins.organization"
class="net.javabeat.eclipse.plugins.organization.views.OrganizationView"
id="net.javabeat.eclipse.plugins.organization.views.DepartmentView">
</view>
</extension>
</plugin>
Note that the name of the New View is 'Department' and the corresponding View Id is 'net.javabeat.eclipse.plugins.countries.views.DepartmentView', though the class name for this view is still pointing to 'net.javabeat.eclipse.plugins.organization.views.OrganizationView'. Since we are re-using the same class for both the Employee View as well as the Department View, make the following changes in the OrganizationView.java file so that Employee View and Department View looks different in terms of appearance. Navigate to the getElements(Object) method found in the inner class ViewContentProvider and add the following code.
public Object[] getElements(Object parent) {
if (parent instanceof IViewSite){
IViewSite view = (IViewSite)parent;
String viewId = view.getId();
if (viewId.equals(EMPLOYEE_VIEW_ID)){
return new String[]{"Harry", "David"};
}else if (viewId.equals(DEPARTMENT_VIEW_ID)){
return new String[]{"Admin", "Travel"};
}
}
return null;
}
Don't forget to declare the variables EMPLOYEE_VIEW_ID and DEPARTMENT_VIEW_ID which are pointing to their respective View Identifiers as being configured in the plugin.xml file.
private static final String EMPLOYEE_VIEW_ID = "net.javabeat.eclipse.plugins.organization.views.EmployeeView";
private static final String DEPARTMENT_VIEW_ID = "net.javabeat.eclipse.plugins.organization.views.DepartmentView";
Run the Application, then you will something like the one in the following figure.
Employee and Department Views
|