Professional Documents
Culture Documents
Books
Contact us
213
by Lars Vogel
Revision 0.1
01.04.2008
Created
14.04.2009 - 10.06.2013
Eclipse Plug-ins Tutorial This article describes the creation and deployment of Eclipse plug-ins. The article is based on Eclipse 4.2 (Eclipse Juno) and Java 1.6.
Table of Contents
1. Developing Eclipse plug-ins 2. Prerequisites 3. Installation 3.1. Downloading or upgrading 3.2. Download the Eclipse plug-in distribution 3.3. Update an Eclipse Java IDE 4. Exercise: Developing a simple plug-in 4.1. Create project 4.2. Start an Eclipse IDE with your plug-in 5. Local deployment of your plug-in 5.1. Options for installation 5.2. Installing your plug-in from your Eclipse IDE 5.3. Export plug-in and put into dropins folder 6. Create update site for your plug-in 6.1. Creating update sites 6.2. Create feature project 6.3. Create category definition 6.4. Create an update site 6.5. Install feature via the Eclipse update manager 7. Exercise: Deploy and install your plug-in via update site 8. Contribute a menu to existing Part 8.1. Overview 8.2. Contribute to package explorer 8.3. Restrict the extension - Visible When 9. Resources and Marker 10. Tutorial: Create Markers for resources 11. Adapters 11.1. Overview 11.2. Example 12. Eclipse Resources 13. Tools 13.1. Eclipse Yari
13.2. VisualVM 13.3. JVM Monitor 14. Thank you 15. Questions and Discussion 16. Links and Literature 16.1. Source Code 16.2. Eclipse Resources 16.3. vogella Resources
2. Prerequisites
This tutorial assumes that you are already familiar with standard Java development with the Eclipse IDE .
3. Installation
3.1. Downloading or upgrading
To get the required Eclipse tooling for plug-in development you can download a special version of Eclipse to develop plug-ins or your can upgrade an existing Eclipse Java IDE. Both approaches require that you have Java already installed.
Warning
Avoid having special characters or spaces in the path to your extract Eclipse.
Install General Purpose Tools Eclipse Plug-in Development Environment Eclipse RCP Plug-in Developer Resources from the Eclipse update site for your release. This would be for example http://download.eclipse.org/releases/kepler for the Eclipse 4.3. release. You may have to remove the Group items by category flag to see all available features.
Select the Hello, World Command! template and press the Next button.
The last page of the wizard allows you to customize the values of the wizard. You can leave the default values and press the Finish button.
Eclipse may ask you if you want to switch to the plug-in development perspective. Answer Yes if you are prompted.
A new Eclipse workbench starts. This runtime Eclipse has your new menu entry included. If you select this menu entry a message box will be displayed.
Database Management
www.synametrics.com
useful for a simple distribution scenario, you only have to provide the exported plug-in to your users. On the other hand it is not very convenient for the end user as it requires manual file copying and provides no simple way to update the plug-in. Create a update site and use the Eclipse update manager to install it from this site. This is a bit more complex to setup but is the most simple for the end user and provides the developer with a simple way to provide updates to all users.
In the export wizard dialog select in this case Install into host. Repository. This is depicted in the following screenshot.
Press the Finish button. This creates a J A Rfile with the exported plug-in in the selected directory. Copy this J A Rfile to the d r o p i n sdirectory in your Eclipse installation directory and restart your runnig Eclipse. After this restart your new plug-in is available in your Eclipse installation and ready to be used.
Press the New Category button and create a category with a name which describes your functionality. Add your feature to this category.
To use your category, switch to the Options tab and select the path to your c a t e g o r y . x m lfile in the Categorize repository option.
Restart the Eclipse IDE after the installation. Afterwards plug-in is available in your Eclipse installation and can be used.
option to create a HTML page from a Java source file. To contribute to an existing menu or toolbar you need to know the corresponding ID. This ID can be found via the Menu Spy. See Eclipse Source Code Guide for details.
Eclipse allows to save additional information for each file. You can use the I R e s o u r c einterface and the s e t P e r s i s t e n t P r o p e r t y ( )and g e t P e r s i s t e n t P r o p e r t y ( )methods. With these functions you can save Strings on files. We use these functions to save a directory for Java source files which already were exported via HTML. Create the following C o n v e r t H a n d l e rclass.
p a c k a g ed e . v o g e l l a . p l u g i n . h t m l c o n v e r t e r . h a n d l e r ; i m p o r tj a v a . i o . B u f f e r e d W r i t e r ; i m p o r tj a v a . i o . F i l e W r i t e r ; i m p o r tj a v a . i o . I O E x c e p t i o n ; i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t o r g . e c l i p s e . c o r e . c o m m a n d s . A b s t r a c t H a n d l e r ; o r g . e c l i p s e . c o r e . c o m m a n d s . E x e c u t i o n E v e n t ; o r g . e c l i p s e . c o r e . c o m m a n d s . E x e c u t i o n E x c e p t i o n ; o r g . e c l i p s e . c o r e . r e s o u r c e s . I R e s o u r c e ; o r g . e c l i p s e . c o r e . r u n t i m e . C o r e E x c e p t i o n ; o r g . e c l i p s e . c o r e . r u n t i m e . Q u a l i f i e d N a m e ; o r g . e c l i p s e . j d t . c o r e . I C o m p i l a t i o n U n i t ; o r g . e c l i p s e . j d t . c o r e . J a v a M o d e l E x c e p t i o n ; o r g . e c l i p s e . j f a c e . d i a l o g s . M e s s a g e D i a l o g ; o r g . e c l i p s e . j f a c e . v i e w e r s . I S e l e c t i o n ; o r g . e c l i p s e . j f a c e . v i e w e r s . I S t r u c t u r e d S e l e c t i o n ; o r g . e c l i p s e . s w t . w i d g e t s . D i r e c t o r y D i a l o g ; o r g . e c l i p s e . s w t . w i d g e t s . S h e l l ; o r g . e c l i p s e . u i . h a n d l e r s . H a n d l e r U t i l ;
p u b l i cc l a s sC o n v e r t H a n d l e re x t e n d sA b s t r a c t H a n d l e r{
p r i v a t eQ u a l i f i e d N a m ep a t h=n e wQ u a l i f i e d N a m e ( " h t m l " ," p a t h " ) ; @ O v e r r i d e p u b l i cO b j e c te x e c u t e ( E x e c u t i o n E v e n te v e n t )t h r o w sE x e c u t i o n E x c e p t i o n{ S h e l ls h e l l=H a n d l e r U t i l . g e t A c t i v e S h e l l ( e v e n t ) ; I S e l e c t i o ns e l=H a n d l e r U t i l . g e t A c t i v e M e n u S e l e c t i o n ( e v e n t ) ; I S t r u c t u r e d S e l e c t i o ns e l e c t i o n=( I S t r u c t u r e d S e l e c t i o n )s e l ; O b j e c tf i r s t E l e m e n t=s e l e c t i o n . g e t F i r s t E l e m e n t ( ) ; i f( f i r s t E l e m e n ti n s t a n c e o fI C o m p i l a t i o n U n i t ){ c r e a t e O u t p u t ( s h e l l ,f i r s t E l e m e n t ) ; }e l s e{ M e s s a g e D i a l o g . o p e n I n f o r m a t i o n ( s h e l l ," I n f o " , " P l e a s es e l e c taJ a v as o u r c ef i l e " ) ; } r e t u r nn u l l ; } p r i v a t ev o i dc r e a t e O u t p u t ( S h e l ls h e l l ,O b j e c tf i r s t E l e m e n t ){ S t r i n gd i r e c t o r y ; I C o m p i l a t i o n U n i tc u=( I C o m p i l a t i o n U n i t )f i r s t E l e m e n t ; I R e s o u r c er e s=c u . g e t R e s o u r c e ( ) ; b o o l e a nn e w D i r e c t o r y=t r u e ; d i r e c t o r y=g e t P e r s i s t e n t P r o p e r t y ( r e s ,p a t h ) ; i f( d i r e c t o r y! =n u l l& &d i r e c t o r y . l e n g t h ( )>0 ){ n e w D i r e c t o r y=! ( M e s s a g e D i a l o g . o p e n Q u e s t i o n ( s h e l l ," Q u e s t i o n " , " U s et h ep r e v i o u so u t p u td i r e c t o r y ? " ) ) ; } i f( n e w D i r e c t o r y ){ D i r e c t o r y D i a l o gf i l e D i a l o g=n e wD i r e c t o r y D i a l o g ( s h e l l ) ; d i r e c t o r y=f i l e D i a l o g . o p e n ( ) ; } i f( d i r e c t o r y! =n u l l& &d i r e c t o r y . l e n g t h ( )>0 ){ s e t P e r s i s t e n t P r o p e r t y ( r e s ,p a t h ,d i r e c t o r y ) ; w r i t e ( d i r e c t o r y ,c u ) ; } } p r o t e c t e dS t r i n gg e t P e r s i s t e n t P r o p e r t y ( I R e s o u r c er e s ,Q u a l i f i e d N a m eq n ){ t r y{ r e t u r nr e s . g e t P e r s i s t e n t P r o p e r t y ( q n ) ; }c a t c h( C o r e E x c e p t i o ne ){ r e t u r n" " ; } } p r o t e c t e dv o i ds e t P e r s i s t e n t P r o p e r t y ( I R e s o u r c er e s ,Q u a l i f i e d N a m eq n , S t r i n gv a l u e ){ t r y{ r e s . s e t P e r s i s t e n t P r o p e r t y ( q n ,v a l u e ) ; }c a t c h( C o r e E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; } } p r i v a t ev o i dw r i t e ( S t r i n gd i r ,I C o m p i l a t i o n U n i tc u ){ t r y{ c u . g e t C o r r e s p o n d i n g R e s o u r c e ( ) . g e t N a m e ( ) ; S t r i n gt e s t=c u . g e t C o r r e s p o n d i n g R e s o u r c e ( ) . g e t N a m e ( ) ; / /N e e d S t r i n g [ ]n a m e=t e s t . s p l i t ( " \ \ . " ) ; S t r i n gh t m l F i l e=d i r+" \ \ "+n a m e [ 0 ]+" . h t m l " ; F i l e W r i t e ro u t p u t=n e wF i l e W r i t e r ( h t m l F i l e ) ; B u f f e r e d W r i t e rw r i t e r=n e wB u f f e r e d W r i t e r ( o u t p u t ) ; w r i t e r . w r i t e ( " < h t m l > " ) ; w r i t e r . w r i t e ( " < h e a d > " ) ; w r i t e r . w r i t e ( " < / h e a d > " ) ; w r i t e r . w r i t e ( " < b o d y > " ) ; w r i t e r . w r i t e ( " < p r e > " ) ; w r i t e r . w r i t e ( c u . g e t S o u r c e ( ) ) ; w r i t e r . w r i t e ( " < / p r e > " ) ; w r i t e r . w r i t e ( " < / b o d y > " ) ; w r i t e r . w r i t e ( " < / h t m l > " ) ; w r i t e r . f l u s h ( ) ; }c a t c h( J a v a M o d e l E x c e p t i o ne ){ }c a t c h( I O E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; } } }
If you start this plug-in you should be able to create HTML output from a Java source file.
If you now start your plug-in, the menu entry should only be visible if at least one compilation unit has been selected.
If you run you can create a marker in the TODO list if you select a Java project and click your menu entry.
11. Adapters
11.1. Overview
Adapters help to display information about objects in view without having to adjust the existing views. In this example we will create a small view which allows to select objects and use the properties view to display them. Adapters are used on several places for example you can use an adapter to display your data in the outline view. See Outline View Example for an example how to do this.
11.2. Example
We will simple use an adapter to show our data in the property view. Create a new plug-in project "de.vogella.plugin.adapter". Use the "Plug-in with a view" template with the following settings.
Add the dependency "org.eclipse.ui.views" in tab dependencies of plugin.xml. Create the following data model.
p a c k a g ed e . v o g e l l a . p l u g i n . a d a p t e r . m o d e l ;
p u b l i cc l a s sT o d o{ p r i v a t eS t r i n gs u m m a r y ; p r i v a t eS t r i n gd e s c r i p t i o n ; p u b l i cS t r i n gg e t S u m m a r y ( ){ r e t u r ns u m m a r y ; } p u b l i cv o i ds e t S u m m a r y ( S t r i n gs u m m a r y ){ t h i s . s u m m a r y=s u m m a r y ; } p u b l i cS t r i n gg e t D e s c r i p t i o n ( ){ r e t u r nd e s c r i p t i o n ; } p u b l i cv o i ds e t D e s c r i p t i o n ( S t r i n gd e s c r i p t i o n ){ t h i s . d e s c r i p t i o n=d e s c r i p t i o n ; } }
Change the code of SampleView.java to the following. After this change you should be able to run your project, open your view and see your todo items.
p a c k a g ed e . v o g e l l a . p l u g i n . a d a p t e r . v i e w s ; i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t i m p o r t o r g . e c l i p s e . j f a c e . v i e w e r s . A r r a y C o n t e n t P r o v i d e r ; o r g . e c l i p s e . j f a c e . v i e w e r s . I T a b l e L a b e l P r o v i d e r ; o r g . e c l i p s e . j f a c e . v i e w e r s . L a b e l P r o v i d e r ; o r g . e c l i p s e . j f a c e . v i e w e r s . T a b l e V i e w e r ; o r g . e c l i p s e . s w t . S W T ; o r g . e c l i p s e . s w t . g r a p h i c s . I m a g e ; o r g . e c l i p s e . s w t . w i d g e t s . C o m p o s i t e ; o r g . e c l i p s e . u i . I S h a r e d I m a g e s ; o r g . e c l i p s e . u i . P l a t f o r m U I ; o r g . e c l i p s e . u i . p a r t . V i e w P a r t ;
/ * * *P a s s i n gt h ef o c u sr e q u e s tt ot h ev i e w e r ' sc o n t r o l . * / p u b l i cv o i ds e t F o c u s ( ){ v i e w e r . g e t C o n t r o l ( ) . s e t F o c u s ( ) ; } / /B u i l du pas i m p l ed a t am o d e l p r i v a t eT o d o [ ]g e t E l e m e n t s ( ){ T o d o [ ]t o d o s=n e wT o d o [ 2 ] ; T o d ot o d o=n e wT o d o ( ) ; t o d o . s e t S u m m a r y ( " F i r s tT o d o " ) ; t o d o . s e t D e s c r i p t i o n ( " Av e r yg o o dd e s c r i p t i o n " ) ; t o d o s [ 0 ]=t o d o ; t o d o=n e wT o d o ( ) ; t o d o . s e t S u m m a r y ( " S e c o n dT o d o " ) ; t o d o . s e t D e s c r i p t i o n ( " S e c o n ds u p e rd e s c r i p t i o n " ) ; t o d o s [ 1 ]=t o d o ; r e t u r nt o d o s ;
} }
To displays its values in the property view, add the extension point "org.eclipse.core.runtime.adapters" to your project. The data of the extension point should be like the following.
< e x t e n s i o n p o i n t = " o r g . e c l i p s e . c o r e . r u n t i m e . a d a p t e r s " > < f a c t o r y a d a p t a b l e T y p e = " d e . v o g e l l a . p l u g i n . a d a p t e r . m o d e l . T o d o " c l a s s = " d e . v o g e l l a . p l u g i n . a d a p t e r . T o d o A d a p t e r F a c t o r y " > < a d a p t e r t y p e = " o r g . e c l i p s e . u i . v i e w s . p r o p e r t i e s . I P r o p e r t y S o u r c e " > < / a d a p t e r > < / f a c t o r y > < / e x t e n s i o n >
Implement the factory and the new class "TodoPropertySource" which implements "IPropertySource".
p a c k a g ed e . v o g e l l a . p l u g i n . a d a p t e r ; i m p o r to r g . e c l i p s e . c o r e . r u n t i m e . I A d a p t e r F a c t o r y ; i m p o r to r g . e c l i p s e . u i . v i e w s . p r o p e r t i e s . I P r o p e r t y S o u r c e ; i m p o r td e . v o g e l l a . p l u g i n . a d a p t e r . m o d e l . T o d o ; p u b l i cc l a s sT o d o A d a p t e r F a c t o r yi m p l e m e n t sI A d a p t e r F a c t o r y{ @ O v e r r i d e p u b l i cO b j e c tg e t A d a p t e r ( O b j e c ta d a p t a b l e O b j e c t ,C l a s sa d a p t e r T y p e ){ i f( a d a p t e r T y p e = =I P r o p e r t y S o u r c e . c l a s s& &a d a p t a b l e O b j e c ti n s t a n c e o fT o d o ) { r e t u r nn e wT o d o P r o p e r t y S o u r c e ( ( T o d o )a d a p t a b l e O b j e c t ) ; } r e t u r nn u l l ; } @ O v e r r i d e p u b l i cC l a s s [ ]g e t A d a p t e r L i s t ( ){ r e t u r nn e wC l a s s [ ]{I P r o p e r t y S o u r c e . c l a s s} ; } }
p a c k a g ed e . v o g e l l a . p l u g i n . a d a p t e r ; i m p o r to r g . e c l i p s e . u i . v i e w s . p r o p e r t i e s . I P r o p e r t y D e s c r i p t o r ; i m p o r to r g . e c l i p s e . u i . v i e w s . p r o p e r t i e s . I P r o p e r t y S o u r c e ; i m p o r to r g . e c l i p s e . u i . v i e w s . p r o p e r t i e s . T e x t P r o p e r t y D e s c r i p t o r ; i m p o r td e . v o g e l l a . p l u g i n . a d a p t e r . m o d e l . T o d o ; p u b l i cc l a s sT o d o P r o p e r t y S o u r c ei m p l e m e n t sI P r o p e r t y S o u r c e{ p r i v a t ef i n a lT o d ot o d o ; p u b l i cT o d o P r o p e r t y S o u r c e ( T o d ot o d o ){ t h i s . t o d o=t o d o ; }
@ O v e r r i d e p u b l i cb o o l e a ni s P r o p e r t y S e t ( O b j e c ti d ){ r e t u r nf a l s e ; } @ O v e r r i d e p u b l i cO b j e c tg e t E d i t a b l e V a l u e ( ){ r e t u r nt h i s ; } @ O v e r r i d e p u b l i cI P r o p e r t y D e s c r i p t o r [ ]g e t P r o p e r t y D e s c r i p t o r s ( ){ r e t u r nn e wI P r o p e r t y D e s c r i p t o r [ ]{ n e wT e x t P r o p e r t y D e s c r i p t o r ( " s u m m a r y " ," S u m m a r y " ) , n e wT e x t P r o p e r t y D e s c r i p t o r ( " d e s c r i p t i o n " ," D e s c r i p t i o n " )} ; } @ O v e r r i d e p u b l i cO b j e c tg e t P r o p e r t y V a l u e ( O b j e c ti d ){ i f( i d . e q u a l s ( " s u m m a r y " ) ){ r e t u r nt o d o . g e t S u m m a r y ( ) ; } i f( i d . e q u a l s ( " d e s c r i p t i o n " ) ){ r e t u r nt o d o . g e t D e s c r i p t i o n ( ) ; } r e t u r nn u l l ; } @ O v e r r i d e p u b l i cv o i dr e s e t P r o p e r t y V a l u e ( O b j e c ti d ){
If you run your workbench and open your View via Windows -> Show View -> Others -> Sample Category -> Sample View and the property view you should be able to view your data.
13. Tools
13.1. Eclipse Yari
It's a comprehensive tool suite to debug, spy, spider, inspect and navigate Eclipse based application GUIs (Workbench or RCP). http://sourceforge.net/projects/yari/
13.2. VisualVM