Difference between revisions of "Teams Winter 2011/team1/RCP/Define and use commands"

From CDOT Wiki
Jump to: navigation, search
("Add Student" Command)
("Delete Student" Command)
Line 58: Line 58:
  
 
==== "Delete Student" Command ====
 
==== "Delete Student" Command ====
 +
<br/>
 +
[[Image: RCPCommand17.jpg | 400px]]<br/>
 +
<br/>
 +
[[Image: RCPCommand18.jpg | 400px]]<br/>
 +
 
==== "Add Student" Command ====
 
==== "Add Student" Command ====
 
<br/>
 
<br/>

Revision as of 23:11, 8 March 2011

Define and Use Commands

In order to be able to take advantages of user invoked operations and menus in the RCP application we create Commands and add them to menus. For this purpose we will demonstrate here how to create a "Print Students" command, which prints the student's info in the console and then add that command to the top bar menu. Then later, we demonstrate how to implement other commands like "Add Student", "Delete Student" and "Exit" to the top bar.

Add a Command (Print Students) to Your Project

Double click on the plugin.xml file of your project in the project explorer view, and then navigate to "Extentions" tab in the project overview window.
RCPCommand1.jpg

Click on "Add" button to add a new extension and add the extension point "org.eclipse.ui.commands" (do not select any template) and click on Finish buttton.
RCPCommand2.jpg

Right click on the added extention point and select command from the New menu, to add a command to your commands.
RCPCommand3.jpg

select the command and in "Extention Element Details" section, set the id of the command as "cs.rcp.simpleRCP.commands.print". Also name the command "Print", and set the "defaultHandler" class to be "cs.ecl.rcp.simpleRCP.commands.PrintHandler. Save the file (Ctrl+S). RCPCommand4.jpg

Double click on the "defaultHandler" link to create the Handler class. The New Java Class window will open with the package name and class name set in it. Make sure that the class inherits from "org.eclipse.core.commands.AbstractHandler" by setting it as the Superclass. It should also implement the interface "org.eclipse.core.commands.IHandler".
RCPCommand5.jpg
Then click on Finish button to create the class.

Implement The Command Handler

Here is the class code which is automatically generated. We have to Add our code to the "execute" method. RCPCommand6.jpg

Modify the "execute" method with the following code:

PrintHandler.java

@Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        List<Student> studentList = ModelProvider.INSTANCE.getStudents();
        for (Student s : studentList) {
            System.out.println(s);
        }
        return null;
    }

Use The Command in The Menu

RCPCommand8.jpg

RCPCommand9.jpg

RCPCommand10.jpg

RCPCommand11.jpg

RCPCommand12.jpg

Add Other Necessary Commands to The Application

"Exit" Command


RCPCommand19.jpg

"Delete Student" Command


RCPCommand17.jpg

RCPCommand18.jpg

"Add Student" Command


RCPCommand13.jpg

RCPCommand14.jpg

RCPCommand15.jpg

RCPCommand16.jpg

Define Views in The Application

Create Views

Add view(s) to perspective via code

Run your app with view(s)