View Javadoc

1   /*
2    * @(#)$Id: SortedListModel.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Michele Croci, mcroci@gmail.com
5    *
6    * Purpose		:  Provide a model for sorted JList
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Nov 22, 2007		crocimi    	Initial Release
15   *
16   * -----------------------------------------------------------------------
17   *
18   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
19   *
20   * This software is the proprietary information of ETH Zurich.
21   * Use is subject to license terms.
22   * 
23   */
24  
25  
26  package org.ximtec.igesture.geco.util;
27  
28  import java.util.Comparator;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.SortedSet;
32  import java.util.TreeSet;
33  
34  import javax.swing.AbstractListModel;
35  
36  
37  
38  /**
39   * Provide a model for sorted JList
40   * @version 0.9, Nov 22, 2007
41   * @author Michele Croci, mcroci@gmail.com
42   */
43  
44   public class SortedListModel<T> extends AbstractListModel {
45  
46  
47      SortedSet<T> model;
48      
49      public SortedListModel(Comparator<T> c) {
50         model = new TreeSet<T>(c);
51       }//SortedListModel
52       
53      
54      public SortedListModel() {
55         model = new TreeSet<T>();
56         }//SortedListModel
57      
58  
59      public int getSize() {
60        return model.size();
61      }//getSize
62  
63      public Object getElementAt(int index) {
64        return model.toArray()[index];
65      }//getElementAt
66  
67  
68      public void add(T element) {
69        if (model.add(element)) {
70          fireContentsChanged(this, 0, getSize());
71        }
72      }//add
73      
74      public void addAll(List<T> elements) {
75         model.addAll(elements);
76         fireContentsChanged(this, 0, getSize());
77       }//addAll
78  
79      public void clear() {
80        model.clear();
81        fireContentsChanged(this, 0, getSize());
82      }//clear
83  
84      public boolean contains(Object element) {
85        return model.contains(element);
86      }//contains
87  
88      public Object firstElement() {
89        return model.first();
90      }//firstElement
91  
92      public Iterator<T> iterator() {
93        return model.iterator();
94      }//iterator
95  
96      public Object lastElement() {
97        // Return the appropriate element
98        return model.last();
99      }//lastElement
100 
101     public boolean removeElement(Object element) {
102       boolean removed = model.remove(element);
103       if (removed) {
104         fireContentsChanged(this, 0, getSize());
105       }
106       return removed;   
107     }//removeElement
108    
109 }
110 
111