UIThreadAwareIntegerProperty.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2018 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.javafx.beans.binding;
19 
20 import javafx.beans.InvalidationListener;
21 import javafx.beans.property.IntegerProperty;
22 import javafx.beans.value.ChangeListener;
23 
24 import javax.annotation.Nonnull;
25 
26 import static javafx.application.Platform.isFxApplicationThread;
27 import static javafx.application.Platform.runLater;
28 
29 /**
30  @author Andres Almiray
31  @since 2.9.0
32  */
33 class UIThreadAwareIntegerProperty extends IntegerPropertyDecorator implements UIThreadAware {
34     UIThreadAwareIntegerProperty(@Nonnull IntegerProperty delegate) {
35         super(delegate);
36     }
37 
38     @Override
39     public void set(final int value) {
40         if (isFxApplicationThread()) {
41             getDelegate().set(value);
42         else {
43             runLater(() -> getDelegate().set(value));
44         }
45     }
46 
47     @Override
48     public void addListener(ChangeListener<? super Number> listener) {
49         if (listener instanceof UIThreadAware) {
50             getDelegate().addListener(listener);
51         else {
52             getDelegate().addListener(new UIThreadAwareChangeListener<>(listener));
53         }
54     }
55 
56     @Override
57     public void removeListener(ChangeListener<? super Number> listener) {
58         if (listener instanceof UIThreadAware) {
59             getDelegate().removeListener(listener);
60         else {
61             getDelegate().removeListener(new UIThreadAwareChangeListener<>(listener));
62         }
63     }
64 
65     @Override
66     public void addListener(InvalidationListener listener) {
67         if (listener instanceof UIThreadAware) {
68             getDelegate().addListener(listener);
69         else {
70             getDelegate().addListener(new UIThreadAwareInvalidationListener(listener));
71         }
72     }
73 
74     @Override
75     public void removeListener(InvalidationListener listener) {
76         if (listener instanceof UIThreadAware) {
77             getDelegate().removeListener(listener);
78         else {
79             getDelegate().removeListener(new UIThreadAwareInvalidationListener(listener));
80         }
81     }
82 }