Corrected page numbering.
[photos] / src / main / java / org / wamblee / photos / wicket / AlbumPanel.java
index 0c7841c7c32da4de8afc308dac7915e9a2bd2279..0b9666d900a9fbd67112c783e7cfa976f0ca5af2 100644 (file)
  */
 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,6 +39,16 @@ import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
  */
 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;
@@ -49,6 +59,9 @@ public class AlbumPanel extends Panel {
     @AuthorizedPhotos
     private transient Album authorized;
 
+    @Inject
+    private ServletContext context;
+
     private class SerializableEntryLink extends Link {
 
         private String path;
@@ -60,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);
 
@@ -98,19 +110,7 @@ public class AlbumPanel extends Panel {
             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();
@@ -118,19 +118,18 @@ public class AlbumPanel extends Panel {
                 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();
@@ -138,6 +137,11 @@ public class AlbumPanel extends Panel {
                 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);
 
@@ -164,7 +168,8 @@ public class AlbumPanel extends Panel {
 
         RepeatingView pageLinks = new RepeatingView("pageLinks");
         add(pageLinks);
-        for (int i = 0; i < album.size() / MAX_ROWS / MAX_COLUMNS; i++) {
+        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") {
                 {
@@ -181,98 +186,79 @@ public class AlbumPanel extends Panel {
                     setResponsePage(HomePage.class, pars);
                 }
             };
-            pageLink.add(new Label("label", i + ""));
+            pageLink.add(new Label("label", (i + 1) + ""));
             pageLinks.add(pageLink);
             WebMarkupContainer container = new WebMarkupContainer(pageLinks.newChildId());
             container.add(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);
+        // upload panel
+        if (path.equals("/")) {
+            add(new WebMarkupContainer("uploadPanel"));
+            add(new WebMarkupContainer("createFolderPanel"));
         } else {
-            throw new RuntimeException("Unsupported type " + aEntry.getClass().getName());
+            add(new UploadPanel("uploadPanel", path));
+            add(new CreateFolderPanel("createFolderPanel", path));
         }
     }
 
-    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);
-        }
-    }
-
-    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);
-        }
-        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