/**
* GeoDress - A program for reverse geocoding
* Copyright (C) 2010 Stefan T.
*
* See COPYING for Details.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package geodress.model;
import geodress.exceptions.FileTypeNotSupportedException;
import geodress.exceptions.MetaDataErrorException;
import geodress.exceptions.NoMetaDataException;
import geodress.exceptions.ParameterOutOfRangeException;
import geodress.main.InfoConstants;
import geodress.main.Logging;
import geodress.model.reader.MetaDataReader;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Represents a loaded picture and has methods to access meta data.<br>
* There is a caching mechanism in this class: If data are requested the first
* time, they are read directly from the files and saved into
* {@link Picture#cache}. From now on, the data are returned from this cache
* instead of reading directly from the files for a better performance until
* {@link Picture#deleteCache()} is called.
*
* @author Stefan T.
*/
public class Picture extends File {
/** Logger object */
private Logger logger;
/** the reader for meta data */
private MetaDataReader metaReader;
/** the address where the picture was taken */
private Address pictureAddress;
/** this will be shown if no data are available for a certain field */
public static final String NO_DATA = "-";
/** an array that caches data for better performance */
private Object[] cache = new Object[6];
/** formatter for date/time */
private final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
"yyyy-MM-dd kk:mm:ss");
/**
* Creates a new picture with given pathname and an empty address field.
*
* @param pictureFile
* the file of the picture
*/
public Picture(File pictureFile) {
super(pictureFile.getAbsolutePath());
logger = Logging.getLogger(this.getClass().getName());
setAddress(new Address());
}
/**
* Gets the current reader for meta data.
*
* @return reader for meta data
*/
public MetaDataReader getMetaDataReader() {
return metaReader;
}
/**
* Registers a new reader for meta data.
*
* @param metaReader
* the new meta data reader
* @throws IOException
* see {@link MetaDataReader#setFile(File)}
* @throws FileTypeNotSupportedException
* see {@link MetaDataReader#setFile(File)}
*/
public void registerMetaDataReader(MetaDataReader metaReader)
throws IOException, FileTypeNotSupportedException {
this.metaReader = metaReader;
getMetaDataReader().setFile(this);
}
/**
* Gets the coordinates saved in the picture file (data are cached).
*
* @return the geographic coordinates
* @throws MetaDataErrorException
* thrown if an error occurs while reading meta data
* @throws ParameterOutOfRangeException
* thrown if the saved meta data are invalid
*/
public Coordinate getCoordinates() throws MetaDataErrorException,
ParameterOutOfRangeException {
if (cache[InfoConstants.GPS_COORDINATES] == null) {
logger.log(Level.FINEST, getName()
+ " - no caching of picture coordinates");
/* no caching */
String latitude = getMetaDataReader().getData(
InfoConstants.GPS_LATITUDE);
String longitude = getMetaDataReader().getData(
InfoConstants.GPS_LONGITUDE);
Coordinate coordinate = new Coordinate(latitude, longitude);
/* cache data */
cache[InfoConstants.GPS_COORDINATES] = coordinate;
return coordinate;
} else {
logger.log(Level.FINEST, getName()
+ " - caching of picture coordinates");
/* caching */
return (Coordinate) cache[InfoConstants.GPS_COORDINATES];
}
}
/**
* Gets the date/time of this picture as a string (data are cached).
*
* @return the date/time as a string (yyyy-MM-dd kk:mm:ss)
* @throws MetaDataErrorException
* thrown if an error occurs while reading meta data
*/
public String getDateAsString() throws MetaDataErrorException {
return DATE_FORMATTER.format(getDate().getTime());
}
/**
* Gets the date/time of this picture (data are cached).
*
* @return the date/time
* @throws MetaDataErrorException
* thrown if an error occurs while reading meta data
*/
public GregorianCalendar getDate() throws MetaDataErrorException {
if (cache[InfoConstants.DATE_TIME] == null) {
/* no caching */
GregorianCalendar resultCal = new GregorianCalendar();
try {
String time = getMetaDataReader().getData(
InfoConstants.DATE_TIME);
if (time == null) {
throw new NoMetaDataException("no date/time found in "
+ getPath());
}
/* parse String to Date */
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy:MM:dd kk:mm:ss");
resultCal.setTime(sdf.parse(time));
} catch (NoMetaDataException nmde) {
/* if no meta data available, take last modified date */
logger.log(Level.FINEST,
"no date/time meta data, take last modified date for "
+ getPath());
resultCal.setTimeInMillis(this.lastModified());
} catch (ParseException pe) {
logger.log(Level.WARNING, "date/time could not be parsed", pe);
}
/* cache data */
cache[InfoConstants.DATE_TIME] = resultCal;
return resultCal;
} else {
/* caching */
return (GregorianCalendar) cache[InfoConstants.DATE_TIME];
}
}
/**
* Gets the image description of this picture (data are cached).
*
* @return the image description
* @throws MetaDataErrorException
* thrown if an error occurs while reading meta data
*/
public String getDescription() throws MetaDataErrorException {
if (cache[InfoConstants.IMAGE_DESCRIPTION] == null) {
/* no caching */
String result = getMetaDataReader().getData(
InfoConstants.IMAGE_DESCRIPTION);
if (result == null || result.isEmpty()) {
result = NO_DATA;
}
/* cache data */
cache[InfoConstants.IMAGE_DESCRIPTION] = result;
return result;
} else {
/* caching */
return (String) cache[InfoConstants.IMAGE_DESCRIPTION];
}
}
/**
* Gets the user comment of this picture (data are cached).
*
* @return the user comment
* @throws MetaDataErrorException
* thrown if an error occurs while reading meta data
*/
public String getComment() throws MetaDataErrorException {
if (cache[InfoConstants.USER_COMMENT] == null) {
/* no caching */
String result = getMetaDataReader().getData(
InfoConstants.USER_COMMENT);
if (result == null || result.isEmpty()) {
result = NO_DATA;
}
/* cache data */
cache[InfoConstants.USER_COMMENT] = result;
return result;
} else {
/* caching */
return (String) cache[InfoConstants.USER_COMMENT];
}
}
/**
* Deletes the cache.
*
* @see Picture#cache
*/
public void deleteCache() {
for (int i = 0; i < cache.length; i++) {
cache[i] = null;
}
}
/**
* Gets the thumbnail of this picture.
*
* @return the thumbnail picture
*/
public Image getThumbnail() {
return null;
}
/**
* Set a picture address.
*
* @param pictureAddress
* the pictureAddress to set
*/
public void setAddress(Address pictureAddress) {
this.pictureAddress = pictureAddress;
}
/**
* Get the picture address.
*
* @return the pictureAddress
*/
public Address getAddress() {
return pictureAddress;
}
}