01 /*
02 * Copyright 2008-2015 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.builder.lanterna.factory
17
18 import com.googlecode.lanterna.gui.Action
19 import griffon.lanterna.support.LanternaAction
20 import griffon.lanterna.widgets.MutableButton
21
22 /**
23 * @author Andres Almiray
24 */
25 class ButtonFactory extends ComponentFactory {
26 ButtonFactory() {
27 super(MutableButton, false)
28 }
29
30 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
31 if (value instanceof Action) {
32 return new MutableButton((Action) value)
33 }
34
35 def text = attributes.remove('text')
36 if (text == null && value instanceof CharSequence) text = String.valueOf(value)
37 text = text != null ? text.toString() : ''
38
39 def action = attributes.remove('action')
40 if (action instanceof Runnable || action instanceof Action) {
41 action = new LanternaAction(action)
42 } else if (action != null && !(action instanceof Action)) {
43 throw new IllegalArgumentException("In $name action: is not a Runnable nor an Action")
44 }
45
46 new MutableButton(text, action)
47 }
48
49 boolean isHandlesNodeChildren() {
50 true
51 }
52
53 boolean onNodeChildren(FactoryBuilderSupport builder, Object node, Closure childContent) {
54 node.setAction(childContent)
55 false
56 }
57 }
|