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