DefaultAction.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.codehaus.griffon.runtime.core.controller;
19 
20 import griffon.core.artifact.GriffonController;
21 import griffon.core.controller.ActionManager;
22 import griffon.core.controller.ActionMetadata;
23 import griffon.core.threading.UIThreadManager;
24 
25 import javax.annotation.Nonnull;
26 import java.beans.PropertyChangeEvent;
27 import java.beans.PropertyChangeListener;
28 
29 import static java.util.Objects.requireNonNull;
30 
31 /**
32  @author Andres Almiray
33  @since 2.0.0
34  */
35 public class DefaultAction extends AbstractAction {
36     private final DefaultToolkitAction toolkitAction;
37 
38     public DefaultAction(@Nonnull final UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final ActionMetadata actionMetadata) {
39         super(actionManager, controller, actionMetadata);
40         requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
41 
42         toolkitAction = new DefaultToolkitAction(new Runnable() {
43             @Override
44             public void run() {
45                 actionManager.invokeAction(controller, actionMetadata.getActionName());
46             }
47         });
48         addPropertyChangeListener(new PropertyChangeListener() {
49             public void propertyChange(final PropertyChangeEvent evt) {
50                 uiThreadManager.runInsideUIAsync(new Runnable() {
51                     public void run() {
52                         toolkitAction.setName(String.valueOf(evt.getNewValue()));
53                     }
54                 });
55             }
56         });
57     }
58 
59     @Nonnull
60     @Override
61     public Object getToolkitAction() {
62         return toolkitAction;
63     }
64 
65     @Override
66     protected void doExecute(Object... args) {
67         toolkitAction.execute();
68     }
69 
70     @Override
71     protected void doInitialize() {
72         toolkitAction.setName(getName());
73     }
74 }