1 /* 2 * Copyright (c) 2010, 2017, 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 26 package javafx.collections; 27 28 import java.util.Collections; 29 import java.util.List; 30 31 /** 32 * Interface that receives notifications of changes to an ObservableList. 33 * 34 * @param <E> the list element type 35 * @see Change 36 * @since JavaFX 2.0 37 */ 38 @FunctionalInterface 39 public interface ListChangeListener<E> { 40 41 /** 42 * Represents a report of changes done to an {@link ObservableList}. The change may consist of one or more actual 43 * changes and must be iterated by calling the {@link #next()} method. 44 * 45 * Each change must be one of the following: 46 * <ul> 47 * <li><b>Permutation change</b> : {@link #wasPermutated()} returns true in this case. 48 * The permutation happened at range between {@link #getFrom() from} (inclusive) and {@link #getTo() to} (exclusive) and 49 * can be queried by calling {@link #getPermutation(int)} method. 50 * <li><b>Add or remove change</b> : In this case, at least one of the {@link #wasAdded()}, {@link #wasRemoved()} returns true. 51 * If both methods return true, {@link #wasReplaced()} will also return true. 52 * <p>The {@link #getRemoved()} method returns a list of elements that have been 53 * replaced or removed from the list. 54 * <p> The range between {@link #getFrom() from} (inclusive) and {@link #getTo() to} (exclusive) 55 * denotes the sublist of the list that contain new elements. Note that this is a half-open 56 * interval, so if no elements were added, {@code getFrom()} is equal to {@code getTo()}. 57 * <p>It is possible to get a list of added elements by calling getAddedSubList(). 58 * <p>Note that in order to maintain correct indexes of the separate add/remove changes, these changes 59 * <b>must</b> be sorted by their {@code from} index. 60 * <li><b>Update change</b> : {@link #wasUpdated()} return true on an update change. 61 * All elements between {@link #getFrom() from} (inclusive) and {@link #getTo() to} (exclusive) were updated. 62 * </ul> 63 * 64 * <b>Important:</b> It's necessary to call {@link #next()} method before calling 65 * any other method of {@code Change}. The same applies after calling {@link #reset()}. 66 * The only methods that works at any time is {@link #getList()}. 67 * 68 *<p> 69 * Typical usage is to observe changes on an ObservableList in order 70 * to hook or unhook (or add or remove a listener) or in order to maintain 71 * some invariant on every element in that ObservableList. A common code 72 * pattern for doing this looks something like the following:<br> 73 * 74 * <blockquote><pre> 75 * ObservableList<Item> theList = ...; 76 * 77 * theList.addListener(new ListChangeListener<Item>() { 78 * public void onChanged(Change<Item> c) { 79 * while (c.next()) { 80 * if (c.wasPermutated()) { 81 * for (int i = c.getFrom(); i < c.getTo(); ++i) { 82 * //permutate 83 * } 84 * } else if (c.wasUpdated()) { 85 * //update item 86 * } else { 87 * for (Item remitem : c.getRemoved()) { 88 * remitem.remove(Outer.this); 89 * } 90 * for (Item additem : c.getAddedSubList()) { 91 * additem.add(Outer.this); 92 * } 93 * } 94 * } 95 * }); 96 * 97 * }</pre></blockquote> 98 * <p> 99 * <b>Warning:</b> This class directly accesses the source list to acquire information about the changes. 100 * <br> This effectively makes the Change object invalid when another change occurs on the list. 101 * <br> For this reason it is <b>not safe to use this class on a different thread</b>. 102 * <br> It also means <b>the source list cannot be modified inside the listener</b> since that would invalidate this Change object 103 * for all subsequent listeners. 104 * <p> 105 * Note: in case the change contains multiple changes of different type, these changes must be in the following order: 106 * <em> permutation change(s), add or remove changes, update changes </em> 107 * This is because permutation changes cannot go after add/remove changes as they would change the position of added elements. 108 * And on the other hand, update changes must go after add/remove changes because they refer with their indexes to the current 109 * state of the list, which means with all add/remove changes applied. 110 * @param <E> the list element type 111 * @since JavaFX 2.0 112 */ 113 public abstract static class Change<E> { 114 private final ObservableList<E> list; 115 116 /** 117 * Goes to the next change. 118 * The Change instance, in its initial state, is invalid and requires a call to {@code next()} before 119 * calling other methods. The first {@code next()} call will make this object 120 * represent the first change. 121 * @return true if switched to the next change, false if this is the last change. 122 */ 123 public abstract boolean next(); 124 125 /** 126 * Resets to the initial stage. After this call, {@link #next()} must be called 127 * before working with the first change. 128 */ 129 public abstract void reset(); 130 131 /** 132 * Constructs a new Change instance on the given list. 133 * @param list The list that was changed 134 */ 135 public Change(ObservableList<E> list) { 136 this.list = list; 137 } 138 139 /** 140 * The source list of the change. 141 * @return a list that was changed 142 */ 143 public ObservableList<E> getList() { 144 return list; 145 } 146 147 /** 148 * If {@link #wasAdded()} is true, the interval contains all the values that were added. 149 * If {@link #wasPermutated()} is true, the interval marks the values that were permutated. 150 * If {@link #wasRemoved()} is true and {@code wasAdded} is false, {@link #getFrom()} and {@link #getTo()} 151 * should return the same number - the place where the removed elements were positioned in the list. 152 * @return a beginning (inclusive) of an interval related to the change 153 * @throws IllegalStateException if this Change instance is in initial state 154 */ 155 public abstract int getFrom(); 156 157 /** 158 * The end of the change interval. 159 * @return an end (exclusive) of an interval related to the change. 160 * @throws IllegalStateException if this Change instance is in initial state 161 * @see #getFrom() 162 */ 163 public abstract int getTo(); 164 165 /** 166 * An immutable list of removed/replaced elements. If no elements 167 * were removed from the list, an empty list is returned. 168 * @return a list with all the removed elements 169 * @throws IllegalStateException if this Change instance is in initial state 170 */ 171 public abstract List<E> getRemoved(); 172 173 /** 174 * Indicates if the change was only a permutation. 175 * @return true if the change was just a permutation. 176 * @throws IllegalStateException if this Change instance is in initial state 177 */ 178 public boolean wasPermutated() { 179 return getPermutation().length != 0; 180 } 181 182 /** 183 * Indicates if elements were added during this change. 184 * @return true if something was added to the list 185 * @throws IllegalStateException if this Change instance is in initial state 186 */ 187 public boolean wasAdded() { 188 return !wasPermutated() && !wasUpdated() && getFrom() < getTo(); 189 } 190 191 /** 192 * Indicates if elements were removed during this change. 193 * Note that using set will also produce a change with {@code wasRemoved()} returning 194 * true. See {@link #wasReplaced()}. 195 * @return true if something was removed from the list 196 * @throws IllegalStateException if this Change instance is in initial state 197 */ 198 public boolean wasRemoved() { 199 return !getRemoved().isEmpty(); 200 } 201 202 /** 203 * Indicates if elements were replaced during this change. 204 * This is usually true when set is called on the list. 205 * Set operation will act like remove and add operation at the same time. 206 * <p> 207 * Usually, it's not necessary to use this method directly. 208 * Handling remove operation and then add operation, as in the example in 209 * the {@link Change} class javadoc, will effectively handle the set operation. 210 * 211 * @return same as {@code wasAdded() && wasRemoved()} 212 * @throws IllegalStateException if this Change instance is in initial state 213 */ 214 public boolean wasReplaced() { 215 return wasAdded() && wasRemoved(); 216 } 217 218 /** 219 * Indicates that the elements between {@link #getFrom()} (inclusive) 220 * to {@link #getTo()} exclusive has changed. 221 * This is the only optional event type and may not be 222 * fired by all ObservableLists. 223 * @return true if the current change is an update change. 224 * @since JavaFX 2.1 225 */ 226 public boolean wasUpdated() { 227 return false; 228 } 229 230 /** 231 * Returns a subList view of the list that contains only the elements added. This is actually a shortcut to 232 * <code>c.getList().subList(c.getFrom(), c.getTo());</code> 233 * 234 * <pre>{@code 235 * for (Node n : change.getAddedSubList()) { 236 * // do something 237 * } 238 * }</pre> 239 * @return the newly created sublist view that contains all the added elements. 240 * @throws IllegalStateException if this Change instance is in initial state 241 */ 242 public List<E> getAddedSubList() { 243 return wasAdded()? getList().subList(getFrom(), getTo()) : Collections.<E>emptyList(); 244 } 245 246 /** 247 * Returns the size of {@link #getRemoved()} list. 248 * @return the number of removed items 249 * @throws IllegalStateException if this Change instance is in initial state 250 */ 251 public int getRemovedSize() { 252 return getRemoved().size(); 253 } 254 255 /** 256 * Returns the size of the interval that was added. 257 * @return the number of added items 258 * @throws IllegalStateException if this Change instance is in initial state 259 */ 260 public int getAddedSize() { 261 return wasAdded() ? getTo() - getFrom() : 0; 262 } 263 264 /** 265 * If this change is a permutation, it returns an integer array 266 * that describes the permutation. 267 * This array maps directly from the previous indexes to the new ones. 268 * This method is not publicly accessible and therefore can return an array safely. 269 * The 0 index of the array corresponds to index {@link #getFrom()} of the list. The same applies 270 * for the last index and {@link #getTo()}. 271 * The method is used by {@link #wasPermutated() } and {@link #getPermutation(int)} methods. 272 * @return empty array if this is not permutation or an integer array containing the permutation 273 * @throws IllegalStateException if this Change instance is in initial state 274 */ 275 protected abstract int[] getPermutation(); 276 277 /** 278 * This method allows developers to observe the permutations that occurred in this change. In order to get the 279 * new position of an element, you must call: 280 * <pre> 281 * change.getPermutation(oldIndex); 282 * </pre> 283 * 284 * Note: default implementation of this method takes the information 285 * from {@link #getPermutation()} method. You don't have to override this method. 286 * @param i the old index that contained the element prior to this change 287 * @throws IndexOutOfBoundsException if i is out of the bounds of the list 288 * @throws IllegalStateException if this is not a permutation change 289 * @return the new index of the same element 290 */ 291 public int getPermutation(int i) { 292 if (!wasPermutated()) { 293 throw new IllegalStateException("Not a permutation change"); 294 } 295 return getPermutation()[i - getFrom()]; 296 } 297 298 } 299 /** 300 * Called after a change has been made to an ObservableList. 301 * 302 * @param c an object representing the change that was done 303 * @see Change 304 */ 305 public void onChanged(Change<? extends E> c); 306 }