001: /*
002: * Copyright 2005 jWic group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.base.ControlContainer
017: * $Id: ControlContainer.java,v 1.6 2006/09/26 14:26:54 lordsam Exp $
018: */
019: package de.jwic.base;
020:
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: /**
026: * A ControlContainer is a control that can contain other controls.
027: * @author Florian Lippisch
028: * @version $Revision: 1.6 $
029: */
030: public class ControlContainer extends Control implements
031: IControlContainer {
032:
033: private static final long serialVersionUID = 2L;
034:
035: private Map controls = new HashMap();
036:
037: /**
038: * @param container
039: */
040: public ControlContainer(IControlContainer container) {
041: super (container, null);
042: }
043:
044: /**
045: * @param container
046: */
047: public ControlContainer(IControlContainer container, String name) {
048: super (container, name);
049: }
050:
051: /* (non-Javadoc)
052: * @see de.jwic.base.IControlContainer#adopt(de.jwic.base.Control)
053: */
054: public void adopt(Control control, String name) {
055:
056: // notify the old container that the control has been moved (remove it)
057: IControlContainer oldParent = control.getContainer();
058: if (oldParent == this ) {
059: return; // nothing to do
060: }
061: oldParent.unregisterControl(control);
062: registerControl(control, name);
063:
064: }
065:
066: /* (non-Javadoc)
067: * @see de.jwic.base.Control#setControlID(java.lang.String)
068: */
069: void setControlID(String newControlID) {
070: super .setControlID(newControlID);
071: // update childs, if there are already any
072: if (controls != null) {
073: for (Iterator it = getControls(); it.hasNext();) {
074: Control child = (Control) it.next();
075: child
076: .setControlID(newControlID + "."
077: + child.getName());
078: }
079: }
080: }
081:
082: /* (non-Javadoc)
083: * @see de.jwic.base.IControlContainer#addControl(java.lang.String, de.jwic.base.Control)
084: */
085: public void registerControl(Control control, String name)
086: throws JWicException {
087:
088: if (control.getContainer() != null) {
089: throw new JWicException(
090: "The control has already been registerd to a container.");
091: }
092:
093: if (name == null) {
094: int idx = controls.size();
095: name = "c" + idx;
096: while (controls.containsKey(name)) {
097: idx++;
098: name = "c" + idx;
099: }
100: } else {
101: // the control must not contain spaces or dot's
102: for (int i = 0; i < name.length(); i++) {
103: char c = name.charAt(i);
104: if (c == ' ' || c == '.') {
105: throw new IllegalArgumentException(
106: "A control-name must not contain spaces or dot's.");
107: }
108: }
109: }
110:
111: if (controls.containsKey(name)) {
112: throw new JWicException(
113: "A control with that name does already exist.");
114: }
115:
116: control.setSessionContext(getSessionContext());
117: control.setContainer(this );
118: control.setName(name);
119: control.setControlID(getControlID() + "." + control.getName());
120: controls.put(control.getName(), control);
121: setRequireRedraw(true);
122:
123: }
124:
125: /* (non-Javadoc)
126: * @see de.jwic.base.IControlContainer#unregisterControl(de.jwic.base.Control)
127: */
128: public void unregisterControl(Control control) {
129:
130: if (control.getContainer() == this ) {
131: controls.remove(control.getName());
132: control.setContainer(null);
133: setRequireRedraw(true);
134: }
135:
136: }
137:
138: /**
139: * Remove all controls in the context and destroy them.
140: *
141: */
142: public void destroy() {
143: super .destroy();
144:
145: for (Iterator it = controls.values().iterator(); it.hasNext();) {
146: Control control = (Control) it.next();
147: it.remove();
148: control.destroy();
149: }
150:
151: }
152:
153: /* (non-Javadoc)
154: * @see de.jwic.base.IControlContainer#getControl(java.lang.String)
155: */
156: public Control getControl(String name) {
157:
158: return (Control) controls.get(name);
159: }
160:
161: /* (non-Javadoc)
162: * @see de.jwic.base.IControlContainer#getControls()
163: */
164: public Iterator getControls() {
165: return controls.values().iterator();
166: }
167:
168: /* (non-Javadoc)
169: * @see de.jwic.base.IControlContainer#removeControl(java.lang.String)
170: */
171: public void removeControl(String controlName) {
172: Control control = getControl(controlName);
173: if (control != null) {
174: unregisterControl(control);
175: control.destroy();
176: }
177: }
178:
179: /**
180: * Returns true if the specified childControl is visible. This method is
181: * used by the rendering engine to determine if the specified control can
182: * be rendered. By default, all child controls are relevant for rendering.
183: * Some container implementations (like the TabStripControl) can override
184: * this method to prevent rendering of a child, i.e. a TabControl that is
185: * visible but not 'active'.
186: *
187: * @param childControl
188: * @return
189: */
190: public boolean isRenderingRelevant(Control childControl) {
191: return true;
192: }
193:
194: }
|