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