PivotGriffonControllerAction.java
001 /*
002  * Copyright 2008-2016 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.threading.UIThreadManager;
022 import griffon.pivot.support.PivotAction;
023 import org.apache.pivot.wtk.Component;
024 import org.codehaus.griffon.runtime.core.controller.AbstractAction;
025 
026 import javax.annotation.Nonnull;
027 import javax.annotation.Nullable;
028 import java.beans.PropertyChangeEvent;
029 import java.beans.PropertyChangeListener;
030 
031 import static java.util.Objects.requireNonNull;
032 
033 /**
034  @author Andres Almiray
035  @since 2.0.0
036  */
037 public class PivotGriffonControllerAction extends AbstractAction {
038     public static final String KEY_DESCRIPTION = "description";
039     private final PivotAction toolkitAction;
040     private String description;
041 
042     public PivotGriffonControllerAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
043         super(actionManager, controller, actionName);
044         requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
045 
046         toolkitAction = createAction(actionManager, controller, actionName);
047 
048         addPropertyChangeListener(new PropertyChangeListener() {
049             public void propertyChange(final PropertyChangeEvent evt) {
050                 uiThreadManager.runInsideUIAsync(new Runnable() {
051                     public void run() {
052                         handlePropertyChange(evt);
053                     }
054                 });
055             }
056         });
057     }
058 
059     @Nonnull
060     protected PivotAction createAction(final @Nonnull ActionManager actionManager, final @Nonnull GriffonController controller, final @Nonnull String actionName) {
061         return new PivotAction(new RunnableWithArgs() {
062             public void run(@Nullable Object... args) {
063                 actionManager.invokeAction(controller, actionName, args);
064             }
065         });
066     }
067 
068     protected void handlePropertyChange(@Nonnull PropertyChangeEvent evt) {
069         if (KEY_NAME.equals(evt.getPropertyName())) {
070             toolkitAction.setName((Stringevt.getNewValue());
071         else if (KEY_DESCRIPTION.equals(evt.getPropertyName())) {
072             toolkitAction.setDescription((Stringevt.getNewValue());
073         else if (KEY_ENABLED.equals(evt.getPropertyName())) {
074             toolkitAction.setEnabled((Booleanevt.getNewValue());
075         }
076     }
077 
078     protected void doInitialize() {
079         toolkitAction.setName(getName());
080         toolkitAction.setDescription(getDescription());
081         toolkitAction.setEnabled(isEnabled());
082     }
083 
084     @Nullable
085     public String getDescription() {
086         return description;
087     }
088 
089     public void setDescription(@Nullable String description) {
090         firePropertyChange(KEY_DESCRIPTION, this.description, this.description = description);
091     }
092 
093     @Nonnull
094     public Object getToolkitAction() {
095         return toolkitAction;
096     }
097 
098     protected void doExecute(Object... args) {
099         Component component = null;
100         if (args != null && args.length == && args[0instanceof Component) {
101             component = (Componentargs[0];
102         }
103         toolkitAction.perform(component);
104     }
105 }