001 /*
002 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.event;
026
027 import javax.swing.RowSorter;
028
029 /**
030 * <code>RowSorterEvent</code> provides notification of changes to
031 * a <code>RowSorter</code>. Two types of notification are possible:
032 * <ul>
033 * <li><code>Type.SORT_ORDER_CHANGED</code>: indicates the sort order has
034 * changed. This is typically followed by a notification of:
035 * <li><code>Type.SORTED</code>: indicates the contents of the model have
036 * been transformed in some way. For example, the contents may have
037 * been sorted or filtered.
038 * </ul>
039 *
040 * @version 1.9 05/05/07
041 * @see javax.swing.RowSorter
042 * @since 1.6
043 */
044 public class RowSorterEvent extends java.util.EventObject {
045 private Type type;
046 private int[] oldViewToModel;
047
048 /**
049 * Enumeration of the types of <code>RowSorterEvent</code>s.
050 *
051 * @since 1.6
052 */
053 public enum Type {
054 /**
055 * Indicates the sort order has changed.
056 */
057 SORT_ORDER_CHANGED,
058
059 /**
060 * Indicates the contents have been newly sorted or
061 * transformed in some way.
062 */
063 SORTED
064 }
065
066 /**
067 * Creates a <code>RowSorterEvent</code> of type
068 * <code>SORT_ORDER_CHANGED</code>.
069 *
070 * @param source the source of the change
071 * @throws IllegalArgumentException if <code>source</code> is
072 * <code>null</code>
073 */
074 public RowSorterEvent(RowSorter source) {
075 this (source, Type.SORT_ORDER_CHANGED, null);
076 }
077
078 /**
079 * Creates a <code>RowSorterEvent</code>.
080 *
081 * @param source the source of the change
082 * @param type the type of event
083 * @param previousRowIndexToModel the mapping from model indices to
084 * view indices prior to the sort, may be <code>null</code>
085 * @throws IllegalArgumentException if source or <code>type</code> is
086 * <code>null</code>
087 */
088 public RowSorterEvent(RowSorter source, Type type,
089 int[] previousRowIndexToModel) {
090 super (source);
091 if (type == null) {
092 throw new IllegalArgumentException("type must be non-null");
093 }
094 this .type = type;
095 this .oldViewToModel = previousRowIndexToModel;
096 }
097
098 /**
099 * Returns the source of the event as a <code>RowSorter</code>.
100 *
101 * @return the source of the event as a <code>RowSorter</code>
102 */
103 public RowSorter getSource() {
104 return (RowSorter) super .getSource();
105 }
106
107 /**
108 * Returns the type of event.
109 *
110 * @return the type of event
111 */
112 public Type getType() {
113 return type;
114 }
115
116 /**
117 * Returns the location of <code>index</code> in terms of the
118 * model prior to the sort. This method is only useful for events
119 * of type <code>SORTED</code>. This method will return -1 if the
120 * index is not valid, or the locations prior to the sort have not
121 * been provided.
122 *
123 * @param index the index in terms of the view
124 * @return the index in terms of the model prior to the sort, or -1 if
125 * the location is not valid or the mapping was not provided.
126 */
127 public int convertPreviousRowIndexToModel(int index) {
128 if (oldViewToModel != null && index >= 0
129 && index < oldViewToModel.length) {
130 return oldViewToModel[index];
131 }
132 return -1;
133 }
134
135 /**
136 * Returns the number of rows before the sort. This method is only
137 * useful for events of type <code>SORTED</code> and if the
138 * last locations have not been provided will return 0.
139 *
140 * @return the number of rows in terms of the view prior to the sort
141 */
142 public int getPreviousRowCount() {
143 return (oldViewToModel == null) ? 0 : oldViewToModel.length;
144 }
145 }
|