|
1
|
|
/*
|
|
2
|
|
* Angkor Web Framework
|
|
3
|
|
*
|
|
4
|
|
* Distributable under LGPL license.
|
|
5
|
|
* See terms of license at gnu.org.
|
|
6
|
|
*/
|
|
7
|
|
package com.tirsen.angkor.beans;
|
|
8
|
|
|
|
9
|
|
import com.tirsen.angkor.event.ChangeListener;
|
|
10
|
|
import com.tirsen.angkor.event.ChangeSource;
|
|
11
|
|
import com.tirsen.angkor.event.ChangeSourceHelper;
|
|
12
|
|
import com.tirsen.angkor.widget.ValueModel;
|
|
13
|
|
|
|
14
|
|
import java.io.Serializable;
|
|
15
|
|
import java.util.StringTokenizer;
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
/**
|
|
19
|
|
* A BeanModel acts as the proxy for JavaBean instances which may or may not actually
|
|
20
|
|
* exist enabling them to be lazily evaluated only when a view actually requests the value
|
|
21
|
|
* of the JavaBean. Useful when creating views which should be bound to properties of JavaBeans
|
|
22
|
|
* but the view is not yet visible and the JavaBean is not yet instantiated. Also has a lot of
|
|
23
|
|
* utility methods for creating various models bound to the JavaBean so it may be useful even if
|
|
24
|
|
* the JavaBean actually exists. For this reason the name <code>BeanModel</code> may be a little bit
|
|
25
|
|
* inaccurate, maybe <code>BeanModel</code> may be better.
|
|
26
|
|
*
|
|
27
|
|
* <!-- $Id: BeanModel.java,v 1.2 2002/10/09 21:37:37 tirsen Exp $ -->
|
|
28
|
|
*
|
|
29
|
|
* @author $Author: tirsen $
|
|
30
|
|
* @version $Revision: 1.2 $
|
|
31
|
|
*/
|
|
32
|
|
public class BeanModel implements Serializable, ChangeSource
|
|
33
|
|
{
|
|
34
|
|
private Object valueObject;
|
|
35
|
|
private ValueModel model;
|
|
36
|
|
private Class objectClass;
|
|
37
|
|
private ChangeSourceHelper changeSourceHelper = new ChangeSourceHelper(this);
|
|
38
|
|
|
|
39
|
6
|
public BeanModel(Class objectClass)
|
|
40
|
|
{
|
|
41
|
6
|
this.objectClass = objectClass;
|
|
42
|
|
}
|
|
43
|
|
|
|
44
|
3
|
public BeanModel(ValueModel model)
|
|
45
|
|
{
|
|
46
|
3
|
this.model = model;
|
|
47
|
|
}
|
|
48
|
|
|
|
49
|
3
|
public BeanModel(Object valueObject)
|
|
50
|
|
{
|
|
51
|
3
|
setBean(valueObject);
|
|
52
|
|
}
|
|
53
|
|
|
|
54
|
9
|
public void setBean(Object valueObject)
|
|
55
|
|
{
|
|
56
|
9
|
this.valueObject = valueObject;
|
|
57
|
9
|
changeSourceHelper.signalChangeEvent();
|
|
58
|
|
}
|
|
59
|
|
|
|
60
|
112
|
public Object getBean()
|
|
61
|
|
{
|
|
62
|
112
|
Object object = valueObject;
|
|
63
|
6
|
if (object == null && model != null) object = model.getValue();
|
|
64
|
112
|
return object;
|
|
65
|
|
}
|
|
66
|
|
|
|
67
|
12
|
public Class getObjectClass()
|
|
68
|
|
{
|
|
69
|
12
|
Class klass = objectClass;
|
|
70
|
3
|
if (klass == null && valueObject != null) klass = valueObject.getClass();
|
|
71
|
3
|
if (klass == null && model != null) klass = model.getValueClass();
|
|
72
|
12
|
return klass;
|
|
73
|
|
}
|
|
74
|
|
|
|
75
|
1
|
public BeanTableModel getTable(String expression)
|
|
76
|
|
{
|
|
77
|
1
|
return getTable(expression, null);
|
|
78
|
|
}
|
|
79
|
|
|
|
80
|
6
|
public BeanTableModel getTable(String expression, Class propertyClass)
|
|
81
|
|
{
|
|
82
|
6
|
PropertySpec spec = parseExpression(expression);
|
|
83
|
6
|
return new PropertyTableModel(spec.getBean(), spec.getProperty(), propertyClass);
|
|
84
|
|
}
|
|
85
|
|
|
|
86
|
|
private static class PropertySpec
|
|
87
|
|
{
|
|
88
|
|
private BeanModel bean;
|
|
89
|
|
private String property;
|
|
90
|
|
|
|
91
|
20
|
public PropertySpec(BeanModel bean, String property)
|
|
92
|
|
{
|
|
93
|
20
|
this.bean = bean;
|
|
94
|
20
|
this.property = property;
|
|
95
|
|
}
|
|
96
|
|
|
|
97
|
10
|
public BeanModel getBean()
|
|
98
|
|
{
|
|
99
|
10
|
return bean;
|
|
100
|
|
}
|
|
101
|
|
|
|
102
|
10
|
public String getProperty()
|
|
103
|
|
{
|
|
104
|
10
|
return property;
|
|
105
|
|
}
|
|
106
|
|
}
|
|
107
|
|
|
|
108
|
3
|
public ValueModel getValue(String expression)
|
|
109
|
|
{
|
|
110
|
3
|
PropertySpec parsed = parseExpression(expression);
|
|
111
|
3
|
return new PropertyValueModel(parsed.getBean(), parsed.getProperty());
|
|
112
|
|
}
|
|
113
|
|
|
|
114
|
10
|
public PropertySpec parseExpression(String expression)
|
|
115
|
|
{
|
|
116
|
10
|
StringTokenizer tokens = new StringTokenizer(expression, ".", false);
|
|
117
|
10
|
BeanModel current = this;
|
|
118
|
10
|
PropertySpec spec = new PropertySpec(this, expression);
|
|
119
|
10
|
while (tokens.hasMoreElements())
|
|
120
|
|
{
|
|
121
|
12
|
String property = tokens.nextToken();
|
|
122
|
12
|
if (tokens.hasMoreElements())
|
|
123
|
2
|
current = current.getPropertyBean(property);
|
|
124
|
|
else
|
|
125
|
10
|
spec = new PropertySpec(current, property);
|
|
126
|
|
}
|
|
127
|
10
|
return spec;
|
|
128
|
|
}
|
|
129
|
|
|
|
130
|
3
|
private BeanModel getPropertyBean(String property)
|
|
131
|
|
{
|
|
132
|
3
|
return new BeanModel(new PropertyValueModel(this, property));
|
|
133
|
|
}
|
|
134
|
|
|
|
135
|
1
|
public BeanModel getBean(String expression)
|
|
136
|
|
{
|
|
137
|
1
|
PropertySpec parsed = parseExpression(expression);
|
|
138
|
1
|
return parsed.getBean().getPropertyBean(parsed.getProperty());
|
|
139
|
|
}
|
|
140
|
|
|
|
141
|
0
|
public void addChangeListener(ChangeListener listener)
|
|
142
|
|
{
|
|
143
|
0
|
changeSourceHelper.addChangeListener(listener);
|
|
144
|
|
}
|
|
145
|
|
|
|
146
|
0
|
public void removeChangeListener(ChangeListener listener)
|
|
147
|
|
{
|
|
148
|
0
|
changeSourceHelper.removeChangeListener(listener);
|
|
149
|
|
}
|
|
150
|
|
}
|
|
151
|
|
|