JavaFXAction.java
001 /*
002  * Copyright 2008-2015 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.support;
017 
018 import javafx.beans.property.BooleanProperty;
019 import javafx.beans.property.ObjectProperty;
020 import javafx.beans.property.SimpleBooleanProperty;
021 import javafx.beans.property.SimpleObjectProperty;
022 import javafx.beans.property.SimpleStringProperty;
023 import javafx.beans.property.StringProperty;
024 import javafx.event.ActionEvent;
025 import javafx.event.EventHandler;
026 import javafx.scene.Node;
027 import javafx.scene.image.Image;
028 import javafx.scene.input.KeyCombination;
029 
030 /**
031  @author Andres Almiray
032  */
033 public class JavaFXAction {
034     // -- onAction
035 
036     private ObjectProperty<EventHandler<ActionEvent>> onAction;
037 
038     public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() {
039         if (onAction == null) {
040             onAction = new SimpleObjectProperty<>(this, "onAction");
041         }
042         return onAction;
043     }
044 
045     public void setOnAction(EventHandler<ActionEvent> value) {
046         onActionProperty().set(value);
047     }
048 
049     public EventHandler<ActionEvent> getOnAction() {
050         return onAction == null null : onActionProperty().get();
051     }
052 
053     // -- name
054 
055     private StringProperty name;
056 
057     public final StringProperty nameProperty() {
058         if (name == null) {
059             name = new SimpleStringProperty(this, "name");
060         }
061         return name;
062     }
063 
064     public void setName(String name) {
065         nameProperty().set(name);
066     }
067 
068     public String getName() {
069         return name == null null : nameProperty().get();
070     }
071 
072     // -- description
073 
074     private StringProperty description;
075 
076     public final StringProperty descriptionProperty() {
077         if (description == null) {
078             description = new SimpleStringProperty(this, "description");
079         }
080         return description;
081     }
082 
083     public void setDescription(String description) {
084         descriptionProperty().set(description);
085     }
086 
087     public String getDescription() {
088         return description == null null : descriptionProperty().get();
089     }
090 
091     // -- enabled
092 
093     private BooleanProperty enabled;
094 
095     public final BooleanProperty enabledProperty() {
096         if (enabled == null) {
097             enabled = new SimpleBooleanProperty(this, "enabled"true);
098         }
099         return enabled;
100     }
101 
102     public void setEnabled(boolean enabled) {
103         enabledProperty().set(enabled);
104     }
105 
106     public boolean getEnabled() {
107         return enabled != null && enabledProperty().get();
108     }
109 
110     // -- accelerator
111 
112     private ObjectProperty<KeyCombination> accelerator;
113 
114     public void setAccelerator(String accelerator) {
115         setAccelerator(KeyCombination.keyCombination(accelerator));
116     }
117 
118     public final void setAccelerator(KeyCombination value) {
119         acceleratorProperty().set(value);
120     }
121 
122     public final KeyCombination getAccelerator() {
123         return accelerator == null null : accelerator.get();
124     }
125 
126     public final ObjectProperty<KeyCombination> acceleratorProperty() {
127         if (accelerator == null) {
128             accelerator = new SimpleObjectProperty<>(this, "accelerator");
129         }
130         return accelerator;
131     }
132 
133     // -- icon
134 
135     private StringProperty icon;
136 
137     public final StringProperty iconProperty() {
138         if (icon == null) {
139             icon = new SimpleStringProperty(this, "icon");
140         }
141         return icon;
142     }
143 
144     public void setIcon(String icon) {
145         iconProperty().set(icon);
146     }
147 
148     public String getIcon() {
149         return icon == null null : iconProperty().get();
150     }
151 
152     // -- image
153 
154     private ObjectProperty<Image> image;
155 
156     public final ObjectProperty<Image> imageProperty() {
157         if (image == null) {
158             image = new SimpleObjectProperty<>(this, "image");
159         }
160         return image;
161     }
162 
163     public void setImage(Image image) {
164         imageProperty().set(image);
165     }
166 
167     public Image getImage() {
168         return image == null null : imageProperty().get();
169     }
170 
171     // -- graphic
172 
173     private ObjectProperty<Node> graphic;
174 
175     public final ObjectProperty<Node> graphicProperty() {
176         if (graphic == null) {
177             graphic = new SimpleObjectProperty<>(this, "graphic");
178         }
179         return graphic;
180     }
181 
182     public void setGraphic(Node graphic) {
183         graphicProperty().set(graphic);
184     }
185 
186     public Node getGraphic() {
187         return graphic == null null : graphicProperty().get();
188     }
189 
190     // -- selected
191 
192     private BooleanProperty selected;
193 
194     public final BooleanProperty selectedProperty() {
195         if (selected == null) {
196             selected = new SimpleBooleanProperty(this, "selected");
197         }
198         return selected;
199     }
200 
201     public void setSelected(boolean selected) {
202         selectedProperty().set(selected);
203     }
204 
205     public boolean getSelected() {
206         return selected != null && selectedProperty().get();
207     }
208 }