Hi all,

I know I'm not addressing the exact issue, but just my two cents:

I needed to dynamically specify the src attribute of the img tag, and I 
couldn't find great examples for doing this. And I adopted the following:

WebMarkupContainer flag = new WebMarkupContainer("flag-image-path");
flag.add(new SimpleAttributeModifier("src", 
Application.get().getApplicationSettings().getContextPath() + File.separator + 
"images/flags/" + entry.getPhoto().getLocation().getCountry().toLowerCase() + 
".png"));
add(flag);

It worked but I wasn't deeply convinced with it. As I have to do this type of 
stuff quite often in my code, and, moreover, I thought it wasn't the cleanest 
way possible (I prefer to leave paths like 'images/flags' and '.png' in my HTML 
page), I wrote a WebComponent that maybe some people could find useful.

Basically, it watchs the 'src' attribute, looking for the @DYNAMIC@ placeholder 
that it inserts the specified value at java code level. I also added the 
external flag, to point to dynamic external image sources.

I use it a lot, although I don't know if it has a good "wicket approach". Let 
me know if anything's wrong with it. Here it goes:

public class DynamicPathImage extends WebComponent {
    
    private static final long serialVersionUID = 1L;
    
    private boolean externalSrc = false;

    public DynamicPathImage(String id, String text) {
        super(id, new Model(text));
    }

    @Override
    public void onComponentTag(final ComponentTag tag) {
        checkComponentTag(tag, "img");
        String dynamicText = getModelObjectAsString();
        super.onComponentTag(tag);
        
        String src = (String) tag.getAttributes().getString("src");
        if (src != null && src.contains("@DYNAMIC@")) {
            src = src.replaceAll("@DYNAMIC@", dynamicText);
        } else {
            src = dynamicText;
        }
        
        if (isExternalSrc()) {
            tag.put("src", src);
        } else {
            tag.put("src", 
Application.get().getApplicationSettings().getContextPath() + File.separator + 
src);
        }
    }

    public boolean isExternalSrc() {
        return externalSrc;
    }

    public void setExternalSrc(boolean externalSrc) {
        this.externalSrc = externalSrc;
    }
}

...and in my page I would call it like this:

// Set path for the country flag image

add(new DynamicPathImage("flag-image-path", 
entry.getPhoto().getLocation().getCountry().toLowerCase()));


If I need to include a dynamic path not in my Wicket-enabled application:

// Set path for the main photo
DynamicPathImage photo = new DynamicPathImage("entry-image-path", PHOTO_DIR_URL 
+ entry.getPhoto().getImagePath());
photo.setExternalSrc(true);
add(photo);

Thanks,
Francisco



        

        
                
___________________________________________________________________________ 
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions ! 
Profitez des connaissances, des opinions et des expériences des internautes sur 
Yahoo! Questions/Réponses 
http://fr.answers.yahoo.com
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to