KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > event > RowSorterEvent


1 /*
2  * @(#)RowSorterEvent.java 1.3 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.event;
8
9 import javax.swing.RowSorter JavaDoc;
10
11 /**
12  * <code>RowSorterEvent</code> provides notification of changes to
13  * a <code>RowSorter</code>. Two types of notification are possible:
14  * <ul>
15  * <li><code>Type.SORT_ORDER_CHANGED</code>: indicates the sort order has
16  * changed. This is typically followed by a notification of:
17  * <li><code>Type.SORTED</code>: indicates the contents of the model have
18  * been transformed in some way. For example, the contents may have
19  * been sorted or filtered.
20  * </ul>
21  *
22  * @version 1.3 11/17/05
23  * @see javax.swing.RowSorter
24  * @since 1.6
25  */

26 public class RowSorterEvent extends java.util.EventObject JavaDoc {
27     private Type type;
28     private int[] oldViewToModel;
29
30     /**
31      * Enumeration of the types of <code>RowSorterEvent</code>s.
32      *
33      * @since 1.6
34      */

35     public enum Type {
36         /**
37          * Indicates the sort order has changed.
38          */

39         SORT_ORDER_CHANGED,
40
41         /**
42          * Indicates the contents have been newly sorted or
43          * transformed in some way.
44          */

45         SORTED
46     }
47
48     /**
49      * Creates a <code>RowSorterEvent</code> of type
50      * <code>SORT_ORDER_CHANGED</code>.
51      *
52      * @param source the source of the change
53      * @throws IllegalArgumentException if <code>source</code> is
54      * <code>null</code>
55      */

56     public RowSorterEvent(RowSorter JavaDoc source) {
57         this(source, Type.SORT_ORDER_CHANGED, null);
58     }
59
60     /**
61      * Creates a <code>RowSorterEvent</code>.
62      *
63      * @param source the source of the change
64      * @param type the type of event
65      * @param previousRowIndexToModel the mapping from model indices to
66      * view indices prior to the sort, may be <code>null</code>
67      * @throws IllegalArgumentException if source or <code>type</code> is
68      * <code>null</code>
69      */

70     public RowSorterEvent(RowSorter JavaDoc source, Type type,
71                           int[] previousRowIndexToModel) {
72         super(source);
73         if (type == null) {
74             throw new IllegalArgumentException JavaDoc("type must be non-null");
75         }
76         this.type = type;
77         this.oldViewToModel = previousRowIndexToModel;
78     }
79
80     /**
81      * Returns the source of the event as a <code>RowSorter</code>.
82      *
83      * @return the source of the event as a <code>RowSorter</code>
84      */

85     public RowSorter JavaDoc getSource() {
86         return (RowSorter JavaDoc)super.getSource();
87     }
88
89     /**
90      * Returns the type of event.
91      *
92      * @return the type of event
93      */

94     public Type getType() {
95         return type;
96     }
97
98     /**
99      * Returns the location of <code>index</code> in terms of the
100      * model prior to the sort. This method is only useful for events
101      * of type <code>SORTED</code>. This method will return -1 if the
102      * index is not valid, or the locations prior to the sort have not
103      * been provided.
104      *
105      * @param index the index in terms of the view
106      * @return the index in terms of the model prior to the sort, or -1 if
107      * the location is not valid or the mapping was not provided.
108      */

109     public int convertPreviousRowIndexToModel(int index) {
110         if (oldViewToModel != null && index >= 0 &&
111                 index < oldViewToModel.length) {
112             return oldViewToModel[index];
113         }
114         return -1;
115     }
116
117     /**
118      * Returns the number of rows before the sort. This method is only
119      * useful for events of type <code>SORTED</code> and if the
120      * last locations have not been provided will return 0.
121      *
122      * @return the number of rows in terms of the view prior to the sort
123      */

124     public int getPreviousRowCount() {
125         return (oldViewToModel == null) ? 0 : oldViewToModel.length;
126     }
127 }
128
Popular Tags