/* ********************************************************************** **
** Copyright notice **
** **
** (c) 2005-2006 RSSOwl Development Team **
** http://www.rssowl.org/ **
** **
** All rights reserved **
** **
** This program and the accompanying materials are made available under **
** the terms of the Eclipse Public License v1.0 which accompanies this **
** distribution, and is available at: **
** http://www.rssowl.org/legal/epl-v10.html **
** **
** A copy is found in the file epl-v10.html and important notices to the **
** license from the team is found in the textfile LICENSE.txt distributed **
** in this package. **
** **
** This copyright notice MUST APPEAR in all copies of the file! **
** **
** Contributors: **
** Christophe Bouhier - podcast plugin **
** **
** ********************************************************************** */
package org.rssowl.contrib.podcast.ui.media;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.rssowl.contrib.podcast.content.ContentAssociation;
import org.rssowl.contrib.podcast.model.IPersonalAttachment;
import org.rssowl.contrib.podcast.model.IPersonalBookMark;
import org.rssowl.contrib.podcast.model.IXFile;
import org.rssowl.contrib.podcast.model.PersonalFiles;
import org.rssowl.contrib.podcast.util.Util;
/**
* The model preparator accepts a feed and preps it to be viewed without
* minimal processing. The feed data is pre-processed and can be retrieved
* for each of the applicable columns.
*/
public class ModelPreparator {
public Color STATUS_INCOMPLETE_COLOR;
// CSS #f5aef9
public Color STATUS_SCHEDULED_COLOR;
// CSS #FDDA4
public Color LOCAL_ODD_BACKGROUND_COLOR;
public Color APP_FONT3_COLOR;
public Color FEED_TITLE_COLOR;
public Color TABLE_ODD_BACKGROUND_COLOR;
@SuppressWarnings("unused")
private final MediaView mMediaView;
protected IPersonalBookMark mBookMark;
protected HashMap<String,Image> mImageMap;
protected Object[] mElements;
public ModelPreparator(MediaView pMediaView, IPersonalBookMark pBookMark) {
STATUS_INCOMPLETE_COLOR = new Color(Display
.getDefault(), 251, 251, 192);
STATUS_SCHEDULED_COLOR = new Color(Display.getDefault(), 245,
174, 249);
LOCAL_ODD_BACKGROUND_COLOR = new Color(Display
.getDefault(), 253, 221, 164);
APP_FONT3_COLOR = new Color(Display.getDefault(),
96, 96, 96);
FEED_TITLE_COLOR = new Color(Display.getDefault(), 0,
57, 163);
TABLE_ODD_BACKGROUND_COLOR = new Color(Display
.getDefault(), 250, 250, 237);
mMediaView = pMediaView;
mBookMark = pBookMark;
mImageMap = buildImageMap(mBookMark);
updateModel();
}
/**
* When the model changes, we should call this on the preparator. TODO
* The preparator should really listen to model changes.
*/
public void updateModel() {
mElements = PersonalFiles.getInstance().getMergedArray(mBookMark, true);
}
public IPersonalBookMark getPersonalBookMark() {
return mBookMark;
}
public int indexOf(IXFile pFile) {
for (int i = 0; i < mElements.length; i++) {
if (mElements[i].equals(pFile)) {
return i;
}
}
return 0;
}
/**
* Build a map for the required view for this feed. (File extensions in
* the enclosures).
*
* @return
*/
public HashMap<String, Image> buildImageMap(IPersonalBookMark pBookMark) {
HashMap<String,Image> lImageMap = new HashMap<String,Image>();
mElements = PersonalFiles.getInstance().getMergedArray(pBookMark, true);
for (int i = 0; i < mElements.length; i++) {
IXFile lFile = (IXFile) mElements[i];
String lFileName = null;
if (lFile.getFile() != null) {
lFileName = lFile.getName();
}
if (lFileName != null && lFileName.length() > 0) {
String lExtension = Util.stripName(lFileName);
if (lExtension != null && lImageMap.get(lExtension) == null) {
Image lImage = getImage(lExtension);
if (lImage != null) {
lImageMap.put(lExtension, lImage);
}
}
}
}
return lImageMap;
}
public Image getImage(IPersonalAttachment pEnclosure) {
return getImage((IXFile) pEnclosure);
}
public Image getImage(IXFile pFile) {
if (pFile.getFile() == null) {
return null;
}
String lName = pFile.getFile().getName();
String lExtension = Util.stripName(lName);
if (lExtension != null && mImageMap.containsKey(lExtension)) {
return (Image) mImageMap.get(lExtension);
}
return null;
}
public void dispose(){
destroyImageMap();
destroyColors();
}
private void destroyImageMap() {
Collection<Image> lValues = mImageMap.values();
Iterator<Image> lIt = lValues.iterator();
while (lIt.hasNext()) {
Image lImage = (Image) lIt.next();
lImage.dispose();
}
}
private void destroyColors() {
APP_FONT3_COLOR.dispose();
STATUS_INCOMPLETE_COLOR.dispose();
LOCAL_ODD_BACKGROUND_COLOR.dispose();
STATUS_SCHEDULED_COLOR.dispose();
TABLE_ODD_BACKGROUND_COLOR.dispose();
FEED_TITLE_COLOR.dispose();
}
private Image getImage(String lExtension) {
Program lProgram = ContentAssociation.getOSAssociation(lExtension);
if (lProgram != null) {
try {
ImageData lData = lProgram.getImageData();
if (lData != null) {
return new Image(null, lData);
}
} catch (ArrayIndexOutOfBoundsException aio) {
// CB TODO This is a SWT bug, have to report with SWT.
}
}
// CB TODO, Should perhaps return a default icon.
return null;
}
}