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