001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library 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 GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.swing.layouts;
019:
020: import java.util.*;
021:
022: import net.ar.webonswing.render.*;
023: import net.ar.webonswing.render.markup.*;
024: import net.ar.webonswing.render.templates.*;
025: import net.ar.webonswing.render.templates.html.*;
026: import net.ar.webonswing.visitor.*;
027:
028: import org.apache.commons.logging.*;
029:
030: public class TemplateGridBody implements TemplateBody {
031: public TemplateGridBody(int aRows, int aCols,
032: Template aFirstTemplate) {
033: theFirstTemplate = aFirstTemplate;
034: theSecondTemplate = aFirstTemplate;
035: theRows = aRows;
036: theCols = aCols;
037: }
038:
039: protected StringBuffer theReplacedBodyString = new StringBuffer();
040: protected Template theFirstTemplate;
041: protected Template theSecondTemplate;
042: protected int theRows;
043: protected int theCols;
044:
045: public TemplateGridBody(int aRows, int aCols,
046: Template aFirstTemplate, Template aSecondTemplate) {
047: theFirstTemplate = aFirstTemplate;
048: theSecondTemplate = aSecondTemplate;
049: theRows = aRows;
050: theCols = aCols;
051: }
052:
053: public void replace(List theElements) {
054: try {
055: int theRowCount = 0;
056: int theColCount = 0;
057: StringBuffer theRowContent = new StringBuffer();
058: theReplacedBodyString = new StringBuffer();
059: Template theTemplate = (Template) ((PublicCloneable) theFirstTemplate)
060: .clone();
061:
062: for (Iterator i = theElements.iterator(); i.hasNext();) {
063: theRowContent.append(new ContentRenderer(
064: ((DefaultTemplateElement) i.next())
065: .getContent()).getResult());
066:
067: if (theColCount == theCols - 1 || !i.hasNext()) {
068: theTemplate
069: .addElement(new IdTagTemplateElement(
070: "theContent",
071: new Tag("span")
072: .addElementToContainer(new TagContent(
073: theRowContent))));
074: theReplacedBodyString
075: .append(new TextTemplateRenderer(
076: theTemplate).getResult());
077: theRowContent = new StringBuffer();
078: theColCount = 0;
079: theRowCount++;
080:
081: if ((theRowCount % 2) == 1)
082: theTemplate = (Template) ((PublicCloneable) theSecondTemplate)
083: .clone();
084: else
085: theTemplate = (Template) ((PublicCloneable) theFirstTemplate)
086: .clone();
087: } else
088: theColCount++;
089: }
090: } catch (CloneNotSupportedException e) {
091: LogFactory.getLog(TemplateGridBody.class).error("", e);
092: }
093: }
094:
095: public Object getResult() {
096: return theReplacedBodyString;
097: }
098:
099: public Object clone() throws CloneNotSupportedException {
100: return super.clone();
101: }
102: }
|