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