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 import javafx.collections.ListChangeListener; 31 import javafx.collections.ObservableList; 32 33 import static javafx.collections.ListChangeListener.Change; 34 35 /** 36 * This class provides a convenient class to define read-only properties. It 37 * creates two properties that are synchronized. One property is read-only 38 * and can be passed to external users. The other property is read- and 39 * writable and should be used internally only. 40 * 41 * @since JavaFX 2.1 42 */ 43 public class ReadOnlyListWrapper<E> extends SimpleListProperty<E> { 44 45 private ReadOnlyPropertyImpl readOnlyProperty; 46 47 /** 48 * The constructor of {@code ReadOnlyListWrapper} 49 */ 50 public ReadOnlyListWrapper() { 51 } 52 53 /** 54 * The constructor of {@code ReadOnlyListWrapper} 55 * 56 * @param initialValue 57 * the initial value of the wrapped value 58 */ 59 public ReadOnlyListWrapper(ObservableList<E> initialValue) { 60 super(initialValue); 61 } 62 63 /** 64 * The constructor of {@code ReadOnlyListWrapper} 65 * 66 * @param bean 67 * the bean of this {@code ReadOnlyListWrapper} 68 * @param name 69 * the name of this {@code ReadOnlyListWrapper} 70 */ 71 public ReadOnlyListWrapper(Object bean, String name) { 72 super(bean, name); 73 } 74 75 /** 76 * The constructor of {@code ReadOnlyListWrapper} 77 * 78 * @param bean 79 * the bean of this {@code ReadOnlyListWrapper} 80 * @param name 81 * the name of this {@code ReadOnlyListWrapper} 82 * @param initialValue 83 * the initial value of the wrapped value 84 */ 85 public ReadOnlyListWrapper(Object bean, String name, 86 ObservableList<E> initialValue) { 87 super(bean, name, initialValue); 88 } 89 90 /** 91 * Returns the readonly property, that is synchronized with this 92 * {@code ReadOnlyListWrapper}. 93 * 94 * @return the readonly property 95 */ 96 public ReadOnlyListProperty<E> getReadOnlyProperty() { 97 if (readOnlyProperty == null) { 98 readOnlyProperty = new ReadOnlyPropertyImpl(); 99 } 100 return readOnlyProperty; 101 } 102 103 /** 104 * {@inheritDoc} 105 */ 106 @Override 107 public void addListener(InvalidationListener listener) { 108 getReadOnlyProperty().addListener(listener); 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 @Override 115 public void removeListener(InvalidationListener listener) { 116 if (readOnlyProperty != null) { 117 readOnlyProperty.removeListener(listener); 118 } 119 } 120 121 /** 122 * {@inheritDoc} 123 */ 124 @Override 125 public void addListener(ChangeListener<? super ObservableList<E>> listener) { 126 getReadOnlyProperty().addListener(listener); 127 } 128 129 /** 130 * {@inheritDoc} 131 */ 132 @Override 133 public void removeListener(ChangeListener<? super ObservableList<E>> listener) { 134 if (readOnlyProperty != null) { 135 readOnlyProperty.removeListener(listener); 136 } 137 } 138 139 /** 140 * {@inheritDoc} 141 */ 142 @Override 143 public void addListener(ListChangeListener<? super E> listener) { 144 getReadOnlyProperty().addListener(listener); 145 } 146 147 /** 148 * {@inheritDoc} 149 */ 150 @Override 151 public void removeListener(ListChangeListener<? super E> listener) { 152 if (readOnlyProperty != null) { 153 readOnlyProperty.removeListener(listener); 154 } 155 } 156 157 /** 158 * {@inheritDoc} 159 */ 160 @Override 161 protected void fireValueChangedEvent() { 162 if (readOnlyProperty != null) { 163 readOnlyProperty.fireValueChangedEvent(); 164 } 165 } 166 167 /** 168 * {@inheritDoc} 169 */ 170 @Override 171 protected void fireValueChangedEvent(Change<? extends E> change) { 172 if (readOnlyProperty != null) { 173 readOnlyProperty.fireValueChangedEvent(change); 174 } 175 } 176 177 private class ReadOnlyPropertyImpl extends ReadOnlyListPropertyBase<E> { 178 179 @Override 180 public ObservableList<E> get() { 181 return ReadOnlyListWrapper.this.get(); 182 } 183 184 @Override 185 public Object getBean() { 186 return ReadOnlyListWrapper.this.getBean(); 187 } 188 189 @Override 190 public String getName() { 191 return ReadOnlyListWrapper.this.getName(); 192 } 193 194 @Override 195 public ReadOnlyIntegerProperty sizeProperty() { 196 return ReadOnlyListWrapper.this.sizeProperty(); 197 } 198 199 @Override 200 public ReadOnlyBooleanProperty emptyProperty() { 201 return ReadOnlyListWrapper.this.emptyProperty(); 202 } 203 } 204 }