*/
package org.wamblee.photos.wicket;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.Serializable;
import java.util.logging.Logger;
import javax.inject.Inject;
+import javax.servlet.ServletContext;
import org.apache.wicket.PageParameters;
+import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
-import org.apache.wicket.resource.ByteArrayResource;
+import org.wamblee.general.ValueHolder;
import org.wamblee.photos.model.Album;
import org.wamblee.photos.model.Photo;
import org.wamblee.photos.model.PhotoEntry;
*/
public class AlbumPanel extends Panel {
+ public static class MyValueHolder<T extends Serializable> extends ValueHolder<T> implements Serializable {
+ public MyValueHolder(T aValue) {
+ super(aValue);
+ }
+
+ public MyValueHolder() {
+ super();
+ }
+ }
+
private static final Logger LOGGER = Logger.getLogger(AlbumPanel.class.getName());
private static final long serialVersionUID = 1L;
@AuthorizedPhotos
private transient Album authorized;
+ @Inject
+ private ServletContext context;
+
private class SerializableEntryLink extends Link {
private String path;
@Override
public void onClick() {
- System.out.println("Entry " + path + " was clicked");
PageParameters pars = new PageParameters();
pars.put("path", path);
index = 0;
}
- PhotoEntry current = authorized.getEntry(path);
- if (current instanceof Photo) {
- throw new RuntimeException("AlbumPanel can only show album: " + current.getClass().getName());
- }
- final Album album = (Album) current;
-
Link prevLink = new Link("prevLink") {
- {
- if (index - MAX_ROWS * MAX_COLUMNS < 0) {
- setEnabled(false);
- }
- }
-
@Override
public void onClick() {
PageParameters pars = new PageParameters();
pars.put("index", index - MAX_ROWS * MAX_COLUMNS);
setResponsePage(HomePage.class, pars);
}
+
+ @Override
+ public boolean isEnabled() {
+ return index - MAX_ROWS * MAX_COLUMNS >= 0;
+ }
};
add(prevLink);
// Avoid implicit references to the album to keep the link objects
// small and serializable.
- final int albumSize = album.size();
+ final int albumSize = getAlbum().size();
Link nextLink = new Link("nextLink") {
- {
- if (index + MAX_ROWS * MAX_COLUMNS >= albumSize) {
- setEnabled(false);
- }
- }
-
@Override
public void onClick() {
PageParameters pars = new PageParameters();
pars.put("index", index + MAX_ROWS * MAX_COLUMNS);
setResponsePage(HomePage.class, pars);
}
+
+ @Override
+ public boolean isEnabled() {
+ return index + MAX_ROWS * MAX_COLUMNS < albumSize;
+ }
};
add(nextLink);
RepeatingView pageLinks = new RepeatingView("pageLinks");
add(pageLinks);
+ Album album = getAlbum();
for (int i = 0; i < album.size() / MAX_ROWS / MAX_COLUMNS; i++) {
final int istart = i * MAX_ROWS * MAX_COLUMNS;
Link pageLink = new Link("pageLink") {
pageLinks.add(container);
}
- int ientry = index;
- int irow = 0;
- RepeatingView row = new RepeatingView("row");
- add(row);
- while (irow < MAX_ROWS && ientry < album.size()) {
- int icolumn = 0;
- WebMarkupContainer columns = new WebMarkupContainer(row.newChildId());
- row.add(columns);
- RepeatingView column = new RepeatingView("column");
- columns.add(column);
- while (icolumn < MAX_COLUMNS && ientry < album.size()) {
- WebMarkupContainer thumbnail = new WebMarkupContainer(column.newChildId());
- column.add(thumbnail);
-
- final PhotoEntry entry = album.getEntry(ientry);
- Link link = new SerializableEntryLink("thumbnail", entry.getPath());
- thumbnail.add(link);
- ImageData data = getData(entry);
-
- // TODO very inefficient. all data is loaded when generating the page.
- link.add(new Image("image", new ByteArrayResource(data.getContentType(), data.getData())));
-
- link.add(new Label("name", album.getEntry(ientry).getId()));
- icolumn++;
- ientry++;
+ RepeatingView row = new RepeatingView("row") {
+ @Override
+ protected void onPopulate() {
+ removeAll();
+ final ValueHolder<Integer> ientry = new MyValueHolder<Integer>(index);
+ int irow = 0;
+ Album album = getAlbum();
+ while (irow < MAX_ROWS && ientry.getValue() < album.size()) {
+ WebMarkupContainer columns = new WebMarkupContainer(newChildId());
+ add(columns);
+ RepeatingView column = new RepeatingView("column") {
+ @Override
+ protected void onPopulate() {
+ removeAll();
+ int icolumn = 0;
+ Album album = getAlbum();
+ while (icolumn < MAX_COLUMNS && ientry.getValue() < album.size()) {
+ WebMarkupContainer thumbnail = new WebMarkupContainer(newChildId());
+ add(thumbnail);
+
+ final PhotoEntry entry = album.getEntry(ientry.getValue());
+ Link link = new SerializableEntryLink("thumbnail", entry.getPath());
+ thumbnail.add(link);
+
+ final ValueHolder<String> pathinfo = new MyValueHolder<String>();
+ if (entry instanceof Photo) {
+ pathinfo.setValue("/image/thumbnail/" + entry.getPath());
+ } else {
+ pathinfo.setValue("/image/resource/folder.png");
+ }
+ link.add(new Image("image") {
+ @Override
+ protected void onComponentTag(ComponentTag tag) {
+ tag.put("src", context.getContextPath() + pathinfo.getValue());
+ }
+ });
+
+ link.add(new Label("name", album.getEntry(ientry.getValue()).getId()));
+ icolumn++;
+ ientry.setValue(ientry.getValue() + 1);
+ }
+ }
+ };
+ columns.add(column);
+ irow++;
+ }
}
- irow++;
- }
- }
-
- public static final class ImageData {
- private String contentType;
- private byte[] data;
-
- public ImageData(String aContentType, byte[] aData) {
- contentType = aContentType;
- data = aData;
- }
-
- public String getContentType() {
- return contentType;
- }
+ };
- public byte[] getData() {
- return data;
- }
- }
+ add(row);
- private ImageData getData(PhotoEntry aEntry) {
- if (aEntry instanceof Photo) {
- return getData((Photo) aEntry);
- } else if (aEntry instanceof Album) {
- return getData((Album) aEntry);
- } else {
- throw new RuntimeException("Unsupported type " + aEntry.getClass().getName());
- }
- }
+ // upload panel
+ if (path.equals("/"))
- private ImageData getData(Photo aPhoto) {
- try (InputStream is = aPhoto.getThumbNail()) {
- return new ImageData("image/jpeg", getBytes(is));
- }
- catch (IOException e) {
- // to improve.
- throw new RuntimeException("Cannot read photo", e);
- }
- }
+ {
+ add(new WebMarkupContainer("uploadPanel"));
+ } else
- private byte[] getBytes(InputStream is) throws IOException {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- byte[] block = new byte[1024];
- int n = is.read(block);
- while (n > 0) {
- bos.write(block, 0, n);
- n = is.read(block);
+ {
+ add(new UploadPanel("uploadPanel", path));
}
- return bos.toByteArray();
}
- private ImageData getData(Album aAlbum) {
- try (InputStream is = getClass().getResourceAsStream("folder.png")) {
- return new ImageData("image/png", getBytes(is));
- }
- catch (IOException e) {
- throw new RuntimeException("Cannot read album jpg", e);
+ private Album getAlbum() {
+ PhotoEntry current = authorized.getEntry(path);
+ if (current instanceof Photo) {
+ throw new RuntimeException("AlbumPanel can only show album: " + current.getClass().getName());
}
+ return (Album) current;
}
}
\ No newline at end of file