2 * Sone - ImageAccessor.java - Copyright © 2011–2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.template;
20 import net.pterodactylus.sone.data.Album;
21 import net.pterodactylus.sone.data.Image;
22 import net.pterodactylus.util.template.Accessor;
23 import net.pterodactylus.util.template.ReflectionAccessor;
24 import net.pterodactylus.util.template.TemplateContext;
27 * {@link Accessor} implementation for {@link Image} objects. It adds the
28 * following properties:
30 * <li>{@code previous}: returns the previous image in the image’s album, or
31 * {@code null} if the image is the first image of its album.</li>
32 * <li>{@code next}: returns the next image in the image’s album, or {@code
33 * null} if the image is the last image of its album.</li>
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class ImageAccessor extends ReflectionAccessor {
44 public Object get(TemplateContext templateContext, Object object, String member) {
45 Image image = (Image) object;
46 if ("next".equals(member)) {
47 Album album = image.getAlbum();
48 int imagePosition = album.getImages().indexOf(image);
49 if (imagePosition < album.getImages().size() - 1) {
50 return album.getImages().get(imagePosition + 1);
53 } else if ("previous".equals(member)) {
54 Album album = image.getAlbum();
55 int imagePosition = album.getImages().indexOf(image);
56 if (imagePosition > 0) {
57 return album.getImages().get(imagePosition - 1);
61 return super.get(templateContext, object, member);