01 /*
02 * Copyright 2008-2015 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.codehaus.griffon.runtime.pivot.controller;
17
18 import griffon.core.RunnableWithArgs;
19 import griffon.core.artifact.GriffonController;
20 import griffon.core.controller.ActionManager;
21 import griffon.core.threading.UIThreadManager;
22 import griffon.pivot.support.PivotAction;
23 import org.apache.pivot.wtk.Component;
24 import org.codehaus.griffon.runtime.core.controller.AbstractAction;
25
26 import javax.annotation.Nonnull;
27 import javax.annotation.Nullable;
28 import java.beans.PropertyChangeEvent;
29 import java.beans.PropertyChangeListener;
30
31 import static java.util.Objects.requireNonNull;
32
33 /**
34 * @author Andres Almiray
35 * @since 2.0.0
36 */
37 public class PivotGriffonControllerAction extends AbstractAction {
38 public static final String KEY_DESCRIPTION = "description";
39 private final PivotAction toolkitAction;
40 private String description;
41
42 public PivotGriffonControllerAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
43 super(actionManager, controller, actionName);
44 requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
45
46 toolkitAction = new PivotAction(new RunnableWithArgs() {
47 public void run(@Nullable Object... args) {
48 actionManager.invokeAction(controller, actionName, args);
49 }
50 });
51
52 addPropertyChangeListener(new PropertyChangeListener() {
53 public void propertyChange(final PropertyChangeEvent evt) {
54 uiThreadManager.runInsideUIAsync(new Runnable() {
55 public void run() {
56 if (KEY_NAME.equals(evt.getPropertyName())) {
57 toolkitAction.setName((String) evt.getNewValue());
58 } else if (KEY_DESCRIPTION.equals(evt.getPropertyName())) {
59 toolkitAction.setDescription((String) evt.getNewValue());
60 } else if (KEY_ENABLED.equals(evt.getPropertyName())) {
61 toolkitAction.setEnabled((Boolean) evt.getNewValue());
62 }
63 }
64 });
65 }
66 });
67 }
68
69 protected void doInitialize() {
70 toolkitAction.setName(getName());
71 toolkitAction.setDescription(getDescription());
72 toolkitAction.setEnabled(isEnabled());
73 }
74
75 @Nullable
76 public String getDescription() {
77 return description;
78 }
79
80 public void setDescription(@Nullable String description) {
81 firePropertyChange(KEY_DESCRIPTION, this.description, this.description = description);
82 }
83
84 @Nonnull
85 public Object getToolkitAction() {
86 return toolkitAction;
87 }
88
89 protected void doExecute(Object... args) {
90 Component component = null;
91 if (args != null && args.length == 1 && args[0] instanceof Component) {
92 component = (Component) args[0];
93 }
94 toolkitAction.perform(component);
95 }
96 }
|