Source code: com/arranger/jarl/widget/ContainerWidget.java
1 package com.arranger.jarl.widget;
2
3 import com.arranger.jarl.base.IContext;
4 import com.arranger.jarl.base.Time;
5 import com.arranger.jarl.base.IJarlObjectCollection;
6 import com.arranger.jarl.util.ObjectUtil;
7
8 import java.awt.*;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Iterator;
12 import java.util.List;
13
14 /**
15 * ParentWidget created on Feb 24, 2003
16 */
17 public abstract class ContainerWidget extends BaseWidget implements IJarlObjectCollection {
18
19 protected boolean m_initChildren = false;
20 protected List m_children = new ArrayList();
21
22 /**
23 * Get any children widgets
24 * If you want to modify this, you may
25 *
26 * @return a collection of {@link IWidget} empty or not
27 */
28 public Collection getChildren() {
29 return m_children;
30 }
31
32 /**
33 * The main paint routine
34 * @param graphics2D should be cast to a {@link Graphics2D}
35 * @param context the current context
36 */
37 public void paint(IContext context, Graphics2D graphics2D) {
38 if (!m_initChildren) {
39 initializeChildren();
40 m_initChildren = true;
41 }
42
43 //paint children
44 for (Iterator it = m_children.iterator(); it.hasNext();) {
45 paintChild((IWidget) it.next(), context, graphics2D);
46 }
47
48 //are we done rendering?
49 if (context.getTime().isGreater(m_endTime) || context.getTime().equals(m_endTime)) {
50 context.getRenderManager().unregister(this);
51 }
52 }
53
54 protected void paintChild(IWidget widget, IContext context, Graphics2D graphics2D) {
55 context.getRenderManager().onRender(widget, context);
56 widget.paint(context, graphics2D);
57 }
58
59 /**
60 * Called from within {@link #paint}
61 *
62 * @param context
63 * @param graphics2D
64 */
65 protected void _paint(IContext context, Graphics2D graphics2D) {
66 //no-op
67 }
68
69 /**
70 * You might want to iterate through the children initializing something?
71 */
72 protected void initializeChildren() {
73 for (Iterator it = m_children.iterator(); it.hasNext();) {
74 IWidget widget = (IWidget) it.next();
75 ObjectUtil.mergeCollections(getStrokes(), widget.getStrokes());
76 ObjectUtil.mergeCollections(getTraits(), widget.getTraits());
77 ObjectUtil.mergeCollections(getFilters(), widget.getFilters());
78 }
79 }
80
81 /**
82 * Always remember some attrs might not be there
83 * @param context
84 */
85 protected void initAttributes(IContext context) {
86 super.initAttributes(context);
87 for (Iterator it = m_children.iterator(); it.hasNext();) {
88 IWidget widget = (IWidget) it.next();
89 widget.init(context, m_configElement);
90 context.getRenderManager().unregister(widget);
91 }
92 }
93
94 public Object clone() {
95 ContainerWidget containerWidget = (ContainerWidget) super.clone();
96
97 //deep copy of the children
98 List otherChildren = new ArrayList();
99 for (Iterator it = m_children.iterator(); it.hasNext();) {
100 IWidget widget = (IWidget) it.next();
101 otherChildren.add(widget.clone());
102 }
103 containerWidget.m_children = otherChildren;
104
105 return containerWidget;
106 }
107
108 /**
109 * @param time to start
110 */
111 public void setStartTime(Time time) {
112 super.setStartTime(time);
113 for (Iterator it = m_children.iterator(); it.hasNext();) {
114 ((IWidget) it.next()).setStartTime(time);
115 }
116 }
117
118 /**
119 * @param time to end
120 */
121 public void setEndTime(Time time) {
122 super.setEndTime(time);
123 for (Iterator it = m_children.iterator(); it.hasNext();) {
124 ((IWidget) it.next()).setEndTime(time);
125 }
126 }
127 }