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