PivotAction.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.pivot.support;
19 
20 import griffon.core.CallableWithArgs;
21 import griffon.core.RunnableWithArgs;
22 import org.apache.pivot.wtk.Action;
23 import org.apache.pivot.wtk.Component;
24 
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 
28 import static java.util.Objects.requireNonNull;
29 
30 /**
31  @author Andres Almiray
32  */
33 public class PivotAction extends Action {
34     private static final String ERROR_CALLABLE_NULL = "Argument 'callable' must not be null";
35     private static final String ERROR_RUNNABLE_NULL = "Argument 'runnable' must not be null";
36     private String name;
37     private String description;
38     private RunnableWithArgs runnable;
39 
40     public PivotAction() {
41 
42     }
43 
44     public PivotAction(@Nonnull RunnableWithArgs runnable) {
45         this.runnable = requireNonNull(runnable, ERROR_RUNNABLE_NULL);
46     }
47 
48     @Deprecated
49     public PivotAction(@Nonnull CallableWithArgs<Void> callable) {
50         setCallable(callable);
51     }
52 
53     public String getName() {
54         return name;
55     }
56 
57     public void setName(String name) {
58         this.name = name;
59     }
60 
61     @Override
62     public String getDescription() {
63         return description;
64     }
65 
66     public void setDescription(String description) {
67         this.description = description;
68     }
69 
70     @Deprecated
71     public void setCallable(@Nonnull final CallableWithArgs<Void> callable) {
72         requireNonNull(callable, ERROR_CALLABLE_NULL);
73         this.runnable = new RunnableWithArgs() {
74             @Override
75             public void run(@Nullable Object... args) {
76                 callable.call(args);
77             }
78         };
79     }
80 
81     public RunnableWithArgs getRunnable() {
82         return runnable;
83     }
84 
85     public void setRunnable(RunnableWithArgs runnable) {
86         this.runnable = runnable;
87     }
88 
89     public String toString() {
90         return "Action[" + name + ", " + description + "]";
91     }
92 
93     @Override
94     public void perform(Component component) {
95         requireNonNull(runnable, "Argument 'runnable' must not be null for " this);
96         runnable.run(component);
97     }
98 }