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 org.codehaus.griffon.runtime.javafx.controller;
017
018 import griffon.core.artifact.GriffonController;
019 import griffon.core.controller.ActionManager;
020 import griffon.core.threading.UIThreadManager;
021 import griffon.javafx.support.JavaFXAction;
022 import javafx.event.ActionEvent;
023 import javafx.scene.Node;
024 import javafx.scene.image.Image;
025 import org.codehaus.griffon.runtime.core.controller.AbstractAction;
026
027 import javax.annotation.Nonnull;
028 import javax.annotation.Nullable;
029 import java.beans.PropertyChangeEvent;
030 import java.beans.PropertyEditor;
031
032 import static griffon.core.editors.PropertyEditorResolver.findEditor;
033 import static griffon.util.GriffonNameUtils.isBlank;
034 import static griffon.util.TypeUtils.castToBoolean;
035 import static java.util.Objects.requireNonNull;
036
037 /**
038 * @author Andres Almiray
039 */
040 public class JavaFXGriffonControllerAction extends AbstractAction {
041 public static final String KEY_DESCRIPTION = "description";
042 public static final String KEY_ICON = "icon";
043 public static final String KEY_IMAGE = "image";
044 public static final String KEY_GRAPHIC = "graphic";
045 public static final String KEY_SELECTED = "selected";
046 public static final String KEY_VISIBLE = "visible";
047 public static final String KEY_ACCELERATOR = "accelerator";
048 public static final String KEY_STYLECLASS = "styleClass";
049 public static final String KEY_STYLE = "style";
050 public static final String KEY_GRAPHICSTYLECLASS = "graphicStyleClass";
051 public static final String KEY_GRAPHICSTYLE = "graphicStyle";
052
053 private final JavaFXAction toolkitAction;
054 private String description;
055 private String icon;
056 private String image;
057 private Node graphic;
058 private String accelerator;
059 private String styleClass;
060 private String style;
061 private String graphicStyleClass;
062 private String graphicStyle;
063 private boolean selected;
064 private boolean visible = true;
065
066 public JavaFXGriffonControllerAction(@Nonnull final UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
067 super(actionManager, controller, actionName);
068 requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
069
070 toolkitAction = createAction(actionManager, controller, actionName);
071 toolkitAction.setOnAction(actionEvent -> actionManager.invokeAction(controller, actionName, actionEvent));
072
073 addPropertyChangeListener(evt -> uiThreadManager.runInsideUIAsync(() -> handlePropertyChange(evt)));
074 }
075
076 protected JavaFXAction createAction(@Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
077 return new JavaFXAction();
078 }
079
080 protected void handlePropertyChange(@Nonnull PropertyChangeEvent evt) {
081 if (KEY_NAME.equals(evt.getPropertyName())) {
082 toolkitAction.setName(String.valueOf(evt.getNewValue()));
083 } else if (KEY_DESCRIPTION.equals(evt.getPropertyName())) {
084 toolkitAction.setDescription(String.valueOf(evt.getNewValue()));
085 } else if (KEY_ENABLED.equals(evt.getPropertyName())) {
086 toolkitAction.setEnabled(castToBoolean(evt.getNewValue()));
087 } else if (KEY_SELECTED.equals(evt.getPropertyName())) {
088 toolkitAction.setSelected(castToBoolean(evt.getNewValue()));
089 } else if (KEY_VISIBLE.equals(evt.getPropertyName())) {
090 toolkitAction.setVisible(castToBoolean(evt.getNewValue()));
091 } else if (KEY_ACCELERATOR.equals(evt.getPropertyName())) {
092 String accelerator = (String) evt.getNewValue();
093 if (!isBlank(accelerator)) { toolkitAction.setAccelerator(accelerator); }
094 } else if (KEY_STYLECLASS.equals(evt.getPropertyName())) {
095 String styleClass = (String) evt.getNewValue();
096 if (!isBlank(styleClass)) { toolkitAction.setStyleClass(styleClass); }
097 } else if (KEY_STYLE.equals(evt.getPropertyName())) {
098 String style = (String) evt.getNewValue();
099 if (!isBlank(style)) { toolkitAction.setStyle(style); }
100 } else if (KEY_ICON.equals(evt.getPropertyName())) {
101 String icon = (String) evt.getNewValue();
102 if (!isBlank(icon)) { toolkitAction.setIcon(icon); }
103 } else if (KEY_IMAGE.equals(evt.getPropertyName())) {
104 Image image = (Image) evt.getNewValue();
105 if (null != image) { toolkitAction.setImage(image); }
106 } else if (KEY_GRAPHIC.equals(evt.getPropertyName())) {
107 Node graphic = (Node) evt.getNewValue();
108 if (null != graphic) { toolkitAction.setGraphic(graphic); }
109 } else if (KEY_GRAPHICSTYLECLASS.equals(evt.getPropertyName())) {
110 String graphicStyleClass = (String) evt.getNewValue();
111 if (!isBlank(graphicStyleClass)) { toolkitAction.setGraphicStyleClass(graphicStyleClass); }
112 } else if (KEY_GRAPHICSTYLE.equals(evt.getPropertyName())) {
113 String graphicStyle = (String) evt.getNewValue();
114 if (!isBlank(graphicStyle)) { toolkitAction.setGraphicStyle(graphicStyle); }
115 }
116 }
117
118 @Nullable
119 public String getStyleClass() {
120 return styleClass;
121 }
122
123 public void setStyleClass(@Nullable String styleClass) {
124 firePropertyChange(KEY_STYLECLASS, this.styleClass, this.styleClass = styleClass);
125 }
126
127 @Nullable
128 public String getStyle() {
129 return style;
130 }
131
132 public void setStyle(@Nullable String style) {
133 firePropertyChange(KEY_STYLE, this.style, this.style = style);
134 }
135
136 @Nullable
137 public String getGraphicStyleClass() {
138 return graphicStyleClass;
139 }
140
141 public void setGraphicStyleClass(@Nullable String graphicStyleClass) {
142 firePropertyChange(KEY_GRAPHICSTYLECLASS, this.graphicStyleClass, this.graphicStyleClass = graphicStyleClass);
143 }
144
145 @Nullable
146 public String getGraphicStyle() {
147 return graphicStyle;
148 }
149
150 public void setGraphicStyle(@Nullable String graphicStyle) {
151 firePropertyChange(KEY_GRAPHICSTYLE, this.graphicStyle, this.graphicStyle = graphicStyle);
152 }
153
154 @Nullable
155 public String getAccelerator() {
156 return accelerator;
157 }
158
159 public void setAccelerator(@Nullable String accelerator) {
160 firePropertyChange(KEY_ACCELERATOR, this.accelerator, this.accelerator = accelerator);
161 }
162
163 public boolean isSelected() {
164 return selected;
165 }
166
167 public void setSelected(boolean selected) {
168 firePropertyChange(KEY_SELECTED, this.selected, this.selected = selected);
169 }
170
171 public boolean isVisible() {
172 return visible;
173 }
174
175 public void setVisible(boolean visible) {
176 firePropertyChange(KEY_SELECTED, this.visible, this.visible = visible);
177 }
178
179 @Nullable
180 public String getDescription() {
181 return description;
182 }
183
184 public void setDescription(@Nullable String description) {
185 firePropertyChange(KEY_DESCRIPTION, this.description, this.description = description);
186 }
187
188 @Nullable
189 public String getIcon() {
190 return icon;
191 }
192
193 public void setIcon(@Nullable String icon) {
194 firePropertyChange(KEY_ICON, this.icon, this.icon = icon);
195 }
196
197 @Nullable
198 public Image getImage() {
199 PropertyEditor editor = findEditor(Image.class);
200 editor.setValue(image);
201 return (Image) editor.getValue();
202 }
203
204 public void setImage(@Nullable String image) {
205 firePropertyChange(KEY_IMAGE, this.image, this.image = image);
206 }
207
208 @Nullable
209 public Node getGraphic() {
210 return graphic;
211 }
212
213 public void setGraphic(@Nullable Node graphic) {
214 firePropertyChange(KEY_ICON, this.graphic, this.graphic = graphic);
215 }
216
217 @Nonnull
218 public Object getToolkitAction() {
219 return toolkitAction;
220 }
221
222 protected void doExecute(Object... args) {
223 ActionEvent event = null;
224 if (args != null && args.length == 1 && args[0] instanceof ActionEvent) {
225 event = (ActionEvent) args[0];
226 }
227 toolkitAction.onActionProperty().get().handle(event);
228 }
229
230 @Override
231 protected void doInitialize() {
232 toolkitAction.setName(getName());
233 toolkitAction.setDescription(getDescription());
234 toolkitAction.setEnabled(isEnabled());
235 toolkitAction.setSelected(isSelected());
236 toolkitAction.setVisible(isVisible());
237 String accelerator = getAccelerator();
238 if (!isBlank(accelerator)) { toolkitAction.setAccelerator(accelerator); }
239 if (!isBlank(style)) { toolkitAction.setStyle(style); }
240 if (!isBlank(styleClass)) { toolkitAction.setStyleClass(styleClass); }
241 String icon = getIcon();
242 if (!isBlank(icon)) { toolkitAction.setIcon(icon); }
243 if (null != getImage()) { toolkitAction.setImage(getImage()); }
244 if (null != getGraphic()) { toolkitAction.setGraphic(getGraphic()); }
245 if (!isBlank(graphicStyle)) { toolkitAction.setGraphicStyle(graphicStyle); }
246 if (!isBlank(graphicStyleClass)) { toolkitAction.setGraphicStyleClass(graphicStyleClass); }
247 }
248 }
|