Friday, March 23, 2012

Adding Action in Menu or Toolbar of Sample View programmatically


1. To add Action in Menu, we need to create Action as well as Menu for this View. To do that, we need to modify the SampleView.java file. Open the SampleView.java file and add the given code in "createPartControl" method:



2. Then create these four private method in the same java file and modify it with the give below code.




3. Add two icon image files named as : " fields_co.gif " and " alphab_sort_co.gif " in "icons" folder.
Project folder structure with icon files.
Add three data members in this SampleView class:
private Action filterAction;
private SampleFilter vFilter;
private Action sortAction;

4.  Create a new Java class named as "SampleFilter" in "samplecode.source.view" package folder with extends "org.eclipse.jface.viewers.ViewerFilter" super class.
Create new Java class dialog box
Click Finish.

5. Modify this java file with the following below code snippet:




6. Create another java class named as "SampleViwerSorter" within "samplecode.source.view" package and extends "org.eclipse.jface.viewers.ViewerSorter" super class.
 Create new Java class dialog box for SampleViewSorter
Click Finish.

7. Modify this class with the following code snippet:




8. Modify the constructor of the SampleView class with below given code snippet:




9. Run the project and see the effect of the newly created two buttons in view toolbar.
Sample View
- Filter button filtering the all files.
- Sort button sorting all folders in alphabetical order.

Attachment: To Download this project click here.

No comments:

Post a Comment