001: /*
002: * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.widget;
030:
031: import com.caucho.util.L10N;
032:
033: import java.io.IOException;
034: import java.util.Collection;
035: import java.util.LinkedHashMap;
036: import java.util.Map;
037: import java.util.Set;
038: import java.util.logging.Logger;
039:
040: /**
041: * A Widget that contains other widgets.
042: * If the WidgetContainer has no parent, then
043: * this Widget stores the state of children
044: * as request attributes that correspond to their id's.
045: */
046: public class WidgetContainer extends Widget {
047: private static L10N L = new L10N(WidgetContainer.class);
048:
049: static protected final Logger log = Logger
050: .getLogger(WidgetContainer.class.getName());
051:
052: private String _attributePrefix;
053: private Map<String, Widget> _childMap;
054:
055: public WidgetContainer() {
056: }
057:
058: public WidgetContainer(String id) {
059: super (id);
060: }
061:
062: public WidgetContainer(Widget parent) {
063: super (parent);
064: }
065:
066: public WidgetContainer(Widget parent, String id) {
067: super (parent, id);
068: }
069:
070: /**
071: * Prefix to use for request attribute's of children when this is a toplevel
072: * container, default is null which means no prefix.
073: */
074: public void setAttributePrefix(String attributePrefix) {
075: _attributePrefix = attributePrefix;
076: }
077:
078: public Set<Map.Entry<String, Widget>> entrySet() {
079: if (_childMap == null)
080: return super .entrySet();
081: else
082: return _childMap.entrySet();
083: }
084:
085: public boolean isEmpty() {
086: return _childMap == null ? true : _childMap.isEmpty();
087: }
088:
089: public Widget put(String id, Widget value) {
090: if (_childMap == null)
091: _childMap = new LinkedHashMap<String, Widget>();
092:
093: if (id == null)
094: id = calculateId(value);
095:
096: if ((id == null && value.getId() != null)
097: || (!id.equals(value.getId())))
098: value.setId(id);
099:
100: return _childMap.put(id, value);
101: }
102:
103: public Widget remove(String id) {
104: if (_childMap == null)
105: return null;
106: else
107: return _childMap.remove(id);
108: }
109:
110: protected WidgetContainerState createState(
111: WidgetConnection connection) throws WidgetException {
112: return new WidgetContainerState();
113: }
114:
115: protected WidgetState decodeChild(WidgetConnection connection,
116: WidgetState this State, Widget child) throws WidgetException {
117: WidgetContainerState state = (WidgetContainerState) this State;
118:
119: WidgetState childState = super .decodeChild(connection, state,
120: child);
121:
122: String childId = child.getId();
123:
124: if (state.getParent() == null) {
125: if (_attributePrefix != null
126: && _attributePrefix.length() > 0)
127: connection.setAttribute(_attributePrefix + childId,
128: childState);
129: else
130: connection.setAttribute(childId, childState);
131: }
132:
133: return childState;
134: }
135:
136: public void render(String contentType, WidgetConnection connection,
137: WidgetContainerState widgetState) throws WidgetException,
138: IOException {
139: renderChildren(connection, widgetState);
140: }
141:
142: public void renderChildren(WidgetConnection connection,
143: WidgetContainerState widgetState) throws WidgetException,
144: IOException {
145: if (!isEmpty()) {
146: for (Widget child : (Collection<Widget>) values()) {
147: child
148: .render(connection, widgetState.get(child
149: .getId()));
150: }
151: }
152: }
153: }
|