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