UIThreadAwareSetProperty.java
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.beans.binding;
019 
020 import javafx.beans.InvalidationListener;
021 import javafx.beans.property.SetProperty;
022 import javafx.beans.value.ChangeListener;
023 import javafx.collections.ObservableSet;
024 import javafx.collections.SetChangeListener;
025 
026 import javax.annotation.Nonnull;
027 
028 import static javafx.application.Platform.isFxApplicationThread;
029 import static javafx.application.Platform.runLater;
030 
031 /**
032  @author Andres Almiray
033  @since 2.9.0
034  */
035 class UIThreadAwareSetProperty<E> extends SetPropertyDecorator<E> implements UIThreadAware {
036     UIThreadAwareSetProperty(@Nonnull SetProperty<E> delegate) {
037         super(delegate);
038     }
039 
040     @Override
041     public void set(final ObservableSet<E> value) {
042         if (isFxApplicationThread()) {
043             getDelegate().set(value);
044         else {
045             runLater(() -> getDelegate().set(value));
046         }
047     }
048 
049     @Override
050     public void addListener(SetChangeListener<? super E> listener) {
051         if (listener instanceof UIThreadAware) {
052             getDelegate().addListener(listener);
053         else {
054             getDelegate().addListener(new UIThreadAwareSetChangeListener<>(listener));
055         }
056     }
057 
058     @Override
059     public void removeListener(SetChangeListener<? super E> listener) {
060         if (listener instanceof UIThreadAware) {
061             getDelegate().removeListener(listener);
062         else {
063             getDelegate().removeListener(new UIThreadAwareSetChangeListener<>(listener));
064         }
065     }
066 
067     @Override
068     public void addListener(ChangeListener<? super ObservableSet<E>> listener) {
069         if (listener instanceof UIThreadAware) {
070             getDelegate().addListener(listener);
071         else {
072             getDelegate().addListener(new UIThreadAwareChangeListener<>(listener));
073         }
074     }
075 
076     @Override
077     public void removeListener(ChangeListener<? super ObservableSet<E>> listener) {
078         if (listener instanceof UIThreadAware) {
079             getDelegate().removeListener(listener);
080         else {
081             getDelegate().removeListener(new UIThreadAwareChangeListener<>(listener));
082         }
083     }
084 
085     @Override
086     public void addListener(InvalidationListener listener) {
087         if (listener instanceof UIThreadAware) {
088             getDelegate().addListener(listener);
089         else {
090             getDelegate().addListener(new UIThreadAwareInvalidationListener(listener));
091         }
092     }
093 
094     @Override
095     public void removeListener(InvalidationListener listener) {
096         if (listener instanceof UIThreadAware) {
097             getDelegate().removeListener(listener);
098         else {
099             getDelegate().removeListener(new UIThreadAwareInvalidationListener(listener));
100         }
101     }
102 }