< prev index next >

modules/javafx.base/src/main/java/javafx/collections/ListChangeListener.java

Print this page




  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&lt;Item&gt;() {
  78      *     public void onChanged(Change&lt;tem&gt; c) {
  79      *         while (c.next()) {
  80      *             if (c.wasPermutated()) {
  81      *                     for (int i = c.getFrom(); i &lt; 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      *         }


 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




  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&lt;Item&gt; theList = ...;
  76      *
  77      * theList.addListener(new ListChangeListener&lt;Item&gt;() {
  78      *     public void onChanged(Change&lt;tem&gt; c) {
  79      *         while (c.next()) {
  80      *             if (c.wasPermutated()) {
  81      *                     for (int i = c.getFrom(); i &lt; 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      *         }


 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
 207          * {@link Change} above, will effectively handle also set operation.
 208          *
 209          * @return same as {@code wasAdded() && wasRemoved()}
 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


< prev index next >