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