TableFactory.groovy
01 /*
02  * Copyright 2008-2016 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.Component
19 import com.googlecode.lanterna.gui.Window
20 import com.googlecode.lanterna.gui.component.Table
21 
22 /**
23  @author Andres Almiray
24  */
25 class TableFactory extends ComponentFactory {
26     TableFactory() {
27         super(Table, false)
28     }
29 
30     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
31         def title = attributes.remove('title')
32         if (title == null && value instanceof CharSequencetitle = value
33         int cols = ((attributes.remove('columns') ?: attributes.remove('cols')) ?: 1as int
34 
35         builder.context.cols = cols
36         builder.context.row = []
37 
38         new Table(cols, title)
39     }
40 
41     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
42         if (!(child instanceof Component|| (child instanceof Window)) {
43             return
44         }
45 
46         builder.parentContext.row << child
47 
48         if (builder.parentContext.row.size() == builder.parentContext.cols) {
49             def components = []
50             components.addAll(builder.parentContext.row)
51             builder.parentContext.row.clear()
52             parent.addRow(* components)
53         }
54     }
55 
56     void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
57         if (builder.context.row) {
58             def components = []
59             components.addAll(builder.context.row)
60             builder.context.row.clear()
61             node.addRow(* components)
62         }
63     }
64 }