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