Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 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.beans.property;
027
028import java.util.List;
029import java.util.ListIterator;
030import javafx.beans.binding.Bindings;
031import javafx.beans.binding.ListExpression;
032import javafx.collections.ObservableList;
033
034/**
035 * Super class for all readonly properties wrapping an {@link javafx.collections.ObservableList}.
036 *
037 * @see javafx.collections.ObservableList
038 * @see javafx.beans.value.ObservableListValue
039 * @see javafx.beans.binding.ListExpression
040 * @see ReadOnlyProperty
041 *
042 * @param <E> the type of the {@code List} elements
043 */
044public abstract class ReadOnlyListProperty<E> extends ListExpression<E>
045        implements ReadOnlyProperty<ObservableList<E>>  {
046
047    /**
048     * The constructor of {@code ReadOnlyListProperty}.
049     */
050    public ReadOnlyListProperty() {
051    }
052
053    /**
054     * Creates a bidirectional content binding of the {@link javafx.collections.ObservableList}, that is
055     * wrapped in this {@code ReadOnlyListProperty}, and another {@code ObservableList}.
056     * <p>
057     * A bidirectional content binding ensures that the content of two {@code ObservableLists} is the
058     * same. If the content of one of the lists changes, the other one will be updated automatically.
059     *
060     * @param list the {@code ObservableList} this property should be bound to
061     * @throws NullPointerException if {@code list} is {@code null}
062     * @throws IllegalArgumentException if {@code list} is the same list that this {@code ReadOnlyListProperty} points to
063     */
064    public void bindContentBidirectional(ObservableList<E> list) {
065        Bindings.bindContentBidirectional(this, list);
066    }
067
068    /**
069     * Deletes a bidirectional content binding between the {@link javafx.collections.ObservableList}, that is
070     * wrapped in this {@code ReadOnlyListProperty}, and another {@code Object}.
071     *
072     * @param object the {@code Object} to which the bidirectional binding should be removed
073     * @throws NullPointerException if {@code object} is {@code null}
074     * @throws IllegalArgumentException if {@code object} is the same list that this {@code ReadOnlyListProperty} points to
075     */
076    public void unbindContentBidirectional(Object object) {
077        Bindings.unbindContentBidirectional(this, object);
078    }
079
080    /**
081     * Creates a content binding between the {@link javafx.collections.ObservableList}, that is
082     * wrapped in this {@code ReadOnlyListProperty}, and another {@code ObservableList}.
083     * <p>
084     * A content binding ensures that the content of the wrapped {@code ObservableLists} is the
085     * same as that of the other list. If the content of the other list changes, the wrapped list will be updated
086     * automatically. Once the wrapped list is bound to another list, you must not change it directly.
087     *
088     * @param list the {@code ObservableList} this property should be bound to
089     * @throws NullPointerException if {@code list} is {@code null}
090     * @throws IllegalArgumentException if {@code list} is the same list that this {@code ReadOnlyListProperty} points to
091     */
092    public void bindContent(ObservableList<E> list) {
093        Bindings.bindContent(this, list);
094    }
095
096    /**
097     * Deletes a content binding between the {@link javafx.collections.ObservableList}, that is
098     * wrapped in this {@code ReadOnlyListProperty}, and another {@code Object}.
099     *
100     * @param object the {@code Object} to which the binding should be removed
101     * @throws NullPointerException if {@code object} is {@code null}
102     * @throws IllegalArgumentException if {@code object} is the same list that this {@code ReadOnlyListProperty} points to
103     */
104    public void unbindContent(Object object) {
105        Bindings.unbindContent(this, object);
106    }
107
108    @Override
109    public boolean equals(Object obj) {
110        if (this == obj) {
111            return true;
112        }
113        if (!(obj instanceof List)) {
114            return false;
115        }
116        final List list = (List)obj;
117        
118        if (size() != list.size()) {
119            return false;
120        }
121
122        ListIterator<E> e1 = listIterator();
123        ListIterator e2 = list.listIterator();
124        while (e1.hasNext() && e2.hasNext()) {
125            E o1 = e1.next();
126            Object o2 = e2.next();
127            if (!(o1==null ? o2==null : o1.equals(o2)))
128                return false;
129        }
130        return true;
131    }
132    
133    @Override
134    public int hashCode() {
135        int hashCode = 1;
136        for (E e : this)
137            hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
138        return hashCode;
139    }
140
141    /**
142     * Returns a string representation of this {@code ReadOnlyListProperty} object.
143     * @return a string representation of this {@code ReadOnlyListProperty} object.
144     */
145    @Override
146    public String toString() {
147        final Object bean = getBean();
148        final String name = getName();
149        final StringBuilder result = new StringBuilder(
150                "ReadOnlyListProperty [");
151        if (bean != null) {
152            result.append("bean: ").append(bean).append(", ");
153        }
154        if ((name != null) && !name.equals("")) {
155            result.append("name: ").append(name).append(", ");
156        }
157        result.append("value: ").append(get()).append("]");
158        return result.toString();
159    }
160
161}