Wednesday, 30 December 2015

Show Perspective switch bar in Eclipse RCP application

Show Perspective switch bar in Eclipse RCP application

Open file in your plug-in project.

Override the method, preWindowOpen and as shown below, setShowPerspectiveBar as True.

    public void preWindowOpen ()
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setShowPerspectiveBar( true );

Run your RCP application, and on Right side top, you will be able to see perspective Switch.

Following Books are good to refer :

Create (pulldown) Menu in Toolbar in Eclipse RCP

Create (pulldown) Menu in Toolbar

Step 1. Create Toolbar with PullDown
Step 2. Create MenuContribution 
Step 3. Add items to MenuContribution
Step 4. Run the application

Step 1. Create Toolbar with PullDown

Create your Eclipse RCP application ( with mail template).

Suppose open command id is "" then create a toolbar with command in it, 
as shown below:

now, convert the style to "pulldown".

Now, if you run the application, you will see a pulldown button next to toolbar icon as follow :

Step 2: Create MenuContribution

Now, create a MenuContribution, which will define the items for pulldown menu.

Define, locationURI to the same name as the menu:commandId , here we provide the LocationURI as ""
Also Define Class name as "AllMenuItems".

Step 3. Add items to MenuContribution

Click on "class", you will see the dialogbox as shown below :

Click on Finish.
Now, go to MANIFEST.MF and add "org.eclipse.core.expressions" to "Required Plug-ins".

Enter code as follow in the file

Code :
public void createContributionItems(IServiceLocator serviceLocator,IContributionRoot additions) {

CommandContributionItemParameter commandContributionItemParameter = new CommandContributionItemParameter(
                serviceLocator, "",
commandContributionItemParameter.label = "Exit the application";
commandContributionItemParameter.icon = Activator.getImageDescriptor("icons/alt_window_16.gif");

        CommandContributionItem commandContributionItem = new CommandContributionItem(commandContributionItemParameter);
        additions.addContributionItem(commandContributionItem, null);


Step 4. Run the application

Run your application and your toolbar with Menu is ready.

Monday, 28 December 2015

Add Method Body Using EMF ( Add code inside EOperaion)

Add Method Body Using EMF ( Add code inside EOperaion )

Steps : 
  1. Create Ecore model with Code
  2. Create GenModel
  3. Verify generated Code

1. Create Ecore model with CodeIn RCP project create new Ecore model :

Now, press on Next and give the name of the Ecore model. Here the name of the Ecore model is given as Office.ecore.

Click on "Finish". It will create Ecore model in project.

Now provide the name to the package. Here the name is given as "Office"

Add a EClass to the package "Office"

Give the name of the class as "Employee"

Add attribute to the Employee class and give name.

Add EOperation to the Class Employee. Give name as "calculateSalary" , set type as "EInt" to the EOperation.

Add, EAnnotation to the EOperation "calculateSalary".

In properties view, add source to ""

Now, Add Details Entry by right clicking on EAnnotation and selecting New Child.
In Properties view, enter "body" in Key .

In body part, enter the code you want to define in the method
int i=10;
return i;

2. Create Gen model

Create Generate Model, by clicking on new and select "EMF Generator Model"

Give name as , "Office.genmodel" to the generator model. ( you can give any name with prefix ".genmodel" to this Generator model file.
Generate the Gen Model from the "Ecore Model" and select the office.ecore model from the directory.

Select Finish and, GenModel is ready now.

Right click on the Gen model and select "Generate Model Code".

3. Verify generated Code

In the class EmployeeImpl, you check the method "calculateSalary()", it will contains the code which you want to enter into the method. 

Wednesday, 9 December 2015

Creating Image Registry in Eclipse RCP

Advantage of Creating Image Registry in Eclipse RCP
  1. Share the same image between different plugin.
  2. Don't dispose the image and keep it for another plugin.

How to create Image Registry?

Override the initializeImageRegistry method inside the Activator and you are ready to use t

 public class Activator  {
      public static final String MY_PLUGIN_ID = "";
      public static final String MY_IMAGE_ID = "";

      protected void initializeImageRegistry(ImageRegistry registry) {
         Bundle bundle = Platform.getBundle(MY_PLUGIN_ID);
         IPath path = new Path("icons/save_image.gif");
         URL url = FileLocator.find(bundle, path, null);
         ImageDescriptor desc = ImageDescriptor.createFromURL(url);
         registry.put(MY_IMAGE_ID , desc);

How to use in Code :

In same plugin :
            imageRegistry.getDescriptor( Activator.MY_IMAGE_ID) );

In Different plugin :

AbstractUIPlugin.imageDescriptorFromPlugin( "", "" );

Best Practice for Maintaining Images in Eclipse Plugin

If we maintain the same image in multiple Plugins the each plugin will be of large size. Instead of maintaining the copy of Image at multiple locations, reusing the images is the best practice.
By doing this, we can avoid loading of the same image multiple times and as a result, RCP application will perform faster.
So I would suggest reusing the images as much as possible in the Eclipse Plugin Development.