2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.photos.wicket;
18 import java.io.Serializable;
19 import java.util.logging.Logger;
20 import javax.inject.Inject;
21 import javax.servlet.ServletContext;
23 import org.apache.wicket.PageParameters;
24 import org.apache.wicket.markup.ComponentTag;
25 import org.apache.wicket.markup.html.WebMarkupContainer;
26 import org.apache.wicket.markup.html.basic.Label;
27 import org.apache.wicket.markup.html.image.Image;
28 import org.apache.wicket.markup.html.link.Link;
29 import org.apache.wicket.markup.html.panel.Panel;
30 import org.apache.wicket.markup.repeater.RepeatingView;
31 import org.wamblee.general.ValueHolder;
32 import org.wamblee.photos.model.Album;
33 import org.wamblee.photos.model.Photo;
34 import org.wamblee.photos.model.PhotoEntry;
35 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
40 public class AlbumPanel extends Panel {
42 public static class MyValueHolder<T extends Serializable> extends ValueHolder<T> implements Serializable {
43 public MyValueHolder(T aValue) {
47 public MyValueHolder() {
52 private static final Logger LOGGER = Logger.getLogger(AlbumPanel.class.getName());
54 private static final long serialVersionUID = 1L;
55 public static final int MAX_ROWS = 5;
56 public static final int MAX_COLUMNS = 5;
60 private transient Album authorized;
63 private ServletContext context;
65 private class SerializableEntryLink extends Link {
69 public SerializableEntryLink(String aId, String aPath) {
75 public void onClick() {
76 PageParameters pars = new PageParameters();
77 pars.put("path", path);
79 setResponsePage(HomePage.class, pars);
87 * Constructor that is invoked when page is invoked without a session.
89 * @param parameters Page parameters
91 public AlbumPanel(String aId, final PageParameters parameters) throws Exception {
94 path = parameters.getString("path", "/");
95 if (!path.startsWith("/")) {
96 info("Invalid album '" + path + "', showing root album instead");
99 add(new Label("path", path));
102 String indexString = parameters.getString("index", "0");
104 index = Integer.parseInt(indexString);
106 catch (NumberFormatException e) {
107 // use default value 0
113 Link prevLink = new Link("prevLink") {
115 public void onClick() {
116 PageParameters pars = new PageParameters();
117 pars.put("path", path);
118 pars.put("index", index - MAX_ROWS * MAX_COLUMNS);
119 setResponsePage(HomePage.class, pars);
123 public boolean isEnabled() {
124 return index - MAX_ROWS * MAX_COLUMNS >= 0;
129 // Avoid implicit references to the album to keep the link objects
130 // small and serializable.
131 final int albumSize = getAlbum().size();
132 Link nextLink = new Link("nextLink") {
134 public void onClick() {
135 PageParameters pars = new PageParameters();
136 pars.put("path", path);
137 pars.put("index", index + MAX_ROWS * MAX_COLUMNS);
138 setResponsePage(HomePage.class, pars);
142 public boolean isEnabled() {
143 return index + MAX_ROWS * MAX_COLUMNS < albumSize;
148 Link parentLink = new Link("parentLink") {
150 if ("/".equals(path)) {
156 public void onClick() {
157 PageParameters pars = new PageParameters();
158 String parentPath = path.substring(0, path.lastIndexOf("/"));
159 if (parentPath.length() == 0) {
162 pars.put("path", parentPath);
163 pars.put("index", 0);
164 setResponsePage(HomePage.class, pars);
169 RepeatingView pageLinks = new RepeatingView("pageLinks");
171 Album album = getAlbum();
172 for (int i = 0; i < album.size() / MAX_ROWS / MAX_COLUMNS; i++) {
173 final int istart = i * MAX_ROWS * MAX_COLUMNS;
174 Link pageLink = new Link("pageLink") {
176 if (istart == index) {
182 public void onClick() {
183 PageParameters pars = new PageParameters();
184 pars.put("path", path);
185 pars.put("index", istart);
186 setResponsePage(HomePage.class, pars);
189 pageLink.add(new Label("label", i + ""));
190 pageLinks.add(pageLink);
191 WebMarkupContainer container = new WebMarkupContainer(pageLinks.newChildId());
192 container.add(pageLink);
193 pageLinks.add(container);
196 RepeatingView row = new RepeatingView("row") {
198 protected void onPopulate() {
200 final ValueHolder<Integer> ientry = new MyValueHolder<Integer>(index);
202 Album album = getAlbum();
203 while (irow < MAX_ROWS && ientry.getValue() < album.size()) {
204 WebMarkupContainer columns = new WebMarkupContainer(newChildId());
206 RepeatingView column = new RepeatingView("column") {
208 protected void onPopulate() {
211 Album album = getAlbum();
212 while (icolumn < MAX_COLUMNS && ientry.getValue() < album.size()) {
213 WebMarkupContainer thumbnail = new WebMarkupContainer(newChildId());
216 final PhotoEntry entry = album.getEntry(ientry.getValue());
217 Link link = new SerializableEntryLink("thumbnail", entry.getPath());
220 final ValueHolder<String> pathinfo = new MyValueHolder<String>();
221 if (entry instanceof Photo) {
222 pathinfo.setValue("/image/thumbnail/" + entry.getPath());
224 pathinfo.setValue("/image/resource/folder.png");
226 link.add(new Image("image") {
228 protected void onComponentTag(ComponentTag tag) {
229 tag.put("src", context.getContextPath() + pathinfo.getValue());
233 link.add(new Label("name", album.getEntry(ientry.getValue()).getId()));
235 ientry.setValue(ientry.getValue() + 1);
248 if (path.equals("/")) {
249 add(new WebMarkupContainer("uploadPanel"));
250 add(new WebMarkupContainer("createFolderPanel"));
252 add(new UploadPanel("uploadPanel", path));
253 add(new CreateFolderPanel("createFolderPanel", path));
257 private Album getAlbum() {
258 PhotoEntry current = authorized.getEntry(path);
259 if (current instanceof Photo) {
260 throw new RuntimeException("AlbumPanel can only show album: " + current.getClass().getName());
262 return (Album) current;