PivotGriffonControllerAction.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package org.codehaus.griffon.runtime.pivot.controller;
019 
020 import griffon.core.RunnableWithArgs;
021 import griffon.core.artifact.GriffonController;
022 import griffon.core.controller.ActionManager;
023 import griffon.core.controller.ActionMetadata;
024 import griffon.core.threading.UIThreadManager;
025 import griffon.pivot.support.PivotAction;
026 import org.apache.pivot.wtk.Component;
027 import org.codehaus.griffon.runtime.core.controller.AbstractAction;
028 
029 import javax.annotation.Nonnull;
030 import javax.annotation.Nullable;
031 import java.beans.PropertyChangeEvent;
032 import java.beans.PropertyChangeListener;
033 
034 import static java.util.Objects.requireNonNull;
035 
036 /**
037  @author Andres Almiray
038  @since 2.0.0
039  */
040 public class PivotGriffonControllerAction extends AbstractAction {
041     public static final String KEY_DESCRIPTION = "description";
042     private final PivotAction toolkitAction;
043     private String description;
044 
045     public PivotGriffonControllerAction(@Nonnull final UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final ActionMetadata actionMetadata) {
046         super(actionManager, controller, actionMetadata);
047         requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
048 
049         toolkitAction = createAction(actionManager, controller, actionMetadata.getActionName());
050 
051         addPropertyChangeListener(new PropertyChangeListener() {
052             public void propertyChange(final PropertyChangeEvent evt) {
053                 uiThreadManager.runInsideUIAsync(new Runnable() {
054                     public void run() {
055                         handlePropertyChange(evt);
056                     }
057                 });
058             }
059         });
060     }
061 
062     @Nonnull
063     protected PivotAction createAction(@Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
064         return new PivotAction(new RunnableWithArgs() {
065             public void run(@Nullable Object... args) {
066                 actionManager.invokeAction(controller, actionName, args);
067             }
068         });
069     }
070 
071     protected void handlePropertyChange(@Nonnull PropertyChangeEvent evt) {
072         if (KEY_NAME.equals(evt.getPropertyName())) {
073             toolkitAction.setName((Stringevt.getNewValue());
074         else if (KEY_DESCRIPTION.equals(evt.getPropertyName())) {
075             toolkitAction.setDescription((Stringevt.getNewValue());
076         else if (KEY_ENABLED.equals(evt.getPropertyName())) {
077             toolkitAction.setEnabled((Booleanevt.getNewValue());
078         }
079     }
080 
081     protected void doInitialize() {
082         toolkitAction.setName(getName());
083         toolkitAction.setDescription(getDescription());
084         toolkitAction.setEnabled(isEnabled());
085     }
086 
087     @Nullable
088     public String getDescription() {
089         return description;
090     }
091 
092     public void setDescription(@Nullable String description) {
093         firePropertyChange(KEY_DESCRIPTION, this.description, this.description = description);
094     }
095 
096     @Nonnull
097     public Object getToolkitAction() {
098         return toolkitAction;
099     }
100 
101     @Override
102     protected void doExecute(Object... args) {
103         Component component = null;
104         if (args != null && args.length == && args[0instanceof Component) {
105             component = (Componentargs[0];
106         }
107         toolkitAction.perform(component);
108     }
109 }