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