By the way, here is my BImage component.
Just beware of the package structure... Maybe it could be included in a
contrib package, or even in the core ? (this code is not much of a
revolution :)
Regards,
Franck
/*
* HTMLImageRenderer.java
*
* Created on 9 dec 2006
* Copyright (c) 2006 mecadu.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.mecadu.core.pres.comp.renderer.html;
import org.apache.log4j.Logger;
import org.barracudamvc.core.comp.BComponent;
import org.barracudamvc.core.comp.NoSuitableRendererException;
import org.barracudamvc.core.comp.RenderException;
import org.barracudamvc.core.comp.UnsupportedFormatException;
import org.barracudamvc.core.comp.View;
import org.barracudamvc.core.comp.ViewContext;
import org.barracudamvc.core.comp.renderer.html.HTMLComponentRenderer;
import org.mecadu.core.pres.comp.BImage;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.html.HTMLImageElement;
/**
* This class handles the default rendering of image into an HTML view.
*/
public class HTMLImageRenderer extends HTMLComponentRenderer {
protected static final Logger logger =
Logger.getLogger(HTMLImageRenderer.class.getName());
public Node createDefaultNode(Document doc, BComponent comp, ViewContext
vc) throws UnsupportedFormatException {
if (vc.getTemplateNode() instanceof HTMLImageElement) {
return super.createDefaultNode(doc, comp, vc);
}
Node defaultNode = doc.createElement("IMG");
if (logger.isInfoEnabled()) logger.info("Creating default
node:"+defaultNode);
return defaultNode;
}
/**
*
*/
public void renderComponent(BComponent comp, View view, ViewContext vc)
throws RenderException {
//make sure the component is an image component
if (!(comp instanceof BImage))
throw new NoSuitableRendererException("This renderer can only
render BImage components");
//show what we're doing
showNodeInterfaces(view, logger);
//first, allow the parent class to do anything it needs to
super.renderComponent(comp, view, vc);
BImage bimage = (BImage) comp;
String alt = bimage.getAlt();
String title = bimage.getTitle();
String url = bimage.getUrl();
String height = bimage.getHeight();
String width = bimage.getWidth();
Node node = view.getNode();
//HTMLElement Interface
//---------------------
//Supported Elements:
//..HTMLImageElement
if (node instanceof HTMLImageElement) {
if (logger.isInfoEnabled()) logger.info("Rendering based on
HTMLImageElement interface...");
((HTMLImageElement) node).setAlt(alt);
((HTMLImageElement) node).setTitle(title);
((HTMLImageElement) node).setSrc(url);
((HTMLImageElement) node).setHeight(height);
((HTMLImageElement) node).setWidth(width);
} else {
String errmsg = "Node does not implement HTMLImageElement and
cannot be rendered: "+node;
logger.warn(errmsg);
throw new NoSuitableRendererException(errmsg);
}
}
}
/*
* HTMLImageRenderer.java
*
* Created on 9 dec 2006
* Copyright (c) 2006 mecadu.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.mecadu.core.pres.comp;
import org.apache.log4j.Logger;
import org.barracudamvc.core.comp.BComponent;
import org.barracudamvc.core.comp.ViewContext;
import org.barracudamvc.core.comp.renderer.Renderer;
import org.barracudamvc.core.comp.renderer.RendererFactory;
import org.mecadu.core.pres.comp.renderer.html.HTMLImageRenderer;
import org.w3c.dom.html.HTMLElement;
public class BImage extends BComponent {
//public vars
protected static final Logger logger =
Logger.getLogger(BImage.class.getName());
//private vars
protected String alt = "no alt text";
protected String title = null;
protected String url = null;
protected String width = null;
protected String height = null;
//--------------- Constructors -------------------------------
/**
* Public noargs constructor
*/
public BImage() {}
/**
* Public constructor which creates the component and sets the text,
* and target values. This link will fire the default action event (unless
* you manually specify an action).
*
* @param itext the text string that backs this component
*/
public BImage(String ialt, String iurl) {
setAlt(ialt);
setUrl(iurl);
// Let strict spec compliant engines (ie Gecko)
// display alt text on mouse over
setTitle(ialt);
}
/**
* Public constructor which creates the component and sets the text,
* and target values. This link will fire the default action event (unless
* you manually specify an action).
*
* @param itext the text string that backs this component
*/
public BImage(String ialt, String ititle, String iurl) {
setAlt(ialt);
setTitle(ititle);
setUrl(iurl);
}
/**
* Public constructor which creates the component and sets the text,
* and target values. This link will fire the default action event (unless
* you manually specify an action).
*
* @param itext the text string that backs this component
*/
public BImage(String ialt, String iurl, String iheight, String iwidth) {
setAlt(ialt);
setUrl(iurl);
setHeight(iheight);
setWidth(iwidth);
// Let strict spec compliant engines (ie Gecko)
// display alt text on mouse over
setTitle(ialt);
}
/**
* Public constructor which creates the component and sets the text,
* and target values. This link will fire the default action event (unless
* you manually specify an action).
*
* @param itext the text string that backs this component
*/
public BImage(String ialt, String ititle, String iurl, String iheight,
String iwidth) {
setAlt(ialt);
setTitle(ititle);
setUrl(iurl);
setHeight(iheight);
setWidth(iwidth);
}
//--------------- Renderer -----------------------------------
/**
* Default component renderer factory registrations
*/
static {
HTMLRendererFactory rfHTML = new HTMLRendererFactory();
installRendererFactory(rfHTML, BImage.class, HTMLElement.class);
}
/**
* HTML RendererFactory
*/
static class HTMLRendererFactory implements RendererFactory {
public Renderer getInstance() {return new HTMLImageRenderer();}
}
//--------------- BImage --------------------------------------
/**
* Set the text for this particular component
*
* @param itext the text representation of this component
*/
public BImage setAlt(String itext) {
alt = itext;
invalidate();
return this;
}
/**
* Get the text for this particular component
*
* @return the text for this particular component
*/
public String getAlt() {
return alt;
}
/**
* Set the text for this particular component
*
* @param itext the text representation of this component
*/
public BImage setTitle(String itext) {
title = itext;
invalidate();
return this;
}
/**
* Get the text for this particular component
*
* @return the text for this particular component
*/
public String getTitle() {
return title;
}
/**
* Set the target for this particular component
*
* @param itarget the ext representation of the target
*/
public BImage setUrl(String iurl) {
url = iurl;
invalidate();
return this;
}
/**
* Get the target for this particular component
*
* @return the target for this particular component
*/
public String getUrl() {
return url;
}
/**
* Set the target for this particular component
*
* @param itarget the ext representation of the target
*/
public BImage setHeight(String iheight) {
height = iheight;
invalidate();
return this;
}
/**
* Get the target for this particular component
*
* @return the target for this particular component
*/
public String getHeight() {
return height;
}
/**
* Set the target for this particular component
*
* @param itarget the ext representation of the target
*/
public BImage setWidth(String iwidth) {
width = iwidth;
invalidate();
return this;
}
/**
* Get the target for this particular component
*
* @return the target for this particular component
*/
public String getWidth() {
return width;
}
/**
* if has vc, but no views: render as an <a> link, otherwise use
* super.toString(ViewContext)
*
* @see super#toString(ViewContext)
*/
public String toString(ViewContext vc) {
//csc_122205_1 - added (jrk_122505_01 moved from AbstractBComponent to
here)
//if the component HAS a view context, but DOESN'T have any views, then
//render as a link (this shouldn't break anything, since when
TemplateHelper
//gets a component back it creates a view for it. Basically, this allows
//us to inline BLinks even when we are not using DOMs. A _better_ way
of
//doing this would be to modify the renderer to work w/ out a dom, but
//that's probably easier said then done. So for now, we'll go w/ this.
if (vc != null && !hasViews()) {
return this.getAlt();
} else {
return super.toString(vc);
}
}
}
--
Barracuda mailing list
Barracuda@xxxxxxxxxxxxx
http://www.objectweb.org/wws/lists/projects/barracuda