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