forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainer.java
More file actions
executable file
·137 lines (134 loc) · 3.54 KB
/
Copy pathContainer.java
File metadata and controls
executable file
·137 lines (134 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package javaforce.webui;
/** Base class for components that can "contain" other components.
*
* @author pquiring
*/
import java.util.*;
public class Container extends Component {
public void setClient(WebUIClient client) {
super.setClient(client);
int cnt = count();
for(int a=0;a<cnt;a++) {
get(a).setClient(client);
}
}
public void init() {
super.init();
int cnt = count();
for(int a=0;a<cnt;a++) {
get(a).init();
}
}
public void events() {
super.events();
int cnt = count();
for(int a=0;a<cnt;a++) {
get(a).events();
}
}
/** Get Component by user assigned name. */
public Component getComponent(String name) {
if (this.name != null && this.name.equals(name)) {
return this;
}
int cnt = count();
for(int a=0;a<cnt;a++) {
Component child = get(a);
if (child instanceof Container) {
Container container = (Container)child;
child = container.getComponent(name);
if (child != null) return child;
} else {
if (child.name != null && child.name.equals(name)) {
return child;
}
}
}
return null;
}
private ArrayList<Component> components = new ArrayList<Component>();
public Component get(int idx) {
return components.get(idx);
}
/** Get Component by id. */
public Component get(String id) {
int cnt = count();
for(int a=0;a<cnt;a++) {
Component comp = get(a);
if (comp.id.equals(id)) return comp;
if (comp instanceof Container) {
Container container = (Container)comp;
comp = container.get(id);
if (comp != null) return comp;
}
}
return null;
}
public Component[] getAll() {
return components.toArray(new Component[count()]);
}
public void set(int idx, Component c) {
remove(idx);
add(idx, c);
}
/** Add component to end of components. */
public void add(Component comp) {
comp.parent = this;
components.add(comp);
if (client != null) {
comp.setClient(client);
comp.init();
}
if (id != null) {
sendEvent("add", new String[] {"html=" + comp.html()});
comp.events();
}
}
/** Add component at index. */
public void add(int idx, Component comp) {
comp.parent = this;
Component before = (idx < 0 || idx >= count()) ? null : components.get(idx);
components.add(idx, comp);
if (client != null) {
comp.setClient(client);
comp.init();
}
if (id != null) {
if (before == null)
sendEvent("add", new String[] {"html=" + comp.html()});
else
sendEvent("addbefore", new String[] {"html=" + comp.html(), "beforeid=" + before.id});
comp.events();
}
}
public void remove(Component comp) {
components.remove(comp);
if (id != null) {
sendEvent("remove", new String[] {"child=" + comp.id});
}
}
public void remove(int idx) {
Component comp = components.remove(idx);
if (id != null) {
sendEvent("remove", new String[] {"child=" + comp.id});
}
}
public void removeAll() {
while (count() > 0) {
remove(0);
}
}
public int count() {
return components.size();
}
public String html() {
StringBuilder sb = new StringBuilder();
sb.append("<div" + getAttrs() + ">");
int cnt = count();
for(int a=0;a<cnt;a++) {
sb.append(get(a).html());
}
sb.append("</div>");
return sb.toString();
}
}