Wednesday, December 3, 2008

Add an Icon to an Eclipse RCP Application View

In the last Java Eclipse RCP tutorial, Adding a View to an Eclipse RCP Application, I demonstrated how to add a view to a simple RCP application. In RCP applications, views are where you put things like tables, buttons, combo boxes, labels, charts, etc. - all the graphical interfaces that a user will interact with when using your application. In this article I will show where to get icons and how to add an icon to a view, which is placed on a tab corresponding to that view. I will also show how to label that tab with some custom text.



Step 1: Get some icons. Mark James at www.famfamfam.com has graciously created and made available over 1000 icons suitable for Eclipse RCP applications. His work is licensed under a Creative Commons Attribution 2.5 License, so you can use the icons any way you like. Download the icons, browse through them, and choose one that you'd like to use for your view.





Step 2: Add the icon to your RCP application. With Eclipse open and the Package Explorer showing the contents of your project, drag and drop an icon into the "icons" folder. For this example, I put the bomb.png icon into the icons folder.



Step 3: Add the icon to the view. Make sure the View is highlighted in the All Extensions list. To the right are the properties of the view. To the right of the icon property, click on the "Browse..." button. In the dialog box that pops up, choose the icon that you just added. The icon should show up next to the view's name in the "All Extensions" list.




Step 4: Specify that the view's title should be shown. Now that the icon is added to the application project and associated with the view, some code is needed to tie everything together. In Perspective.java set the boolean flag for the second argument in the "addStandAloneView" method to true. This is the "showTitle" flag which tells the RCP application to show a tab for the view containing the icon and a title for the view.

package com.blogspot.obscuredclarity.addicon;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

 public void createInitialLayout(IPageLayout layout) {

  layout.addStandaloneView(MainView.ID, true, IPageLayout.LEFT, 1.0f,   layout.getEditorArea());
  layout.setEditorAreaVisible(false); //hide the editor in the perspective

 }
}


Step 5: Give the view's tab a name. In the view's class, add the line "this.setPartName("Hello!");" to the createPartControl method (line 16). Change the "Hello!" String to whatever you want.

package com.blogspot.obscuredclarity.addicon;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {

 public static final String ID = "com.blogspot.eclipsercptutorials.addicon.mainView"; //the ID needs to match the id set in the view's properties

 public MainView() {    }

 public void createPartControl(Composite parent) {

  this.setPartName("Hello!");

  Label label = new Label(parent, SWT.None); //new up a Label widget
  label.setText("  All this program does is display this text in a view."); //set the label's text

 }

 public void setFocus() { }

}


Step 6: Run the application and test if everything worked. Your application should now look something like this:



Step 7: If you want the view to be non-closeable, add layout.getViewLayout(MainView.ID).setCloseable(false);" to the createInitialLayout" method (line 12). This will hide the little "X" on the tab and not allow the tab to be closed, which you probably want to do if your program only has one view.

package com.blogspot.obscuredclarity.addicon;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

 public void createInitialLayout(IPageLayout layout) {

  layout.addStandaloneView(MainView.ID, true, IPageLayout.LEFT, 1.0f,   layout.getEditorArea());
  layout.setEditorAreaVisible(false); //hide the editor in the perspective
  layout.getViewLayout(MainView.ID).setCloseable(false);
 }
}




Piece of cake!

Next ---> Add a Menu to an Eclipse RCP Application
<--- Previous - Adding a View to an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents

No comments: