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 org.codehaus.griffon.runtime.core.controller.AbstractAction;
025
026 import javax.annotation.Nonnull;
027 import javax.annotation.Nullable;
028 import java.beans.PropertyChangeEvent;
029 import java.beans.PropertyChangeListener;
030
031 import static griffon.util.GriffonNameUtils.isBlank;
032 import static griffon.util.TypeUtils.castToBoolean;
033 import static java.util.Objects.requireNonNull;
034
035 /**
036 * @author Andres Almiray
037 */
038 public class JavaFXGriffonControllerAction extends AbstractAction {
039 public static final String KEY_DESCRIPTION = "description";
040 public static final String KEY_ICON = "icon";
041 public static final String KEY_SELECTED = "selected";
042 public static final String KEY_ACCELERATOR = "accelerator";
043 private final JavaFXAction toolkitAction;
044 private String description;
045 private String icon;
046 private String accelerator;
047 private boolean selected;
048
049 public JavaFXGriffonControllerAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
050 super(actionManager, controller, actionName);
051 requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
052
053 toolkitAction = new JavaFXAction();
054 toolkitAction.setOnAction(new EventHandler<ActionEvent>() {
055 public void handle(ActionEvent actionEvent) {
056 actionManager.invokeAction(controller, actionName, actionEvent);
057 }
058 });
059 addPropertyChangeListener(new PropertyChangeListener() {
060 public void propertyChange(final PropertyChangeEvent evt) {
061 uiThreadManager.runInsideUIAsync(new Runnable() {
062 public void run() {
063 if (KEY_NAME.equals(evt.getPropertyName())) {
064 toolkitAction.setName(String.valueOf(evt.getNewValue()));
065 } else if (KEY_DESCRIPTION.equals(evt.getPropertyName())) {
066 toolkitAction.setDescription(String.valueOf(evt.getNewValue()));
067 } else if (KEY_ENABLED.equals(evt.getPropertyName())) {
068 toolkitAction.setEnabled(castToBoolean(evt.getNewValue()));
069 } else if (KEY_SELECTED.equals(evt.getPropertyName())) {
070 toolkitAction.setSelected(castToBoolean(evt.getNewValue()));
071 } else if (KEY_ACCELERATOR.equals(evt.getPropertyName())) {
072 String accelerator = (String) evt.getNewValue();
073 if (!isBlank(accelerator))
074 toolkitAction.setAccelerator(accelerator);
075 } else if (KEY_ICON.equals(evt.getPropertyName())) {
076 final String icon = (String) evt.getNewValue();
077 if (!isBlank(icon)) toolkitAction.setIcon(icon);
078 }
079 }
080 });
081 }
082 });
083 }
084
085 @Nullable
086 public String getAccelerator() {
087 return accelerator;
088 }
089
090 public void setAccelerator(@Nullable String accelerator) {
091 firePropertyChange(KEY_ACCELERATOR, this.accelerator, this.accelerator = accelerator);
092 }
093
094 public boolean isSelected() {
095 return selected;
096 }
097
098 public void setSelected(boolean selected) {
099 firePropertyChange(KEY_SELECTED, this.selected, this.selected = selected);
100 }
101
102 @Nullable
103 public String getDescription() {
104 return description;
105 }
106
107 public void setDescription(@Nullable String description) {
108 firePropertyChange(KEY_DESCRIPTION, this.description, this.description = description);
109 }
110
111 @Nullable
112 public String getIcon() {
113 return icon;
114 }
115
116 public void setIcon(@Nullable String icon) {
117 firePropertyChange(KEY_ICON, this.icon, this.icon = icon);
118 }
119
120 @Nonnull
121 public Object getToolkitAction() {
122 return toolkitAction;
123 }
124
125 protected void doExecute(Object... args) {
126 ActionEvent event = null;
127 if (args != null && args.length == 1 && args[0] instanceof ActionEvent) {
128 event = (ActionEvent) args[0];
129 }
130 toolkitAction.onActionProperty().get().handle(event);
131 }
132
133 @Override
134 protected void doInitialize() {
135 toolkitAction.setName(getName());
136 toolkitAction.setDescription(getDescription());
137 toolkitAction.setEnabled(isEnabled());
138 toolkitAction.setSelected(isSelected());
139 String accelerator = getAccelerator();
140 if (!isBlank(accelerator)) toolkitAction.setAccelerator(accelerator);
141 String icon = getIcon();
142 if (!isBlank(icon)) toolkitAction.setIcon(icon);
143 }
144 }
|