--- /dev/null
+/*
+ * 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