001 /*
002 * Copyright 2008-2014 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 @Override
064 public boolean isEnabled() {
065 return enabled;
066 }
067
068 @Override
069 public void setEnabled(boolean enabled) {
070 firePropertyChange(KEY_ENABLED, this.enabled, this.enabled = enabled);
071 }
072
073 @Nullable
074 public String getName() {
075 return name;
076 }
077
078 @Override
079 public void setName(@Nullable String name) {
080 firePropertyChange(KEY_NAME, this.name, this.name = name);
081 }
082
083 @Override
084 public final void execute(Object... args) {
085 if (isEnabled()) {
086 doExecute(args);
087 }
088 }
089
090 protected abstract void doExecute(Object... args);
091
092 public final void initialize() {
093 synchronized (lock) {
094 if (initialized) return;
095 doInitialize();
096 initialized = true;
097 }
098 }
099
100 protected abstract void doInitialize();
101 }
|