Difference between revisions of "Teams Winter 2011/team1/RCP/Create RPC Application"

From CDOT Wiki
Jump to: navigation, search
(Add Model Provider)
(Add Model Provider)
Line 177: Line 177:
 
</source>
 
</source>
 
<br/>
 
<br/>
[[Define_and_use_commands| Next>>]]
+
[[Teams_Winter_2011/team1/RCP/Define_and_use_commands| Next>>]]

Revision as of 16:46, 8 March 2011

Create a RCP Application

This tutorial uses the ... Tutorial as reference.
In this tutorial we are going to demonstrate how to build a simple RCP application to maintain student's information. We keep the data in a List object and will not preserve data.

To begin with, we create a simple RCP from the "Hello World" RCP template. The will add our Model classes. Eventually we use perspective, view, Jface viewe and Commands, to display, edit, Add and Delete students' records.

Start The Application

Create a new plug-in project from File menu (File>New>Project>Plug-in Development> Plug-in project).
CreateRCPApp1.jpg

Name it: "cs.ecl.rcp.SimpleRCP"
CreateRCPApp2.jpg

Select "Yes" in responce to the question:" Would you like to create a rich client application?"
CreateRCPApp3.jpg

Select the "Hello RCP" in the Templates screen.
CreateRCPApp4.jpg

Un-check "Add branding".
CreateRCPApp5.jpg

The "RCP" project is created with some classes already in it. We will modify this project to create our RCP application for the purpose if this tutorial.
CreateRCPApp6.jpg

In the "ApplicationWorkbenchWindowAdvisor" class, modify the preWindoOpen() method to set the title of the window ar "Simple RCP Application".
ApplicationWorkbenchWindowAdvisor.java to true

public void preWindowOpen() {
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setInitialSize(new Point(400, 300));
        configurer.setShowCoolBar(false);
        configurer.setShowStatusLine(false);
        configurer.setTitle("Simple RCP Application"); //$NON-NLS-1$
        configurer.setShowStatusLine(true);
        configurer.setShowPerspectiveBar(true);
    }

Run The Application

To run the application, right click on the project and select :Run As> Eclipse Application. Alternatively, you can click on the "Run an Eclipse Application" In the "Testing section of the project Overview window. RCPRun1.jpg

At this point, the application starts and looks like this:
RCPRun2.jpg

Add Model

In order to have a data model for Student, we need to create a "Student Class" which keeps an student's data and "ModelProvider" Class that creates and keeps a list of Student class objects.

Create Model Package

Create a package named "cs.ecl.rcp.simplercp.model" in the src directory of your project.

Add Model Class(s)

Add a class named "Student" in the above package. Add the student related fields and make sure that the class uses "PropertyChangeSupport" and "PropertyChangeListener" when setting the properties. For this purpose we need to have an instance of the "PropertyChangeSupport" class to be build by passing the Student object(this) to it, and implement the metods:"addPropertyChangeListener" and "removePropertyChangeListener". The following code demonstrates how to create the Student Model and how to add Property Change Support to the class.

Student.java

package cs.ecl.rcp.simplercp.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class Student {
    private String id;
    private String firstName;
    private String lastName;
    private String program;   
   
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
   
    public Student(){
    }
   
    public Student(String id, String firstName, String lastName, String program){
        super();
        this.id= id;
        this.firstName= firstName;
        this.lastName= lastName;
        this.program= program;
    }
   
   
    public void addPropertyChangeListener(String propertyName,PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
    }
   
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    public String getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getProgram() {
        return program;
    }

   
   
    public void setId(String id) {
        propertyChangeSupport.firePropertyChange("id", this.id,
                this.id = id);
    }
   
    public void setFirstName(String firstName) {
        propertyChangeSupport.firePropertyChange("firstName", this.firstName,
                this.firstName = firstName);
    }
   
    public void setLastName(String lastName) {
        propertyChangeSupport.firePropertyChange("lastName", this.lastName,
                this.lastName = lastName);
    }
   
    public void setProgram(String program) {
        propertyChangeSupport.firePropertyChange("program", this.program,
                this.program = program);
    }
    public String toString() {
        return id +" "+firstName + " " + lastName + " "+ program ;
    }
}

Add Model Provider

"ModelProvider" class creates and maintains an ArrayList of Student objects and helps us find an student by it's id from the list of students.
ModelProvider.java

package cs.ecl.rcp.simplercp.model;

import java.util.ArrayList;
import java.util.List;

public enum ModelProvider {
    INSTANCE;

    private List<Student> students;
   
    private ModelProvider() {
        students = new ArrayList<Student>();
        // Data could be retrieved here from database
        students.add(new Student("01234567","Ladan", "Zahiroleslam", "CPA"));
        students.add(new Student("09876543", "Anastasia", "Semionova", "CPA"));
        students.add(new Student("01122334", "Minoo", "Ziaei", "CPA"));
        students.add(new Student("02233445", "Sergiu", "Ecob", "CPA"));
    }

    public Student getPersonById(String id) {
        for (Student student : students) {
            if (student.getId() == id) {
                return student;
            }
        }
        return null;
    }

    public List<Student> getStudents() {
        return students;
    }

}


Next>>