Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.collections;
027
028import java.util.Collections;
029import java.util.List;
030
031/**
032 * Interface that receives notifications of changes to an ObservableList.
033 *
034 * @param <E> the list element type
035 * @see Change
036 */
037public interface ListChangeListener<E> {
038
039    /**
040     * Represents a report of a changes done to an Observablelist.
041     * The Change may consist of one or more actual changes and must be iterated by {@link #next()} method.
042     *
043     * Each change must be one of the following:
044     * <ul>
045     * <li><b>Permutation change</b> : {@link #wasPermutated()} returns true in this case.
046     * The permutation happened at range between {@link #getFrom() from}(inclusive) and {@link #getTo() to}(exclusive) and
047     * can be queried by calling {@link #getPermutation(int)} method.
048     * <li><b>Add or remove change</b> : In this case, at least one of the {@link #wasAdded()}, {@link #wasRemoved()} returns true.
049     * If both methods return true, {@link #wasReplaced()} will also return true.
050     * <p>The {@link #getRemoved()} method returns a list of elements that have been
051     * replaced or removed from the list.
052     * <p> The range between {@link #getFrom() from}(inclusive) and {@link #getTo() to}(exclusive)
053     * denotes the sublist of the list that contain new elements. Note that this is a half-open
054     * interval, so if no elements were added, {@code getFrom()} is equal to {@code getTo()}.
055     * <p>It is possible to get a list of added elements by calling getAddedSubList().
056     * <p>Note that in order to maintain correct indexes of the separate add/remove changes, these changes
057     * <b>must</b> be sorted by their {@code from} index.
058     * <li><b>Update change</b> : {@link #wasUpdated()} return true on an update change.
059     * All elements between {@link #getFrom() from}(inclusive) and {@link #getTo() to}(exclusive) were updated.
060     * </ul>
061     *
062     *<p>
063     * Typical usage is to observe changes on an ObservableList in order
064     * to hook or unhook (or add or remove a listener) or in order to maintain
065     * some invariant on every element in that ObservableList. A common code
066     * pattern for doing this looks something like the following:<br>
067     *
068     * <blockquote><pre>
069     * ObservableList<Item> theList = ...;
070     *
071     * theList.addListener(new ListChangeListener&lt;Item&gt;() {
072     *     public void onChanged(Change&lt;tem&gt; c) {
073     *         while (c.next()) {
074     *             if (c.wasPermutated()) {
075     *                     for (int i = c.getFrom(); i &lt; c.getTo(); ++i) {
076     *                          //permutate
077     *                     }
078     *                 } else if (c.wasUpdated()) {
079     *                          //update item
080     *                 } else {
081     *                     for (Item remitem : c.getRemoved()) {
082     *                         remitem.remove(Outer.this);
083     *                     }
084     *                     for (Item additem : c.getAddedSubList()) {
085     *                         additem.add(Outer.this);
086     *                     }
087     *                 }
088     *             }
089     *         }
090     *     });
091     *
092     * }</pre></blockquote>
093     * <p>
094     * <b>Warning:</b> This class directly accesses the source list to acquire information about the changes.
095     * <br> This effectively makes the Change object invalid when another change occurs on the list.
096     * <br> For this reason it is <b>not</b> safe to use this class on a different thread.
097     * <p>
098     * Note: in case the change contains multiple changes of different type, these changes must be in the following order:
099     * <em> permutation change(s), add or remove changes, update changes </em>
100     * This is because permutation changes cannot go after add/remove changes as they would change the position of added elements.
101     * And on the other hand, update changes must go after add/remove changes because they refer with their indexes to the current
102     * state of the list, which means with all add/remove changes applied.
103     * @param <E> the list element type
104     */
105    public abstract static class Change<E> {
106        private final ObservableList<E> list;
107
108        /**
109         * Go to the next change.
110         * The Change in the initial state is invalid a requires a call to next() before
111         * calling other methods. The first next() call will make this object
112         * represent the first change.
113         * @return true if switched to the next change, false if this is the last change.
114         */
115        public abstract boolean next();
116
117        /**
118         * Reset to the initial stage. After this call, the next() must be called
119         * before working with the first change.
120         */
121        public abstract void reset();
122
123        /**
124         * Constructs a new change done to a list.
125         * @param list that was changed
126         */
127        public Change(ObservableList<E> list) {
128            this.list = list;
129        }
130
131        /**
132         * The source list of the change.
133         * @return a list that was changed
134         */
135        public ObservableList<E> getList() {
136            return list;
137        }
138
139        /**
140         * If wasAdded is true, the interval contains all the values that were added.
141         * If wasPermutated is true, the interval marks the values that were permutated.
142         * If wasRemoved is true and wasAdded is false, getFrom() and getTo() should
143         * return the same number - the place where the removed elements were positioned in the list.
144         * @return a beginning (inclusive) of an interval related to the change
145         * @throws IllegalStateException if this Change is in initial state
146         */
147        public abstract int getFrom();
148        /**
149         * The end of the change interval.
150         * @return a end (exclusive) of an interval related to the change.
151         * @throws IllegalStateException if this Change is in initial state
152         * @see #getFrom()
153         */
154        public abstract int getTo();
155        /**
156         * An immutable list of removed/replaced elements. If no elements
157         * were removed from the list, an empty list is returned.
158         * @return a list with all the removed elements
159         * @throws IllegalStateException if this Change is in initial state
160         */
161        public abstract List<E> getRemoved();
162        /**
163         * Indicates if the change was only a permutation.
164         * @return true if the change was just a permutation.
165         * @throws IllegalStateException if this Change is in initial state
166         */
167        public boolean wasPermutated() {
168            return getPermutation().length != 0;
169        }
170
171        /**
172         * Indicates if elements were added during this change
173         * @return true if something was added to the list
174         * @throws IllegalStateException if this Change is in initial state
175         */
176        public boolean wasAdded() {
177            return !wasPermutated() && !wasUpdated() && getFrom() < getTo();
178        }
179
180        /**
181         * Indicates if elements were removed during this change.
182         * Note that using set will also produce a change with wasRemoved() returning
183         * true. See {@link #wasReplaced()}.
184         * @return true if something was removed from the list
185         * @throws IllegalStateException if this Change is in initial state
186         */
187        public boolean wasRemoved() {
188            return !getRemoved().isEmpty();
189        }
190
191        /**
192         * Indicates if elements were replaced during this change.
193         * This is usually true when set is called on the list.
194         * Set operation will act like remove and add operation at the same time.
195         * <p>
196         * Usually, it's not necessary to use this method directly.
197         * Handling remove operation and then add operation, as in the example {@link ListChangeListener$Change above},
198         * will effectively handle also set operation.
199         *
200         * @return same <code> as wasAdded() && wasRemoved() </code>
201         * @throws IllegalStateException if this Change is in initial state
202         */
203        public boolean wasReplaced() {
204            return wasAdded() && wasRemoved();
205        }
206
207        /**
208         * Indicates that the elements between getFrom() (inclusive)
209         * to getTo() exclusive has changed.
210         * This is the only optional event type and may not be
211         * fired by all ObservableLists.
212         * @return true if the current change is an update change.
213         * @since 2.1
214         */
215        public boolean wasUpdated() {
216            return false;
217        }
218
219        /**
220         * To get a subList view of the list that contains only the elements
221         * added, use getAddedSubList() method.
222         * This is actually a shortcut to <code>c.getList().subList(c.getFrom(), c.getTo());</code><br>
223         *
224         * <pre><code>
225         * for (Node n : change.getAddedSubList()) {
226         *       // do something
227         * }
228         * </code></pre>
229         * @return the newly created sublist view that contains all the added elements.
230         * @throws IllegalStateException if this Change is in initial state
231         */
232        public List<E> getAddedSubList() {
233            return wasAdded()? getList().subList(getFrom(), getTo()) : Collections.<E>emptyList();
234        }
235
236        /**
237         * Size of getRemoved() list.
238         * @return the number of removed items
239         * @throws IllegalStateException if this Change is in initial state
240         */
241        public int getRemovedSize() {
242            return getRemoved().size();
243        }
244
245        /**
246         * Size of the interval that was added.
247         * @return the number of added items
248         * @throws IllegalStateException if this Change is in initial state
249         */
250        public int getAddedSize() {
251            return wasAdded() ? getTo() - getFrom() : 0;
252        }
253
254        /**
255         * If this change is an permutation, it returns an integer array
256         * that describes the permutation.
257         * This array maps directly from the previous indexes to the new ones.
258         * This method is not publicly accessible and therefore can return an array safely.
259         * The 0 index of the array corresponds to index {@link #getFrom()} of the list. The same applies
260         * for the last index and {@link #getTo()}.
261         * The method is used by {@link #wasPermutated() } and {@link #getPermutation(int)} methods.
262         * @return empty array if this is not permutation or an integer array containing the permutation
263         * @throws IllegalStateException if this Change is in initial state
264         */
265        protected abstract int[] getPermutation();
266
267        /**
268         * By calling these method, you can observe the permutation that happened.
269         * In order to get the new position of an element, you must call:
270         * <pre>
271         *    change.getPermutation(oldIndex);
272         * </pre>
273         *
274         * Note: default implementation of this method takes the information
275         * from {@link #getPermutation()} method. You don't have to override this method.
276         * @param i the old index that contained the element prior to this change
277         * @throws IndexOutOfBoundsException if i is out of the bounds of the list
278         * @throws IllegalStateException if this is not a permutation change
279         * @return the new index of the same element
280         */
281        public int getPermutation(int i) {
282            if (!wasPermutated()) {
283                throw new IllegalStateException("Not a permutation change");
284            }
285            return getPermutation()[i - getFrom()];
286        }
287
288    }
289    /**
290     * Called after a change has been made to an ObservableList.
291     *
292     * @param c an object representing the change that was done
293     * @see Change
294     */
295    public void onChanged(Change<? extends E> c);
296}