001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.render.web;
029:
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.List;
033:
034: import thinwire.ui.Component;
035: import thinwire.ui.Container;
036: import thinwire.ui.Container.ScrollType;
037: import thinwire.ui.event.ItemChangeEvent;
038: import thinwire.ui.event.ItemChangeListener;
039: import thinwire.ui.event.PropertyChangeEvent;
040:
041: /**
042: * @author Joshua J. Gertzen
043: */
044: class ContainerRenderer extends ComponentRenderer implements
045: ItemChangeListener {
046: private static final String CONTAINER_CLASS = "tw_Container";
047: private static final String SET_SCROLL_TYPE = "setScrollType";
048: private static final String REMOVE_COMPONENT = "removeComponent";
049:
050: private boolean fullyRendered;
051: private Map<Component, ComponentRenderer> compToRenderer;
052:
053: void render(WindowRenderer wr, Component comp,
054: ComponentRenderer container) {
055: if (jsClass == null)
056: init(CONTAINER_CLASS, wr, comp, container);
057: setPropertyChangeIgnored(Component.PROPERTY_FOCUS, true);
058: setPropertyChangeIgnored(Component.PROPERTY_ENABLED, true);
059: Container<Component> c = (Container<Component>) comp;
060: if (!isPropertyChangeIgnored(Container.PROPERTY_SCROLL_TYPE))
061: addInitProperty(Container.PROPERTY_SCROLL_TYPE,
062: getScrollTypeCode(c.getScrollType()));
063:
064: c.addItemChangeListener(this );
065: super .render(wr, comp, container);
066: renderChildren(c);
067:
068: comp = c.getChildWithFocus();
069:
070: if (comp != null && !(comp instanceof Container)) {
071: ComponentRenderer cr = compToRenderer.get(comp);
072: if (!cr.isPropertyChangeIgnored(Component.PROPERTY_FOCUS))
073: cr.postClientEvent(SET_FOCUS, true);
074: }
075: }
076:
077: int getScrollTypeCode(ScrollType st) {
078: int sti;
079:
080: if (st == ScrollType.AS_NEEDED) {
081: sti = 1;
082: } else if (st == ScrollType.ALWAYS) {
083: sti = 2;
084: } else {
085: sti = 0;
086: }
087:
088: return sti;
089: }
090:
091: void destroy() {
092: Container<Component> c = (Container<Component>) comp;
093: c.removeItemChangeListener(this );
094: super .destroy();
095:
096: for (Component comp : c.getChildren()) {
097: compToRenderer.get(comp).destroy();
098: }
099:
100: compToRenderer.clear();
101: compToRenderer = null;
102: }
103:
104: void renderChildren(Container<Component> c) {
105: List<Component> children = c.getChildren();
106: if (compToRenderer == null)
107: compToRenderer = new HashMap<Component, ComponentRenderer>(
108: children.size());
109: fullyRendered = false;
110:
111: for (Component comp : children) {
112: renderChild(comp);
113: }
114:
115: fullyRendered = true;
116: }
117:
118: void renderChild(Component comp) {
119: ComponentRenderer r = compToRenderer.get(comp);
120: if (r == null)
121: compToRenderer.put(comp, r = wr.ai.getRenderer(comp));
122: r.render(wr, comp, this );
123: }
124:
125: boolean isFullyRendered() {
126: return fullyRendered;
127: }
128:
129: public void propertyChange(PropertyChangeEvent pce) {
130: if (pce.getPropertyName()
131: .equals(Container.PROPERTY_SCROLL_TYPE)) {
132: if (isPropertyChangeIgnored(Container.PROPERTY_SCROLL_TYPE))
133: return;
134: postClientEvent(SET_SCROLL_TYPE,
135: getScrollTypeCode((ScrollType) pce.getNewValue()));
136: } else {
137: super .propertyChange(pce);
138: }
139: }
140:
141: public void itemChange(ItemChangeEvent ice) {
142: ItemChangeEvent.Type type = ice.getType();
143:
144: if (type == ItemChangeEvent.Type.REMOVE
145: || type == ItemChangeEvent.Type.SET) {
146: Component comp = (Component) ice.getOldValue();
147: postClientEvent(REMOVE_COMPONENT, wr.getComponentId(comp));
148: ((ComponentRenderer) compToRenderer.remove(comp)).destroy();
149: }
150:
151: if (type == ItemChangeEvent.Type.ADD
152: || type == ItemChangeEvent.Type.SET) {
153: renderChild((Component) ice.getNewValue());
154: }
155: }
156: }
|