JavaFXGriffonControllerAction.java
001 /*
002  * Copyright 2008-2016 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     private final JavaFXAction toolkitAction;
050     private String description;
051     private String icon;
052     private String image;
053     private Node graphic;
054     private String accelerator;
055     private String styleClass;
056     private boolean selected;
057     private boolean visible = true;
058 
059     public JavaFXGriffonControllerAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
060         super(actionManager, controller, actionName);
061         requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
062 
063         toolkitAction = createAction(actionManager, controller, actionName);
064         toolkitAction.setOnAction(actionEvent -> actionManager.invokeAction(controller, actionName, actionEvent));
065 
066         addPropertyChangeListener(evt -> uiThreadManager.runInsideUIAsync(() -> handlePropertyChange(evt)));
067     }
068 
069     protected JavaFXAction createAction(final @Nonnull ActionManager actionManager, final @Nonnull GriffonController controller, final @Nonnull String actionName) {
070         return new JavaFXAction();
071     }
072 
073     protected void handlePropertyChange(@Nonnull PropertyChangeEvent evt) {
074         if (KEY_NAME.equals(evt.getPropertyName())) {
075             toolkitAction.setName(String.valueOf(evt.getNewValue()));
076         else if (KEY_DESCRIPTION.equals(evt.getPropertyName())) {
077             toolkitAction.setDescription(String.valueOf(evt.getNewValue()));
078         else if (KEY_ENABLED.equals(evt.getPropertyName())) {
079             toolkitAction.setEnabled(castToBoolean(evt.getNewValue()));
080         else if (KEY_SELECTED.equals(evt.getPropertyName())) {
081             toolkitAction.setSelected(castToBoolean(evt.getNewValue()));
082         else if (KEY_VISIBLE.equals(evt.getPropertyName())) {
083             toolkitAction.setVisible(castToBoolean(evt.getNewValue()));
084         else if (KEY_ACCELERATOR.equals(evt.getPropertyName())) {
085             String accelerator = (Stringevt.getNewValue();
086             if (!isBlank(accelerator)) toolkitAction.setAccelerator(accelerator);
087         else if (KEY_STYLECLASS.equals(evt.getPropertyName())) {
088             String styleClass = (Stringevt.getNewValue();
089             if (!isBlank(styleClass)) toolkitAction.setStyleClass(styleClass);
090         else if (KEY_ICON.equals(evt.getPropertyName())) {
091             String icon = (Stringevt.getNewValue();
092             if (!isBlank(icon)) toolkitAction.setIcon(icon);
093         else if (KEY_IMAGE.equals(evt.getPropertyName())) {
094             Image image = (Imageevt.getNewValue();
095             if (null != imagetoolkitAction.setImage(image);
096         else if (KEY_GRAPHIC.equals(evt.getPropertyName())) {
097             Node graphic = (Nodeevt.getNewValue();
098             if (null != graphictoolkitAction.setGraphic(graphic);
099         }
100     }
101 
102     @Nullable
103     public String getStyleClass() {
104         return styleClass;
105     }
106 
107     public void setStyleClass(@Nullable String styleClass) {
108         firePropertyChange(KEY_STYLECLASS, this.styleClass, this.styleClass = styleClass);
109     }
110 
111     @Nullable
112     public String getAccelerator() {
113         return accelerator;
114     }
115 
116     public void setAccelerator(@Nullable String accelerator) {
117         firePropertyChange(KEY_ACCELERATOR, this.accelerator, this.accelerator = accelerator);
118     }
119 
120     public boolean isSelected() {
121         return selected;
122     }
123 
124     public void setSelected(boolean selected) {
125         firePropertyChange(KEY_SELECTED, this.selected, this.selected = selected);
126     }
127 
128     public boolean isVisible() {
129         return visible;
130     }
131 
132     public void setVisible(boolean visible) {
133         firePropertyChange(KEY_SELECTED, this.visible, this.visible = visible);
134     }
135 
136     @Nullable
137     public String getDescription() {
138         return description;
139     }
140 
141     public void setDescription(@Nullable String description) {
142         firePropertyChange(KEY_DESCRIPTION, this.description, this.description = description);
143     }
144 
145     @Nullable
146     public String getIcon() {
147         return icon;
148     }
149 
150     public void setIcon(@Nullable String icon) {
151         firePropertyChange(KEY_ICON, this.icon, this.icon = icon);
152     }
153 
154     @Nullable
155     public Image getImage() {
156         PropertyEditor editor = findEditor(Image.class);
157         editor.setValue(image);
158         return (Imageeditor.getValue();
159     }
160 
161     public void setImage(@Nullable String image) {
162         firePropertyChange(KEY_IMAGE, this.image, this.image = image);
163     }
164 
165     @Nullable
166     public Node getGraphic() {
167         return graphic;
168     }
169 
170     public void setGraphic(@Nullable Node graphic) {
171         firePropertyChange(KEY_ICON, this.graphic, this.graphic = graphic);
172     }
173 
174     @Nonnull
175     public Object getToolkitAction() {
176         return toolkitAction;
177     }
178 
179     protected void doExecute(Object... args) {
180         ActionEvent event = null;
181         if (args != null && args.length == && args[0instanceof ActionEvent) {
182             event = (ActionEventargs[0];
183         }
184         toolkitAction.onActionProperty().get().handle(event);
185     }
186 
187     @Override
188     protected void doInitialize() {
189         toolkitAction.setName(getName());
190         toolkitAction.setDescription(getDescription());
191         toolkitAction.setEnabled(isEnabled());
192         toolkitAction.setSelected(isSelected());
193         toolkitAction.setVisible(isVisible());
194         String accelerator = getAccelerator();
195         if (!isBlank(accelerator)) toolkitAction.setAccelerator(accelerator);
196         if (!isBlank(styleClass)) toolkitAction.setStyleClass(styleClass);
197         String icon = getIcon();
198         if (!isBlank(icon)) toolkitAction.setIcon(icon);
199         if (null != getImage()) toolkitAction.setImage(getImage());
200         if (null != getGraphic()) toolkitAction.setGraphic(getGraphic());
201     }
202 }