DefaultAction.java
01 /*
02  * Copyright 2008-2014 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.core.controller;
17 
18 import griffon.core.artifact.GriffonController;
19 import griffon.core.controller.ActionManager;
20 import griffon.core.threading.UIThreadManager;
21 
22 import javax.annotation.Nonnull;
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25 
26 import static java.util.Objects.requireNonNull;
27 
28 /**
29  @author Andres Almiray
30  @since 2.0.0
31  */
32 public class DefaultAction extends AbstractAction {
33     private final DefaultToolkitAction toolkitAction;
34 
35     public DefaultAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
36         super(actionManager, controller, actionName);
37         requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
38 
39         toolkitAction = new DefaultToolkitAction(new Runnable() {
40             @Override
41             public void run() {
42                 actionManager.invokeAction(controller, actionName);
43             }
44         });
45         addPropertyChangeListener(new PropertyChangeListener() {
46             public void propertyChange(final PropertyChangeEvent evt) {
47                 uiThreadManager.runInsideUIAsync(new Runnable() {
48                     public void run() {
49                         toolkitAction.setName(String.valueOf(evt.getNewValue()));
50                     }
51                 });
52             }
53         });
54     }
55 
56     @Nonnull
57     public Object getToolkitAction() {
58         return toolkitAction;
59     }
60 
61     protected void doExecute(Object... args) {
62         toolkitAction.execute();
63     }
64 
65     @Override
66     protected void doInitialize() {
67         toolkitAction.setName(getName());
68     }
69 }