/* * 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)); String parentPath = getParentPath(); Link prevLink = new Link("prevLink") { @Override public void onClick() { Photo before = getPrevPhoto(); if (before == null) { return; } PageParameters pars = new PageParameters(); pars.put("path", before.getPath()); setResponsePage(HomePage.class, pars); } @Override public boolean isEnabled() { return getPrevPhoto() != null; } }; add(prevLink); Link nextLink = new Link("nextLink") { @Override public void onClick() { Photo after = getNextPhoto(); if (after == null) { return; } PageParameters pars = new PageParameters(); pars.put("path", after.getPath()); setResponsePage(HomePage.class, pars); } @Override public boolean isEnabled() { return getNextPhoto() != null; } }; add(nextLink); Link parentLink = new Link("parentLink") { { if ("/".equals(path)) { setEnabled(false); } } @Override public void onClick() { PageParameters pars = new PageParameters(); pars.put("path", getParentPath()); pars.put("index", 0); setResponsePage(HomePage.class, pars); } }; add(parentLink); 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); } 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(); } private Album getAuthorizedPhotos() { return authorized; } }