PivotGriffonControllerAction.java
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.CallableWithArgs;
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 
40     private String description;
41     private final PivotAction toolkitAction;
42 
43     public PivotGriffonControllerAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
44         super(actionManager, controller, actionName);
45         requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
46 
47         toolkitAction = new PivotAction(new CallableWithArgs<Void>() {
48             @Nullable
49             public Void call(@Nullable Object... args) {
50                 actionManager.invokeAction(controller, actionName, args);
51                 return null;
52             }
53         });
54 
55         addPropertyChangeListener(new PropertyChangeListener() {
56             public void propertyChange(final PropertyChangeEvent evt) {
57                 uiThreadManager.runInsideUIAsync(new Runnable() {
58                     public void run() {
59                         if (KEY_NAME.equals(evt.getPropertyName())) {
60                             toolkitAction.setName((Stringevt.getNewValue());
61                         else if (KEY_DESCRIPTION.equals(evt.getPropertyName())) {
62                             toolkitAction.setDescription((Stringevt.getNewValue());
63                         else if (KEY_ENABLED.equals(evt.getPropertyName())) {
64                             toolkitAction.setEnabled((Booleanevt.getNewValue());
65                         }
66                     }
67                 });
68             }
69         });
70     }
71 
72     protected void doInitialize() {
73         toolkitAction.setName(getName());
74         toolkitAction.setDescription(getDescription());
75         toolkitAction.setEnabled(isEnabled());
76     }
77 
78     @Nullable
79     public String getDescription() {
80         return description;
81     }
82 
83     public void setDescription(@Nullable String description) {
84         firePropertyChange(KEY_DESCRIPTION, this.description, this.description = description);
85     }
86 
87     @Nonnull
88     public Object getToolkitAction() {
89         return toolkitAction;
90     }
91 
92     protected void doExecute(Object... args) {
93         Component component = null;
94         if (args != null && args.length == && args[0instanceof Component) {
95             component = (Componentargs[0];
96         }
97         toolkitAction.perform(component);
98     }
99 }