Full navigation is working including viewing individual photos.
authorErik Brakkee <erik@brakkee.org>
Sun, 22 Sep 2013 20:02:47 +0000 (22:02 +0200)
committerErik Brakkee <erik@brakkee.org>
Sun, 22 Sep 2013 20:02:47 +0000 (22:02 +0200)
src/main/java/org/wamblee/photos/wicket/AlbumPanel.html
src/main/java/org/wamblee/photos/wicket/AlbumPanel.java
src/main/java/org/wamblee/photos/wicket/HomePage.java
src/main/java/org/wamblee/photos/wicket/PhotoPanel.html [new file with mode: 0644]
src/main/java/org/wamblee/photos/wicket/PhotoPanel.java [new file with mode: 0644]

index 5b6011b6c794dc5cf04ea349b697e5ec13d55b8b..0df1af42ee18b0f067267999100264ea036423cf 100644 (file)
         <br/>
         <br/>
         <a href="#" wicket:id="prevLink">Prev</a>
-        &nbsp; &nbsp;
+        &nbsp;
         <span wicket:id="pageLinks">
             <a href="#" wicket:id="pageLink"><span wicket:id="label">pageno</span></a>
         </span>
-        &nbsp; &nbsp;
+        &nbsp;
         <a href="#" wicket:id="nextLink">Next</a>
         <a href="#" wicket:id="parentLink">Parent</a>
         <br/>
index 57e95c7983f11c796580976935612ec23096321e..0c7841c7c32da4de8afc308dac7915e9a2bd2279 100644 (file)
@@ -121,9 +121,12 @@ public class AlbumPanel extends Panel {
         };
         add(prevLink);
 
+        // Avoid implicit references to the album to keep the link objects
+        // small and serializable.
+        final int albumSize = album.size();
         Link nextLink = new Link("nextLink") {
             {
-                if (index + MAX_ROWS * MAX_COLUMNS >= album.size()) {
+                if (index + MAX_ROWS * MAX_COLUMNS >= albumSize) {
                     setEnabled(false);
                 }
             }
index a5d42faaa194aa655e12545e9a85f121a353c474..95290d34dd93dd021180864ef7afd2e70d95ebc0 100644 (file)
@@ -111,11 +111,9 @@ public class HomePage extends BasePage {
         PhotoEntry current = authorized.getEntry(path);
 
         if (current instanceof Photo) {
-            throw new RuntimeException("Photo entry viewing not yet implemented");
+            add(new PhotoPanel("content", parameters));
+        } else {
+            add(new AlbumPanel("content", parameters));
         }
-
-        Album album = (Album) current;
-
-        add(new AlbumPanel("content", parameters));
     }
 }
\ No newline at end of file
diff --git a/src/main/java/org/wamblee/photos/wicket/PhotoPanel.html b/src/main/java/org/wamblee/photos/wicket/PhotoPanel.html
new file mode 100644 (file)
index 0000000..cd0f3bb
--- /dev/null
@@ -0,0 +1,31 @@
+<html
+        xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
+<head>
+    <title>Wicket Quickstart Archetype Homepage</title>
+</head>
+<body>
+<strong>Wicket Quickstart Archetype Homepage</strong>
+<br/>
+<br/>
+
+
+<span wicket:id="message">Message here.</span>
+
+<wicket:panel>
+    <div id="navigation">
+        <h1>Photo <span class="path" wicket:id="path">/phot/entry/xxx</span></h1>
+        <br/>
+        <br/>
+        <a href="#" wicket:id="prevLink">Prev</a>
+        <a href="#" wicket:id="nextLink">Next</a>
+        <a href="#" wicket:id="parentLink">Parent</a>
+        <br/>
+        <br/>
+    </div>
+    <div id="photo">
+        <img src="#" wicket:id="photo" border="0" width="65%"/>
+    </div>
+</wicket:panel>
+
+</body>
+</html>
diff --git a/src/main/java/org/wamblee/photos/wicket/PhotoPanel.java b/src/main/java/org/wamblee/photos/wicket/PhotoPanel.java
new file mode 100644 (file)
index 0000000..a0fb7c1
--- /dev/null
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2005-2010 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.photos.wicket;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.logging.Logger;
+import javax.inject.Inject;
+
+import org.apache.wicket.PageParameters;
+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.resource.ByteArrayResource;
+import org.wamblee.photos.model.Album;
+import org.wamblee.photos.model.Photo;
+import org.wamblee.photos.model.PhotoEntry;
+import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
+
+/**
+ * Homepage
+ */
+public class PhotoPanel extends Panel {
+
+    private static final Logger LOGGER = Logger.getLogger(PhotoPanel.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;
+
+    private String path;
+
+    /**
+     * Constructor that is invoked when page is invoked without a session.
+     *
+     * @param parameters Page parameters
+     */
+    public PhotoPanel(String aId, final PageParameters parameters) throws Exception {
+        super(aId);
+
+        path = parameters.getString("path", "/");
+        if (!path.startsWith("/")) {
+            info("Invalid album '" + path + "', showing root album instead");
+            path = "/";
+        }
+        add(new Label("path", path));
+
+        PhotoEntry current = authorized.getEntry(path);
+        if (current instanceof Album) {
+            throw new RuntimeException("PhotoPanel can only show a photo: " + current.getClass().getName());
+        }
+        final Photo photo = (Photo) current;
+
+        String parentPath_ = path.substring(0, path.lastIndexOf("/"));
+        if (parentPath_.length() == 0) {
+            parentPath_ = "/";
+        }
+        final String parentPath = parentPath_;
+        final Album parent = (Album) authorized.getEntry(parentPath);
+        final Photo before = parent.findPhotoBefore(photo.getId());
+        final Photo after = parent.findPhotoAfter(photo.getId());
+
+        Link prevLink = new Link("prevLink") {
+            {
+                if (before == null) {
+                    setEnabled(false);
+                }
+            }
+
+            @Override
+            public void onClick() {
+                if (before == null) {
+                    return;
+                }
+                PageParameters pars = new PageParameters();
+                pars.put("path", before.getPath());
+                setResponsePage(HomePage.class, pars);
+            }
+        };
+
+        add(prevLink);
+
+        Link nextLink = new Link("nextLink") {
+            {
+                if (after == null) {
+                    setEnabled(false);
+                }
+            }
+
+            @Override
+            public void onClick() {
+                if (after == null) {
+                    return;
+                }
+                PageParameters pars = new PageParameters();
+                pars.put("path", after.getPath());
+                setResponsePage(HomePage.class, pars);
+            }
+        };
+
+        add(nextLink);
+
+        Link parentLink = new Link("parentLink") {
+            {
+                if ("/".equals(path)) {
+                    setEnabled(false);
+                }
+            }
+
+            @Override
+            public void onClick() {
+                PageParameters pars = new PageParameters();
+
+                pars.put("path", parentPath);
+                pars.put("index", 0);
+                setResponsePage(HomePage.class, pars);
+            }
+        };
+
+        add(parentLink);
+
+        Image image = new Image("photo", new ByteArrayResource("image/jpeg", getData(photo)));
+        add(image);
+    }
+
+    private byte[] getData(Photo aPhoto) {
+        try (InputStream is = aPhoto.getPhoto()) {
+            return 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();
+    }
+}
\ No newline at end of file