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 @SuppressWarnings("serial") // Same-version serialization only 44 public class RowSorterEvent extends java.util.EventObject { 45 private Type type; 46 private int[] oldViewToModel; 47 48 /** 49 * Enumeration of the types of <code>RowSorterEvent</code>s. 50 * 51 * @since 1.6 52 */ 53 public enum Type { 54 /** 55 * Indicates the sort order has changed. 56 */ 57 SORT_ORDER_CHANGED, 58 59 /** 60 * Indicates the contents have been newly sorted or 61 * transformed in some way. 62 */ 63 SORTED 64 } 65 66 /** 67 * Creates a <code>RowSorterEvent</code> of type 68 * <code>SORT_ORDER_CHANGED</code>. 69 * 70 * @param source the source of the change 71 * @throws IllegalArgumentException if <code>source</code> is 72 * <code>null</code> 73 */ 74 public RowSorterEvent(RowSorter<?> source) { 75 this(source, Type.SORT_ORDER_CHANGED, null); 76 } 77 78 /** 79 * Creates a <code>RowSorterEvent</code>. 80 * 81 * @param source the source of the change 82 * @param type the type of event 83 * @param previousRowIndexToModel the mapping from model indices to 84 * view indices prior to the sort, may be <code>null</code> 85 * @throws IllegalArgumentException if source or <code>type</code> is 86 * <code>null</code> 87 */ 88 public RowSorterEvent(RowSorter<?> source, Type type, 89 int[] previousRowIndexToModel) { 90 super(source); 91 if (type == null) { 92 throw new IllegalArgumentException("type must be non-null"); 93 } 94 this.type = type; 95 this.oldViewToModel = previousRowIndexToModel; 96 } 97 98 /** 99 * 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 @Override 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 }