/* ********************************************************************** **
** 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.io.File;
import java.util.Date;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.rssowl.contrib.podcast.model.IPersonalAttachment;
import org.rssowl.contrib.podcast.model.IXFile;
import org.rssowl.contrib.podcast.util.Util;
/**
* @author <a href="mailto:christophe@kualasoft.com">Christophe Bouhier </a>
* @version 1.1
*/
public class MediaTableProvider extends LabelProvider implements
ITableLabelProvider, ITableColorProvider, ITableFontProvider {
ModelPreparator mPreparator;
FontRegistry registry = new FontRegistry();
public MediaTableProvider(ModelPreparator pPreparator) {
mPreparator = pPreparator;
}
/**
*/
public String getColumnText(Object element, int columnIndex) {
Object lResult = getValueAt((IXFile) element, columnIndex);
if (lResult instanceof Integer) {
String lFormatted = Util.formatSize(lResult);
return ((Number) lResult).intValue() == 0 ? "" : lFormatted;
}
if (lResult instanceof Boolean) {
if (((Boolean) lResult).booleanValue()) {
return "Yes";
} else {
return "No";
}
}
if (lResult instanceof String) {
return (String) lResult;
}
return lResult == null ? "?????" : lResult.toString();
}
/**
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
* int)
*/
public Image getColumnImage(Object pElement, int pColumnIndex) {
if (pElement instanceof IXFile) {
IXFile lEnclosure = (IXFile) pElement;
switch (pColumnIndex) {
case 0: {
return null; // Bug in Windows?
}
case 1: {
if (mPreparator != null) {
return mPreparator.getImage(lEnclosure);
}
}
}
}
return null;
}
public Color getForeground(Object pElement, int pColumnIndex) {
Color lColor = null;
if (!(pElement instanceof IXFile)) {
return null;
}
IXFile lFile = (IXFile) pElement;
int lRowIndex = mPreparator.indexOf((IXFile) pElement);
if (pColumnIndex == 1) {
// CB TODO, Note that the status subscribed is not fetched from ther
// Bookmark iteslf.
int lStatus = lFile.getStatus();
if (lStatus >= 0) {
// Check the enclosure status if a new pRow is detected.
if ((lStatus & 8) != 8 || (lStatus & 4) == 4) {
lColor = mPreparator.APP_FONT3_COLOR;
return lColor;
}
} else { // These are the local files.
}
}
return lColor;
}
public Color getBackground(Object pElement, int pColumnIndex) {
Color lColor = null;
int lRowIndex;
if (!(pElement instanceof IXFile)) {
return null;
}
IXFile lFile = (IXFile) pElement;
lRowIndex = mPreparator.indexOf((IXFile) pElement);
if (lRowIndex >= 0) {
// CB TODO, Note that the status subscribed is not fetched from ther
// Bookmark iteslf.
int lStatus = lFile.getStatus();
if (lStatus >= 0) {
// Check the enclosure status if a new pRow is detected.
if ((lStatus & 2) == 2) {
lColor = mPreparator.STATUS_SCHEDULED_COLOR;
return lColor;
}
if ((lStatus & 4) == 4) {
lColor = mPreparator.STATUS_INCOMPLETE_COLOR;
return lColor;
}
} else { // These are the local files.
if ((lRowIndex & 1) == 0) {
lColor = mPreparator.LOCAL_ODD_BACKGROUND_COLOR;
}
}
}
return lColor;
}
public Font getFont(Object pElement, int pColumnIndex) {
Font lFont = null;
if (!(pElement instanceof IXFile)) {
return null;
}
IXFile lFile = (IXFile) pElement;
// int lRowIndex = mPreparator.indexOf((IXFile) pElement);
if (pColumnIndex == 1) {
// CB TODO, Note that the status subscribed is not fetched from ther
// Bookmark iteslf.
int lStatus = lFile.getStatus();
if (lStatus >= 0) {
// Check the enclosure status if a new pRow is detected.
if ((lStatus & 8) != 8 || (lStatus & 4) == 4) {
// CB TODO A bit of hack.
return registry.getItalic(Display.getCurrent()
.getSystemFont().getFontData()[0].getName());
}
} else { // These are the local files.
}
}
return lFont;
}
/**
* Returns the image with the given key, or <code>null</code> if not
* found.
*/
public Object getValueAt(IXFile pFile, int column) {
Object lReturn = "";
switch (column) {
case 0: {
return new Integer(mPreparator.indexOf(pFile)).toString();
}
case 1: {
if (pFile instanceof IPersonalAttachment) {
IPersonalAttachment lEncl = (IPersonalAttachment) pFile;
return lEncl.getNews().getTitle();
}
return pFile.getName();
}
case 2: {// Size web
if (pFile instanceof IPersonalAttachment) {
lReturn = new Integer(pFile.getFileLength());
} else {
lReturn = new Integer(0);
}
break;
}
case 3: {// Size disk
File localFile = pFile.getFile();
if (localFile != null && !localFile.getPath().equals("null")) {
lReturn = new Integer((int) localFile.length());
} else {
lReturn = "";
}
break;
}
case 4: { // If in player.
lReturn = new Boolean(pFile.getInPlayer());
break;
}
case 5: { // Last modified
if (pFile instanceof IPersonalAttachment) {
IPersonalAttachment lEnclosure = (IPersonalAttachment) pFile;
Date lDate = lEnclosure.getNews().getPublishDate();
lReturn = Util.formatDate(lDate, "EEEE, MMM d hh:mm a");
} else {
Date lDate = pFile.getDate();
lReturn = Util.formatDate(lDate, "EEEE, MMM d hh:mm a");
}
break;
}
case 6: { // marked
lReturn = new Boolean(pFile.isMarked());
break;
}
default:
lReturn = "";
}
return lReturn;
}
}