2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.meviz.htmlindexer.layouts;
12 import java.io.UnsupportedEncodingException;
13 import java.util.List;
15 import org.apache.log4j.Logger;
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.IndexingContext;
22 import eu.svjatoslav.meviz.htmlindexer.Layout;
23 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
24 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
25 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
26 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
28 public class MixedLayout implements Layout {
30 Logger logger = Logger.getLogger(MixedLayout.class);
32 StringBuffer dirHtml = new StringBuffer();
34 StringBuffer filesHtml = new StringBuffer();
40 public static final String encoding = "UTF-8";
42 int currentColumn = 0;
44 int horizontalColumnsLimit = 2;
46 private AbstractIndexer indexer;
48 private IndexingContext indexingContext;
50 public void columnAdded() {
52 if (currentColumn > horizontalColumnsLimit) {
56 filesHtml.append("</TR>\n");
61 public void enlistDirectory(final AbstractFile directory) {
62 dirHtml.append("<TR>");
63 dirHtml.append("<TD><b><a href=\""
64 + indexer.getDirectoryUrl(directory, indexingContext)
65 + "\"> ==> " + directory.fileName + "</a></b></TD>");
66 dirHtml.append("</TR>\n");
71 public void enlistFile(final AbstractFile file) {
74 final String fullFileName = file.fileName;
76 filesHtml.append("<TR>\n");
77 filesHtml.append(" <TD colspan=\""
78 + String.valueOf(horizontalColumnsLimit)
80 + indexingContext.getGlobalUrl()
81 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
82 + fullFileName) + "\">" + fullFileName + "</a>\n");
83 filesHtml.append(" ("
84 + FilePathParser.getFileSizeDescription(file.getFileLength())
86 filesHtml.append("</TR>\n");
91 public void enlistImage(final AbstractFile abstractFile) {
94 final Picture picture = (Picture) abstractFile;
96 final Dimension imageSize = picture.getDimensions();
98 final List<Dimension> recommendedDimensions = Picture
99 .getThumbnailDimensions(imageSize);
101 Dimension listingDimension = new Dimension(500, 500);
103 if (listingDimension.width > imageSize.width)
104 if (listingDimension.height > imageSize.height)
105 listingDimension = imageSize;
107 final String listingThumbnailUrl = indexer.getThumbnailUrl(picture,
108 listingDimension, indexingContext);
112 filesHtml.append("<TD><center><a href=\""
113 + indexingContext.getGlobalUrl()
114 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
115 + "/" + picture.fileName) + "\">\n");
117 filesHtml.append("<img border=\"0\" src=\"" + listingThumbnailUrl
119 filesHtml.append("<br/><b>"
121 .getFileNameWithoutExtension(picture.fileName)
125 for (final Dimension dimension : recommendedDimensions) {
128 filesHtml.append(", ");
129 filesHtml.append("<a href=\""
130 + indexer.getThumbnailUrl(picture, dimension,
131 indexingContext) + "\">");
132 filesHtml.append(dimension.width + "x" + dimension.height);
133 filesHtml.append("</a>");
136 filesHtml.append(")</TD>\n");
140 } catch (final Exception e) {
142 "Error generating thumbnails for image file: "
144 + ", thumbnail generation skipped, enlisting as ordinary file instead.",
146 enlistFile(abstractFile);
152 public void enlistOgv(final AbstractFile abstractFile)
153 throws UnsupportedEncodingException {
155 final GeneralFile file = (GeneralFile) abstractFile;
160 .append("<TR style=\"background-color: #404040; text-align: center\">\n");
161 filesHtml.append("<TD style=\"text-align: center\" colspan=\""
162 + String.valueOf(horizontalColumnsLimit) + "\">\n");
164 filesHtml.append("<video controls=\"controls\" tabindex=\"0\">\n");
166 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
167 + indexingContext.getGlobalUrl()
168 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
169 + file.fileName) + "\">\n");
170 filesHtml.append("</source>\n");
172 // if browser cannot play video directry, fall back to java Applet
174 .append("<applet code=\"com.fluendo.player.Cortado.class\" archive=\"http://theora.org/cortado.jar\" width=\"800\" height=\"600\">\n");
175 filesHtml.append(" <param name=\"url\" value=\""
176 + indexingContext.getGlobalUrl()
177 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
178 + file.fileName) + "\"/>\n");
179 filesHtml.append(" <param name=\"keepAspect\" value=\"true\"/>\n");
180 filesHtml.append(" <param name=\"autoPlay\" value=\"false\"/>\n");
181 filesHtml.append("</applet>\n");
183 filesHtml.append("</video>\n");
185 filesHtml.append("<br/>\n");
190 .getFileNameWithoutExtension(file.fileName)
191 + "<b/> ");
192 filesHtml.append("<a href=\""
193 + indexingContext.getGlobalUrl()
194 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
195 + file.fileName) + "\">download as OGV</a>\n");
197 filesHtml.append("</TD></TR>\n");
201 public void finishRow() {
203 if (currentColumn == 0)
206 while (currentColumn <= horizontalColumnsLimit) {
207 filesHtml.append("<TD></TD>");
210 filesHtml.append("</TR>\n");
216 public String getFileNameSuffix() {
222 public String getHtml() {
224 // assemble final page HTML
225 final StringBuffer finalHtml = new StringBuffer();
228 finalHtml.append("<HTML>\n");
229 finalHtml.append("<HEAD>\n");
231 .append(" <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
232 finalHtml.append("</HEAD>\n");
234 .append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
235 finalHtml.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
236 finalHtml.append("<!-- " + Constants.HTML_MEMO + " -->\n");
237 finalHtml.append("<FONT color=\"#ffffff\">\n");
238 finalHtml.append("<CENTER><H1>" + galleryTitle + "</H1>\n");
240 finalHtml.append("<H2>");
241 for (final String p : path)
242 finalHtml.append(" / " + p);
243 finalHtml.append("</H2></CENTER>\n");
245 // append directory listing
246 finalHtml.append("<TABLE>\n");
248 if (path.size() > 0) {
249 finalHtml.append("<TR>");
250 finalHtml.append("<TD><b><a href=\""
251 + indexer.getParentDirectoryUrl(indexingContext)
252 + "\"> <== ..</a></b></TD>");
253 finalHtml.append("</TR>\n");
256 finalHtml.append(dirHtml.toString());
257 finalHtml.append("\n<TR><TD><br></TD></TR>\n");
258 finalHtml.append("</TABLE>\n");
260 // append file listing
263 finalHtml.append("<TABLE width=\"1100\">\n");
264 finalHtml.append(filesHtml.toString());
265 finalHtml.append("\n</TABLE>\n");
267 finalHtml.append("</font></BODY></HTML>\n");
269 return finalHtml.toString();
273 public void init(final String galleryTitle, final List<String> path,
274 final AbstractIndexer indexer, final IndexingContext indexingContext) {
276 this.indexer = indexer;
277 this.galleryTitle = galleryTitle;
279 this.indexingContext = indexingContext;
281 dirHtml = new StringBuffer();
282 filesHtml = new StringBuffer();
285 public void startcolumn() {
286 if (currentColumn == 0) {
287 filesHtml.append("<TR>");