refactored code for better readability
[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.IndexingContext;
22 import eu.svjatoslav.meviz.htmlindexer.Layout;
23 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
24 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
25 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
26 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
27 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
28
29 public class MixedLayout implements Layout {
30
31         Logger logger = Logger.getLogger(MixedLayout.class);
32
33         StringBuffer dirHtml = new StringBuffer();
34
35         StringBuffer filesHtml = new StringBuffer();
36
37         String galleryTitle;
38
39         List<String> path;
40
41         public static final String encoding = "UTF-8";
42
43         private AbstractIndexer indexer;
44
45         private IndexingContext indexingContext;
46
47         @Override
48         public void enlistDirectory(final AbstractFile directory,
49                         final DirectoryMetadata parentDirectoryMetadata) {
50                 dirHtml.append("<TR>");
51                 dirHtml.append("<TD><b><a href=\""
52                                 + indexer.getDirectoryUrl(directory, indexingContext)
53                                 + "\"><h2> ==&gt; " + directory.fileName + "</h2></a></b></TD>");
54                 dirHtml.append("</TR>\n");
55
56         }
57
58         @Override
59         public void enlistFile(final AbstractFile file,
60                         final DirectoryMetadata parentDirectoryMetadata) {
61
62                 final String fullFileName = file.fileName;
63
64                 filesHtml.append("<div style=\"clear: both;\"><a href=\""
65                                 + indexingContext.getGlobalUrl()
66                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
67                                                 + fullFileName) + "\">" + fullFileName + "</a>\n");
68                 filesHtml.append("&nbsp;&nbsp;("
69                                 + FilePathParser.getFileSizeDescription(file.getFileLength())
70                                 + ")</div><br/>\n");
71
72         }
73
74         @Override
75         public void enlistImage(final Picture picture,
76                         final DirectoryMetadata parentDirectoryMetadata) {
77                 try {
78                         enlistImageInternal(picture);
79                 } catch (final Exception e) {
80                         logger.error(
81                                         "Error generating thumbnails for image file: "
82                                                         + e.toString()
83                                                         + ", thumbnail generation skipped, enlisting as ordinary file instead.",
84                                         e);
85                         enlistFile(picture, parentDirectoryMetadata);
86                 }
87         }
88
89         private String enlistImageDimensions(final Picture picture) {
90                 final StringBuilder html = new StringBuilder();
91
92                 final List<Dimension> recommendedDimensions = Picture
93                                 .getThumbnailDimensions(picture.getDimensions());
94
95                 int count = 0;
96                 for (final Dimension dimension : recommendedDimensions) {
97                         count++;
98                         if (count > 1)
99                                 html.append(", ");
100                         html.append("<a href=\""
101                                         + indexer.getThumbnailUrl(picture, dimension,
102                                                         indexingContext) + "\">");
103                         html.append(dimension.width + "x" + dimension.height);
104                         html.append("</a>");
105                 }
106
107                 return html.toString();
108         }
109
110         private void enlistImageInternal(final Picture picture) {
111
112                 filesHtml.append("<div style=\"float:left; margin: 10px;\">");
113
114                 // clickable image thumbnail
115                 filesHtml.append("<a href=\""
116                                 + indexingContext.getGlobalUrl()
117                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
118                                                 + picture.fileName) + "\">\n");
119                 filesHtml.append("<img border=\"0\" src=\""
120                                 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
121
122                 // file name
123                 filesHtml.append("<b>"
124                                 + FilePathParser.getFileNameWithoutExtension(picture.fileName)
125                                 + "</b><br/>");
126
127                 // links to thumbnails of various sizes
128                 filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
129
130                 filesHtml.append("</div>");
131         }
132
133         /** {@inheritDoc} */
134         @Override
135         public void enlistOgv(final AbstractFile abstractFile,
136                         final DirectoryMetadata parentDirectoryMetadata)
137                         throws UnsupportedEncodingException {
138
139                 final GeneralFile file = (GeneralFile) abstractFile;
140
141                 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
142
143                 // watchable video
144                 filesHtml
145                                 .append("<video preload=\"metadata\" controls=\"controls\" tabindex=\"0\">\n");
146                 filesHtml
147                                 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
148                                                 + indexingContext.getGlobalUrl()
149                                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
150                                                                 + "/" + file.fileName) + "\">\n");
151                 filesHtml.append("</source>\n");
152                 filesHtml.append("</video>\n");
153                 filesHtml.append("<br/>\n");
154
155                 // video title
156                 filesHtml
157                                 .append("Video: <b>"
158                                                 + FilePathParser
159                                                                 .getFileNameWithoutExtension(file.fileName)
160                                                 + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
161
162                 // video download link
163                 filesHtml.append("<a href=\""
164                                 + indexingContext.getGlobalUrl()
165                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
166                                                 + file.fileName) + "\">download as OGV</a>");
167
168                 filesHtml.append("</div>\n");
169
170                 filesHtml.append("\n");
171
172         }
173
174         /** {@inheritDoc} */
175         @Override
176         public String getFileNameSuffix() {
177                 return "";
178         }
179
180         /** {@inheritDoc} */
181         @Override
182         public String getHtml(final boolean showTitle,
183                         final boolean doHtmlHeaderAndFooter) {
184
185                 // assemble final page HTML
186                 final StringBuffer finalHtml = new StringBuffer();
187
188                 if (doHtmlHeaderAndFooter) {
189                         // page header
190                         finalHtml.append("<HTML>\n");
191                         finalHtml.append("<HEAD>\n");
192                         finalHtml
193                                         .append("    <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
194                         finalHtml.append("</HEAD>\n");
195                         finalHtml
196                                         .append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
197                         finalHtml.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
198                         finalHtml.append("<!-- " + Constants.HTML_MEMO + " -->\n");
199                         finalHtml.append("<font color=\"#ffffff\">\n");
200                 }
201
202                 if (showTitle)
203                         finalHtml
204                                         .append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
205
206                 showCurrentPath(finalHtml);
207
208                 listDirectories(finalHtml);
209
210                 finalHtml.append(filesHtml.toString());
211
212                 if (doHtmlHeaderAndFooter)
213                         finalHtml.append("</font></BODY></HTML>\n");
214
215                 return finalHtml.toString();
216         }
217
218         private String getImageThumbnailUrl(final Picture picture) {
219                 final Dimension imageSize = picture.getDimensions();
220
221                 Dimension listingDimension = new Dimension(1500, 450);
222
223                 if (listingDimension.width > imageSize.width)
224                         if (listingDimension.height > imageSize.height)
225                                 listingDimension = imageSize;
226
227                 return indexer.getThumbnailUrl(picture, listingDimension,
228                                 indexingContext);
229         }
230
231         @Override
232         public void init(final String galleryTitle, final List<String> path,
233                         final AbstractIndexer indexer, final IndexingContext indexingContext) {
234
235                 this.indexer = indexer;
236                 this.galleryTitle = galleryTitle;
237                 this.path = path;
238                 this.indexingContext = indexingContext;
239
240                 dirHtml = new StringBuffer();
241                 filesHtml = new StringBuffer();
242         }
243
244         private void listDirectories(final StringBuffer finalHtml) {
245                 finalHtml.append("<TABLE>\n");
246
247                 if (path.size() > 0) {
248                         finalHtml.append("<TR>");
249                         finalHtml.append("<TD><b><a href=\""
250                                         + indexer.getParentDirectoryUrl(indexingContext)
251                                         + "\"><h2> &lt;== parent directory</h2></a></b></TD>");
252                         finalHtml.append("</TR>\n");
253                 }
254
255                 finalHtml.append(dirHtml.toString());
256                 finalHtml.append("\n<TR><TD><br></TD></TR>\n");
257                 finalHtml.append("</TABLE>\n");
258         }
259
260         private void showCurrentPath(final StringBuffer finalHtml) {
261                 finalHtml.append("<CENTER><H2>");
262                 for (final String p : path)
263                         finalHtml.append(" / " + p);
264                 finalHtml.append("</H2></CENTER>\n");
265         }
266
267 }