From: Erik Brakkee Date: Sun, 22 Sep 2013 20:02:47 +0000 (+0200) Subject: Full navigation is working including viewing individual photos. X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=3aabf590ddc0719004ef84ccdb95e4af45f2574b;p=photos Full navigation is working including viewing individual photos. --- diff --git a/src/main/java/org/wamblee/photos/wicket/AlbumPanel.html b/src/main/java/org/wamblee/photos/wicket/AlbumPanel.html index 5b6011b..0df1af4 100644 --- a/src/main/java/org/wamblee/photos/wicket/AlbumPanel.html +++ b/src/main/java/org/wamblee/photos/wicket/AlbumPanel.html @@ -18,11 +18,11 @@

Prev -     +   pageno -     +   Next Parent
diff --git a/src/main/java/org/wamblee/photos/wicket/AlbumPanel.java b/src/main/java/org/wamblee/photos/wicket/AlbumPanel.java index 57e95c7..0c7841c 100644 --- a/src/main/java/org/wamblee/photos/wicket/AlbumPanel.java +++ b/src/main/java/org/wamblee/photos/wicket/AlbumPanel.java @@ -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); } } diff --git a/src/main/java/org/wamblee/photos/wicket/HomePage.java b/src/main/java/org/wamblee/photos/wicket/HomePage.java index a5d42fa..95290d3 100644 --- a/src/main/java/org/wamblee/photos/wicket/HomePage.java +++ b/src/main/java/org/wamblee/photos/wicket/HomePage.java @@ -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 index 0000000..cd0f3bb --- /dev/null +++ b/src/main/java/org/wamblee/photos/wicket/PhotoPanel.html @@ -0,0 +1,31 @@ + + + Wicket Quickstart Archetype Homepage + + +Wicket Quickstart Archetype Homepage +
+
+ + +Message here. + + + +
+ +
+
+ + + 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 index 0000000..a0fb7c1 --- /dev/null +++ b/src/main/java/org/wamblee/photos/wicket/PhotoPanel.java @@ -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