/* * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.photos.wicket; import java.io.Serializable; import java.util.logging.Logger; import javax.inject.Inject; import javax.servlet.ServletContext; import org.apache.wicket.PageParameters; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.image.Image; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.html.panel.Panel; import org.apache.wicket.markup.repeater.RepeatingView; import org.wamblee.general.ValueHolder; import org.wamblee.photos.model.Album; import org.wamblee.photos.model.Photo; import org.wamblee.photos.model.PhotoEntry; import org.wamblee.photos.model.plumbing.AuthorizedPhotos; /** * Homepage */ public class AlbumPanel extends Panel { public static class MyValueHolder extends ValueHolder implements Serializable { public MyValueHolder(T aValue) { super(aValue); } public MyValueHolder() { super(); } } private static final Logger LOGGER = Logger.getLogger(AlbumPanel.class.getName()); private static final long serialVersionUID = 1L; public static final int MAX_ROWS = 5; public static final int MAX_COLUMNS = 5; @Inject @AuthorizedPhotos private transient Album authorized; @Inject private ServletContext context; private class SerializableEntryLink extends Link { private String path; public SerializableEntryLink(String aId, String aPath) { super(aId); path = aPath; } @Override public void onClick() { PageParameters pars = new PageParameters(); pars.put("path", path); setResponsePage(HomePage.class, pars); } } private String path; private int index; /** * Constructor that is invoked when page is invoked without a session. * * @param parameters Page parameters */ public AlbumPanel(String aId, final PageParameters parameters) throws Exception { super(aId); path = parameters.getString("path", "/"); if (!path.startsWith("/")) { info("Invalid album '" + path + "', showing root album instead"); path = "/"; } add(new Label("path", path)); index = 0; String indexString = parameters.getString("index", "0"); try { index = Integer.parseInt(indexString); } catch (NumberFormatException e) { // use default value 0 } if (index < 0) { index = 0; } Link prevLink = new Link("prevLink") { @Override public void onClick() { PageParameters pars = new PageParameters(); pars.put("path", path); pars.put("index", index - MAX_ROWS * MAX_COLUMNS); setResponsePage(HomePage.class, pars); } @Override public boolean isEnabled() { return index - MAX_ROWS * MAX_COLUMNS >= 0; } }; add(prevLink); // Avoid implicit references to the album to keep the link objects // small and serializable. final int albumSize = getAlbum().size(); Link nextLink = new Link("nextLink") { @Override public void onClick() { PageParameters pars = new PageParameters(); pars.put("path", path); pars.put("index", index + MAX_ROWS * MAX_COLUMNS); setResponsePage(HomePage.class, pars); } @Override public boolean isEnabled() { return index + MAX_ROWS * MAX_COLUMNS < albumSize; } }; add(nextLink); Link parentLink = new Link("parentLink") { { if ("/".equals(path)) { setEnabled(false); } } @Override public void onClick() { PageParameters pars = new PageParameters(); String parentPath = path.substring(0, path.lastIndexOf("/")); if (parentPath.length() == 0) { parentPath = "/"; } pars.put("path", parentPath); pars.put("index", 0); setResponsePage(HomePage.class, pars); } }; add(parentLink); RepeatingView pageLinks = new RepeatingView("pageLinks"); add(pageLinks); Album album = getAlbum(); for (int i = 0; i < album.size() / MAX_ROWS / MAX_COLUMNS; i++) { final int istart = i * MAX_ROWS * MAX_COLUMNS; Link pageLink = new Link("pageLink") { { if (istart == index) { setEnabled(false); } } @Override public void onClick() { PageParameters pars = new PageParameters(); pars.put("path", path); pars.put("index", istart); setResponsePage(HomePage.class, pars); } }; pageLink.add(new Label("label", i + "")); pageLinks.add(pageLink); WebMarkupContainer container = new WebMarkupContainer(pageLinks.newChildId()); container.add(pageLink); pageLinks.add(container); } RepeatingView row = new RepeatingView("row") { @Override protected void onPopulate() { removeAll(); final ValueHolder ientry = new MyValueHolder(index); int irow = 0; Album album = getAlbum(); while (irow < MAX_ROWS && ientry.getValue() < album.size()) { WebMarkupContainer columns = new WebMarkupContainer(newChildId()); add(columns); RepeatingView column = new RepeatingView("column") { @Override protected void onPopulate() { removeAll(); int icolumn = 0; Album album = getAlbum(); while (icolumn < MAX_COLUMNS && ientry.getValue() < album.size()) { WebMarkupContainer thumbnail = new WebMarkupContainer(newChildId()); add(thumbnail); final PhotoEntry entry = album.getEntry(ientry.getValue()); Link link = new SerializableEntryLink("thumbnail", entry.getPath()); thumbnail.add(link); final ValueHolder pathinfo = new MyValueHolder(); if (entry instanceof Photo) { pathinfo.setValue("/image/thumbnail/" + entry.getPath()); } else { pathinfo.setValue("/image/resource/folder.png"); } link.add(new Image("image") { @Override protected void onComponentTag(ComponentTag tag) { tag.put("src", context.getContextPath() + pathinfo.getValue()); } }); link.add(new Label("name", album.getEntry(ientry.getValue()).getId())); icolumn++; ientry.setValue(ientry.getValue() + 1); } } }; columns.add(column); irow++; } } }; add(row); // upload panel if (path.equals("/")) { add(new WebMarkupContainer("uploadPanel")); } else { add(new UploadPanel("uploadPanel", path)); } } private Album getAlbum() { PhotoEntry current = authorized.getEntry(path); if (current instanceof Photo) { throw new RuntimeException("AlbumPanel can only show album: " + current.getClass().getName()); } return (Album) current; } }