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 griffon.lanterna.support;
019
020 import com.googlecode.lanterna.gui.Action;
021 import org.codehaus.griffon.runtime.core.AbstractObservable;
022
023 /**
024 * @author Andres Almiray
025 * @since 2.0.0
026 */
027 public class LanternaAction extends AbstractObservable implements Action {
028 public static final String NAME = "name";
029
030 private Runnable runnable;
031 private Action delegate;
032 private String name;
033
034 public enum ResolveStrategy {
035 DELEGATE_FIRST,
036 RUNNABLE_FIRST,
037 DELEGATE_ONLY,
038 RUNNABLE_ONLY
039 }
040
041 private ResolveStrategy resolveStrategy = ResolveStrategy.DELEGATE_FIRST;
042
043 public ResolveStrategy getResolveStrategy() {
044 return resolveStrategy;
045 }
046
047 public void setResolveStrategy(ResolveStrategy resolveStrategy) {
048 this.resolveStrategy = resolveStrategy != null ? resolveStrategy : ResolveStrategy.DELEGATE_FIRST;
049 }
050
051 public LanternaAction() {
052 }
053
054 public LanternaAction(String name) {
055 this.name = name;
056 }
057
058 public LanternaAction(Runnable runnable) {
059 this.runnable = runnable;
060 }
061
062 public LanternaAction(Action delegate) {
063 this.delegate = delegate;
064 }
065
066 public Action getDelegate() {
067 return delegate;
068 }
069
070 public void setDelegate(Action delegate) {
071 this.delegate = delegate;
072 }
073
074 public Runnable getRunnable() {
075 return runnable;
076 }
077
078 public void setRunnable(Runnable runnable) {
079 this.runnable = runnable;
080 }
081
082 public String getName() {
083 return name;
084 }
085
086 public void setName(String name) {
087 pcs.firePropertyChange(NAME, this.name, this.name = name);
088 }
089
090 public String toString() {
091 return name;
092 }
093
094 public void doAction() {
095 switch (resolveStrategy) {
096 case DELEGATE_ONLY:
097 if (delegate != null) {
098 delegate.doAction();
099 }
100 break;
101 case DELEGATE_FIRST:
102 if (delegate != null) {
103 delegate.doAction();
104 } else if (runnable != null) {
105 runnable.run();
106 }
107 break;
108 case RUNNABLE_FIRST:
109 if (runnable != null) {
110 runnable.run();
111 } else if (delegate != null) {
112 delegate.doAction();
113 }
114 break;
115 case RUNNABLE_ONLY:
116 if (runnable != null) {
117 runnable.run();
118 }
119 break;
120 }
121 }
122 }
|