initial commit
[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         public void columnAdded() {
48                 currentColumn++;
49                 if (currentColumn > horizontalColumnsLimit) {
50
51                         currentColumn = 0;
52
53                         filesHtml.append("</TR>\n");
54                 }
55         }
56
57         @Override
58         public void enlistDirectory(final AbstractFile directory) {
59                 dirHtml.append("<TR>");
60                 dirHtml.append("<TD><b><a href=\"" + directory.fileName
61                                 + "/index.html\"> ==&gt; " + directory.fileName
62                                 + "</a></b></TD>");
63                 dirHtml.append("</TR>\n");
64
65         }
66
67         @Override
68         public void enlistFile(final AbstractFile file) {
69                 finishRow();
70
71                 final String fullFileName = file.fileName;
72
73                 filesHtml.append("<TR>\n");
74                 filesHtml.append("    <TD colspan=\""
75                                 + String.valueOf(horizontalColumnsLimit) + "\"><a href=\""
76                                 + UrlParamEncoder.encode(fullFileName) + "\">" + fullFileName
77                                 + "</a>\n");
78                 filesHtml.append("&nbsp;&nbsp;("
79                                 + FilePathParser.getFileSizeDescription(file.getFileLength())
80                                 + ")</TD>\n");
81                 filesHtml.append("</TR>\n");
82
83         }
84
85         @Override
86         public void enlistImage(final AbstractFile abstractFile) {
87                 try {
88
89                         final Picture picture = (Picture) abstractFile;
90
91                         final Dimension imageSize = picture.getDimensions();
92
93                         final List<Dimension> recommendedDimensions = Picture
94                                         .getThumbnailDimensions(imageSize);
95
96                         Dimension listingDimension = new Dimension(500, 500);
97
98                         if (listingDimension.width > imageSize.width)
99                                 if (listingDimension.height > imageSize.height)
100                                         listingDimension = imageSize;
101
102                         final String listingThumbnailPath = indexer.getThumbnailPath(
103                                         picture, listingDimension);
104
105                         startcolumn();
106
107                         filesHtml.append("<TD><center><a href=\""
108                                         + UrlParamEncoder.encode(picture.fileName) + "\">\n");
109
110                         filesHtml.append("<img border=\"0\" src=\""
111                                         + UrlParamEncoder.encode(listingThumbnailPath)
112                                         + "\"/></a>\n");
113                         filesHtml.append("<br/><b>"
114                                         + FilePathParser
115                                                         .getFileNameWithoutExtension(picture.fileName)
116                                         + "</b><br/>(");
117
118                         int count = 0;
119                         for (final Dimension dimension : recommendedDimensions) {
120                                 count++;
121                                 if (count > 1)
122                                         filesHtml.append(", ");
123                                 filesHtml.append("<a href=\""
124                                                 + UrlParamEncoder.encode(indexer.getThumbnailPath(
125                                                                 picture, dimension)) + "\">");
126                                 filesHtml.append(dimension.width + "x" + dimension.height);
127                                 filesHtml.append("</a>");
128                         }
129
130                         filesHtml.append(")</TD>\n");
131
132                         columnAdded();
133
134                 } catch (final Exception e) {
135                         logger.error(
136                                         "Error generating thumbnails for image file: "
137                                                         + e.toString()
138                                                         + ", thumbnail generation skipped, enlisting as ordinary file instead.",
139                                         e);
140                         enlistFile(abstractFile);
141                 }
142         }
143
144         /** {@inheritDoc} */
145         @Override
146         public void enlistOgv(final AbstractFile abstractFile)
147                         throws UnsupportedEncodingException {
148
149                 final GeneralFile file = (GeneralFile) abstractFile;
150
151                 finishRow();
152
153                 filesHtml
154                                 .append("<TR style=\"background-color: #404040; text-align: center\">\n");
155                 filesHtml.append("<TD style=\"text-align: center\" colspan=\""
156                                 + String.valueOf(horizontalColumnsLimit) + "\">\n");
157
158                 filesHtml.append("<video controls=\"controls\" tabindex=\"0\">\n");
159                 filesHtml
160                                 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
161                                                 + UrlParamEncoder.encode(file.fileName) + "\">\n");
162                 filesHtml.append("</source>\n");
163
164                 // if browser cannot play video directry, fall back to java Applet
165                 filesHtml
166                                 .append("<applet code=\"com.fluendo.player.Cortado.class\" archive=\"http://theora.org/cortado.jar\" width=\"800\" height=\"600\">\n");
167                 filesHtml.append("    <param name=\"url\" value=\""
168                                 + UrlParamEncoder.encode(file.fileName) + "\"/>\n");
169                 filesHtml.append("    <param name=\"keepAspect\" value=\"true\"/>\n");
170                 filesHtml.append("    <param name=\"autoPlay\" value=\"false\"/>\n");
171                 filesHtml.append("</applet>\n");
172
173                 filesHtml.append("</video>\n");
174
175                 filesHtml.append("<br/>\n");
176
177                 filesHtml
178                                 .append("Video: <b>"
179                                                 + FilePathParser
180                                                                 .getFileNameWithoutExtension(file.fileName)
181                                                 + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
182                 filesHtml.append("<a href=\"" + UrlParamEncoder.encode(file.fileName)
183                                 + "\">download as OGV</a>\n");
184
185                 filesHtml.append("</TD></TR>\n");
186
187         }
188
189         public void finishRow() {
190
191                 if (currentColumn == 0)
192                         return;
193
194                 while (currentColumn <= horizontalColumnsLimit) {
195                         filesHtml.append("<TD></TD>");
196                         currentColumn++;
197                 }
198                 filesHtml.append("</TR>\n");
199                 currentColumn = 0;
200         }
201
202         /** {@inheritDoc} */
203         @Override
204         public String getFileNameSuffix() {
205                 return "";
206         }
207
208         /** {@inheritDoc} */
209         @Override
210         public String getHtml() {
211
212                 // assemble final page HTML
213                 final StringBuffer finalHtml = new StringBuffer();
214
215                 // page header
216                 finalHtml.append("<HTML>\n");
217                 finalHtml.append("<HEAD>\n");
218                 finalHtml
219                                 .append("    <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
220                 finalHtml.append("</HEAD>\n");
221                 finalHtml
222                                 .append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
223                 finalHtml.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
224                 finalHtml.append("<!-- " + Constants.HTML_MEMO + " -->\n");
225                 finalHtml.append("<FONT color=\"#ffffff\">\n");
226                 finalHtml.append("<CENTER><H1>" + galleryTitle + "</H1>\n");
227
228                 finalHtml.append("<H2>");
229                 for (final String p : path)
230                         finalHtml.append(" / " + p);
231                 finalHtml.append("</H2></CENTER>\n");
232
233                 // append directory listing
234                 finalHtml.append("<TABLE>\n");
235
236                 if (path.length > 0) {
237                         finalHtml.append("<TR>");
238                         finalHtml
239                                         .append("<TD><b><a href=\"../index.html\"> &lt;== ..</a></b></TD>");
240                         finalHtml.append("</TR>\n");
241                 }
242
243                 finalHtml.append(dirHtml.toString());
244                 finalHtml.append("\n<TR><TD><br></TD></TR>\n");
245                 finalHtml.append("</TABLE>\n");
246
247                 // append file listing
248
249                 finishRow();
250                 finalHtml.append("<TABLE width=\"1100\">\n");
251                 finalHtml.append(filesHtml.toString());
252                 finalHtml.append("\n</TABLE>\n");
253
254                 finalHtml.append("</font></BODY></HTML>\n");
255
256                 return finalHtml.toString();
257         }
258
259         @Override
260         public void init(final String galleryTitle, final String[] path,
261                         final AbstractIndexer indexer) {
262                 this.indexer = indexer;
263                 this.galleryTitle = galleryTitle;
264                 this.path = path;
265         }
266
267         public void startcolumn() {
268                 if (currentColumn == 0) {
269                         filesHtml.append("<TR>");
270                         currentColumn = 1;
271                 }
272         }
273
274 }