1 /* 2 * Copyright (c) 2010, 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 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 a changes done to an Observablelist. 43 * The Change may consist of one or more actual changes and must be iterated by {@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<tem> 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 * 98 * }</pre></blockquote> 99 * <p> 100 * <b>Warning:</b> This class directly accesses the source list to acquire information about the changes. 101 * <br> This effectively makes the Change object invalid when another change occurs on the list. 102 * <br> For this reason it is <b>not safe to use this class on a different thread</b>. 103 * <br> It also means <b>the source list cannot be modified inside the listener</b> since that would invalidate this Change object 104 * for all subsequent listeners. 105 * <p> 106 * Note: in case the change contains multiple changes of different type, these changes must be in the following order: 107 * <em> permutation change(s), add or remove changes, update changes </em> 108 * This is because permutation changes cannot go after add/remove changes as they would change the position of added elements. 109 * And on the other hand, update changes must go after add/remove changes because they refer with their indexes to the current 110 * state of the list, which means with all add/remove changes applied. 111 * @param <E> the list element type 112 * @since JavaFX 2.0 113 */ 114 public abstract static class Change<E> { 115 private final ObservableList<E> list; 116 117 /** 118 * Go to the next change. 119 * The Change in the initial state is invalid a requires a call to next() before 120 * calling other methods. The first next() call will make this object 121 * represent the first change. 122 * @return true if switched to the next change, false if this is the last change. 123 */ 124 public abstract boolean next(); 125 126 /** 127 * Reset to the initial stage. After this call, the next() must be called 128 * before working with the first change. 129 */ 130 public abstract void reset(); 131 132 /** 133 * Constructs a new change done to a list. 134 * @param list that was changed 135 */ 136 public Change(ObservableList<E> list) { 137 this.list = list; 138 } 139 140 /** 141 * The source list of the change. 142 * @return a list that was changed 143 */ 144 public ObservableList<E> getList() { 145 return list; 146 } 147 148 /** 149 * If wasAdded is true, the interval contains all the values that were added. 150 * If wasPermutated is true, the interval marks the values that were permutated. 151 * If wasRemoved is true and wasAdded is false, getFrom() and getTo() should 152 * return the same number - the place where the removed elements were positioned in the list. 153 * @return a beginning (inclusive) of an interval related to the change 154 * @throws IllegalStateException if this Change is in initial state 155 */ 156 public abstract int getFrom(); 157 /** 158 * The end of the change interval. 159 * @return a end (exclusive) of an interval related to the change. 160 * @throws IllegalStateException if this Change is in initial state 161 * @see #getFrom() 162 */ 163 public abstract int getTo(); 164 /** 165 * An immutable list of removed/replaced elements. If no elements 166 * were removed from the list, an empty list is returned. 167 * @return a list with all the removed elements 168 * @throws IllegalStateException if this Change is in initial state 169 */ 170 public abstract List<E> getRemoved(); 171 /** 172 * Indicates if the change was only a permutation. 173 * @return true if the change was just a permutation. 174 * @throws IllegalStateException if this Change is in initial state 175 */ 176 public boolean wasPermutated() { 177 return getPermutation().length != 0; 178 } 179 180 /** 181 * Indicates if elements were added during this change 182 * @return true if something was added to the list 183 * @throws IllegalStateException if this Change is in initial state 184 */ 185 public boolean wasAdded() { 186 return !wasPermutated() && !wasUpdated() && getFrom() < getTo(); 187 } 188 189 /** 190 * Indicates if elements were removed during this change. 191 * Note that using set will also produce a change with wasRemoved() returning 192 * true. See {@link #wasReplaced()}. 193 * @return true if something was removed from the list 194 * @throws IllegalStateException if this Change is in initial state 195 */ 196 public boolean wasRemoved() { 197 return !getRemoved().isEmpty(); 198 } 199 200 /** 201 * Indicates if elements were replaced during this change. 202 * This is usually true when set is called on the list. 203 * Set operation will act like remove and add operation at the same time. 204 * <p> 205 * Usually, it's not necessary to use this method directly. 206 * Handling remove operation and then add operation, as in the example {@link ListChangeListener$Change above}, 207 * will effectively handle also set operation. 208 * 209 * @return same <code> as wasAdded() && wasRemoved() </code> 210 * @throws IllegalStateException if this Change is in initial state 211 */ 212 public boolean wasReplaced() { 213 return wasAdded() && wasRemoved(); 214 } 215 216 /** 217 * Indicates that the elements between getFrom() (inclusive) 218 * to getTo() exclusive has changed. 219 * This is the only optional event type and may not be 220 * fired by all ObservableLists. 221 * @return true if the current change is an update change. 222 * @since JavaFX 2.1 223 */ 224 public boolean wasUpdated() { 225 return false; 226 } 227 228 /** 229 * To get a subList view of the list that contains only the elements 230 * added, use getAddedSubList() method. 231 * This is actually a shortcut to <code>c.getList().subList(c.getFrom(), c.getTo());</code><br> 232 * 233 * <pre><code> 234 * for (Node n : change.getAddedSubList()) { 235 * // do something 236 * } 237 * </code></pre> 238 * @return the newly created sublist view that contains all the added elements. 239 * @throws IllegalStateException if this Change is in initial state 240 */ 241 public List<E> getAddedSubList() { 242 return wasAdded()? getList().subList(getFrom(), getTo()) : Collections.<E>emptyList(); 243 } 244 245 /** 246 * Size of getRemoved() list. 247 * @return the number of removed items 248 * @throws IllegalStateException if this Change is in initial state 249 */ 250 public int getRemovedSize() { 251 return getRemoved().size(); 252 } 253 254 /** 255 * Size of the interval that was added. 256 * @return the number of added items 257 * @throws IllegalStateException if this Change is in initial state 258 */ 259 public int getAddedSize() { 260 return wasAdded() ? getTo() - getFrom() : 0; 261 } 262 263 /** 264 * If this change is an permutation, it returns an integer array 265 * that describes the permutation. 266 * This array maps directly from the previous indexes to the new ones. 267 * This method is not publicly accessible and therefore can return an array safely. 268 * The 0 index of the array corresponds to index {@link #getFrom()} of the list. The same applies 269 * for the last index and {@link #getTo()}. 270 * The method is used by {@link #wasPermutated() } and {@link #getPermutation(int)} methods. 271 * @return empty array if this is not permutation or an integer array containing the permutation 272 * @throws IllegalStateException if this Change is in initial state 273 */ 274 protected abstract int[] getPermutation(); 275 276 /** 277 * By calling these method, you can observe the permutation that happened. 278 * In order to get the new position of an element, you must call: 279 * <pre> 280 * change.getPermutation(oldIndex); 281 * </pre> 282 * 283 * Note: default implementation of this method takes the information 284 * from {@link #getPermutation()} method. You don't have to override this method. 285 * @param i the old index that contained the element prior to this change 286 * @throws IndexOutOfBoundsException if i is out of the bounds of the list 287 * @throws IllegalStateException if this is not a permutation change 288 * @return the new index of the same element 289 */ 290 public int getPermutation(int i) { 291 if (!wasPermutated()) { 292 throw new IllegalStateException("Not a permutation change"); 293 } 294 return getPermutation()[i - getFrom()]; 295 } 296 297 } 298 /** 299 * Called after a change has been made to an ObservableList. 300 * 301 * @param c an object representing the change that was done 302 * @see Change 303 */ 304 public void onChanged(Change<? extends E> c); 305 }