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.InvalidationListener; 29 import javafx.beans.value.ChangeListener; 30 31 /** 32 * This class provides a convenient class to define read-only properties. It 33 * creates two properties that are synchronized. One property is read-only 34 * and can be passed to external users. The other property is read- and 35 * writable and should be used internally only. 36 * 37 * @since JavaFX 2.0 38 */ 39 public class ReadOnlyStringWrapper extends SimpleStringProperty { 40 41 private ReadOnlyPropertyImpl readOnlyProperty; 42 43 /** 44 * The constructor of {@code ReadOnlyStringWrapper} 45 */ 46 public ReadOnlyStringWrapper() { 47 } 48 49 /** 50 * The constructor of {@code ReadOnlyStringWrapper} 51 * 52 * @param initialValue 53 * the initial value of the wrapped value 54 */ 55 public ReadOnlyStringWrapper(String initialValue) { 56 super(initialValue); 57 } 58 59 /** 60 * The constructor of {@code ReadOnlyStringWrapper} 61 * 62 * @param bean 63 * the bean of this {@code ReadOnlyStringProperty} 64 * @param name 65 * the name of this {@code ReadOnlyStringProperty} 66 */ 67 public ReadOnlyStringWrapper(Object bean, String name) { 68 super(bean, name); 69 } 70 71 /** 72 * The constructor of {@code ReadOnlyStringWrapper} 73 * 74 * @param bean 75 * the bean of this {@code ReadOnlyStringProperty} 76 * @param name 77 * the name of this {@code ReadOnlyStringProperty} 78 * @param initialValue 79 * the initial value of the wrapped value 80 */ 81 public ReadOnlyStringWrapper(Object bean, String name, 82 String initialValue) { 83 super(bean, name, initialValue); 84 } 85 86 /** 87 * Returns the readonly property, that is synchronized with this 88 * {@code ReadOnlyStringWrapper}. 89 * 90 * @return the readonly property 91 */ 92 public ReadOnlyStringProperty getReadOnlyProperty() { 93 if (readOnlyProperty == null) { 94 readOnlyProperty = new ReadOnlyPropertyImpl(); 95 } 96 return readOnlyProperty; 97 } 98 99 /** 100 * {@inheritDoc} 101 */ 102 @Override 103 public void addListener(InvalidationListener listener) { 104 getReadOnlyProperty().addListener(listener); 105 get(); 106 } 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override 112 public void removeListener(InvalidationListener listener) { 113 if (readOnlyProperty != null) { 114 readOnlyProperty.removeListener(listener); 115 } 116 } 117 118 /** 119 * {@inheritDoc} 120 */ 121 @Override 122 public void addListener(ChangeListener<? super String> listener) { 123 getReadOnlyProperty().addListener(listener); 124 } 125 126 /** 127 * {@inheritDoc} 128 */ 129 @Override 130 public void removeListener(ChangeListener<? super String> listener) { 131 if (readOnlyProperty != null) { 132 readOnlyProperty.removeListener(listener); 133 } 134 } 135 136 /** 137 * {@inheritDoc} 138 */ 139 @Override 140 protected void fireValueChangedEvent() { 141 if (readOnlyProperty != null) { 142 readOnlyProperty.fireValueChangedEvent(); 143 } 144 } 145 146 private class ReadOnlyPropertyImpl extends ReadOnlyStringPropertyBase { 147 148 @Override 149 public String get() { 150 return ReadOnlyStringWrapper.this.get(); 151 } 152 153 @Override 154 public Object getBean() { 155 return ReadOnlyStringWrapper.this.getBean(); 156 } 157 158 @Override 159 public String getName() { 160 return ReadOnlyStringWrapper.this.getName(); 161 } 162 }; 163 }