Writing a plugin for Eclipse sounds scary and difficult. A
few years ago I had to do a project with some other students for school. The
project was writing an application that works as a plugin in Eclipse. It was terrible, no documentation about creating the plugin,
the javadoc didn’t help either and the teachers that should have helped use
were busy figuring out how it worked themselves. That time is past.
Creating plugins is easy now.
Setup
Let’s create a simple plugin that reads the headlines of the
AMIS technology blog (that was the most useful plugin I could come up with )
Go to the menu File, New, Project.. and select “Plug-in
Projectâ€
Enter the name of your project and select next. Change the
class name to nl.amis.BlogPlugin at the next screen.
The following screen will appear after clicking next
Select “Plug-in with a viewâ€, click next and change first five fields:
Click finish. A new view will open with the plugin details.
You can open this view any time by double clicking the plugin.xml file.
Click on ‘Launch an Eclipse application’ to test your plugin.
It’s also possible to debug the plugin, but there is nothing to debug yet. A new Eclipse window will open. The new plugin is a view for Eclipse, so
we should open a view by clicking on Window, show view, RSS Feeds, Amis RSS and
click ok.
At the bottom of your Eclipse window a new view will
appear. The view contains 3 items which
are definitely not articles on the blog.
Add RSS to the plugin
Let’s put the RSS-feed in the plugin. Of course I’ll use ROME for it.
Create a lib dir in your plugin directory and copy the files
for JDOM and ROME
to it. Add the libraries to your project by right clicking your project,
properties, java build path, libraries, add jars and select both jars in the
lib directory and hit ok.
The class where all the action happens is BlogView (package
nl.amis.plugin.views). Today we’re only changing the method createPartControl.
Remove the String array in the getElements method of the
inner class ViewContentProvider :
public Object[]
getElements(Object parent) {
return new String[] {};
}
Overwrite the method createPartControl with the following
code:
public void
createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI
| SWT.H_SCROLL
| SWT.V_SCROLL);
viewer.setContentProvider(new
ViewContentProvider());
viewer.setLabelProvider(new
ViewLabelProvider());
viewer.setInput(getViewSite());
// create table headers
TableColumn titleColumn = new
TableColumn(viewer.getTable(), SWT.LEFT);
titleColumn.setText(“title”);
titleColumn.setWidth(640);
TableColumn titleColumn2 = new
TableColumn(viewer.getTable(), SWT.LEFT);
titleColumn2.setText(“publication
date”);
titleColumn2.setWidth(240);
viewer.getTable().setHeaderVisible(true);
// parsing rss and putting it in the
TableViewer
try {
SyndFeedInput sfi = new
SyndFeedInput();
URL url = new
URL(“https://technology.amis.nl/blog/?feed=rss2”);
SyndFeed feed = sfi.build(new
XmlReader(url));
List<SyndEntry> entries =
feed.getEntries();
Table table = viewer.getTable();
for (SyndEntry entry : entries) {
TableItem item = new TableItem(table,
SWT.NONE);
item.setText(0, entry.getTitle());
item.setText(1,
entry.getPublishedDate() + “”);
}
} catch (Exception e) {
// TODO catch specific exceptions and
do something useful with them
}
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
}
So what did I do? I removed the Sorter because the items are
already sorted. In the next section I created the table headers so we get nice
headers with the title and date.
After creating the headers I parsed the RSS (if you don’t
have a clue what happens there read the blog entry about ROME (https://technology.amis.nl/blog/?p=1165). The entries of the feed are put in a table object. This is a standard SWT object. So it should be easy to port your plugin to a SWT application.
When you run the plugin again the blog entries should show
up.
It would be nice if the purple Eclipse icon was something
else, like the AMIS company logo. The icon can be changed in the plugin page at
the tab Extensions. Expand org.eclipse.ui.views and select Amis RSS (view).
Click on the browse button and select amis.gif (which you just saved in your
icons directory )
Now you have a basic plugin running, the next step is
auto-updating the RSS view, if you want to update now you have to restart the
view and that isn’t really practical. But this article was just to show how
easy it is to create a plugin.
SWT
When you start creating serious plugins you should first
learn SWT because creating a plugin involves a lot of SWT. A useful book is Eclipse:
Building Commercial Quality Plug-ins http://www.qualityeclipse.com/
There is a second edition available that covers Eclipse 3.1
(my edition covered 3.0). I don’t think the differences are big, in Eclipse 3.1
the wizard screens are a bit more intuitive.
SWT stands for Standards Widget Toolkit and is the
foundation of the Eclipse user interface. The goal of SWT is to go native as
soon as possible. This results in a component toolkit that looks and feels the
same as a normal application in every operating system. SWT was initiated as an
alternative for AWT and Swing and did a great job. I think it’s easier to
understand and performs better. In the book is also an example of a standalone
application using SWT, so you don’t need Eclipse when you want to use SWT.
Have you included the files in the lib directory? That should do the trick.
Hi, i am newbie to write plug-in.
i have some problem with your example.
When the example is ready, and launch Eclipse application with this plug-in, it view show “Unable to create view: com/sun/syndication/io/XmlReader”.
This is caused by rome-0.8.jar. How to import this jar library into Eclipse?
Thanks.