3

我有一个表,其数据源是BeanItemContainer.

当用户选择表中的一行时,相应的列填充一组,TextFields以便用户可以编辑信息。我想要做的是找到一种干净的方法来识别Fields用户单击保存后编辑的内容(以便我跟踪用户所做的更改,以便我只保存必要的内容)。

我已经看到我可以用它isModified()来查看 aField是否已从其先前的值更改,但是为每个调用它TextField似乎很多(当我在文本字段上调用它时似乎isModified()不起作用)。所以我基本上是在寻找一种更好的方法来检查一个字段是否已被修改。

谢谢

4

2 回答 2

0

创建 TextField 时,将当前属性值(字符串)放入 TextField 中:

tf.setData(property.getValue())

当用户单击保存时,您可以比较两个值(当前值和已保存值)。

于 2013-10-31T12:52:59.647 回答
0

使用 BeanItem 属性。您必须自己比较值,但您在每个属性的循环中进行比较(请参阅源代码中的方法“between”)。您所需要的只是可以用作 BeanItem 的数据类,或者是 BeanItem 本身。在 UI 中,您需要拥有具有原始数据的对象和具有更改的原始数据的对象。这是我用来提取两个数据版本之间更改的类:

public class Diff implements Iterable<DiffEntry>{

public static class DiffEntry{
    public final Object propertyId;
    public final Object oldValue;
    public final Object newValue;

    public DiffEntry(Object propertyId, Object oldValue, Object newValue) {
        super();
        this.propertyId = propertyId;
        this.oldValue = oldValue;
        this.newValue = newValue;
    }
}

public static <T> Diff between(T oldPojo, T newPojo) {
    //HERE WE EXTRACT WHAT WAS CHANGED
    // this could also take BeanItems directly if data are BeanItems
    Diff diff = new Diff();
    BeanItem<T> oldBean = new BeanItem<T>(oldPojo);
    BeanItem<T> newBean = new BeanItem<T>(newPojo);
    for(Object propertyId : oldBean.getItemPropertyIds()) {
        Object oldValue = oldBean.getItemProperty(propertyId).getValue();
        Object newValue = newBean.getItemProperty(propertyId).getValue();
        if(oldValue == null) {
            if(newValue != null) {
                DiffEntry entry = new DiffEntry(propertyId, oldValue, newValue);
                diff.add(entry);
            }
        }
        else if(newValue == null) {
            DiffEntry entry = new DiffEntry(propertyId, oldValue, newValue);
            diff.add(entry);
        }
        else if(!oldValue.equals(newValue)) {
            DiffEntry entry = new DiffEntry(propertyId, oldValue, newValue);
            diff.add(entry);
        }
        else {
            //old and new values are equal
        }
    }
    return diff;
}

private final Map<Object, DiffEntry> entries = new HashMap<>();

public Diff() {

}

private void add(DiffEntry entry) {
    this.entries.put(entry.propertyId, entry);
}

/**
 * Returns true if this diff contains difference for specified property id
 * @param propertyId id of property we test for difference
 * @return true if this diff contains difference for specified property id
 */
public boolean contains(Object propertyId) {
    return this.entries.containsKey(propertyId);
}

/**
 * Returns true if there are no differencies
 * @return true if there are no differencies
 */
public boolean isEmpty() {
    return this.entries.isEmpty();
}

@Override
public Iterator<DiffEntry> iterator() {
    return entries.values().iterator();
}
}

在您的上下文中“跟踪用户所做的更改,并且只保存必要的”这将完成这项工作,但它不会阻止处理 UI 中的所有字段,因为这是在读取所有数据之后完成的来自字段并存储在newPojo中!

于 2017-07-28T04:56:25.497 回答