`
louisling
  • 浏览: 144623 次
  • 性别: Icon_minigender_1
  • 来自: ZhuHai
社区版块
存档分类
最新评论

Change background for new or changed records

    博客分类:
  • RCP
阅读更多
TableViewer: Change background for new or changed records

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;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics