From 9a884536fcf2d56a822b8db44bbc9ca56a61cea2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 14 Nov 2011 21:23:58 +0100 Subject: [PATCH] Add custom accessor for images. --- .../pterodactylus/sone/template/ImageAccessor.java | 64 ++++++++++++++++++++++ .../net/pterodactylus/sone/web/WebInterface.java | 2 + 2 files changed, 66 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/template/ImageAccessor.java diff --git a/src/main/java/net/pterodactylus/sone/template/ImageAccessor.java b/src/main/java/net/pterodactylus/sone/template/ImageAccessor.java new file mode 100644 index 0000000..1c06d31 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/ImageAccessor.java @@ -0,0 +1,64 @@ +/* + * Sone - ImageAccessor.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.util.template.Accessor; +import net.pterodactylus.util.template.ReflectionAccessor; +import net.pterodactylus.util.template.TemplateContext; + +/** + * {@link Accessor} implementation for {@link Image} objects. It adds the + * following properties: + * + * + * @author David ‘Bombe’ Roden + */ +public class ImageAccessor extends ReflectionAccessor { + + /** + * {@inheritDoc} + */ + @Override + public Object get(TemplateContext templateContext, Object object, String member) { + Image image = (Image) object; + if ("next".equals(member)) { + Album album = image.getAlbum(); + int imagePosition = album.getImages().indexOf(image); + if (imagePosition < album.getImages().size() - 1) { + return album.getImages().get(imagePosition + 1); + } + return null; + } else if ("previous".equals(member)) { + Album album = image.getAlbum(); + int imagePosition = album.getImages().indexOf(image); + if (imagePosition > 0) { + return album.getImages().get(imagePosition - 1); + } + return null; + } + return super.get(templateContext, object, member); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 4c41581..5ba3be7 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -53,6 +53,7 @@ import net.pterodactylus.sone.template.CollectionAccessor; import net.pterodactylus.sone.template.CssClassNameFilter; import net.pterodactylus.sone.template.HttpRequestAccessor; import net.pterodactylus.sone.template.IdentityAccessor; +import net.pterodactylus.sone.template.ImageAccessor; import net.pterodactylus.sone.template.ImageLinkFilter; import net.pterodactylus.sone.template.JavascriptFilter; import net.pterodactylus.sone.template.ParserFilter; @@ -231,6 +232,7 @@ public class WebInterface implements CoreListener { templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore())); templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore())); templateContextFactory.addAccessor(Album.class, new AlbumAccessor()); + templateContextFactory.addAccessor(Image.class, new ImageAccessor()); templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore())); templateContextFactory.addAccessor(Trust.class, new TrustAccessor()); templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor()); -- 2.7.4