1 /* 2 * Copyright (c) 2005, 2014, 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 static final long serialVersionUID = -2701905455516718736L; 45 46 private Type type; 47 private int[] oldViewToModel; 48 49 /** 50 * Enumeration of the types of <code>RowSorterEvent</code>s. 51 * 52 * @since 1.6 53 */ 54 public enum Type { 55 /** 56 * Indicates the sort order has changed. 57 */ 58 SORT_ORDER_CHANGED, 59 60 /** 61 * Indicates the contents have been newly sorted or 62 * transformed in some way. 63 */ 64 SORTED 65 } 66 67 /** 68 * Creates a <code>RowSorterEvent</code> of type 69 * <code>SORT_ORDER_CHANGED</code>. 70 * 71 * @param source the source of the change 72 * @throws IllegalArgumentException if <code>source</code> is 73 * <code>null</code> 74 */ 75 public RowSorterEvent(RowSorter source) { 76 this(source, Type.SORT_ORDER_CHANGED, null); 77 } 78 79 /** 80 * Creates a <code>RowSorterEvent</code>. 81 * 82 * @param source the source of the change 83 * @param type the type of event 84 * @param previousRowIndexToModel the mapping from model indices to 85 * view indices prior to the sort, may be <code>null</code> 86 * @throws IllegalArgumentException if source or <code>type</code> is 87 * <code>null</code> 88 */ 89 public RowSorterEvent(RowSorter source, Type type, 90 int[] previousRowIndexToModel) { 91 super(source); 92 if (type == null) { 93 throw new IllegalArgumentException("type must be non-null"); 94 } 95 this.type = type; 96 this.oldViewToModel = previousRowIndexToModel; 97 } 98 99 /** 100 * Returns the source of the event as a <code>RowSorter</code>. 101 * 102 * @return the source of the event as a <code>RowSorter</code> 103 */ 104 public RowSorter getSource() { 105 return (RowSorter)super.getSource(); 106 } 107 108 /** 109 * Returns the type of event. 110 * 111 * @return the type of event 112 */ 113 public Type getType() { 114 return type; 115 } 116 117 /** 118 * Returns the location of <code>index</code> in terms of the 119 * model prior to the sort. This method is only useful for events 120 * of type <code>SORTED</code>. This method will return -1 if the 121 * index is not valid, or the locations prior to the sort have not 122 * been provided. 123 * 124 * @param index the index in terms of the view 125 * @return the index in terms of the model prior to the sort, or -1 if 126 * the location is not valid or the mapping was not provided. 127 */ 128 public int convertPreviousRowIndexToModel(int index) { 129 if (oldViewToModel != null && index >= 0 && 130 index < oldViewToModel.length) { 131 return oldViewToModel[index]; 132 } 133 return -1; 134 } 135 136 /** 137 * Returns the number of rows before the sort. This method is only 138 * useful for events of type <code>SORTED</code> and if the 139 * last locations have not been provided will return 0. 140 * 141 * @return the number of rows in terms of the view prior to the sort 142 */ 143 public int getPreviousRowCount() { 144 return (oldViewToModel == null) ? 0 : oldViewToModel.length; 145 } 146 }