Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2010, 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.binding; 027 028import javafx.beans.value.ObservableBooleanValue; 029import javafx.collections.FXCollections; 030import javafx.collections.ObservableList; 031 032import com.sun.javafx.binding.StringFormatter; 033import com.sun.javafx.collections.annotations.ReturnsUnmodifiableCollection; 034import javafx.beans.value.ObservableValue; 035 036/** 037 * A {@code BooleanExpression} is a 038 * {@link javafx.beans.value.ObservableBooleanValue} plus additional convenience 039 * methods to generate bindings in a fluent style. 040 * <p> 041 * A concrete sub-class of {@code BooleanExpression} has to implement the method 042 * {@link javafx.beans.value.ObservableBooleanValue#get()}, which provides the 043 * actual value of this expression. 044 */ 045public abstract class BooleanExpression implements ObservableBooleanValue { 046 047 @Override 048 public Boolean getValue() { 049 return get(); 050 } 051 052 /** 053 * Returns a {@code BooleanExpression} that wraps a 054 * {@link javafx.beans.value.ObservableBooleanValue}. If the 055 * {@code ObservableBooleanValue} is already a {@code BooleanExpression}, it 056 * will be returned. Otherwise a new 057 * {@link javafx.beans.binding.BooleanBinding} is created that is bound to 058 * the {@code ObservableBooleanValue}. 059 * 060 * @param value 061 * The source {@code ObservableBooleanValue} 062 * @return A {@code BooleanExpression} that wraps the 063 * {@code ObservableBooleanValue} if necessary 064 * @throws NullPointerException 065 * if {@code value} is {@code null} 066 */ 067 public static BooleanExpression booleanExpression( 068 final ObservableBooleanValue value) { 069 if (value == null) { 070 throw new NullPointerException("Value must be specified."); 071 } 072 return (value instanceof BooleanExpression) ? (BooleanExpression) value 073 : new BooleanBinding() { 074 { 075 super.bind(value); 076 } 077 078 @Override 079 public void dispose() { 080 super.unbind(value); 081 } 082 083 @Override 084 protected boolean computeValue() { 085 return value.get(); 086 } 087 088 @Override 089 @ReturnsUnmodifiableCollection 090 public ObservableList<ObservableBooleanValue> getDependencies() { 091 return FXCollections.singletonObservableList(value); 092 } 093 }; 094 } 095 096 /** 097 * Returns a {@code BooleanExpression} that wraps an 098 * {@link javafx.beans.value.ObservableValue}. If the 099 * {@code ObservableValue} is already a {@code BooleanExpression}, it 100 * will be returned. Otherwise a new 101 * {@link javafx.beans.binding.BooleanBinding} is created that is bound to 102 * the {@code ObservableValue}. 103 * 104 * Note: null values will be interpreted as "false". 105 * 106 * @param value 107 * The source {@code ObservableValue} 108 * @return A {@code BooleanExpression} that wraps the 109 * {@code ObservableValue} if necessary 110 * @throws NullPointerException 111 * if {@code value} is {@code null} 112 */ 113 public static BooleanExpression booleanExpression(final ObservableValue<Boolean> value) { 114 if (value == null) { 115 throw new NullPointerException("Value must be specified."); 116 } 117 return (value instanceof BooleanExpression) ? (BooleanExpression) value 118 : new BooleanBinding() { 119 { 120 super.bind(value); 121 } 122 123 @Override 124 public void dispose() { 125 super.unbind(value); 126 } 127 128 @Override 129 protected boolean computeValue() { 130 final Boolean val = value.getValue(); 131 return val == null ? false : val; 132 } 133 134 @Override 135 @ReturnsUnmodifiableCollection 136 public ObservableList<ObservableValue<Boolean>> getDependencies() { 137 return FXCollections.singletonObservableList(value); 138 } 139 }; 140 } 141 142 /** 143 * Creates a new {@code BooleanExpression} that performs the conditional 144 * AND-operation on this {@code BooleanExpression} and a 145 * {@link ObservableBooleanValue}. 146 * 147 * @param other 148 * the other {@code ObservableBooleanValue} 149 * @return the new {@code BooleanExpression} 150 * @throws NullPointerException 151 * if {@code other} is {@code null} 152 */ 153 public BooleanBinding and(final ObservableBooleanValue other) { 154 return Bindings.and(this, other); 155 } 156 157 /** 158 * Creates a new {@code BooleanExpression} that performs the conditional 159 * OR-operation on this {@code BooleanExpression} and a 160 * {@link ObservableBooleanValue}. 161 * 162 * @param other 163 * the other {@code ObservableBooleanValue} 164 * @return the new {@code BooleanExpression} 165 * @throws NullPointerException 166 * if {@code other} is {@code null} 167 */ 168 public BooleanBinding or(final ObservableBooleanValue other) { 169 return Bindings.or(this, other); 170 } 171 172 /** 173 * Creates a new {@code BooleanExpression} that calculates the negation of 174 * this {@code BooleanExpression}. 175 * 176 * @return the new {@code BooleanExpression} 177 */ 178 public BooleanBinding not() { 179 return Bindings.not(this); 180 } 181 182 /** 183 * Creates a new {@code BooleanExpression} that holds {@code true} if this and 184 * another {@link javafx.beans.value.ObservableBooleanValue} are equal. 185 * 186 * @param other 187 * the other {@code ObservableBooleanValue} 188 * @return the new {@code BooleanExpression} 189 * @throws NullPointerException 190 * if {@code other} is {@code null} 191 */ 192 public BooleanBinding isEqualTo(final ObservableBooleanValue other) { 193 return Bindings.equal(this, other); 194 } 195 196 /** 197 * Creates a new {@code BooleanExpression} that holds {@code true} if this and 198 * another {@link javafx.beans.value.ObservableBooleanValue} are equal. 199 * 200 * @param other 201 * the other {@code ObservableBooleanValue} 202 * @return the new {@code BooleanExpression} 203 * @throws NullPointerException 204 * if {@code other} is {@code null} 205 */ 206 public BooleanBinding isNotEqualTo(final ObservableBooleanValue other) { 207 return Bindings.notEqual(this, other); 208 } 209 210 /** 211 * Creates a {@link javafx.beans.binding.StringBinding} that holds the value 212 * of this {@code BooleanExpression} turned into a {@code String}. If the 213 * value of this {@code BooleanExpression} changes, the value of the 214 * {@code StringBinding} will be updated automatically. 215 * 216 * @return the new {@code StringBinding} 217 */ 218 public StringBinding asString() { 219 return (StringBinding) StringFormatter.convert(this); 220 } 221 222 /** 223 * Creates an {@link javafx.beans.binding.ObjectExpression} that holds the value 224 * of this {@code BooleanExpression}. If the 225 * value of this {@code BooleanExpression} changes, the value of the 226 * {@code ObjectExpression} will be updated automatically. 227 * 228 * @return the new {@code ObjectExpression} 229 */ 230 public ObjectExpression<Boolean> asObject() { 231 return new ObjectBinding<Boolean>() { 232 { 233 bind(BooleanExpression.this); 234 } 235 236 @Override 237 public void dispose() { 238 unbind(BooleanExpression.this); 239 } 240 241 @Override 242 protected Boolean computeValue() { 243 return BooleanExpression.this.getValue(); 244 } 245 }; 246 } 247}