Boatwrect
December 13th, 2009 | posted by Alpha Epsilon Delta
View in gallery, Full resolution view, click image above.A song for Melissa
- What's good
- What's bad
- What you like
- What you think
- Give me answer
- What does it mean to you...else?
package com.hddigitalworks;
import org.apache.wicket.AttributeModifier;
public class FlickrPage extends WebPage {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(FlickrPage.class);
private String tags; // flickr search tags, received from form field
WebMarkupContainer photosDiv; // something for Ajax target to add to for update
public FlickrPage(final PageParameters parameters) {
add(new StyleSheetReference("pageCss", getClass(), "flickr.css"));
Form form = new Form("form", new CompoundPropertyModel(this));
add(form);
FeedbackPanel fp = new FeedbackPanel("feedback");
fp.setOutputMarkupPlaceholderTag(true);
fp.setMarkupId("feedback");
form.add(fp);
form.add(new TextField("tags").setRequired(true));
AjaxFallbackButton submitButton = new AjaxFallbackButton("submitButton", form) {
private static final long serialVersionUID = 1L;
@Override protected void onSubmit(AjaxRequestTarget target, Form f) {
renderPhotos(tags);
if (target != null) {
target.addComponent(f.get("feedback")); // clear error feedback if any
target.addComponent(photosDiv); // update our photos display
target.appendJavascript("new Effect.BlindDown('photos', {duration:3});"
+ "Element.hide('spinner');");
}
}
@Override protected void onError(AjaxRequestTarget target, Form f) {
target.addComponent(f.get("feedback")); // show updated error feedback
target.appendJavascript("Element.hide('spinner');");
// target.addComponent(photosDiv); // keep to show existing photo display on error
}
};
// add effects for when submit button onclick
String onClickJavascript = "Element.show('spinner'); new Effect.BlindUp('photos');";
submitButton.add(new AttributeModifier("onclick", true, new Model(onClickJavascript)) {
private static final long serialVersionUID = 1L;
@Override protected String newValue(String oldValue, String newValue) {
return newValue + oldValue;
}
});
form.add(submitButton);
photosDiv = new WebMarkupContainer("photosDiv");
photosDiv.setOutputMarkupId(true);
photosDiv.setMarkupId("photos"); // use our own id instead of let Wicket generate
form.add(photosDiv);
// Initially there is no photo to display
// so add a temporary place holder component to make Wicket happy
photosDiv.add(new WebMarkupContainer("photoEntry").setVisible(false));
}
private void renderPhotos(final String tags) {
IModel list = new LoadableDetachableModel() {
private static final long serialVersionUID = 1L;
@Override protected Object load() {
return searchFlickr(tags, 24, 1);
}
};
ListView photoList = new ListView("photoEntry", list) {
private static final long serialVersionUID = 1L;
@Override protected void populateItem(ListItem item) {
Photo photo = (Photo) item.getModelObject();
item.add(new Thumbnail("t", photo));
}
};
photosDiv.replace(photoList);
}
private PhotoList searchFlickr(String tags, int perPage, int page) {
PhotoList result = null;
try {
Flickr f = new Flickr("xx-get-your-own-key-xx", new REST());
PhotosInterface photos = f.getPhotosInterface();
SearchParameters params = new SearchParameters();
params.setTags(new String[] { tags });
result = photos.search(params, perPage, page); // page start from 1?
} catch(Exception e) {
log.error("Flicker communication error [" + e.getMessage() + "]", e);
throw new RuntimeException(e);
}
return result;
}
}