JavaFXGriffonControllerAction.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 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.event.EventHandler;
024 import javafx.scene.Node;
025 import javafx.scene.image.Image;
026 import org.codehaus.griffon.runtime.core.controller.AbstractAction;
027 
028 import javax.annotation.Nonnull;
029 import javax.annotation.Nullable;
030 import java.beans.PropertyChangeEvent;
031 import java.beans.PropertyChangeListener;
032 import java.beans.PropertyEditor;
033 
034 import static griffon.core.editors.PropertyEditorResolver.findEditor;
035 import static griffon.util.GriffonNameUtils.isBlank;
036 import static griffon.util.TypeUtils.castToBoolean;
037 import static java.util.Objects.isNull;
038 import static java.util.Objects.requireNonNull;
039 
040 /**
041  @author Andres Almiray
042  */
043 public class JavaFXGriffonControllerAction extends AbstractAction {
044     public static final String KEY_DESCRIPTION = "description";
045     public static final String KEY_ICON = "icon";
046     public static final String KEY_IMAGE = "image";
047     public static final String KEY_GRAPHIC = "graphic";
048     public static final String KEY_SELECTED = "selected";
049     public static final String KEY_ACCELERATOR = "accelerator";
050     private final JavaFXAction toolkitAction;
051     private String description;
052     private String icon;
053     private String image;
054     private Node graphic;
055     private String accelerator;
056     private boolean selected;
057 
058     public JavaFXGriffonControllerAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
059         super(actionManager, controller, actionName);
060         requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
061 
062         toolkitAction = new JavaFXAction();
063         toolkitAction.setOnAction(new EventHandler<ActionEvent>() {
064             public void handle(ActionEvent actionEvent) {
065                 actionManager.invokeAction(controller, actionName, actionEvent);
066             }
067         });
068         addPropertyChangeListener(new PropertyChangeListener() {
069             public void propertyChange(final PropertyChangeEvent evt) {
070                 uiThreadManager.runInsideUIAsync(new Runnable() {
071                     public void run() {
072                         if (KEY_NAME.equals(evt.getPropertyName())) {
073                             toolkitAction.setName(String.valueOf(evt.getNewValue()));
074                         else if (KEY_DESCRIPTION.equals(evt.getPropertyName())) {
075                             toolkitAction.setDescription(String.valueOf(evt.getNewValue()));
076                         else if (KEY_ENABLED.equals(evt.getPropertyName())) {
077                             toolkitAction.setEnabled(castToBoolean(evt.getNewValue()));
078                         else if (KEY_SELECTED.equals(evt.getPropertyName())) {
079                             toolkitAction.setSelected(castToBoolean(evt.getNewValue()));
080                         else if (KEY_ACCELERATOR.equals(evt.getPropertyName())) {
081                             String accelerator = (Stringevt.getNewValue();
082                             if (!isBlank(accelerator))
083                                 toolkitAction.setAccelerator(accelerator);
084                         else if (KEY_ICON.equals(evt.getPropertyName())) {
085                             String icon = (Stringevt.getNewValue();
086                             if (!isBlank(icon)) toolkitAction.setIcon(icon);
087                         else if (KEY_IMAGE.equals(evt.getPropertyName())) {
088                             Image image = (Imageevt.getNewValue();
089                             if (!isNull(image)) toolkitAction.setImage(image);
090                         else if (KEY_GRAPHIC.equals(evt.getPropertyName())) {
091                             Node graphic = (Nodeevt.getNewValue();
092                             if (!isNull(graphic)) toolkitAction.setGraphic(graphic);
093                         }
094                     }
095                 });
096             }
097         });
098     }
099 
100     @Nullable
101     public String getAccelerator() {
102         return accelerator;
103     }
104 
105     public void setAccelerator(@Nullable String accelerator) {
106         firePropertyChange(KEY_ACCELERATOR, this.accelerator, this.accelerator = accelerator);
107     }
108 
109     public boolean isSelected() {
110         return selected;
111     }
112 
113     public void setSelected(boolean selected) {
114         firePropertyChange(KEY_SELECTED, this.selected, this.selected = selected);
115     }
116 
117     @Nullable
118     public String getDescription() {
119         return description;
120     }
121 
122     public void setDescription(@Nullable String description) {
123         firePropertyChange(KEY_DESCRIPTION, this.description, this.description = description);
124     }
125 
126     @Nullable
127     public String getIcon() {
128         return icon;
129     }
130 
131     public void setIcon(@Nullable String icon) {
132         firePropertyChange(KEY_ICON, this.icon, this.icon = icon);
133     }
134 
135     @Nullable
136     public Image getImage() {
137         PropertyEditor editor = findEditor(Image.class);
138         editor.setValue(image);
139         return (Imageeditor.getValue();
140     }
141 
142     public void setImage(@Nullable String image) {
143         firePropertyChange(KEY_IMAGE, this.image, this.image = image);
144     }
145 
146     @Nullable
147     public Node getGraphic() {
148         return graphic;
149     }
150 
151     public void setGraphic(@Nullable Node graphic) {
152         firePropertyChange(KEY_ICON, this.graphic, this.graphic = graphic);
153     }
154 
155     @Nonnull
156     public Object getToolkitAction() {
157         return toolkitAction;
158     }
159 
160     protected void doExecute(Object... args) {
161         ActionEvent event = null;
162         if (args != null && args.length == && args[0instanceof ActionEvent) {
163             event = (ActionEventargs[0];
164         }
165         toolkitAction.onActionProperty().get().handle(event);
166     }
167 
168     @Override
169     protected void doInitialize() {
170         toolkitAction.setName(getName());
171         toolkitAction.setDescription(getDescription());
172         toolkitAction.setEnabled(isEnabled());
173         toolkitAction.setSelected(isSelected());
174         String accelerator = getAccelerator();
175         if (!isBlank(accelerator)) toolkitAction.setAccelerator(accelerator);
176         String icon = getIcon();
177         if (!isBlank(icon)) toolkitAction.setIcon(icon);
178         if (!isNull(getImage())) toolkitAction.setImage(getImage());
179         if (!isNull(getGraphic())) toolkitAction.setGraphic(getGraphic());
180     }
181 }