2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.photos.wicket;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.logging.Logger;
22 import javax.inject.Inject;
24 import org.apache.wicket.PageParameters;
25 import org.apache.wicket.markup.html.basic.Label;
26 import org.apache.wicket.markup.html.image.Image;
27 import org.apache.wicket.markup.html.link.Link;
28 import org.apache.wicket.markup.html.panel.Panel;
29 import org.apache.wicket.resource.ByteArrayResource;
30 import org.wamblee.photos.model.Album;
31 import org.wamblee.photos.model.Photo;
32 import org.wamblee.photos.model.PhotoEntry;
33 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
38 public class PhotoPanel extends Panel {
40 private static final Logger LOGGER = Logger.getLogger(PhotoPanel.class.getName());
42 private static final long serialVersionUID = 1L;
43 public static final int MAX_ROWS = 5;
44 public static final int MAX_COLUMNS = 5;
48 private transient Album authorized;
53 * Constructor that is invoked when page is invoked without a session.
55 * @param parameters Page parameters
57 public PhotoPanel(String aId, final PageParameters parameters) throws Exception {
60 path = parameters.getString("path", "/");
61 if (!path.startsWith("/")) {
62 info("Invalid album '" + path + "', showing root album instead");
65 add(new Label("path", path));
67 String parentPath = getParentPath();
69 Link prevLink = new Link("prevLink") {
71 public void onClick() {
72 Photo before = getPrevPhoto();
76 PageParameters pars = new PageParameters();
77 pars.put("path", before.getPath());
78 setResponsePage(HomePage.class, pars);
82 public boolean isEnabled() {
83 return getPrevPhoto() != null;
89 Link nextLink = new Link("nextLink") {
91 public void onClick() {
92 Photo after = getNextPhoto();
96 PageParameters pars = new PageParameters();
97 pars.put("path", after.getPath());
98 setResponsePage(HomePage.class, pars);
102 public boolean isEnabled() {
103 return getNextPhoto() != null;
109 Link parentLink = new Link("parentLink") {
111 if ("/".equals(path)) {
117 public void onClick() {
118 PageParameters pars = new PageParameters();
120 pars.put("path", getParentPath());
121 pars.put("index", 0);
122 setResponsePage(HomePage.class, pars);
128 Image image = new Image("photo", new ByteArrayResource("image/jpeg", getData(getPhoto())));
132 private Photo getPhoto() {
133 PhotoEntry current = authorized.getEntry(path);
134 if (current instanceof Album) {
135 throw new RuntimeException("PhotoPanel can only show a photo: " + current.getClass().getName());
137 return (Photo) current;
140 private Photo getPrevPhoto() {
141 return getAlbum().findPhotoBefore(getPhoto().getId());
144 private Photo getNextPhoto() {
145 return getAlbum().findPhotoAfter(getPhoto().getId());
148 private Album getAlbum() {
149 return (Album) getAuthorizedPhotos().getEntry(getParentPath());
152 private String getParentPath() {
153 String parentPath = path.substring(0, path.lastIndexOf("/"));
154 if (parentPath.length() == 0) {
160 private byte[] getData(Photo aPhoto) {
161 try (InputStream is = aPhoto.getPhoto()) {
164 catch (IOException e) {
166 throw new RuntimeException("Cannot read photo", e);
170 private byte[] getBytes(InputStream is) throws IOException {
171 ByteArrayOutputStream bos = new ByteArrayOutputStream();
172 byte[] block = new byte[1024];
173 int n = is.read(block);
175 bos.write(block, 0, n);
178 return bos.toByteArray();
181 private Album getAuthorizedPhotos() {