Add accessor for Collections.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 4 Nov 2010 10:49:19 +0000 (11:49 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 4 Nov 2010 10:49:19 +0000 (11:49 +0100)
src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/WebInterface.java

diff --git a/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java b/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java
new file mode 100644 (file)
index 0000000..2b91592
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Sone - CollectionAccessor.java - Copyright © 2010 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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.template;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.template.Accessor;
+import net.pterodactylus.util.template.DataProvider;
+import net.pterodactylus.util.template.ReflectionAccessor;
+
+/**
+ * {@link Accessor} for {@link Collection}s that adds a couple of specialized
+ * properties that only work for collections that contain the right types.
+ * <dl>
+ * <dd>soneNames</dd>
+ * <dt>Returns the nice names of all {@link Sone}s in the collection, sorted
+ * ascending by their nice names.</dt>
+ * </dl>
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class CollectionAccessor extends ReflectionAccessor {
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Object get(DataProvider dataProvider, Object object, String member) {
+               if (object == null) {
+                       return null;
+               }
+               Collection<?> collection = (Collection<?>) object;
+               if (member.equals("soneNames")) {
+                       List<Sone> sones = new ArrayList<Sone>();
+                       for (Object sone : collection) {
+                               if (!(sone instanceof Sone)) {
+                                       continue;
+                               }
+                               sones.add((Sone) sone);
+                       }
+                       Collections.sort(sones, Sone.NICE_NAME_COMPARATOR);
+                       StringBuilder soneNames = new StringBuilder();
+                       for (Sone sone : sones) {
+                               if (soneNames.length() > 0) {
+                                       soneNames.append('\n');
+                               }
+                               soneNames.append(SoneAccessor.getNiceName(sone));
+                       }
+                       return soneNames.toString();
+               }
+               return super.get(dataProvider, object, member);
+       }
+
+}
index c518a87..fce52c4 100644 (file)
@@ -24,6 +24,7 @@ import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -35,6 +36,7 @@ import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.L10nFilter;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.main.SonePlugin;
+import net.pterodactylus.sone.template.CollectionAccessor;
 import net.pterodactylus.sone.template.GetPagePlugin;
 import net.pterodactylus.sone.template.IdentityAccessor;
 import net.pterodactylus.sone.template.PostAccessor;
@@ -177,6 +179,7 @@ public class WebInterface extends AbstractService {
        private void registerToadlets() {
                DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
                templateFactory.addAccessor(Object.class, new ReflectionAccessor());
+               templateFactory.addAccessor(Collection.class, new CollectionAccessor());
                templateFactory.addAccessor(Sone.class, new SoneAccessor(core()));
                templateFactory.addAccessor(Post.class, new PostAccessor(core()));
                templateFactory.addAccessor(Reply.class, new ReplyAccessor(core()));