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.render.templates.html;
019:
020: import java.awt.*;
021: import java.util.*;
022: import java.util.List;
023:
024: import net.ar.webonswing.render.*;
025: import net.ar.webonswing.render.markup.*;
026: import net.ar.webonswing.render.templates.*;
027: import net.ar.webonswing.wrapping.*;
028:
029: /**
030: * Plantilla que permite ubicar los componentes de Swing sin usar plantillas de HTML,
031: * mediante divs anidados logra que los componentes se grafiquen en la coordenadas que
032: * cada uno especifica.
033: *
034: * @author Fernando Damian Petrola
035: */
036: public class HtmlDivsTemplate extends DefaultTemplate {
037: public HtmlDivsTemplate() {
038: super (new HtmlDivsTemplateBody());
039: }
040: }
041:
042: class HtmlDivsTemplateBody implements TemplateBody {
043: StringBuffer theReplacedBodyString;
044:
045: public HtmlDivsTemplateBody() {
046: theReplacedBodyString = new StringBuffer();
047: }
048:
049: public void replace(List theElements) {
050: MarkupContainer theMarkupContainer = new MarkupContainer();
051:
052: if (!theElements.isEmpty()) {
053: Tag theMainTag = new Tag("div");
054: theMainTag.addAttribute(new TagAttribute("style",
055: "position:relative"));
056:
057: for (Iterator i = theElements.iterator(); i.hasNext();) {
058: DefaultTemplateElement theElement = (DefaultTemplateElement) i
059: .next();
060: Rectangle theBounds = ((VisualComponent) theElement
061: .getId()).getBounds();
062:
063: Tag theElementTag = new Tag("div");
064:
065: TagStyleAttribute theStyle = new TagStyleAttribute();
066: theStyle.addElement("position", "absolute");
067: theStyle.addElement("top",
068: theBounds.getY() > 0 ? (int) theBounds.getY()
069: : 0);
070: theStyle.addElement("left",
071: theBounds.getX() > 0 ? (int) theBounds.getX()
072: : 0);
073: theElementTag.addAttribute(theStyle);
074:
075: theElementTag
076: .addTextToContainer(new ContentRenderer(
077: theElement.getContent()).getResult()
078: .toString());
079:
080: theMainTag.getTheMarkupContainer().addElement(
081: theElementTag);
082: }
083:
084: theMarkupContainer.addElement(theMainTag);
085: }
086:
087: theReplacedBodyString = new StringBuffer(
088: new TextMarkupRenderer(theMarkupContainer).getResult()
089: .toString());
090: }
091:
092: public Object getResult() {
093: return theReplacedBodyString;
094: }
095:
096: public Object clone() throws CloneNotSupportedException {
097: HtmlDivsTemplateBody theTemplateBody = (HtmlDivsTemplateBody) super
098: .clone();
099: theTemplateBody.theReplacedBodyString = new StringBuffer(
100: theReplacedBodyString.toString());
101:
102: return theTemplateBody;
103: }
104:
105: }
|