1
|
|
/*
|
2
|
|
* Angkor Web Framework
|
3
|
|
*
|
4
|
|
* Distributable under LGPL license.
|
5
|
|
* See terms of license at gnu.org.
|
6
|
|
*/
|
7
|
|
|
8
|
|
package com.tirsen.angkor;
|
9
|
|
|
10
|
|
import com.tirsen.angkor.Application;
|
11
|
|
import com.tirsen.angkor.Debug;
|
12
|
|
import com.tirsen.angkor.RenderContext;
|
13
|
|
import com.tirsen.angkor.View;
|
14
|
|
import com.tirsen.angkor.widget.Container;
|
15
|
|
import org.apache.log4j.Category;
|
16
|
|
|
17
|
|
import java.io.IOException;
|
18
|
|
import java.io.Serializable;
|
19
|
|
import java.util.HashMap;
|
20
|
|
import java.util.Iterator;
|
21
|
|
import java.util.Map;
|
22
|
|
|
23
|
|
/**
|
24
|
|
* The default implementation of the View interface, implements a set of functionality
|
25
|
|
* usable by most widgets in Angkor.
|
26
|
|
*
|
27
|
|
* These needs to be serializable to support being serialized in the session while
|
28
|
|
* being referenced as parsing components.
|
29
|
|
*
|
30
|
|
* <!-- $Id: Component.java,v 1.4 2002/10/13 19:59:22 tirsen Exp $ -->
|
31
|
|
*
|
32
|
|
* @author $Author: tirsen $
|
33
|
|
* @version $Revision: 1.4 $
|
34
|
|
*/
|
35
|
|
public abstract class Component implements View, Serializable
|
36
|
|
{
|
37
|
|
private static final Category logger = Category.getInstance(Debug.LOGGER_NAME);
|
38
|
|
|
39
|
|
private View container;
|
40
|
|
private String id;
|
41
|
|
private boolean visible = true;
|
42
|
|
private Map attributes;
|
43
|
|
|
44
|
|
private boolean debugTables = false;
|
45
|
|
|
46
|
|
/**
|
47
|
|
* Render with specified ID. The ID is checked for uniqueness and an error
|
48
|
|
* will occur at render-time if it is not unique.
|
49
|
|
* This is id is useful when doing functional testing.
|
50
|
|
*
|
51
|
|
*/
|
52
|
0
|
public Component(String id)
|
53
|
|
{
|
54
|
0
|
this.id = id;
|
55
|
|
}
|
56
|
|
|
57
|
|
/**
|
58
|
|
* Create with generated ID.
|
59
|
|
*/
|
60
|
44
|
public Component()
|
61
|
|
{
|
62
|
44
|
this.id = null;
|
63
|
|
}
|
64
|
|
|
65
|
4
|
public View getContainer()
|
66
|
|
{
|
67
|
4
|
return container;
|
68
|
|
}
|
69
|
|
|
70
|
23
|
public void setContainer(View container)
|
71
|
|
{
|
72
|
23
|
if (container != null && this.container != null && this.container != container)
|
73
|
|
{
|
74
|
0
|
throw new IllegalStateException("Can not add an element to more than one container.");
|
75
|
|
}
|
76
|
23
|
this.container = container;
|
77
|
|
}
|
78
|
|
|
79
|
|
public abstract void render(RenderContext context) throws IOException;
|
80
|
|
|
81
|
|
/**
|
82
|
|
* Set an attribute which can be used by for example event-listeners to acquire more information
|
83
|
|
* about the event.
|
84
|
|
* For example a button or a link in a table could have an attribute for which object-id of the
|
85
|
|
* row that was clicked.
|
86
|
|
*/
|
87
|
0
|
public void setAttribute(String name, Object value)
|
88
|
|
{
|
89
|
0
|
if (attributes == null) attributes = new HashMap();
|
90
|
0
|
attributes.put(name, value);
|
91
|
|
}
|
92
|
|
|
93
|
0
|
public Object getAttribute(String name)
|
94
|
|
{
|
95
|
0
|
if (attributes == null) return null;
|
96
|
0
|
return attributes.get(name);
|
97
|
|
}
|
98
|
|
|
99
|
|
/**
|
100
|
|
* @deprecated use {@link #getId()} instead.
|
101
|
|
* @param context
|
102
|
|
* @return
|
103
|
|
*/
|
104
|
35
|
public String uniqueId(RenderContext context)
|
105
|
|
{
|
106
|
35
|
return getId();
|
107
|
|
}
|
108
|
|
|
109
|
|
/**
|
110
|
|
* Sets a custom id to be used instead of a generated one.
|
111
|
|
* The custom id is checked for uniqueness at each use.
|
112
|
|
* Warning! This should really only be used when initializing the component, if the component
|
113
|
|
* has been registered for parsing it will
|
114
|
|
*/
|
115
|
7
|
public void setId(String id)
|
116
|
|
{
|
117
|
7
|
this.id = id;
|
118
|
|
}
|
119
|
|
|
120
|
|
/**
|
121
|
|
* Forces this component to allocate a unique id.
|
122
|
|
*/
|
123
|
0
|
public void allocateUniqueId(RenderContext context)
|
124
|
|
{
|
125
|
0
|
uniqueId(context);
|
126
|
|
}
|
127
|
|
|
128
|
60
|
public String getId()
|
129
|
|
{
|
130
|
60
|
if (id == null)
|
131
|
|
{
|
132
|
10
|
id = Application.getApplication().nextUniqueID();
|
133
|
|
// checks and allocates id.
|
134
|
10
|
Application.getApplication().allocateUniqueID(id, this);
|
135
|
|
}
|
136
|
60
|
return id;
|
137
|
|
}
|
138
|
|
|
139
|
0
|
public void setVisible(boolean visible)
|
140
|
|
{
|
141
|
0
|
this.visible = visible;
|
142
|
|
}
|
143
|
|
|
144
|
36
|
public boolean isVisible()
|
145
|
|
{
|
146
|
36
|
return visible;
|
147
|
|
}
|
148
|
|
|
149
|
0
|
public boolean isParsing()
|
150
|
|
{
|
151
|
0
|
return false;
|
152
|
|
}
|
153
|
|
|
154
|
9
|
public void parse(RenderContext context)
|
155
|
|
{
|
156
|
9
|
debugTables = context.getRequestParameter("debugTables") != null;
|
157
|
|
}
|
158
|
|
|
159
|
|
/**
|
160
|
|
* Returns which index this has of all visible elements of this container.
|
161
|
|
*/
|
162
|
0
|
public int getVisibleIndex()
|
163
|
|
{
|
164
|
0
|
Iterator it = ((Container) getContainer()).iterateAllElements();
|
165
|
0
|
int index = -1;
|
166
|
0
|
while (it.hasNext())
|
167
|
|
{
|
168
|
0
|
View view = (View) it.next();
|
169
|
0
|
if (view.isVisible()) index++;
|
170
|
0
|
if (view == this) break;
|
171
|
|
}
|
172
|
0
|
return index;
|
173
|
|
}
|
174
|
|
|
175
|
0
|
public boolean isDebugTables()
|
176
|
|
{
|
177
|
0
|
return debugTables;
|
178
|
|
}
|
179
|
|
|
180
|
1
|
protected RenderContext getRenderContext()
|
181
|
|
{
|
182
|
1
|
return RenderContext.getRenderContext();
|
183
|
|
}
|
184
|
|
}
|
185
|
|
|