345cd40a6b5cd3ff476a9c0c4db86cf2b580dbd0
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / layouts / MixedLayout.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.htmlindexer.layouts;
11
12 import java.io.UnsupportedEncodingException;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16
17 import eu.svjatoslav.commons.file.FilePathParser;
18 import eu.svjatoslav.commons.network.UrlParamEncoder;
19 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
20 import eu.svjatoslav.meviz.htmlindexer.Constants;
21 import eu.svjatoslav.meviz.htmlindexer.Layout;
22 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
23 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
24 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
25 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
26
27 public class MixedLayout implements Layout {
28
29         Logger logger = Logger.getLogger(MixedLayout.class);
30
31         StringBuffer dirHtml = new StringBuffer();
32
33         StringBuffer filesHtml = new StringBuffer();
34
35         String galleryTitle;
36
37         String[] path;
38
39         public static final String encoding = "UTF-8";
40
41         int currentColumn = 0;
42
43         int horizontalColumnsLimit = 2;
44
45         private AbstractIndexer indexer;
46
47         private String pathPrefix;
48
49         public void columnAdded() {
50                 currentColumn++;
51                 if (currentColumn > horizontalColumnsLimit) {
52
53                         currentColumn = 0;
54
55                         filesHtml.append("</TR>\n");
56                 }
57         }
58
59         @Override
60         public void enlistDirectory(final AbstractFile directory) {
61                 dirHtml.append("<TR>");
62                 dirHtml.append("<TD><b><a href=\"" + indexer.getDirectoryUrl(directory)
63                                 + "\"> ==&gt; " + directory.fileName + "</a></b></TD>");
64                 dirHtml.append("</TR>\n");
65
66         }
67
68         @Override
69         public void enlistFile(final AbstractFile file) {
70                 finishRow();
71
72                 final String fullFileName = file.fileName;
73
74                 filesHtml.append("<TR>\n");
75                 filesHtml.append("    <TD colspan=\""
76                                 + String.valueOf(horizontalColumnsLimit) + "\"><a href=\""
77                                 + UrlParamEncoder.encode(pathPrefix + fullFileName) + "\">"
78                                 + fullFileName + "</a>\n");
79                 filesHtml.append("&nbsp;&nbsp;("
80                                 + FilePathParser.getFileSizeDescription(file.getFileLength())
81                                 + ")</TD>\n");
82                 filesHtml.append("</TR>\n");
83
84         }
85
86         @Override
87         public void enlistImage(final AbstractFile abstractFile) {
88                 try {
89
90                         final Picture picture = (Picture) abstractFile;
91
92                         final Dimension imageSize = picture.getDimensions();
93
94                         final List<Dimension> recommendedDimensions = Picture
95                                         .getThumbnailDimensions(imageSize);
96
97                         Dimension listingDimension = new Dimension(500, 500);
98
99                         if (listingDimension.width > imageSize.width)
100                                 if (listingDimension.height > imageSize.height)
101                                         listingDimension = imageSize;
102
103                         final String listingThumbnailPath = indexer.getThumbnailPath(
104                                         picture, listingDimension);
105
106                         startcolumn();
107
108                         filesHtml.append("<TD><center><a href=\""
109                                         + UrlParamEncoder.encode(pathPrefix + picture.fileName)
110                                         + "\">\n");
111
112                         filesHtml.append("<img border=\"0\" src=\""
113                                         + UrlParamEncoder.encode(listingThumbnailPath)
114                                         + "\"/></a>\n");
115                         filesHtml.append("<br/><b>"
116                                         + FilePathParser
117                                                         .getFileNameWithoutExtension(picture.fileName)
118                                         + "</b><br/>(");
119
120                         int count = 0;
121                         for (final Dimension dimension : recommendedDimensions) {
122                                 count++;
123                                 if (count > 1)
124                                         filesHtml.append(", ");
125                                 filesHtml.append("<a href=\""
126                                                 + UrlParamEncoder.encode(indexer.getThumbnailPath(
127                                                                 picture, dimension)) + "\">");
128                                 filesHtml.append(dimension.width + "x" + dimension.height);
129                                 filesHtml.append("</a>");
130                         }
131
132                         filesHtml.append(")</TD>\n");
133
134                         columnAdded();
135
136                 } catch (final Exception e) {
137                         logger.error(
138                                         "Error generating thumbnails for image file: "
139                                                         + e.toString()
140                                                         + ", thumbnail generation skipped, enlisting as ordinary file instead.",
141                                         e);
142                         enlistFile(abstractFile);
143                 }
144         }
145
146         /** {@inheritDoc} */
147         @Override
148         public void enlistOgv(final AbstractFile abstractFile)
149                         throws UnsupportedEncodingException {
150
151                 final GeneralFile file = (GeneralFile) abstractFile;
152
153                 finishRow();
154
155                 filesHtml
156                                 .append("<TR style=\"background-color: #404040; text-align: center\">\n");
157                 filesHtml.append("<TD style=\"text-align: center\" colspan=\""
158                                 + String.valueOf(horizontalColumnsLimit) + "\">\n");
159
160                 filesHtml.append("<video controls=\"controls\" tabindex=\"0\">\n");
161                 filesHtml
162                                 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
163                                                 + UrlParamEncoder.encode(pathPrefix + file.fileName)
164                                                 + "\">\n");
165                 filesHtml.append("</source>\n");
166
167                 // if browser cannot play video directry, fall back to java Applet
168                 filesHtml
169                                 .append("<applet code=\"com.fluendo.player.Cortado.class\" archive=\"http://theora.org/cortado.jar\" width=\"800\" height=\"600\">\n");
170                 filesHtml
171                                 .append("    <param name=\"url\" value=\""
172                                                 + UrlParamEncoder.encode(pathPrefix + file.fileName)
173                                                 + "\"/>\n");
174                 filesHtml.append("    <param name=\"keepAspect\" value=\"true\"/>\n");
175                 filesHtml.append("    <param name=\"autoPlay\" value=\"false\"/>\n");
176                 filesHtml.append("</applet>\n");
177
178                 filesHtml.append("</video>\n");
179
180                 filesHtml.append("<br/>\n");
181
182                 filesHtml
183                                 .append("Video: <b>"
184                                                 + FilePathParser
185                                                                 .getFileNameWithoutExtension(file.fileName)
186                                                 + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
187                 filesHtml.append("<a href=\""
188                                 + UrlParamEncoder.encode(pathPrefix + file.fileName)
189                                 + "\">download as OGV</a>\n");
190
191                 filesHtml.append("</TD></TR>\n");
192
193         }
194
195         public void finishRow() {
196
197                 if (currentColumn == 0)
198                         return;
199
200                 while (currentColumn <= horizontalColumnsLimit) {
201                         filesHtml.append("<TD></TD>");
202                         currentColumn++;
203                 }
204                 filesHtml.append("</TR>\n");
205                 currentColumn = 0;
206         }
207
208         /** {@inheritDoc} */
209         @Override
210         public String getFileNameSuffix() {
211                 return "";
212         }
213
214         /** {@inheritDoc} */
215         @Override
216         public String getHtml() {
217
218                 // assemble final page HTML
219                 final StringBuffer finalHtml = new StringBuffer();
220
221                 // page header
222                 finalHtml.append("<HTML>\n");
223                 finalHtml.append("<HEAD>\n");
224                 finalHtml
225                                 .append("    <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
226                 finalHtml.append("</HEAD>\n");
227                 finalHtml
228                                 .append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
229                 finalHtml.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
230                 finalHtml.append("<!-- " + Constants.HTML_MEMO + " -->\n");
231                 finalHtml.append("<FONT color=\"#ffffff\">\n");
232                 finalHtml.append("<CENTER><H1>" + galleryTitle + "</H1>\n");
233
234                 finalHtml.append("<H2>");
235                 for (final String p : path)
236                         finalHtml.append(" / " + p);
237                 finalHtml.append("</H2></CENTER>\n");
238
239                 // append directory listing
240                 finalHtml.append("<TABLE>\n");
241
242                 if (path.length > 0) {
243                         finalHtml.append("<TR>");
244                         finalHtml
245                                         .append("<TD><b><a href=\"../index.html\"> &lt;== ..</a></b></TD>");
246                         finalHtml.append("</TR>\n");
247                 }
248
249                 finalHtml.append(dirHtml.toString());
250                 finalHtml.append("\n<TR><TD><br></TD></TR>\n");
251                 finalHtml.append("</TABLE>\n");
252
253                 // append file listing
254
255                 finishRow();
256                 finalHtml.append("<TABLE width=\"1100\">\n");
257                 finalHtml.append(filesHtml.toString());
258                 finalHtml.append("\n</TABLE>\n");
259
260                 finalHtml.append("</font></BODY></HTML>\n");
261
262                 return finalHtml.toString();
263         }
264
265         @Override
266         public void init(final String galleryTitle, final String[] path,
267                         final AbstractIndexer indexer, final String pathPrefix) {
268                 this.indexer = indexer;
269                 this.galleryTitle = galleryTitle;
270                 this.path = path;
271                 this.pathPrefix = pathPrefix;
272
273                 dirHtml = new StringBuffer();
274                 filesHtml = new StringBuffer();
275         }
276
277         public void startcolumn() {
278                 if (currentColumn == 0) {
279                         filesHtml.append("<TR>");
280                         currentColumn = 1;
281                 }
282         }
283
284 }