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 javafx.beans.InvalidationListener;
029import javafx.beans.value.ChangeListener;
030
031import com.sun.javafx.binding.ExpressionHelper;
032
033/**
034 * This class provides a convenient class to define read-only properties. It
035 * creates two properties that are synchronized. One property is read-only
036 * and can be passed to external users. The other property is read- and
037 * writable and should be used internally only.
038 * 
039 */
040public class ReadOnlyDoubleWrapper extends SimpleDoubleProperty {
041
042    private ReadOnlyPropertyImpl readOnlyProperty;
043
044    /**
045     * The constructor of {@code ReadOnlyDoubleWrapper}
046     */
047    public ReadOnlyDoubleWrapper() {
048    }
049
050    /**
051     * The constructor of {@code ReadOnlyDoubleWrapper}
052     * 
053     * @param initialValue
054     *            the initial value of the wrapped value
055     */
056    public ReadOnlyDoubleWrapper(double initialValue) {
057        super(initialValue);
058    }
059
060    /**
061     * The constructor of {@code ReadOnlyDoubleWrapper}
062     * 
063     * @param bean
064     *            the bean of this {@code ReadOnlyDoubleProperty}
065     * @param name
066     *            the name of this {@code ReadOnlyDoubleProperty}
067     */
068    public ReadOnlyDoubleWrapper(Object bean, String name) {
069        super(bean, name);
070    }
071
072    /**
073     * The constructor of {@code ReadOnlyDoubleWrapper}
074     * 
075     * @param bean
076     *            the bean of this {@code ReadOnlyDoubleProperty}
077     * @param name
078     *            the name of this {@code ReadOnlyDoubleProperty}
079     * @param initialValue
080     *            the initial value of the wrapped value
081     */
082    public ReadOnlyDoubleWrapper(Object bean, String name,
083            double initialValue) {
084        super(bean, name, initialValue);
085    }
086
087    /**
088     * Returns the read-only property, that is synchronized with this
089     * {@code ReadOnlyDoubleWrapper}.
090     * 
091     * @return the read-only property
092     */
093    public ReadOnlyDoubleProperty getReadOnlyProperty() {
094        if (readOnlyProperty == null) {
095            readOnlyProperty = new ReadOnlyPropertyImpl();
096        }
097        return readOnlyProperty;
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public void addListener(InvalidationListener listener) {
105        getReadOnlyProperty().addListener(listener);
106    }
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public void removeListener(InvalidationListener listener) {
113        if (readOnlyProperty != null) {
114            readOnlyProperty.removeListener(listener);
115        }
116    }
117
118    /**
119     * {@inheritDoc}
120     */
121    @Override
122    public void addListener(ChangeListener<? super Number> listener) {
123        getReadOnlyProperty().addListener(listener);
124    }
125
126    /**
127     * {@inheritDoc}
128     */
129    @Override
130    public void removeListener(ChangeListener<? super Number> listener) {
131        if (readOnlyProperty != null) {
132            readOnlyProperty.removeListener(listener);
133        }
134    }
135
136    /**
137     * {@inheritDoc}
138     */
139    @Override
140    protected void fireValueChangedEvent() {
141        if (readOnlyProperty != null) {
142            readOnlyProperty.fireValueChangedEvent();
143        }
144    }
145
146    private class ReadOnlyPropertyImpl extends ReadOnlyDoubleProperty {
147        
148        private ExpressionHelper<Number> helper = null;
149        
150        @Override
151        public double get() {
152            return ReadOnlyDoubleWrapper.this.get();
153        }
154
155        @Override 
156        public void addListener(InvalidationListener listener) {
157            helper = ExpressionHelper.addListener(helper, this, listener);
158        }
159
160        @Override 
161        public void removeListener(InvalidationListener listener) {
162            helper = ExpressionHelper.removeListener(helper, listener);
163        }
164        
165        @Override
166        public void addListener(ChangeListener<? super Number> listener) {
167            helper = ExpressionHelper.addListener(helper, this, listener);
168        }
169
170        @Override 
171        public void removeListener(ChangeListener<? super Number> listener) {
172            helper = ExpressionHelper.removeListener(helper, listener);
173        }
174        
175        protected void fireValueChangedEvent() {
176            ExpressionHelper.fireValueChangedEvent(helper);
177        }
178        
179        @Override
180        public Object getBean() {
181            return ReadOnlyDoubleWrapper.this.getBean();
182        }
183
184        @Override
185        public String getName() {
186            return ReadOnlyDoubleWrapper.this.getName();
187        }
188    };
189}