001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2007
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.components.listener;
025:
026: import java.io.File;
027: import java.io.IOException;
028:
029: import org.riotfamily.common.image.Thumbnailer;
030: import org.riotfamily.components.service.UpdateListener;
031: import org.riotfamily.components.service.ComponentUpdate;
032:
033: /**
034: * @author Felix Gnass [fgnass at neteye dot de]
035: * @since 6.5
036: */
037: public class ThumbnailCreator implements UpdateListener {
038:
039: private String source;
040:
041: private String property;
042:
043: private int width;
044:
045: private int height;
046:
047: private String format = "jpg";
048:
049: private String fileStoreId;
050:
051: private Thumbnailer thumbnailer;
052:
053: private File tempDir;
054:
055: public void setProperty(String property) {
056: this .property = property;
057: }
058:
059: public void setSource(String source) {
060: this .source = source;
061: }
062:
063: public void setFileStoreId(String fileStoreId) {
064: this .fileStoreId = fileStoreId;
065: }
066:
067: public void setThumbnailer(Thumbnailer thumbnailer) {
068: this .thumbnailer = thumbnailer;
069: }
070:
071: public void setFormat(String format) {
072: this .format = format;
073: }
074:
075: public void setWidth(int width) {
076: this .width = width;
077: }
078:
079: public void setHeight(int height) {
080: this .height = height;
081: }
082:
083: public void onUpdate(ComponentUpdate model) throws IOException {
084: File imageFile = model.getFile(source);
085: File thumbFile = model.getFile(property);
086: if (imageFile != null) {
087: if (thumbFile == null
088: || thumbFile.lastModified() < imageFile
089: .lastModified()) {
090: File tempFile = File.createTempFile("thumb", "."
091: + format, tempDir);
092: thumbnailer.renderThumbnail(imageFile, tempFile, width,
093: height);
094: model.setFile(property, tempFile, fileStoreId);
095: }
096: } else {
097: model.setFile(property, null, fileStoreId);
098: }
099: }
100:
101: }
|