Teams Winter 2011/team1/RCP/Define and use commands

From CDOT Wiki
Revision as of 00:03, 9 March 2011 by Ladanzahir (talk | contribs) ("Add Student" Command)
Jump to: navigation, search

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.
Here is what we will see after adding all the commands:

RCPCommandRun.jpg

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

At this point we have a "Print" command but is not yet accessible to the user. In order to expose the command to the user, we have to add this command to the top bar menu. Following these steps will add the print command as a button to the top bar (we are not adding the command to a sub menu, but we are directly adding it to the top bar menu):

Add another extension point called "org.eclipse.ui.menus" in the Extensions tab of the plugin.xml screen.
RCPCommand8.jpg

Then right click on the added extension point and choose New > menuContribution.
RCPCommand9.jpg

In the menuContribution's Extension Element Details section set the locationURI to :"menu:org.eclipse.ui.main.menu" (make sure it is typed correctly).
RCPCommand10.jpg

Then right click on the "menuContibution" in All Extensions section and select New > command.
RCPCommand11.jpg

Now we will add our Print command info to the Extension Element Details of the command we just added. We can give it any lable, but the commandID must be the same as our Print Command.
RCPCommand12.jpg

Add Other Necessary Commands to The Application

Here we will add other necessary commands to our application. We add the command Handler classes the same way we added the PrintHandler class, and then will add the commands to the menuContribution Extension point. However, in order to be able to implement the command execute method, we need to wait until we add the view and the Jface tableViewer to our application. So we add the commands then leave the implementation part and come back to it after the View and editor view creation is done. So for implementation, we assume that you have gone through those parts of the tutorial.

"Delete Student" Command

Add the "DeleteStudent" command to commands extension point and create the Handler Class.

RCPCommand17.jpg

Add the command you just created to the menuContribution extension point.
RCPCommand18.jpg

Here is the code for the execute method of the DeleteStudent class. It finds the student(s) that user has selected on the table viewer to delete from the ModelProvider and then refreshes the viewer.

DeleteStudent.java

package cs.ecl.rcp.simplercp.commands;

import java.util.Iterator;
import java.util.List;

import cs.ecl.rcp.simplercp.StudentsView;
import cs.ecl.rcp.simplercp.model.*;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

public class DeleteStudent extends AbstractHandler implements IHandler {
   
    @SuppressWarnings("unchecked")
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        // TODO Auto-generated method stub
       
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
        IWorkbenchPage page = window.getActivePage();
        StudentsView studentsView = (StudentsView) page.findView(StudentsView.ID);
        ISelection selection = studentsView.getSite().getSelectionProvider().getSelection();
       
       
        if(selection!=null && selection instanceof IStructuredSelection){
            List<Student> students = ModelProvider.INSTANCE.getStudents();
            IStructuredSelection sel = (IStructuredSelection)selection;
        // go through all selections if multiple selection is made
            for(Iterator<Student> iterator =sel.iterator();iterator.hasNext(); ){
                Student student = iterator.next() ;
                students.remove(student);
            }
           
            studentsView.getViewer().refresh();
        }
       
       
        return null;
    }

}

"Add Student" Command

For implementing the "AddStudent" command, we are highly depended on the use of Editor view, so after adding the command Handler class and add the command to the menuContribution extension point, we will leave this part of tutorial and will come back to it for the implementation of the execute method in the handler class. However this will not be the only code, related to the AddStudent command. The reason is, we are going to use the editor view to create a new student. So basically, when user clicks on the "Add Student" on the top bar, the editor view will open up and enables user to enter the information for a new student and there will be a "Save" button to save the student, Then the table viewer will show the updated data.<b/>

Here is whet user can see by clicking on the "Add Student" button:
RCPCommandAddRun.jpg

RCPCommand13.jpg

RCPCommand14.jpg

RCPCommand15.jpg

RCPCommand16.jpg

"Exit" Command

Now we will add the Exit command to our menuContribution Extension point.
Right click on the menuContribution and select New > command. Then set the command's details as follows. It will use the existing exit command in the core commands.
RCPCommand19.jpg

Define Views in The Application

Create Views

Add view(s) to perspective via code

Run your app with view(s)