1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.event; 26 27 import javax.swing.RowSorter; 28 29 /** 30 * <code>RowSorterEvent</code> provides notification of changes to 31 * a <code>RowSorter</code>. Two types of notification are possible: 32 * <ul> 33 * <li><code>Type.SORT_ORDER_CHANGED</code>: indicates the sort order has 34 * changed. This is typically followed by a notification of: 35 * <li><code>Type.SORTED</code>: indicates the contents of the model have 36 * been transformed in some way. For example, the contents may have 37 * been sorted or filtered. 38 * </ul> 39 * 40 * @see javax.swing.RowSorter 41 * @since 1.6 42 */ 43 public class RowSorterEvent extends java.util.EventObject { 44 private Type type; 45 private int[] oldViewToModel; 46 47 /** 48 * Enumeration of the types of <code>RowSorterEvent</code>s. 49 * 50 * @since 1.6 51 */ 52 public enum Type { 53 /** 54 * Indicates the sort order has changed. 55 */ 56 SORT_ORDER_CHANGED, 57 58 /** 59 * Indicates the contents have been newly sorted or 60 * transformed in some way. 61 */ 62 SORTED 63 } 64 65 /** 66 * Creates a <code>RowSorterEvent</code> of type 67 * <code>SORT_ORDER_CHANGED</code>. 68 * 69 * @param source the source of the change 70 * @throws IllegalArgumentException if <code>source</code> is 71 * <code>null</code> 72 */ 73 public RowSorterEvent(RowSorter source) { 74 this(source, Type.SORT_ORDER_CHANGED, null); 75 } 76 77 /** 78 * Creates a <code>RowSorterEvent</code>. 79 * 80 * @param source the source of the change 81 * @param type the type of event 82 * @param previousRowIndexToModel the mapping from model indices to 83 * view indices prior to the sort, may be <code>null</code> 84 * @throws IllegalArgumentException if source or <code>type</code> is 85 * <code>null</code> 86 */ 87 public RowSorterEvent(RowSorter source, Type type, 88 int[] previousRowIndexToModel) { 89 super(source); 90 if (type == null) { 91 throw new IllegalArgumentException("type must be non-null"); 92 } 93 this.type = type; 94 this.oldViewToModel = previousRowIndexToModel; 95 } 96 97 /** 98 * Returns the source of the event as a <code>RowSorter</code>. 99 * 100 * @return the source of the event as a <code>RowSorter</code> 101 */ 102 public RowSorter getSource() { 103 return (RowSorter)super.getSource(); 104 } 105 106 /** 107 * Returns the type of event. 108 * 109 * @return the type of event 110 */ 111 public Type getType() { 112 return type; 113 } 114 115 /** 116 * Returns the location of <code>index</code> in terms of the 117 * model prior to the sort. This method is only useful for events 118 * of type <code>SORTED</code>. This method will return -1 if the 119 * index is not valid, or the locations prior to the sort have not 120 * been provided. 121 * 122 * @param index the index in terms of the view 123 * @return the index in terms of the model prior to the sort, or -1 if 124 * the location is not valid or the mapping was not provided. 125 */ 126 public int convertPreviousRowIndexToModel(int index) { 127 if (oldViewToModel != null && index >= 0 && 128 index < oldViewToModel.length) { 129 return oldViewToModel[index]; 130 } 131 return -1; 132 } 133 134 /** 135 * Returns the number of rows before the sort. This method is only 136 * useful for events of type <code>SORTED</code> and if the 137 * last locations have not been provided will return 0. 138 * 139 * @return the number of rows in terms of the view prior to the sort 140 */ 141 public int getPreviousRowCount() { 142 return (oldViewToModel == null) ? 0 : oldViewToModel.length; 143 } 144 }