我有一个列表,用于监控某些实体在严格升序的数字序列中的到达,并希望在序列中有明显中断的地方显示一个条目。
有什么方法可以突出显示GlazeList
s 中的条目吗?
我有一个列表,用于监控某些实体在严格升序的数字序列中的到达,并希望在序列中有明显中断的地方显示一个条目。
有什么方法可以突出显示GlazeList
s 中的条目吗?
很难确定您是在询问如何突出显示列表中的新元素,还是在字面上突出显示由 GlazedLists 支持的 UI 组件中的一行EventList
。
现在我假设前者,但随时澄清。
GlazedLists 包中有一个ListEvents的概念,它允许人们在影响列表的更改中获得一个小峰值。这不是我玩过太多的东西,而且看起来相当初级,但在适当的情况下可以使用这种机制。
这是一个BasicEventList
包含一些整数的示例类。我创建了一个ListEventListener
并将其附加到EventList
. ListEvents 告诉您元素插入的位置。它还包含对事件列表的引用,因此可以获得新插入的值,以及它之前的元素的值。我做了一个快速比较,看看它们是否乱序。
当然,这里有一些主要的警告。事件处理是异步的,因此在原始触发器的时间和侦听器处理事件的时间之间,底层列表完全有可能发生很大变化。在我的示例中没关系,因为我只使用附加操作。另外我只使用BasicEventList
; 如果它是 aSortedList
那么这些项目将被插入到不同的索引中,所以我用来获取当前值和以前值的方法将非常不可靠。(可能有办法解决这个问题,但老实说,我并没有把自己应用于这个问题。)
至少您可以使用侦听器至少提醒您列表更改,并让侦听器类之外的另一个方法执行列表扫描以确定是否有项目乱序。
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
public class GlazedListListen {
private final EventList<Integer> numbers = new BasicEventList<Integer>();
public GlazedListListen() {
numbers.addListEventListener(new MyEventListListener());
numbers.addAll(GlazedLists.eventListOf(1,2,4,5,7,8));
}
class MyEventListListener implements ListEventListener<Integer> {
@Override
public void listChanged(ListEvent<Integer> le) {
while (le.next()) {
if (le.getType() == ListEvent.INSERT) {
final int startIndex = le.getBlockStartIndex();
if (startIndex == 0) continue; // Inserted at head of list - nothing to compare with to move on.
final Integer previousValue = le.getSourceList().get(startIndex-1);
final Integer newValue = le.getSourceList().get(startIndex);
System.out.println("INSERTING " + newValue + " at " + startIndex);
if ((newValue - previousValue) > 1) {
System.out.println("VALUE OUT OF SEQUENCE! " + newValue + " @ " + startIndex);
}
}
}
}
}
public static void main(String[] args) {
new GlazedListListen();
}
}
注意:我只针对 GlazedLists v1.8 进行了测试。