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