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