1 /* 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.beans.property; 27 28 import javafx.beans.binding.Bindings; 29 import javafx.beans.value.ObservableValue; 30 import javafx.beans.value.WritableStringValue; 31 import javafx.util.StringConverter; 32 33 import java.text.Format; 34 35 /** 36 * This class provides a full implementation of a {@link Property} wrapping a 37 * {@code String} value. 38 * 39 * The value of a {@code StringProperty} can be get and set with {@link #get()}, 40 * {@link #getValue()}, {@link #set(Object)}, and {@link #setValue(String)}. 41 * 42 * A property can be bound and unbound unidirectional with 43 * {@link #bind(ObservableValue)} and {@link #unbind()}. Bidirectional bindings 44 * can be created and removed with {@link #bindBidirectional(Property)} and 45 * {@link #unbindBidirectional(Property)}. 46 * 47 * The context of a {@code StringProperty} can be read with {@link #getBean()} 48 * and {@link #getName()}. 49 * 50 * @see javafx.beans.value.ObservableStringValue 51 * @see javafx.beans.value.WritableStringValue 52 * @see ReadOnlyStringProperty 53 * @see Property 54 * 55 * @since JavaFX 2.0 56 */ 57 public abstract class StringProperty extends ReadOnlyStringProperty implements 58 Property<String>, WritableStringValue { 59 60 /** 61 * {@inheritDoc} 62 */ 63 @Override 64 public void setValue(String v) { 65 set(v); 66 } 67 68 /** 69 * {@inheritDoc} 70 */ 71 @Override 72 public void bindBidirectional(Property<String> other) { 73 Bindings.bindBidirectional(this, other); 74 } 75 76 /** 77 * Create a bidirectional binding between this {@code StringProperty} and another 78 * arbitrary property. Relies on an implementation of {@code Format} for conversion. 79 * 80 * @param other 81 * the other {@code Property} 82 * @param format 83 * the {@code Format} used to convert between this {@code StringProperty} 84 * and the other {@code Property} 85 * @throws NullPointerException 86 * if {@code other} or {@code format} is {@code null} 87 * @throws IllegalArgumentException 88 * if {@code other} is {@code this} 89 * @since JavaFX 2.1 90 */ 91 public void bindBidirectional(Property<?> other, Format format) { 92 Bindings.bindBidirectional(this, other, format); 93 } 94 95 /** 96 * Create a bidirectional binding between this {@code StringProperty} and another 97 * arbitrary property. Relies on an implementation of {@link StringConverter} for conversion. 98 * 99 * @param other 100 * the other {@code Property} 101 * @param converter 102 * the {@code StringConverter} used to convert between this {@code StringProperty} 103 * and the other {@code Property} 104 * @throws NullPointerException 105 * if {@code other} or {@code converter} is {@code null} 106 * @throws IllegalArgumentException 107 * if {@code other} is {@code this} 108 * @since JavaFX 2.1 109 */ 110 public <T> void bindBidirectional(Property<T> other, StringConverter<T> converter) { 111 Bindings.bindBidirectional(this, other, converter); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public void unbindBidirectional(Property<String> other) { 119 Bindings.unbindBidirectional(this, other); 120 } 121 122 /** 123 * Remove a bidirectional binding between this {@code Property} and another 124 * one. 125 * 126 * If no bidirectional binding between the properties exists, calling this 127 * method has no effect. 128 * 129 * @param other 130 * the other {@code Property} 131 * @throws NullPointerException 132 * if {@code other} is {@code null} 133 * @throws IllegalArgumentException 134 * if {@code other} is {@code this} 135 * @since JavaFX 2.1 136 */ 137 public void unbindBidirectional(Object other) { 138 Bindings.unbindBidirectional(this, other); 139 } 140 141 /** 142 * Returns a string representation of this {@code StringProperty} object. 143 * @return a string representation of this {@code StringProperty} object. 144 */ 145 @Override 146 public String toString() { 147 final Object bean = getBean(); 148 final String name = getName(); 149 final StringBuilder result = new StringBuilder( 150 "StringProperty ["); 151 if (bean != null) { 152 result.append("bean: ").append(bean).append(", "); 153 } 154 if ((name != null) && (!name.equals(""))) { 155 result.append("name: ").append(name).append(", "); 156 } 157 result.append("value: ").append(get()).append("]"); 158 return result.toString(); 159 } 160 }