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