View Javadoc
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.widget; 9 10 import com.tirsen.angkor.RenderContext; 11 import com.tirsen.angkor.View; 12 import com.tirsen.angkor.Component; 13 14 import java.io.IOException; 15 import java.util.ArrayList; 16 import java.util.Collection; 17 import java.util.Collections; 18 import java.util.Iterator; 19 import java.util.List; 20 21 /*** 22 * Default implementation of a component that contains other components. 23 * 24 * <!-- $Id: Container.java,v 1.4 2002/10/13 19:59:23 tirsen Exp $ --> 25 * 26 * @author $Author: tirsen $ 27 * @version $Revision: 1.4 $ 28 */ 29 public class Container extends Component 30 { 31 protected List children = new ArrayList(); 32 private boolean childrenCreated = false; 33 34 public Container() 35 { 36 } 37 38 public Container(String id) 39 { 40 super(id); 41 } 42 43 /*** 44 * Iterates the collection even if it is <code>null</code>. 45 */ 46 protected static Iterator iterateCollection(Collection coll) 47 { 48 Iterator it; 49 if (coll == null) 50 { 51 it = Collections.EMPTY_LIST.iterator(); 52 } 53 else 54 { 55 it = coll.iterator(); 56 } 57 return it; 58 } 59 60 61 public void renderChildren(RenderContext context) throws IOException 62 { 63 Collection elements = getChildren(); 64 Iterator iterator = iterateCollection(elements); 65 while (iterator.hasNext()) 66 { 67 View element = (View) iterator.next(); 68 element.render(context); 69 } 70 } 71 72 public List getChildren() 73 { 74 maybeCreateChildren(); 75 return children; 76 } 77 78 public int getNumberOfChildren() 79 { 80 return getChildren().size(); 81 } 82 83 /*** 84 * Iterates <em>all</em> elements of this container, not just the main elements. 85 */ 86 public Iterator iterateAllElements() 87 { 88 return iterateCollection(getChildren()); 89 } 90 91 /*** 92 * Inserts main element at specified index. All other add-methods call this method, thus subclasses 93 * only need to overload this method to provide additional behaviour. 94 */ 95 public void add(int index, View element) 96 { 97 element.setContainer(this); 98 getChildren().add(index, element); 99 } 100 101 /*** 102 * Add a main element. 103 */ 104 public void add(View element) 105 { 106 add(getChildren().size(), element); 107 } 108 109 /*** 110 * Remove a main element. 111 */ 112 public void remove(Component element) 113 { 114 getChildren().remove(element); 115 element.setContainer(null); 116 } 117 118 public void parse(RenderContext context) 119 { 120 super.parse(context); 121 if (isVisible()) 122 { 123 for (Iterator iterator = iterateAllElements(); iterator.hasNext();) 124 { 125 View element = (View) iterator.next(); 126 element.parse(context); 127 } 128 } 129 } 130 131 public void render(RenderContext context) throws IOException 132 { 133 renderChildren(context); 134 } 135 136 /*** 137 * Overload to create any default children in subclasses. Will be executed before any children 138 * is actually needed by the framework. 139 */ 140 protected void createChildren() 141 { 142 // no default children 143 } 144 145 protected void maybeCreateChildren() 146 { 147 if (!isChildrenCreated()) 148 { 149 childrenCreated = true; 150 createChildren(); 151 } 152 } 153 154 public boolean isChildrenCreated() 155 { 156 return childrenCreated; 157 } 158 }

This page was automatically generated by Maven