001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020:
021: package com.salmonllc.jsp;
022:
023: /////////////////////////
024: //$Archive: /SOFIA/SourceCode/com/salmonllc/jsp/JspContainer.java $
025: //$Author: Dan $
026: //$Revision: 31 $
027: //$Modtime: 10/21/04 2:15p $
028: /////////////////////////
029:
030: import java.io.PrintWriter;
031: import java.util.Enumeration;
032: import java.util.Hashtable;
033: import java.util.Vector;
034:
035: import com.salmonllc.html.HtmlComponent;
036: import com.salmonllc.html.HtmlContainer;
037: import com.salmonllc.html.HtmlFormComponent;
038: import com.salmonllc.html.HtmlImage;
039: import com.salmonllc.html.HtmlPage;
040: import com.salmonllc.util.MessageLog;
041: import com.salmonllc.util.Util;
042:
043: /**
044: * This class is the ancestor for all JSP Container objects. It differs from HtmlContainer in that it only contains components for the purpose of processing the parameters returned from a browser. The component's generateHtml method is not called from this container, but is instead governed by the components placement in the JSP page.
045: */
046:
047: public class JspContainer extends HtmlComponent {
048: private Vector _compsVec = new Vector();
049: protected HtmlComponent _submit;
050: public static final int TYPE_COMP = 0;
051: public static final int TYPE_ROW = 1;
052: public static final int TYPE_HEADER = 2;
053: public static final int TYPE_FOOTER = 3;
054: public static final int TYPE_GROUP_FOOTER = 4;
055: public static final int TYPE_GROUP_HEADER = 5;
056: protected boolean _center = false;
057: protected boolean _enabled = true;
058: private boolean _doInit;
059:
060: public JspContainer(String name, HtmlPage p) {
061: super (name, p);
062: }
063:
064: /**
065: * Adds a component to the container. This method should not be called directly. It is only made public because it must be called from objects in the com.salmonllc.jsp.tags package.
066: */
067: public void add(HtmlComponent comp, int type) {
068: Object o[] = new Object[2];
069: o[0] = comp;
070: o[1] = new Integer(type);
071:
072: comp.setParent(this );
073:
074: _compsVec.add(o);
075: }
076:
077: /**
078: * Clears the submit component in this container or children containers. The container stores which component inside it
079: * submitted a particular page for one invocation so it can route to the correct submit performed methods. Once that's done,
080: * 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.
081: */
082: public void clearSubmit() {
083: _submit = null;
084: Enumeration e = getComponents();
085:
086: while (e.hasMoreElements()) {
087: Object o = e.nextElement();
088: if (o instanceof HtmlContainer)
089: ((HtmlContainer) o).clearSubmit();
090: else if (o instanceof HtmlImage)
091: ((HtmlImage) o).clearSubmit();
092: else if (o instanceof JspContainer)
093: ((JspContainer) o).clearSubmit();
094:
095: }
096: }
097:
098: public boolean executeEvent(int eventType) throws Exception {
099: if (eventType == HtmlComponent.EVENT_OTHER) {
100: HtmlComponent h = null;
101: for (int i = 0; i < getComponentCount(); i++) {
102: h = getComponent(i);
103: if (!h.executeEvent(eventType))
104: return false;
105: }
106: } else if (_submit != null
107: && eventType == HtmlComponent.EVENT_SUBMIT) {
108: boolean retVal = _submit.executeEvent(eventType);
109: _submit = null;
110: return retVal;
111: }
112: return true;
113: }
114:
115: public void generateHTML(PrintWriter p, int rowNo)
116: throws java.lang.Exception {
117: }
118:
119: /**
120: * Gets a component from the container from the specified index.
121: */
122: public HtmlComponent getComponent(int i) {
123: String compName = null;
124: Object[] o = (Object[]) _compsVec.elementAt(i);
125: HtmlComponent comp = (HtmlComponent) o[0];
126:
127: if (debug) {
128: compName = comp.getName();
129: if (!Util.isFilled(compName)) {
130: compName = comp.getFullName();
131: }
132: MessageLog.writeDebugMessage("processParms for " + compName
133: + "\n", this );
134: }
135: return comp;
136: }
137:
138: /**
139: * Gets the number of components in the container.
140: */
141: public int getComponentCount() {
142: return _compsVec.size();
143: }
144:
145: /**
146: * This method will return a list of all components in the container.
147: */
148: public Enumeration getComponents() {
149: if (_compsVec == null) {
150: Vector v = new Vector();
151: return v.elements();
152: } else {
153: // WARNING: Do not attempt to optimize this by storing the result in an
154: // instance variable and reusing it, since an Enumeration is supposed to be used
155: // one time only.
156: Vector v = new Vector();
157: int elementsSize = getComponentCount();
158: for (int i = 0; i < elementsSize; i++) {
159: HtmlComponent comp = (HtmlComponent) getComponent(i);
160: if (comp != null)
161: v.addElement(comp);
162: }
163: return v.elements();
164: }
165: // return _compsVec.elements();
166: }
167:
168: /**
169: * Returns true if the component is in this container
170: */
171: public boolean isComponentInContainer(HtmlComponent comp) {
172: HtmlComponent parent = comp.getParent();
173: while (parent != null) {
174: if (parent == this )
175: return true;
176: parent = parent.getParent();
177: }
178: return false;
179: }
180:
181: /**
182: * Gets the type of component at the specified index
183: * valid return values are TYPE_COMP, TYPE_ROW, TYPE_HEADER, TYPE_FOOTER, TYPE_GROUP_FOOTER, TYPE_GROUP_HEADER
184: **/
185: public int getComponentType(int i) {
186: Object[] o = (Object[]) _compsVec.elementAt(i);
187: return ((Integer) o[1]).intValue();
188: }
189:
190: /**
191: * Gets the type of component assuming the component is in the container, otherwise it returns -1
192: * valid return values are TYPE_COMP, TYPE_ROW, TYPE_HEADER, TYPE_FOOTER, TYPE_GROUP_FOOTER, TYPE_GROUP_HEADER
193: **/
194: public int getComponentType(HtmlComponent comp) {
195: int ndx = indexOf(comp);
196: if (ndx == -1)
197: return -1;
198: else
199: return getComponentType(ndx);
200: }
201:
202: /**
203: * Returns the index of the component in the container
204: */
205: public int getComponentIndex(HtmlComponent comp) {
206: return indexOf(comp);
207: }
208:
209: /**
210: * Returns the component that submitted the page if that component is in the container or null otherwise.
211: * @return - HtmlComponent
212: */
213: public HtmlComponent getSubmitComponent() {
214: if (_submit == null)
215: return null;
216: else if (_submit == this )
217: return this ;
218: else if (_submit instanceof HtmlContainer)
219: return ((HtmlContainer) _submit).getSubmitComponent();
220: else if (_submit instanceof JspContainer)
221: return ((JspContainer) _submit).getSubmitComponent();
222: else
223: return _submit;
224: }
225:
226: /**
227: * Pass in an object and this method will return what index it is at
228: */
229: private int indexOf(Object o) {
230:
231: // loop through objects
232: int compSize = getComponentCount();
233: for (int i = 0; i < compSize; i++) {
234: // when you find the one you are looking for return the index
235: if (o == getComponent(i)) {
236: return i;
237: }
238: }
239: // if you were unable to find the object you were looking for return -1
240: return -1;
241: }
242:
243: /**
244: * This method will process the parms from a post for every component in the container.
245: */
246: public boolean processParms(Hashtable parms, int rowNo)
247: throws Exception {
248: String compName = null;
249: try {
250:
251: if (!getVisible())
252: return false;
253:
254: int compSize = getComponentCount();
255: HtmlComponent comp = null;
256:
257: for (int i = 0; i < compSize; i++) {
258: comp = getComponent(i);
259: if (debug) {
260: // assume not null
261: compName = comp.getName();
262: if (!Util.isFilled(compName)) {
263: compName = comp.getFullName();
264: }
265: MessageLog.writeDebugMessage("processParms for "
266: + compName + "\n", this );
267: }
268:
269: if (comp.processParms(parms, rowNo))
270: _submit = getComponent(i);
271: }
272:
273: if (_submit != null)
274: return true;
275: else
276: return false;
277:
278: } catch (Exception e) {
279: MessageLog.writeErrorMessage("processParms for " + compName
280: + "\n", e, this );
281: throw (e);
282: }
283: }
284:
285: /**
286: * Replaces a html component with another one that you pass in.
287: * @param comp com.salmonllc.html.HtmlComponent - conponent that you would like to be in the container after the replace operation
288: * @param compToReplace Object - a handle to the component to replace ( this is the one that should be replaced after the call to this method )
289: * @return boolean
290: */
291: boolean replaceComponent(HtmlComponent comp, Object compToReplace) {
292: // JSP replaceComponent
293: if (_compsVec == null) {
294: return false;
295: }
296:
297: // get the index of the comp so we can replace it
298: int replaceIndex = this .indexOf(compToReplace);
299: if (replaceIndex != -1) {
300:
301: // add componet contained
302: setComponent(comp, replaceIndex);
303: return true;
304: } else {
305: MessageLog
306: .writeDebugMessage(
307: "JSP replaceComponent failed to replace the component ",
308: this );
309: return false;
310: } // end else
311:
312: }
313:
314: /**
315: * This method sets a component at a specified index
316: */
317: private void setComponent(HtmlComponent comp, int index) {
318:
319: // get the componet
320: Object[] o = (Object[]) _compsVec.elementAt(index);
321:
322: // replace the comp
323: comp.setParent(this );
324: o[0] = comp;
325:
326: // put it back in the vector
327: _compsVec.setElementAt(o, index);
328: }
329:
330: /**
331: * Sets all the components in the container to enabled or not depending on the argument passed.
332: */
333:
334: public void setEnabled(boolean enabled) {
335: for (int i = 0; i < _compsVec.size(); i++) {
336: HtmlComponent comp = getComponent(i);
337: if (comp instanceof HtmlFormComponent)
338: ((HtmlFormComponent) comp).setEnabled(enabled);
339: else if (comp instanceof HtmlContainer)
340: ((HtmlContainer) comp).setEnabled(enabled);
341: else if (comp instanceof JspContainer)
342: ((JspContainer) comp).setEnabled(enabled);
343:
344: }
345: _enabled = enabled;
346: }
347:
348: /**
349: * Sets all the components in the container to visible or not depending on the argument passed.
350: */
351: public void setVisible(boolean visible) {
352: super .setVisible(visible);
353: for (int i = 0; i < _compsVec.size(); i++) {
354: HtmlComponent comp = getComponent(i);
355: comp.setVisible(visible);
356: }
357:
358: }
359:
360: /**
361: * Creates a String representation of the contents of the JspContainer
362: * @return - contents of container as a string representation
363: */
364: public String toString() {
365:
366: StringBuffer sBuff = new StringBuffer("[");
367: int compSize = getComponentCount();
368:
369: HtmlComponent c = null;
370: boolean commaFlag = false;
371: if (_compsVec.isEmpty()) {
372: sBuff.append("NULL");
373: }
374:
375: for (int i = 0; i < compSize; i++) {
376: c = (HtmlComponent) getComponent(i);
377: if (commaFlag == true) {
378: sBuff.append(",");
379: }
380:
381: String name = c.getName();
382: if (Util.isNull(name)) {
383: name = "NULL_NAME";
384: } else if (Util.isEmpty(name)) {
385: name = "EMPTY";
386: }
387: sBuff.append(name);
388: commaFlag = true;
389:
390: }
391: sBuff.append("]");
392: //return super.toString() + "\n" + getName();
393: return super .toString() + "\n" + sBuff.toString();
394: }
395:
396: /**
397: * override in sub classes to allow for controller logic to be attached to the container
398: */
399: public void initialize() {
400:
401: }
402:
403: /**
404: * Returns the controller this container is in
405: */
406: public JspController getController() {
407: return (JspController) getPage();
408: }
409:
410: /**
411: * Framework method, do not call directly
412: */
413: public boolean getDoInit() {
414: return _doInit;
415: }
416:
417: /**
418: * Framework method, do not call directly
419: */
420: public void setDoInit(boolean b) {
421: _doInit = b;
422: }
423:
424: }
|