LanternaGriffonControllerAction.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.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 = new LanternaAction(new Runnable() {
41             @Override
42             public void run() {
43                 actionManager.invokeAction(controller, actionName);
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     public Object getToolkitAction() {
59         return toolkitAction;
60     }
61 
62     protected void doExecute(Object... args) {
63         toolkitAction.doAction();
64     }
65 
66     @Override
67     protected void doInitialize() {
68         toolkitAction.setName(getName());
69     }
70 }