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