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 com.sun.javafx.binding.SetExpressionHelper;
029import javafx.beans.InvalidationListener;
030import javafx.beans.value.ChangeListener;
031import javafx.collections.ObservableSet;
032import javafx.collections.SetChangeListener;
033
034import static javafx.collections.SetChangeListener.Change;
035
036/**
037 * This class provides a convenient class to define read-only properties. It
038 * creates two properties that are synchronized. One property is read-only
039 * and can be passed to external users. The other property is read- and
040 * writable and should be used internally only.
041 *
042 */
043public class ReadOnlySetWrapper<E> extends SimpleSetProperty<E> {
044
045    private ReadOnlyPropertyImpl readOnlyProperty;
046
047    /**
048     * The constructor of {@code ReadOnlySetWrapper}
049     */
050    public ReadOnlySetWrapper() {
051    }
052
053    /**
054     * The constructor of {@code ReadOnlySetWrapper}
055     *
056     * @param initialValue
057     *            the initial value of the wrapped value
058     */
059    public ReadOnlySetWrapper(ObservableSet<E> initialValue) {
060        super(initialValue);
061    }
062
063    /**
064     * The constructor of {@code ReadOnlySetWrapper}
065     *
066     * @param bean
067     *            the bean of this {@code ReadOnlySetWrapper}
068     * @param name
069     *            the name of this {@code ReadOnlySetWrapper}
070     */
071    public ReadOnlySetWrapper(Object bean, String name) {
072        super(bean, name);
073    }
074
075    /**
076     * The constructor of {@code ReadOnlySetWrapper}
077     *
078     * @param bean
079     *            the bean of this {@code ReadOnlySetWrapper}
080     * @param name
081     *            the name of this {@code ReadOnlySetWrapper}
082     * @param initialValue
083     *            the initial value of the wrapped value
084     */
085    public ReadOnlySetWrapper(Object bean, String name,
086                              ObservableSet<E> initialValue) {
087        super(bean, name, initialValue);
088    }
089
090    /**
091     * Returns the readonly property, that is synchronized with this
092     * {@code ReadOnlySetWrapper}.
093     *
094     * @return the readonly property
095     */
096    public ReadOnlySetProperty<E> getReadOnlyProperty() {
097        if (readOnlyProperty == null) {
098            readOnlyProperty = new ReadOnlyPropertyImpl();
099        }
100        return readOnlyProperty;
101    }
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    public void addListener(InvalidationListener listener) {
108        getReadOnlyProperty().addListener(listener);
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    public void removeListener(InvalidationListener listener) {
116        if (readOnlyProperty != null) {
117            readOnlyProperty.removeListener(listener);
118        }
119    }
120
121    /**
122     * {@inheritDoc}
123     */
124    @Override
125    public void addListener(ChangeListener<? super ObservableSet<E>> listener) {
126        getReadOnlyProperty().addListener(listener);
127    }
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public void removeListener(ChangeListener<? super ObservableSet<E>> listener) {
134        if (readOnlyProperty != null) {
135            readOnlyProperty.removeListener(listener);
136        }
137    }
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public void addListener(SetChangeListener<? super E> listener) {
144        getReadOnlyProperty().addListener(listener);
145    }
146
147    /**
148     * {@inheritDoc}
149     */
150    @Override
151    public void removeListener(SetChangeListener<? super E> listener) {
152        if (readOnlyProperty != null) {
153            readOnlyProperty.removeListener(listener);
154        }
155    }
156
157    /**
158     * {@inheritDoc}
159     */
160    @Override
161    protected void fireValueChangedEvent() {
162        if (readOnlyProperty != null) {
163            readOnlyProperty.fireValueChangedEvent();
164        }
165    }
166
167    /**
168     * {@inheritDoc}
169     */
170    @Override
171    protected void fireValueChangedEvent(Change<? extends E> change) {
172        if (readOnlyProperty != null) {
173            readOnlyProperty.fireValueChangedEvent(change);
174        }
175    }
176
177    private class ReadOnlyPropertyImpl extends ReadOnlySetProperty<E> {
178
179        private SetExpressionHelper<E> helper = null;
180
181        @Override
182        public ObservableSet<E> get() {
183            return ReadOnlySetWrapper.this.get();
184        }
185
186        @Override
187        public void addListener(InvalidationListener listener) {
188            helper = SetExpressionHelper.addListener(helper, this, listener);
189        }
190
191        @Override
192        public void removeListener(InvalidationListener listener) {
193            helper = SetExpressionHelper.removeListener(helper, listener);
194        }
195
196        @Override
197        public void addListener(ChangeListener<? super ObservableSet<E>> listener) {
198            helper = SetExpressionHelper.addListener(helper, this, listener);
199        }
200
201        @Override
202        public void removeListener(ChangeListener<? super ObservableSet<E>> listener) {
203            helper = SetExpressionHelper.removeListener(helper, listener);
204        }
205
206        @Override
207        public void addListener(SetChangeListener<? super E> listener) {
208            helper = SetExpressionHelper.addListener(helper, this, listener);
209        }
210
211        @Override
212        public void removeListener(SetChangeListener<? super E> listener) {
213            helper = SetExpressionHelper.removeListener(helper, listener);
214        }
215
216        private void fireValueChangedEvent() {
217            SetExpressionHelper.fireValueChangedEvent(helper);
218        }
219
220        private void fireValueChangedEvent(Change<? extends E> change) {
221            SetExpressionHelper.fireValueChangedEvent(helper, change);
222        }
223
224        @Override
225        public Object getBean() {
226            return ReadOnlySetWrapper.this.getBean();
227        }
228
229        @Override
230        public String getName() {
231            return ReadOnlySetWrapper.this.getName();
232        }
233
234        @Override
235        public ReadOnlyIntegerProperty sizeProperty() {
236            return ReadOnlySetWrapper.this.sizeProperty();
237        }
238
239        @Override
240        public ReadOnlyBooleanProperty emptyProperty() {
241            return ReadOnlySetWrapper.this.emptyProperty();
242        }
243    }
244}