X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fwamblee%2Fphotos%2Fwicket%2FAlbumPanel.java;h=0b9666d900a9fbd67112c783e7cfa976f0ca5af2;hb=HEAD;hp=093ed15644faf3b8aaef9c91093dedebdf2d3abc;hpb=8b1bc175311d80b5ca67d8f6ee8a266428cb5fa3;p=photos diff --git a/src/main/java/org/wamblee/photos/wicket/AlbumPanel.java b/src/main/java/org/wamblee/photos/wicket/AlbumPanel.java index 093ed15..0b9666d 100644 --- a/src/main/java/org/wamblee/photos/wicket/AlbumPanel.java +++ b/src/main/java/org/wamblee/photos/wicket/AlbumPanel.java @@ -15,20 +15,20 @@ */ 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; @@ -39,14 +39,29 @@ import org.wamblee.photos.model.plumbing.AuthorizedPhotos; */ public class AlbumPanel extends Panel { + public static class MyValueHolder extends ValueHolder 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; + public static final int MAX_ROWS = 5; + public static final int MAX_COLUMNS = 5; @Inject @AuthorizedPhotos private transient Album authorized; + @Inject + private ServletContext context; + private class SerializableEntryLink extends Link { private String path; @@ -58,7 +73,6 @@ public class AlbumPanel extends Panel { @Override public void onClick() { - System.out.println("Entry " + path + " was clicked"); PageParameters pars = new PageParameters(); pars.put("path", path); @@ -67,6 +81,7 @@ public class AlbumPanel extends Panel { } private String path; + private int index; /** * Constructor that is invoked when page is invoked without a session. @@ -81,100 +96,169 @@ public class AlbumPanel extends Panel { info("Invalid album '" + path + "', showing root album instead"); path = "/"; } + add(new Label("path", path)); - PhotoEntry current = authorized.getEntry(path); - - if (current instanceof Photo) { - throw new RuntimeException("AlbumPanel can only show album: " + current.getClass().getName()); + index = 0; + String indexString = parameters.getString("index", "0"); + try { + index = Integer.parseInt(indexString); + } + catch (NumberFormatException e) { + // use default value 0 + } + if (index < 0) { + index = 0; } - Album album = (Album) current; + Link prevLink = new Link("prevLink") { + @Override + public void onClick() { + PageParameters pars = new PageParameters(); + pars.put("path", path); + pars.put("index", index - MAX_ROWS * MAX_COLUMNS); + setResponsePage(HomePage.class, pars); + } - int ientry = 0; - int irow = 0; - RepeatingView row = new RepeatingView("row"); - add(row); - while (irow < 5 && 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 < 5 && 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++; + @Override + public boolean isEnabled() { + return index - MAX_ROWS * MAX_COLUMNS >= 0; } - irow++; - } - } + }; + add(prevLink); - public static final class ImageData { - private String contentType; - private byte[] data; + // Avoid implicit references to the album to keep the link objects + // small and serializable. + final int albumSize = getAlbum().size(); + Link nextLink = new Link("nextLink") { + @Override + public void onClick() { + PageParameters pars = new PageParameters(); + pars.put("path", path); + pars.put("index", index + MAX_ROWS * MAX_COLUMNS); + setResponsePage(HomePage.class, pars); + } - public ImageData(String aContentType, byte[] aData) { - contentType = aContentType; - data = aData; - } + @Override + public boolean isEnabled() { + return index + MAX_ROWS * MAX_COLUMNS < albumSize; + } + }; + add(nextLink); - public String getContentType() { - return contentType; - } + Link parentLink = new Link("parentLink") { + { + if ("/".equals(path)) { + setEnabled(false); + } + } - public byte[] getData() { - return data; - } - } + @Override + public void onClick() { + PageParameters pars = new PageParameters(); + String parentPath = path.substring(0, path.lastIndexOf("/")); + if (parentPath.length() == 0) { + parentPath = "/"; + } + pars.put("path", parentPath); + pars.put("index", 0); + setResponsePage(HomePage.class, pars); + } + }; + add(parentLink); - 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()); - } - } + RepeatingView pageLinks = new RepeatingView("pageLinks"); + add(pageLinks); + Album album = getAlbum(); + for (int i = 0; i < (album.size() + MAX_ROWS * MAX_COLUMNS - 1) / MAX_ROWS / MAX_COLUMNS; i++) { + final int istart = i * MAX_ROWS * MAX_COLUMNS; + Link pageLink = new Link("pageLink") { + { + if (istart == index) { + setEnabled(false); + } + } - 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); + @Override + public void onClick() { + PageParameters pars = new PageParameters(); + pars.put("path", path); + pars.put("index", istart); + setResponsePage(HomePage.class, pars); + } + }; + pageLink.add(new Label("label", (i + 1) + "")); + pageLinks.add(pageLink); + WebMarkupContainer container = new WebMarkupContainer(pageLinks.newChildId()); + container.add(pageLink); + pageLinks.add(container); } - } - 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); + RepeatingView row = new RepeatingView("row") { + @Override + protected void onPopulate() { + removeAll(); + final ValueHolder ientry = new MyValueHolder(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 pathinfo = new MyValueHolder(); + 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++; + } + } + }; + + add(row); + + // upload panel + if (path.equals("/")) { + add(new WebMarkupContainer("uploadPanel")); + add(new WebMarkupContainer("createFolderPanel")); + } else { + add(new UploadPanel("uploadPanel", path)); + add(new CreateFolderPanel("createFolderPanel", 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