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