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
029 import static java.util.Objects.requireNonNull;
030
031 /**
032 * @author Andres Almiray
033 * @since 2.6.0
034 */
035 public abstract class DelegatingObservableList<E> extends ObservableListBase<E> implements ObservableList<E> {
036 private final ObservableList<E> delegate;
037 private ListChangeListener<E> sourceListener;
038
039 public DelegatingObservableList(@Nonnull ObservableList<E> delegate) {
040 this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
041 this.delegate.addListener(new WeakListChangeListener<>(getListener()));
042 }
043
044 @Nonnull
045 protected ObservableList<E> getDelegate() {
046 return delegate;
047 }
048
049 private ListChangeListener<E> getListener() {
050 if (sourceListener == null) {
051 sourceListener = DelegatingObservableList.this::sourceChanged;
052 }
053 return sourceListener;
054 }
055
056 protected abstract void sourceChanged(@Nonnull ListChangeListener.Change<? extends E> c);
057
058 // --== Delegate methods ==--
059
060 public boolean removeAll(E... elements) {
061 return getDelegate().removeAll(elements);
062 }
063
064 public void remove(int from, int to) {
065 getDelegate().remove(from, to);
066 }
067
068 public E remove(int index) {
069 return getDelegate().remove(index);
070 }
071
072 public int size() {
073 return getDelegate().size();
074 }
075
076 public int lastIndexOf(Object o) {
077 return getDelegate().lastIndexOf(o);
078 }
079
080 public boolean isEmpty() {
081 return getDelegate().isEmpty();
082 }
083
084 public boolean addAll(E... elements) {
085 return getDelegate().addAll(elements);
086 }
087
088 public List<E> subList(int fromIndex, int toIndex) {
089 return getDelegate().subList(fromIndex, toIndex);
090 }
091
092 public E set(int index, E element) {
093 return getDelegate().set(index, element);
094 }
095
096 public void add(int index, E element) {
097 getDelegate().add(index, element);
098 }
099
100 public boolean containsAll(Collection<?> c) {
101 return getDelegate().containsAll(c);
102 }
103
104 public void clear() {
105 getDelegate().clear();
106 }
107
108 public Iterator<E> iterator() {
109 return getDelegate().iterator();
110 }
111
112 public boolean removeAll(Collection<?> c) {
113 return getDelegate().removeAll(c);
114 }
115
116 public <T> T[] toArray(T[] a) {
117 return getDelegate().toArray(a);
118 }
119
120 public boolean remove(Object o) {
121 return getDelegate().remove(o);
122 }
123
124 public boolean addAll(Collection<? extends E> c) {
125 return getDelegate().addAll(c);
126 }
127
128 public boolean retainAll(E... elements) {
129 return getDelegate().retainAll(elements);
130 }
131
132 public boolean retainAll(Collection<?> c) {
133 return getDelegate().retainAll(c);
134 }
135
136 public boolean contains(Object o) {
137 return getDelegate().contains(o);
138 }
139
140 public boolean setAll(Collection<? extends E> col) {
141 return getDelegate().setAll(col);
142 }
143
144 public ListIterator<E> listIterator(int index) {
145 return getDelegate().listIterator(index);
146 }
147
148 public boolean add(E e) {
149 return getDelegate().add(e);
150 }
151
152 public boolean addAll(int index, Collection<? extends E> c) {
153 return getDelegate().addAll(index, c);
154 }
155
156 public Object[] toArray() {
157 return getDelegate().toArray();
158 }
159
160 public ListIterator<E> listIterator() {
161 return getDelegate().listIterator();
162 }
163
164 public E get(int index) {
165 return getDelegate().get(index);
166 }
167
168 public boolean setAll(E... elements) {
169 return getDelegate().setAll(elements);
170 }
171
172 public int indexOf(Object o) {
173 return getDelegate().indexOf(o);
174 }
175 }
|