KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > widget > WidgetContainer


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.widget;
31
32 import com.caucho.util.L10N;
33
34 import java.io.IOException JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.LinkedHashMap JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Set JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * A Widget that contains other widgets.
43  * If the WidgetContainer has no parent, then
44  * this Widget stores the state of children
45  * as request attributes that correspond to their id's.
46  */

47 public class WidgetContainer
48   extends Widget
49 {
50   private static L10N L = new L10N( WidgetContainer.class );
51
52   static protected final Logger JavaDoc log =
53     Logger.getLogger( WidgetContainer.class.getName() );
54
55   private String JavaDoc _attributePrefix;
56   private Map JavaDoc<String JavaDoc,Widget> _childMap;
57
58   public WidgetContainer()
59   {
60   }
61
62   public WidgetContainer( String JavaDoc id )
63   {
64     super( id );
65   }
66
67   public WidgetContainer( Widget parent )
68   {
69     super( parent );
70   }
71
72   public WidgetContainer( Widget parent, String JavaDoc id )
73   {
74     super( parent, id );
75   }
76
77   /**
78    * Prefix to use for request attribute's of children when this is a toplevel
79    * container, default is null which means no prefix.
80    */

81   public void setAttributePrefix( String JavaDoc attributePrefix )
82   {
83     _attributePrefix = attributePrefix;
84   }
85
86   public Set JavaDoc<Map.Entry JavaDoc<String JavaDoc,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 JavaDoc id, Widget value )
100   {
101     if ( _childMap == null )
102       _childMap = new LinkedHashMap JavaDoc<String JavaDoc,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 JavaDoc 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 JavaDoc 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 JavaDoc contentType,
151                       WidgetConnection connection,
152                       WidgetContainerState widgetState )
153     throws WidgetException, IOException JavaDoc
154   {
155     renderChildren( connection, widgetState );
156   }
157
158   public void renderChildren( WidgetConnection connection,
159                               WidgetContainerState widgetState )
160     throws WidgetException, IOException JavaDoc
161   {
162     if ( !isEmpty() ) {
163       for ( Widget child : (Collection JavaDoc<Widget>) values() ) {
164         child.render( connection, widgetState.get( child.getId() ) );
165       }
166     }
167   }
168 }
169
Popular Tags