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.Observable;
030import javafx.beans.WeakInvalidationListener;
031import javafx.beans.binding.DoubleExpression;
032
033/**
034 * Super class for all readonly properties wrapping a {@code double}.
035 * 
036 * @see javafx.beans.value.ObservableDoubleValue
037 * @see javafx.beans.binding.DoubleExpression
038 * @see ReadOnlyProperty
039 * 
040 */
041public abstract class ReadOnlyDoubleProperty extends DoubleExpression implements
042        ReadOnlyProperty<Number> {
043
044    /**
045     * The constructor of {@code ReadOnlyDoubleProperty}.
046     */
047    public ReadOnlyDoubleProperty() {
048    }
049
050    /**
051     * Returns a string representation of this {@code ReadOnlyDoubleProperty} object.
052     * @return a string representation of this {@code ReadOnlyDoubleProperty} object.
053     */ 
054    @Override
055    public String toString() {
056        final Object bean = getBean();
057        final String name = getName();
058        final StringBuilder result = new StringBuilder(
059                "ReadOnlyDoubleProperty [");
060        if (bean != null) {
061            result.append("bean: ").append(bean).append(", ");
062        }
063        if ((name != null) && !name.equals("")) {
064            result.append("name: ").append(name).append(", ");
065        }
066        result.append("value: ").append(get()).append("]");
067        return result.toString();
068    }
069    
070    /**
071     * Returns a {@code ReadOnlyDoubleProperty} that wraps a
072     * {@link javafx.beans.property.ReadOnlyProperty}. If the
073     * {@code ReadOnlyProperty} is already a {@code ReadOnlyDoubleProperty}, it
074     * will be returned. Otherwise a new
075     * {@code ReadOnlyDoubleProperty} is created that is bound to
076     * the {@code ReadOnlyProperty}.
077     * 
078     * Note: null values will be interpreted as 0.0
079     * 
080     * @param property
081     *            The source {@code ReadOnlyProperty}
082     * @return A {@code ReadOnlyDoubleProperty} that wraps the
083     *         {@code ReadOnlyProperty} if necessary
084     * @throws NullPointerException
085     *             if {@code value} is {@code null}
086     */
087    public static <T extends Number> ReadOnlyDoubleProperty readOnlyDoubleProperty(final ReadOnlyProperty<T> property) {
088        if (property == null) {
089            throw new NullPointerException("Property cannot be null");
090        }
091        
092        return property instanceof ReadOnlyDoubleProperty ? (ReadOnlyDoubleProperty) property:
093           new ReadOnlyDoublePropertyBase() {
094            private boolean valid = true;
095            private final InvalidationListener listener = new InvalidationListener() {
096                @Override
097                public void invalidated(Observable observable) {
098                    if (valid) {
099                        valid = false;
100                        fireValueChangedEvent();
101                    }
102                }
103            };
104
105            {
106                property.addListener(new WeakInvalidationListener(listener));
107            }
108                    
109            @Override
110            public double get() {
111                valid = true;
112                final T value = property.getValue();
113                return value == null ? 0.0 : value.doubleValue();
114            }
115
116            @Override
117            public Object getBean() {
118                return null; // Virtual property, no bean
119            }
120
121            @Override
122            public String getName() {
123                return property.getName();
124            }
125        };
126    }
127
128    /**
129     * Creates a {@link javafx.beans.property.ReadOnlyObjectProperty} that holds the value
130     * of this {@code ReadOnlyDoubleProperty}. If the
131     * value of this {@code ReadOnlyDoubleProperty} changes, the value of the
132     * {@code ReadOnlyObjectProperty} will be updated automatically.
133     * 
134     * @return the new {@code ReadOnlyObjectProperty}
135     */
136    @Override
137    public ReadOnlyObjectProperty<Double> asObject() {
138        return new ReadOnlyObjectPropertyBase<Double>() {
139
140            private boolean valid = true;
141            private final InvalidationListener listener = new InvalidationListener() {
142                @Override
143                public void invalidated(Observable observable) {
144                    if (valid) {
145                        valid = false;
146                        fireValueChangedEvent();
147                    }
148                }
149            };
150
151            {
152                ReadOnlyDoubleProperty.this.addListener(new WeakInvalidationListener(listener));
153            }
154            
155            @Override
156            public Object getBean() {
157                return null; // Virtual property, does not exist on a bean
158            }
159
160            @Override
161            public String getName() {
162                return ReadOnlyDoubleProperty.this.getName();
163            }
164
165            @Override
166            public Double get() {
167                valid = true;
168                return ReadOnlyDoubleProperty.this.getValue();
169            }
170        };
171    };
172    
173    
174
175}