2015-01-16 2 views

답변

0

목록에서 새 요소를 강조 표시하는 방법에 대해 질문하거나 GlazedLists EventList에 의해 뒷받침되는 UI 구성 요소에서 행을 문자로 강조 표시할지 여부를 확인하는 것은 어렵습니다.

당분간 나는 전자를 추측 할 것이지만 명확하게 설명해 주시기 바랍니다.

GlazedLists 패키지에는 목록에 영향을주는 변경 사항에 작은 피크를 부여 할 수있는 ListEvents이라는 개념이 있습니다. 제가 많이 해본 적이있는 것은 아니며 다소 초보적인 것처럼 보입니다. 그러나 적절한 상황에서이 메커니즘을 사용할 수 있습니다.

일부 정수가 포함 된 BasicEventList의 샘플 클래스가 있습니다. ListEventListener을 작성하고 EventList에 첨부했습니다. ListEvents는 요소가 삽입 된 위치를 알려줍니다. 또한 eventlist에 대한 참조를 포함하므로 새로 삽입 된 값과 그 앞에 오는 요소의 값을 가져올 수 있습니다. 순서가 맞는지 아닌지 빨리 비교해 봅니다.

물론 몇 가지 중요한주의 사항이 있습니다. 이벤트 처리는 비동기 적이므로 원래 목록이 원래 트리거 시간과 청취자가 이벤트를 처리하는 시간 사이에 상당히 변경 될 가능성이 완전히 있습니다. 내 예제에서는 추가 작업 만 사용하기 때문에 괜찮습니다. 또한 나는 BasicEventList만을 사용하고 있습니다. 그것이 SortedList이면 항목이 다른 색인에 삽입되므로 현재 값과 이전 값을 가져 오는 방법은 매우 신뢰할 수 없습니다. (이 문제를 해결할 방법이 있을지 모르지만 나는이 문제에 모든 정직함을 적용하지 않았다.)

최소한 리스너를 사용하여 적어도 목록 변경을 경고하고 listener 클래스는 목록을 스캔하여 순서가 잘못된 항목이 있는지 여부를 확인합니다.

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에 대해서만 테스트했습니다.

+0

예, 강조하고 싶은 (또는 가능하면 색상을 지정하는) 새로운 요소입니다. 코드를 제공해 주셔서 감사 드리며, 피드백을받는 방법을 알려 드리겠습니다. – user3914455

관련 문제