Full navigation is working including viewing individual photos.
[photos] / src / main / java / org / wamblee / photos / wicket / PhotoPanel.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.photos.wicket;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.logging.Logger;
22 import javax.inject.Inject;
23
24 import org.apache.wicket.PageParameters;
25 import org.apache.wicket.markup.html.basic.Label;
26 import org.apache.wicket.markup.html.image.Image;
27 import org.apache.wicket.markup.html.link.Link;
28 import org.apache.wicket.markup.html.panel.Panel;
29 import org.apache.wicket.resource.ByteArrayResource;
30 import org.wamblee.photos.model.Album;
31 import org.wamblee.photos.model.Photo;
32 import org.wamblee.photos.model.PhotoEntry;
33 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
34
35 /**
36  * Homepage
37  */
38 public class PhotoPanel extends Panel {
39
40     private static final Logger LOGGER = Logger.getLogger(PhotoPanel.class.getName());
41
42     private static final long serialVersionUID = 1L;
43     public static final int MAX_ROWS = 5;
44     public static final int MAX_COLUMNS = 5;
45
46     @Inject
47     @AuthorizedPhotos
48     private transient Album authorized;
49
50     private String path;
51
52     /**
53      * Constructor that is invoked when page is invoked without a session.
54      *
55      * @param parameters Page parameters
56      */
57     public PhotoPanel(String aId, final PageParameters parameters) throws Exception {
58         super(aId);
59
60         path = parameters.getString("path", "/");
61         if (!path.startsWith("/")) {
62             info("Invalid album '" + path + "', showing root album instead");
63             path = "/";
64         }
65         add(new Label("path", path));
66
67         PhotoEntry current = authorized.getEntry(path);
68         if (current instanceof Album) {
69             throw new RuntimeException("PhotoPanel can only show a photo: " + current.getClass().getName());
70         }
71         final Photo photo = (Photo) current;
72
73         String parentPath_ = path.substring(0, path.lastIndexOf("/"));
74         if (parentPath_.length() == 0) {
75             parentPath_ = "/";
76         }
77         final String parentPath = parentPath_;
78         final Album parent = (Album) authorized.getEntry(parentPath);
79         final Photo before = parent.findPhotoBefore(photo.getId());
80         final Photo after = parent.findPhotoAfter(photo.getId());
81
82         Link prevLink = new Link("prevLink") {
83             {
84                 if (before == null) {
85                     setEnabled(false);
86                 }
87             }
88
89             @Override
90             public void onClick() {
91                 if (before == null) {
92                     return;
93                 }
94                 PageParameters pars = new PageParameters();
95                 pars.put("path", before.getPath());
96                 setResponsePage(HomePage.class, pars);
97             }
98         };
99
100         add(prevLink);
101
102         Link nextLink = new Link("nextLink") {
103             {
104                 if (after == null) {
105                     setEnabled(false);
106                 }
107             }
108
109             @Override
110             public void onClick() {
111                 if (after == null) {
112                     return;
113                 }
114                 PageParameters pars = new PageParameters();
115                 pars.put("path", after.getPath());
116                 setResponsePage(HomePage.class, pars);
117             }
118         };
119
120         add(nextLink);
121
122         Link parentLink = new Link("parentLink") {
123             {
124                 if ("/".equals(path)) {
125                     setEnabled(false);
126                 }
127             }
128
129             @Override
130             public void onClick() {
131                 PageParameters pars = new PageParameters();
132
133                 pars.put("path", parentPath);
134                 pars.put("index", 0);
135                 setResponsePage(HomePage.class, pars);
136             }
137         };
138
139         add(parentLink);
140
141         Image image = new Image("photo", new ByteArrayResource("image/jpeg", getData(photo)));
142         add(image);
143     }
144
145     private byte[] getData(Photo aPhoto) {
146         try (InputStream is = aPhoto.getPhoto()) {
147             return getBytes(is);
148         }
149         catch (IOException e) {
150             // to improve.
151             throw new RuntimeException("Cannot read photo", e);
152         }
153     }
154
155     private byte[] getBytes(InputStream is) throws IOException {
156         ByteArrayOutputStream bos = new ByteArrayOutputStream();
157         byte[] block = new byte[1024];
158         int n = is.read(block);
159         while (n > 0) {
160             bos.write(block, 0, n);
161             n = is.read(block);
162         }
163         return bos.toByteArray();
164     }
165 }