001: package com.salmonllc.html;
002:
003: /////////////////////////
004: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlContainer.java $
005: //$Author: Dan $
006: //$Revision: 33 $
007: //$Modtime: 10/17/03 10:27a $
008: /////////////////////////
009:
010: import com.salmonllc.util.MessageLog;
011: import com.salmonllc.util.Util;
012:
013: import java.io.PrintWriter;
014: import java.util.Enumeration;
015: import java.util.Hashtable;
016: import java.util.Vector;
017: import com.salmonllc.jsp.JspContainer;
018:
019: /**
020: * This class is a holder for other components. Placing components in a container allows them all to be acted upon once.
021: */
022: public class HtmlContainer extends HtmlComponent {
023: /** Vector to store all the componets added to the container */
024: protected Vector _componentsVec = new Vector();
025:
026: /** component that does the submit if there is one */
027: protected HtmlComponent _submit;
028:
029: /** Centers the componets in the container */
030: protected boolean _center = false;
031:
032: /** enabled flag */
033: protected boolean _enabled = true;
034:
035: public HtmlContainer(String name, com.salmonllc.html.HtmlPage p) {
036: super (name, p);
037: }
038:
039: /**
040: * Adds a new html component to the container.
041: */
042: public void add(HtmlComponent comp) {
043: if (_componentsVec == null)
044: _componentsVec = new Vector();
045:
046: _componentsVec.addElement(comp);
047: comp.setParent(this );
048: }
049:
050: /**
051: * Clears the submit component in this container or children containers. The container stores which component inside it
052: * submitted a particular page for one invocation so it can route to the correct submit performed methods. Once that's done,
053: * the framework needs to clear out that value for the next page invocation. This method is used by the framework and should not be called directly.
054: */
055: public void clearSubmit() {
056: _submit = null;
057: Enumeration e = getComponents();
058:
059: while (e.hasMoreElements()) {
060: Object o = e.nextElement();
061: if (o instanceof HtmlContainer)
062: ((HtmlContainer) o).clearSubmit();
063: else if (o instanceof HtmlImage)
064: ((HtmlImage) o).clearSubmit();
065: else if (o instanceof JspContainer)
066: ((JspContainer) o).clearSubmit();
067:
068: }
069: }
070:
071: public boolean executeEvent(int eventType) throws Exception {
072: if (eventType == HtmlComponent.EVENT_OTHER) {
073: if (_componentsVec != null) {
074: HtmlComponent h = null;
075: for (int i = 0; i < _componentsVec.size(); i++) {
076: h = (HtmlComponent) _componentsVec.elementAt(i);
077: if (!h.executeEvent(eventType))
078: return false;
079: }
080: }
081: } else if (_submit != null
082: && eventType == HtmlComponent.EVENT_SUBMIT) {
083: boolean retVal = _submit.executeEvent(eventType);
084: _submit = null;
085: return retVal;
086: }
087: return true;
088: }
089:
090: public void generateHTML(PrintWriter p, int rowNo) throws Exception {
091: if (_componentsVec == null)
092: return;
093: //
094: if (!_visible)
095: return;
096: //
097: if (_center)
098: p.print("<CENTER>");
099: //
100: HtmlComponent h = null;
101: for (int i = 0; i < _componentsVec.size(); i++) {
102: h = (HtmlComponent) _componentsVec.elementAt(i);
103: if (h != null) {
104: h.generateHTML(p, rowNo);
105: }
106: }
107: //
108: if (_center)
109: p.print("</CENTER>");
110: }
111:
112: public void generateHTML(PrintWriter p, int rowStart, int rowEnd)
113: throws Exception {
114: if (_componentsVec == null)
115: return;
116: //
117: if (!_visible)
118: return;
119: //
120: if (_center)
121: p.print("<CENTER>");
122: //
123: HtmlComponent h = null;
124: for (int i = 0; i < _componentsVec.size(); i++) {
125: h = (HtmlComponent) _componentsVec.elementAt(i);
126: if (h != null)
127: h.generateHTML(p, rowStart, rowEnd);
128: }
129: //
130: if (_center)
131: p.print("</CENTER>");
132: }
133:
134: public void generateInitialHTML(PrintWriter p) throws Exception {
135: if (!_visible || _componentsVec == null)
136: return;
137: //
138: HtmlComponent h = null;
139: for (int i = 0; i < _componentsVec.size(); i++) {
140: h = (HtmlComponent) _componentsVec.elementAt(i);
141: if (h != null)
142: h.generateInitialHTML(p);
143: }
144: }
145:
146: /**
147: * Use this method to find out if the components will be centered in the container
148: * @return - boolean
149: */
150: public boolean getCenter() {
151: return _center;
152: }
153:
154: /**
155: * This method will return a single component if the
156: * name or a portion of the name is found in this HtmlContainer or any
157: * HtmlContainer contained in this container.
158: * @return HtmlComponent - if a component can not be found null is returned
159: * @param name - name of component being searched for
160: */
161: public HtmlComponent getComponent(String name) {
162: HtmlComponent ret = null;
163: HtmlComponent comp = null;
164: Enumeration enum = getComponents();
165: String compName = null;
166: // we will search all items in this enumeration
167: // recursivly calling the containers getComponent method
168: boolean breakout = enum.hasMoreElements();
169: while (breakout && enum.hasMoreElements()) {
170: ret = null;
171:
172: comp = (HtmlComponent) enum.nextElement();
173: compName = comp.getName();
174:
175: // if passed name or comparision name is null return earily because we can not go any further.
176: if (compName == null || name == null) {
177: MessageLog.writeInfoMessage("getComponent\n\t*** Looking for component named " + name + " but " + getFullName() + " contains a component with a null name. \nSolution may be to pass empty string instead of null. ***", 9, this );
178: return ret;
179: }
180:
181: if (compName.indexOf(name) > -1) {
182: ret = comp;
183: breakout = false;
184: } else {
185: if (comp instanceof HtmlTable) {
186: ret = ((HtmlTable) comp).getComponent(name);
187: // checking the value of the return value
188: breakout = (ret == null);
189: } else if (comp instanceof HtmlContainer) {
190: ret = ((HtmlContainer) comp).getComponent(name);
191: // checking the value of the return value
192: breakout = (ret == null);
193: }
194: }
195: }
196:
197: return ret;
198: }
199:
200: /**
201: * This method will return a list of all components in the container.
202: */
203: public Enumeration getComponents() {
204: if (_componentsVec == null) {
205: Vector v = new Vector();
206: return v.elements();
207: } else
208: return _componentsVec.elements();
209: }
210:
211: /**
212: * Returns true if the component is enabled to respond to user input.
213: * @return boolean
214: */
215: public boolean getEnabled() {
216: return _enabled;
217: }
218:
219: /**
220: * Returns the component that submitted the page if that component is in the container or null otherwise.
221: * @return - HtmlComponent
222: */
223: public HtmlComponent getSubmitComponent() {
224: if (_submit == null)
225: return null;
226: else if (_submit == this )
227: return this ;
228: else if (_submit instanceof HtmlContainer)
229: return ((HtmlContainer) _submit).getSubmitComponent();
230: else if (_submit instanceof JspContainer)
231: return ((JspContainer) _submit).getSubmitComponent();
232: else
233: return _submit;
234: }
235:
236: /**
237: * Adds a new html component to the container at a specified index
238: */
239: public void insertComponentAt(HtmlComponent comp, int index) {
240: if (_componentsVec == null)
241: _componentsVec = new Vector();
242:
243: if (_componentsVec.isEmpty()) {
244:
245: // add componet contained
246: _componentsVec.addElement(comp);
247:
248: } else {
249: // add componet contained at specified location
250: _componentsVec.insertElementAt(comp, index);
251:
252: }
253: comp.setParent(this );
254: }
255:
256: public boolean processParms(Hashtable parms, int rowNo)
257: throws Exception {
258: String compName = null;
259: try {
260: if (!getVisible())
261: return false;
262:
263: if (_componentsVec != null) {
264: int compSize = _componentsVec.size();
265: HtmlComponent comp = null;
266: for (int i = 0; i < compSize; i++) {
267: comp = (HtmlComponent) _componentsVec.elementAt(i);
268:
269: if (debug) {
270: // assume not null
271: compName = comp.getName();
272: if (!Util.isFilled(compName)) {
273: compName = comp.getFullName();
274: }
275: MessageLog.writeDebugMessage(
276: "processParms for " + compName + "\n",
277: this );
278: }
279: if (comp.processParms(parms, rowNo))
280: _submit = (HtmlComponent) _componentsVec
281: .elementAt(i);
282: }
283: }
284: if (_submit != null)
285: return true;
286: else
287: return false;
288:
289: } catch (Exception e) {
290: MessageLog.writeErrorMessage("processParms for " + compName
291: + "\n", e, this );
292: throw (e);
293:
294: }
295: }
296:
297: /**
298: * Removes an html component from this container.
299: * @param comp The component to remove
300: */
301: public void remove(HtmlComponent comp) {
302: if (_componentsVec == null)
303: return;
304:
305: int compSize = _componentsVec.size();
306: for (int i = 0; i < compSize; i++) {
307: if (((HtmlComponent) _componentsVec.elementAt(i)) == comp) {
308: _componentsVec.removeElementAt(i);
309: return;
310: }
311: }
312: }
313:
314: /**
315: * This method removes all the components from a container
316: */
317: public void removeAll() {
318: int compSize = _componentsVec.size();
319: for (int i = 0; i < compSize; i++) {
320: HtmlComponent hc = (HtmlComponent) _componentsVec
321: .elementAt(i);
322: if (hc instanceof HtmlTable) {
323: ((HtmlTable) hc).removeAll();
324: } else if (hc instanceof HtmlContainer) {
325: ((HtmlContainer) hc).removeAll();
326: }
327: }
328: _componentsVec.removeAllElements();
329: }
330:
331: /**
332: * Replaces a html component with another one that you pass in.
333: * @param comp com.salmonllc.html.HtmlComponent - conponent that you would like to be in the container after the replace operation
334: * @param compToReplace Object - a handle to the component to replace ( this is the one that should be replaced after the call to this method )
335: * @return boolean
336: */
337: public boolean replaceComponent(HtmlComponent comp,
338: Object compToReplace) {
339: if (_componentsVec == null) {
340: return false;
341: }
342:
343: // get the index of the comp so we can replace it
344: int replaceIndex = _componentsVec.indexOf(compToReplace);
345: if (replaceIndex != -1) {
346: // add componet contained
347: _componentsVec.setElementAt(comp, replaceIndex);
348: comp.setParent(this );
349: return true;
350: } else {
351: MessageLog
352: .writeDebugMessage(
353: " HtmlContainer replaceComponent failed to replace the component ",
354: this );
355: return false;
356: }
357: }
358:
359: /**
360: * This method will clear all pending events from the event queue for this component and components it contains.
361: */
362: public void reset() {
363: Enumeration comps = getComponents();
364: while (comps.hasMoreElements()) {
365: HtmlComponent c = (HtmlComponent) comps.nextElement();
366: c.reset();
367: }
368: _submit = null;
369: }
370:
371: /**
372: * Use this method to indicate that the components in the container should be centered.
373: */
374: public void setCenter(boolean center) {
375: _center = center;
376: }
377:
378: /**
379: * Sets the flag for ability to respond to user input (true = does respond).
380: */
381: public void setEnabled(boolean enabled) {
382: _enabled = enabled;
383: Enumeration e = getComponents();
384: HtmlComponent c = null;
385: while (e.hasMoreElements()) {
386: c = (HtmlComponent) e.nextElement();
387: try {
388: ((HtmlFormComponent) c).setEnabled(enabled);
389: } catch (ClassCastException ce) {
390: try {
391: ((HtmlContainer) c).setEnabled(enabled);
392: } catch (ClassCastException ce1) {
393: }
394: }
395: }
396: }
397:
398: /**
399: * Creates a String representation of the contents of the HtmlContainer
400: * @return - contents of container as a string representation
401: */
402: public String toString() {
403: StringBuffer sBuff = new StringBuffer("[");
404: int compSize = _componentsVec.size();
405: HtmlComponent c = null;
406: boolean commaFlag = false;
407: if (_componentsVec.isEmpty()) {
408: sBuff.append("NULL");
409: }
410:
411: for (int i = 0; i < compSize; i++) {
412: c = (HtmlComponent) _componentsVec.elementAt(i);
413: if (commaFlag == true) {
414: sBuff.append(",");
415: }
416:
417: String name = c.getName();
418: if (Util.isNull(name)) {
419: name = "NULL_NAME";
420: } else if (Util.isEmpty(name)) {
421: name = "EMPTY";
422: }
423: sBuff.append(name);
424: commaFlag = true;
425:
426: }
427: sBuff.append("]");
428:
429: return super .toString() + "\n" + sBuff.toString();
430: }
431: }
|