GriffonFXCollections.java
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 griffon.javafx.beans.binding.UIThreadAware;
019 import javafx.application.Platform;
020 import javafx.collections.ListChangeListener;
021 import javafx.collections.MapChangeListener;
022 import javafx.collections.ObservableList;
023 import javafx.collections.ObservableMap;
024 import javafx.collections.ObservableSet;
025 import javafx.collections.SetChangeListener;
026 
027 import javax.annotation.Nonnull;
028 
029 import static java.util.Objects.requireNonNull;
030 
031 /**
032  @author Andres Almiray
033  @since 2.10.0
034  */
035 public final class GriffonFXCollections {
036     private static final String ERROR_SOURCE_NULL = "Argument 'source' must not be null";
037 
038     private GriffonFXCollections() {
039         // prevent instantiation
040     }
041 
042     /**
043      * Wraps an <tt>ObservableList</tt>, publishing updates inside the UI thread.
044      *
045      @param source the <tt>ObservableList</tt> to be wrapped
046      @param <E>    the list's parameter type.
047      *
048      @return a new <tt>ObservableList</tt>
049      */
050     @Nonnull
051     public static <E> ObservableList<E> uiThreadAwareObservableList(@Nonnull ObservableList<E> source) {
052         requireNonNull(source, ERROR_SOURCE_NULL);
053         return source instanceof UIThreadAware ? source : new UIThreadAwareObservableList<>(source);
054     }
055 
056     private static class UIThreadAwareObservableList<E> extends DelegatingObservableList<E> implements UIThreadAware {
057         protected UIThreadAwareObservableList(ObservableList<E> delegate) {
058             super(delegate);
059         }
060 
061         @Override
062         protected void sourceChanged(@Nonnull final ListChangeListener.Change<? extends E> c) {
063             if (Platform.isFxApplicationThread()) {
064                 fireChange(c);
065             else {
066                 Platform.runLater(() -> fireChange(c));
067             }
068         }
069     }
070 
071     /**
072      * Wraps an <tt>ObservableSet</tt>, publishing updates inside the UI thread.
073      *
074      @param source the <tt>ObservableSet</tt> to be wrapped
075      @param <E>    the set's parameter type.
076      *
077      @return a new <tt>ObservableSet</tt>
078      */
079     @Nonnull
080     public static <E> ObservableSet<E> uiThreadAwareObservableSet(@Nonnull ObservableSet<E> source) {
081         requireNonNull(source, ERROR_SOURCE_NULL);
082         return source instanceof UIThreadAware ? source : new UIThreadAwareObservableSet<>(source);
083     }
084 
085     private static class UIThreadAwareObservableSet<E> extends DelegatingObservableSet<E> implements UIThreadAware {
086         protected UIThreadAwareObservableSet(ObservableSet<E> delegate) {
087             super(delegate);
088         }
089 
090         @Override
091         protected void sourceChanged(@Nonnull final SetChangeListener.Change<? extends E> c) {
092             if (Platform.isFxApplicationThread()) {
093                 fireChange(c);
094             else {
095                 Platform.runLater(() -> fireChange(c));
096             }
097         }
098     }
099 
100     /**
101      * Wraps an <tt>ObservableMap</tt>, publishing updates inside the UI thread.
102      *
103      @param source the <tt>ObservableMap</tt> to be wrapped
104      @param <K>    the type of keys maintained by the map
105      @param <V>    the type of mapped values
106      *
107      @return a new <tt>ObservableMap</tt>
108      */
109     @Nonnull
110     public static <K, V> ObservableMap<K, V> uiThreadAwareObservableMap(@Nonnull ObservableMap<K, V> source) {
111         requireNonNull(source, ERROR_SOURCE_NULL);
112         return source instanceof UIThreadAware ? source : new UIThreadAwareObservableMap<>(source);
113     }
114 
115     private static class UIThreadAwareObservableMap<K, V> extends DelegatingObservableMap<K, V> implements UIThreadAware {
116         protected UIThreadAwareObservableMap(ObservableMap<K, V> delegate) {
117             super(delegate);
118         }
119 
120         @Override
121         protected void sourceChanged(@Nonnull final MapChangeListener.Change<? extends K, ? extends V> c) {
122             if (Platform.isFxApplicationThread()) {
123                 fireChange(c);
124             else {
125                 Platform.runLater(() -> fireChange(c));
126             }
127         }
128     }
129 }