1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.webmancer.widget;
23
24 import java.util.List;
25 import java.util.Vector;
26
27 import net.sf.webmancer.base.Event;
28 import net.sf.webmancer.model.IModelBuilder;
29 import net.sf.webmancer.model.ModelingException;
30 import net.sf.webmancer.util.ContractChecker;
31
32
33
34
35 public abstract class AbstractContainer extends AbstractWidget {
36
37
38
39 private List<IWidget> children;
40
41
42
43
44 public AbstractContainer() {
45 super();
46 this.children = new Vector<IWidget>();
47 }
48
49
50
51
52 @Override
53 public void handleEvent(final Event event) {
54 ContractChecker.mustNotBeNull(event, "event");
55
56 super.handleEvent(event);
57 if (!event.isHandled()) {
58 for (IWidget child : this.children) {
59 child.handleEvent(event);
60 if (event.isHandled()) {
61 break;
62 }
63 }
64 }
65 }
66
67
68
69
70 public void addChild(final IWidget widget) {
71 ContractChecker.mustNotBeNull(widget, "widget");
72
73 if (widget.getParent() != null) {
74 throw new IllegalStateException(
75 "Cannot add widget as a child to this container - the widget has parent already");
76 }
77 this.children.add(widget);
78 widget.setParent(this);
79 }
80
81
82
83
84 public void setChildren(List<IWidget> children) {
85 ContractChecker.mustNotBeNull(children, "children");
86
87 for (IWidget child : children) {
88 this.addChild(child);
89 }
90 }
91
92
93
94
95 @Override
96 public void build(final IModelBuilder builder) throws ModelingException {
97 ContractChecker.mustNotBeNull(builder, "builder");
98 for (IWidget child : this.children) {
99 buildChild(child, builder);
100 }
101 }
102
103
104
105
106
107
108 protected void buildChild(IWidget child, IModelBuilder builder) throws ModelingException {
109 ContractChecker.mustNotBeNull(child, "child");
110 ContractChecker.mustNotBeNull(builder, "builder");
111 child.build(builder);
112 }
113
114 }