Create a package "de.vogella.jface.tableviewer.model".
Create the following class "Person". This class contains the first name, last name, gender and married status for a person and represents the data model for this example.
package de.vogella.jface.tableviewer.model;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Person {
private String firstName;
private String lastName;
private boolean married;
private String gender;
private Integer age;
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
this);
public Person() {
}
public Person(String firstName, String lastName, String gender,
boolean married) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.married = married;
}
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public String getFirstName() {
return firstName;
}
public String getGender() {
return gender;
}
public String getLastName() {
return lastName;
}
public boolean isMarried() {
return married;
}
public void setFirstName(String firstName) {
propertyChangeSupport.firePropertyChange("firstName", this.firstName,
this.firstName = firstName);
}
public void setGender(String gender) {
propertyChangeSupport.firePropertyChange("gender", this.gender,
this.gender = gender);
}
public void setLastName(String lastName) {
propertyChangeSupport.firePropertyChange("lastName", this.lastName,
this.lastName = lastName);
}
public void setMarried(boolean isMarried) {
propertyChangeSupport.firePropertyChange("married", this.married,
this.married = isMarried);
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
propertyChangeSupport.firePropertyChange("age", this.age,
this.age = age);
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
Tip
This class has also propertyChange support, which is not necessary for this example. Feel free to ignore this and implement your own POJO's with only the getter and setters.Now create a class, called "ModelProvider". This class will be providing the collection of persons. This class is defined as a Singleton (see The Singleton Pattern for details).
package de.vogella.jface.tableviewer.model;
import java.util.ArrayList;
import java.util.List;
public class ModelProvider {
private static ModelProvider content;
private Listpersons;
private ModelProvider() {
persons = new ArrayList();
// Image here some fancy database access to read the persons and to
// put them into the model
Person person;
person = new Person("Rainer", "Zufall", "male", true);
persons.add(person);
person = new Person("Rainer", "Babbel", "male", true);
persons.add(person);
person = new Person("Marie", "Darms", "female", false);
persons.add(person);
person = new Person("Holger", "Adams", "male", true);
persons.add(person);
person = new Person("Juliane", "Adams", "female", true);
persons.add(person);
}
public static synchronized ModelProvider getInstance() {
if (content != null) {
return content;
}
content = new ModelProvider();
return content;
}
public ListgetPersons() {
return persons;
}
}
0 comments:
Post a Comment