Replace utils’ Numbers by Guava’s Optional and Ints/Longs.tryParse.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ImageBrowserPage.java
index 406a762..ea039ca 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - ImageBrowserPage.java - Copyright © 2011 David Roden
+ * Sone - ImageBrowserPage.java - Copyright © 2011–2013 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
 package net.pterodactylus.sone.web;
 
 import java.net.URI;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.page.FreenetRequest;
+import net.pterodactylus.util.collection.Pagination;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 
+import com.google.common.base.Optional;
+import com.google.common.primitives.Ints;
+
 /**
  * The image browser page is the entry page for the image management.
  *
@@ -62,6 +67,7 @@ public class ImageBrowserPage extends SoneTemplatePage {
                        Album album = webInterface.getCore().getAlbum(albumId, false);
                        templateContext.set("albumRequested", true);
                        templateContext.set("album", album);
+                       templateContext.set("page", request.getHttpRequest().getParam("page"));
                        return;
                }
                String imageId = request.getHttpRequest().getParam("image", null);
@@ -81,11 +87,14 @@ public class ImageBrowserPage extends SoneTemplatePage {
                String mode = request.getHttpRequest().getParam("mode", null);
                if ("gallery".equals(mode)) {
                        templateContext.set("galleryRequested", true);
-                       Set<Album> albums = new HashSet<Album>();
+                       List<Album> albums = new ArrayList<Album>();
                        for (Sone sone : webInterface.getCore().getSones()) {
                                albums.addAll(sone.getAllAlbums());
                        }
-                       templateContext.set("albums", albums);
+                       Collections.sort(albums, Album.TITLE_COMPARATOR);
+                       Pagination<Album> albumPagination = new Pagination<Album>(albums, 12).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(0));
+                       templateContext.set("albumPagination", albumPagination);
+                       templateContext.set("albums", albumPagination.getItems());
                        return;
                }
                Sone sone = getCurrentSone(request.getToadletContext(), false);