001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.javafx.collections;
019
020 import javafx.collections.ListChangeListener;
021 import javafx.collections.ObservableList;
022 import javafx.collections.ObservableListBase;
023 import javafx.collections.WeakListChangeListener;
024
025 import javax.annotation.Nonnull;
026 import java.util.Collection;
027 import java.util.Iterator;
028 import java.util.List;
029 import java.util.ListIterator;
030
031 import static java.util.Objects.requireNonNull;
032
033 /**
034 * @author Andres Almiray
035 * @since 2.6.0
036 */
037 public abstract class DelegatingObservableList<E> extends ObservableListBase<E> implements ObservableList<E> {
038 private final ObservableList<E> delegate;
039 private ListChangeListener<E> sourceListener;
040
041 public DelegatingObservableList(@Nonnull ObservableList<E> delegate) {
042 this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
043 this.delegate.addListener(new WeakListChangeListener<>(getListener()));
044 }
045
046 @Nonnull
047 protected ObservableList<E> getDelegate() {
048 return delegate;
049 }
050
051 private ListChangeListener<E> getListener() {
052 if (sourceListener == null) {
053 sourceListener = DelegatingObservableList.this::sourceChanged;
054 }
055 return sourceListener;
056 }
057
058 protected abstract void sourceChanged(@Nonnull ListChangeListener.Change<? extends E> c);
059
060 // --== Delegate methods ==--
061
062 public boolean removeAll(E... elements) {
063 return getDelegate().removeAll(elements);
064 }
065
066 public void remove(int from, int to) {
067 getDelegate().remove(from, to);
068 }
069
070 public E remove(int index) {
071 return getDelegate().remove(index);
072 }
073
074 public int size() {
075 return getDelegate().size();
076 }
077
078 public int lastIndexOf(Object o) {
079 return getDelegate().lastIndexOf(o);
080 }
081
082 public boolean isEmpty() {
083 return getDelegate().isEmpty();
084 }
085
086 public boolean addAll(E... elements) {
087 return getDelegate().addAll(elements);
088 }
089
090 public List<E> subList(int fromIndex, int toIndex) {
091 return getDelegate().subList(fromIndex, toIndex);
092 }
093
094 public E set(int index, E element) {
095 return getDelegate().set(index, element);
096 }
097
098 public void add(int index, E element) {
099 getDelegate().add(index, element);
100 }
101
102 public boolean containsAll(Collection<?> c) {
103 return getDelegate().containsAll(c);
104 }
105
106 public void clear() {
107 getDelegate().clear();
108 }
109
110 public Iterator<E> iterator() {
111 return getDelegate().iterator();
112 }
113
114 public boolean removeAll(Collection<?> c) {
115 return getDelegate().removeAll(c);
116 }
117
118 public <T> T[] toArray(T[] a) {
119 return getDelegate().toArray(a);
120 }
121
122 public boolean remove(Object o) {
123 return getDelegate().remove(o);
124 }
125
126 public boolean addAll(Collection<? extends E> c) {
127 return getDelegate().addAll(c);
128 }
129
130 public boolean retainAll(E... elements) {
131 return getDelegate().retainAll(elements);
132 }
133
134 public boolean retainAll(Collection<?> c) {
135 return getDelegate().retainAll(c);
136 }
137
138 public boolean contains(Object o) {
139 return getDelegate().contains(o);
140 }
141
142 public boolean setAll(Collection<? extends E> col) {
143 return getDelegate().setAll(col);
144 }
145
146 public ListIterator<E> listIterator(int index) {
147 return getDelegate().listIterator(index);
148 }
149
150 public boolean add(E e) {
151 return getDelegate().add(e);
152 }
153
154 public boolean addAll(int index, Collection<? extends E> c) {
155 return getDelegate().addAll(index, c);
156 }
157
158 public Object[] toArray() {
159 return getDelegate().toArray();
160 }
161
162 public ListIterator<E> listIterator() {
163 return getDelegate().listIterator();
164 }
165
166 public E get(int index) {
167 return getDelegate().get(index);
168 }
169
170 public boolean setAll(E... elements) {
171 return getDelegate().setAll(elements);
172 }
173
174 public int indexOf(Object o) {
175 return getDelegate().indexOf(o);
176 }
177 }
|