Working upload of photos (individual and zip)
[photos] / src / main / java / org / wamblee / photos / wicket / PhotoPanel.java
index a0fb7c1d4b1453806da1c5087fe6e12c69828e4b..e47b19a59a2ef8bf5bdaa2fccc30f9fab9ea84f0 100644 (file)
@@ -64,30 +64,12 @@ public class PhotoPanel extends Panel {
         }
         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());
+        String parentPath = getParentPath();
 
         Link prevLink = new Link("prevLink") {
-            {
-                if (before == null) {
-                    setEnabled(false);
-                }
-            }
-
             @Override
             public void onClick() {
+                Photo before = getPrevPhoto();
                 if (before == null) {
                     return;
                 }
@@ -95,19 +77,19 @@ public class PhotoPanel extends Panel {
                 pars.put("path", before.getPath());
                 setResponsePage(HomePage.class, pars);
             }
+
+            @Override
+            public boolean isEnabled() {
+                return getPrevPhoto() != null;
+            }
         };
 
         add(prevLink);
 
         Link nextLink = new Link("nextLink") {
-            {
-                if (after == null) {
-                    setEnabled(false);
-                }
-            }
-
             @Override
             public void onClick() {
+                Photo after = getNextPhoto();
                 if (after == null) {
                     return;
                 }
@@ -115,6 +97,11 @@ public class PhotoPanel extends Panel {
                 pars.put("path", after.getPath());
                 setResponsePage(HomePage.class, pars);
             }
+
+            @Override
+            public boolean isEnabled() {
+                return getNextPhoto() != null;
+            }
         };
 
         add(nextLink);
@@ -130,7 +117,7 @@ public class PhotoPanel extends Panel {
             public void onClick() {
                 PageParameters pars = new PageParameters();
 
-                pars.put("path", parentPath);
+                pars.put("path", getParentPath());
                 pars.put("index", 0);
                 setResponsePage(HomePage.class, pars);
             }
@@ -138,10 +125,38 @@ public class PhotoPanel extends Panel {
 
         add(parentLink);
 
-        Image image = new Image("photo", new ByteArrayResource("image/jpeg", getData(photo)));
+        Image image = new Image("photo", new ByteArrayResource("image/jpeg", getData(getPhoto())));
         add(image);
     }
 
+    private Photo getPhoto() {
+        PhotoEntry current = authorized.getEntry(path);
+        if (current instanceof Album) {
+            throw new RuntimeException("PhotoPanel can only show a photo: " + current.getClass().getName());
+        }
+        return (Photo) current;
+    }
+
+    private Photo getPrevPhoto() {
+        return getAlbum().findPhotoBefore(getPhoto().getId());
+    }
+
+    private Photo getNextPhoto() {
+        return getAlbum().findPhotoAfter(getPhoto().getId());
+    }
+
+    private Album getAlbum() {
+        return (Album) getAuthorizedPhotos().getEntry(getParentPath());
+    }
+
+    private String getParentPath() {
+        String parentPath = path.substring(0, path.lastIndexOf("/"));
+        if (parentPath.length() == 0) {
+            parentPath = "/";
+        }
+        return parentPath;
+    }
+
     private byte[] getData(Photo aPhoto) {
         try (InputStream is = aPhoto.getPhoto()) {
             return getBytes(is);
@@ -162,4 +177,8 @@ public class PhotoPanel extends Panel {
         }
         return bos.toByteArray();
     }
+
+    private Album getAuthorizedPhotos() {
+        return authorized;
+    }
 }
\ No newline at end of file