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