AbstractAction.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.core.controller;
017 
018 import griffon.core.artifact.GriffonController;
019 import griffon.core.controller.Action;
020 import griffon.core.controller.ActionManager;
021 import org.codehaus.griffon.runtime.core.AbstractObservable;
022 
023 import javax.annotation.Nonnull;
024 import javax.annotation.Nullable;
025 
026 import static griffon.util.GriffonNameUtils.requireNonBlank;
027 import static java.util.Objects.requireNonNull;
028 
029 /**
030  @author Andres Almiray
031  @since 2.0.0
032  */
033 public abstract class AbstractAction extends AbstractObservable implements Action {
034     private String name;
035     private boolean enabled = true;
036     private final ActionManager actionManager;
037     private final GriffonController controller;
038     private final String actionName;
039     private boolean initialized;
040     private final Object lock = new Object[0];
041 
042     public AbstractAction(@Nonnull ActionManager actionManager, @Nonnull GriffonController controller, @Nonnull String actionName) {
043         this.actionManager = requireNonNull(actionManager, "Argument 'actionManager' must not be null");
044         this.controller = requireNonNull(controller, "Argument 'controller' must not be null");
045         this.actionName = requireNonBlank(actionName, "Argument 'actionName' must not be blank");
046     }
047 
048     @Nonnull
049     public ActionManager getActionManager() {
050         return actionManager;
051     }
052 
053     @Nonnull
054     public GriffonController getController() {
055         return controller;
056     }
057 
058     @Nonnull
059     public String getActionName() {
060         return actionName;
061     }
062 
063     @Nonnull
064     @Override
065     public String getFullyQualifiedName() {
066         return getController().getClass().getName() "." + getActionName();
067     }
068 
069     @Override
070     public boolean isEnabled() {
071         return enabled;
072     }
073 
074     @Override
075     public void setEnabled(boolean enabled) {
076         firePropertyChange(KEY_ENABLED, this.enabled, this.enabled = enabled);
077     }
078 
079     @Nullable
080     public String getName() {
081         return name;
082     }
083 
084     @Override
085     public void setName(@Nullable String name) {
086         firePropertyChange(KEY_NAME, this.name, this.name = name);
087     }
088 
089     @Override
090     public final void execute(Object... args) {
091         if (isEnabled()) {
092             doExecute(args);
093         }
094     }
095 
096     protected abstract void doExecute(Object... args);
097 
098     public final void initialize() {
099         synchronized (lock) {
100             if (initializedreturn;
101             doInitialize();
102             initialized = true;
103         }
104     }
105 
106     protected abstract void doInitialize();
107 }