LanternaGriffonControllerAction.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.lanterna.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 import griffon.lanterna.support.LanternaAction;
25 import org.codehaus.griffon.runtime.core.controller.AbstractAction;
26 
27 import javax.annotation.Nonnull;
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  */
36 public class LanternaGriffonControllerAction extends AbstractAction {
37     private final LanternaAction toolkitAction;
38 
39     public LanternaGriffonControllerAction(@Nonnull final UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final ActionMetadata actionMetadata) {
40         super(actionManager, controller, actionMetadata);
41         requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
42 
43         toolkitAction = createAction(actionManager, controller, actionMetadata.getActionName());
44         addPropertyChangeListener(new PropertyChangeListener() {
45             public void propertyChange(final PropertyChangeEvent evt) {
46                 uiThreadManager.runInsideUIAsync(new Runnable() {
47                     public void run() {
48                         handlePropertyChange(evt);
49                     }
50                 });
51             }
52         });
53     }
54 
55     @Nonnull
56     protected LanternaAction createAction(@Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
57         return new LanternaAction(new Runnable() {
58             @Override
59             public void run() {
60                 actionManager.invokeAction(controller, actionName);
61             }
62         });
63     }
64 
65     protected void handlePropertyChange(@Nonnull PropertyChangeEvent evt) {
66         toolkitAction.setName(String.valueOf(evt.getNewValue()));
67     }
68 
69     @Nonnull
70     public Object getToolkitAction() {
71         return toolkitAction;
72     }
73 
74     @Override
75     protected void doExecute(Object... args) {
76         toolkitAction.doAction();
77     }
78 
79     @Override
80     protected void doInitialize() {
81         toolkitAction.setName(getName());
82     }
83 }