1 28 29 30 package com.caucho.widget; 31 32 import com.caucho.util.L10N; 33 34 import java.io.IOException ; 35 import java.util.Collection ; 36 import java.util.LinkedHashMap ; 37 import java.util.Map ; 38 import java.util.Set ; 39 import java.util.logging.Logger ; 40 41 47 public class WidgetContainer 48 extends Widget 49 { 50 private static L10N L = new L10N( WidgetContainer.class ); 51 52 static protected final Logger log = 53 Logger.getLogger( WidgetContainer.class.getName() ); 54 55 private String _attributePrefix; 56 private Map <String ,Widget> _childMap; 57 58 public WidgetContainer() 59 { 60 } 61 62 public WidgetContainer( String id ) 63 { 64 super( id ); 65 } 66 67 public WidgetContainer( Widget parent ) 68 { 69 super( parent ); 70 } 71 72 public WidgetContainer( Widget parent, String id ) 73 { 74 super( parent, id ); 75 } 76 77 81 public void setAttributePrefix( String attributePrefix ) 82 { 83 _attributePrefix = attributePrefix; 84 } 85 86 public Set <Map.Entry <String ,Widget>> entrySet() 87 { 88 if ( _childMap == null ) 89 return super.entrySet(); 90 else 91 return _childMap.entrySet(); 92 } 93 94 public boolean isEmpty() 95 { 96 return _childMap == null ? true : _childMap.isEmpty(); 97 } 98 99 public Widget put( String id, Widget value ) 100 { 101 if ( _childMap == null ) 102 _childMap = new LinkedHashMap <String ,Widget>(); 103 104 if ( id == null ) 105 id = calculateId( value ); 106 107 if ( (id == null && value.getId() != null ) 108 || ( !id.equals( value.getId() ) ) ) 109 value.setId( id ); 110 111 return _childMap.put( id, value ); 112 } 113 114 public Widget remove( String id ) 115 { 116 if ( _childMap == null ) 117 return null; 118 else 119 return _childMap.remove( id ); 120 } 121 122 protected WidgetContainerState createState( WidgetConnection connection) 123 throws WidgetException 124 { 125 return new WidgetContainerState(); 126 } 127 128 protected WidgetState decodeChild( WidgetConnection connection, 129 WidgetState thisState, 130 Widget child ) 131 throws WidgetException 132 { 133 WidgetContainerState state = (WidgetContainerState) thisState; 134 135 WidgetState childState 136 = super.decodeChild( connection, state, child ); 137 138 String childId = child.getId(); 139 140 if ( state.getParent() == null ) { 141 if ( _attributePrefix != null && _attributePrefix.length() > 0 ) 142 connection.setAttribute( _attributePrefix + childId, childState ); 143 else 144 connection.setAttribute( childId, childState ); 145 } 146 147 return childState; 148 } 149 150 public void render( String contentType, 151 WidgetConnection connection, 152 WidgetContainerState widgetState ) 153 throws WidgetException, IOException 154 { 155 renderChildren( connection, widgetState ); 156 } 157 158 public void renderChildren( WidgetConnection connection, 159 WidgetContainerState widgetState ) 160 throws WidgetException, IOException 161 { 162 if ( !isEmpty() ) { 163 for ( Widget child : (Collection <Widget>) values() ) { 164 child.render( connection, widgetState.get( child.getId() ) ); 165 } 166 } 167 } 168 } 169 | Popular Tags |