TableViewer: Change background for new or changed records
1) //override hashCode() of the class that displayed in the table.
2) In UI
3) //Init or Save
4) get changed objects
5)//class Provider implements ITableColorProvider
1) //override hashCode() of the class that displayed in the table.
/**
* Calculate these fields that affect the object state to change
*/
public int hashCode() {
return BaseUtils.hashCode(id, name, sex, date, value);
}
2) In UI
//<PrimaryKey, hashCode>
private Map<Object, Integer> states = new HashMap<Object, Integer>();
3) //Init or Save
//After persistenting, the Person.getID() is not null.
for (Person person : persons) {
if (person.getId() != null)
states.put(person.getId(), person.hashCode());
}
4) get changed objects
/** Before persistenting, get changed objects */
List<Person> getChangedObjects(List<Person> list, Map<Object,Integer> states) {
List<Person> changes = new ArrayList<Person>();
for (Person p : list) {
Integer hashCode = states.get(p.getId());
if (p.getId() == null || hashCode == null || hashCode != p.hashCode()) {
changes.add(p);
}
}
return changes;
}
5)//class Provider implements ITableColorProvider
public Color getBackground(Object element, int columnIndex) {
Person p = (Person) element;
boolean isNew = p.getId() == null;
boolean modified = states.containsKey(p.getId()) && states.get(p.getId()) != element.hashCode();
if (isNew || modified)
return RcpUI.COLOR_YELLOW;
else
return null;
}
public Color getForeground(Object element, int columnIndex) {
return null;
}