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.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;
27 public class MixedLayout implements Layout {
29 Logger logger = Logger.getLogger(MixedLayout.class);
31 StringBuffer dirHtml = new StringBuffer();
33 StringBuffer filesHtml = new StringBuffer();
39 public static final String encoding = "UTF-8";
41 int currentColumn = 0;
43 int horizontalColumnsLimit = 2;
45 private AbstractIndexer indexer;
47 private String pathPrefix;
49 public void columnAdded() {
51 if (currentColumn > horizontalColumnsLimit) {
55 filesHtml.append("</TR>\n");
60 public void enlistDirectory(final AbstractFile directory) {
61 dirHtml.append("<TR>");
62 dirHtml.append("<TD><b><a href=\"" + indexer.getDirectoryUrl(directory)
63 + "\"> ==> " + directory.fileName + "</a></b></TD>");
64 dirHtml.append("</TR>\n");
69 public void enlistFile(final AbstractFile file) {
72 final String fullFileName = file.fileName;
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(" ("
80 + FilePathParser.getFileSizeDescription(file.getFileLength())
82 filesHtml.append("</TR>\n");
87 public void enlistImage(final AbstractFile abstractFile) {
90 final Picture picture = (Picture) abstractFile;
92 final Dimension imageSize = picture.getDimensions();
94 final List<Dimension> recommendedDimensions = Picture
95 .getThumbnailDimensions(imageSize);
97 Dimension listingDimension = new Dimension(500, 500);
99 if (listingDimension.width > imageSize.width)
100 if (listingDimension.height > imageSize.height)
101 listingDimension = imageSize;
103 final String listingThumbnailPath = indexer.getThumbnailPath(
104 picture, listingDimension);
108 filesHtml.append("<TD><center><a href=\""
109 + UrlParamEncoder.encode(pathPrefix + picture.fileName)
112 filesHtml.append("<img border=\"0\" src=\""
113 + UrlParamEncoder.encode(listingThumbnailPath)
115 filesHtml.append("<br/><b>"
117 .getFileNameWithoutExtension(picture.fileName)
121 for (final Dimension dimension : recommendedDimensions) {
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>");
132 filesHtml.append(")</TD>\n");
136 } catch (final Exception e) {
138 "Error generating thumbnails for image file: "
140 + ", thumbnail generation skipped, enlisting as ordinary file instead.",
142 enlistFile(abstractFile);
148 public void enlistOgv(final AbstractFile abstractFile)
149 throws UnsupportedEncodingException {
151 final GeneralFile file = (GeneralFile) abstractFile;
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");
160 filesHtml.append("<video controls=\"controls\" tabindex=\"0\">\n");
162 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
163 + UrlParamEncoder.encode(pathPrefix + file.fileName)
165 filesHtml.append("</source>\n");
167 // if browser cannot play video directry, fall back to java Applet
169 .append("<applet code=\"com.fluendo.player.Cortado.class\" archive=\"http://theora.org/cortado.jar\" width=\"800\" height=\"600\">\n");
171 .append(" <param name=\"url\" value=\""
172 + UrlParamEncoder.encode(pathPrefix + file.fileName)
174 filesHtml.append(" <param name=\"keepAspect\" value=\"true\"/>\n");
175 filesHtml.append(" <param name=\"autoPlay\" value=\"false\"/>\n");
176 filesHtml.append("</applet>\n");
178 filesHtml.append("</video>\n");
180 filesHtml.append("<br/>\n");
185 .getFileNameWithoutExtension(file.fileName)
186 + "<b/> ");
187 filesHtml.append("<a href=\""
188 + UrlParamEncoder.encode(pathPrefix + file.fileName)
189 + "\">download as OGV</a>\n");
191 filesHtml.append("</TD></TR>\n");
195 public void finishRow() {
197 if (currentColumn == 0)
200 while (currentColumn <= horizontalColumnsLimit) {
201 filesHtml.append("<TD></TD>");
204 filesHtml.append("</TR>\n");
210 public String getFileNameSuffix() {
216 public String getHtml() {
218 // assemble final page HTML
219 final StringBuffer finalHtml = new StringBuffer();
222 finalHtml.append("<HTML>\n");
223 finalHtml.append("<HEAD>\n");
225 .append(" <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
226 finalHtml.append("</HEAD>\n");
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");
234 finalHtml.append("<H2>");
235 for (final String p : path)
236 finalHtml.append(" / " + p);
237 finalHtml.append("</H2></CENTER>\n");
239 // append directory listing
240 finalHtml.append("<TABLE>\n");
242 if (path.length > 0) {
243 finalHtml.append("<TR>");
245 .append("<TD><b><a href=\"../index.html\"> <== ..</a></b></TD>");
246 finalHtml.append("</TR>\n");
249 finalHtml.append(dirHtml.toString());
250 finalHtml.append("\n<TR><TD><br></TD></TR>\n");
251 finalHtml.append("</TABLE>\n");
253 // append file listing
256 finalHtml.append("<TABLE width=\"1100\">\n");
257 finalHtml.append(filesHtml.toString());
258 finalHtml.append("\n</TABLE>\n");
260 finalHtml.append("</font></BODY></HTML>\n");
262 return finalHtml.toString();
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;
271 this.pathPrefix = pathPrefix;
273 dirHtml = new StringBuffer();
274 filesHtml = new StringBuffer();
277 public void startcolumn() {
278 if (currentColumn == 0) {
279 filesHtml.append("<TR>");