KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > swing > TableBindingBuilder


1 package org.objectstyle.cayenne.swing;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import javax.swing.JTable JavaDoc;
7
8 /**
9  * A builder for a JTable binding.
10  *
11  * @author Andrei Adamchik
12  */

13 public class TableBindingBuilder {
14
15     protected BindingBuilder helper;
16     protected List JavaDoc columns;
17
18     public TableBindingBuilder(BindingFactory factory, Object JavaDoc context) {
19         this(new BindingBuilder(factory, context));
20     }
21
22     public TableBindingBuilder(BindingBuilder helper) {
23         this.helper = helper;
24     }
25
26     /**
27      * Creates a binding using preconfigured parameters.
28      */

29     public ObjectBinding bindToTable(JTable JavaDoc table, String JavaDoc listBinding) {
30         int width = (columns != null) ? columns.size() : 0;
31
32         String JavaDoc[] headers = new String JavaDoc[width];
33         BindingExpression[] expressions = new BindingExpression[width];
34         Class JavaDoc[] classes = new Class JavaDoc[width];
35         boolean[] editableState = new boolean[width];
36
37         for (int i = 0; i < width; i++) {
38             ColumnDescriptor descriptor = (ColumnDescriptor) columns.get(i);
39             headers[i] = descriptor.header;
40             expressions[i] = descriptor.expression;
41             classes[i] = descriptor.columnClass;
42             editableState[i] = descriptor.editable;
43         }
44
45         ObjectBinding binding = helper.getFactory().bindToTable(
46                 table,
47                 listBinding,
48                 headers,
49                 expressions,
50                 classes,
51                 editableState);
52         return helper.initBinding(binding, helper.getDelegate());
53     }
54
55     /**
56      * Adds a column to the table description.
57      */

58     public void addColumn(
59             String JavaDoc header,
60             String JavaDoc expression,
61             Class JavaDoc columnClass,
62             boolean editable) {
63         if (columns == null) {
64             columns = new ArrayList JavaDoc();
65         }
66
67         columns.add(new ColumnDescriptor(
68                 header,
69                 new BindingExpression(expression),
70                 columnClass,
71                 editable));
72     }
73
74     final class ColumnDescriptor {
75
76         String JavaDoc header;
77         BindingExpression expression;
78         boolean editable;
79         Class JavaDoc columnClass;
80
81         ColumnDescriptor(String JavaDoc header, BindingExpression expression, Class JavaDoc columnClass,
82                 boolean editable) {
83             this.header = header;
84             this.expression = expression;
85             this.editable = editable;
86             this.columnClass = columnClass;
87         }
88     }
89 }
Popular Tags