package net.sf.jpluck.apps.jpluckx;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Comparator;
import net.sf.jpluck.jxl.Document;
import net.sf.jpluck.jxl.Feed;
import net.sf.jpluck.jxl.JXL;
import net.sf.jpluck.jxl.Site;
import net.sf.jpluck.util.Language;
import org.xml.sax.SAXException;
public class Showcase extends JXL {
private static final String[] columnNames = { "Name", "Category", "Language"};
private Showcase(URL url) throws SAXException, IOException {
super(url);
}
public static Showcase create() {
try {
URL url = Showcase.class.getClassLoader().getResource("showcase/showcase.jxl");
return new Showcase(url);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int i) {
return columnNames[i];
}
public Object getValue(Object obj, int index) {
Document document = (Document) obj;
switch (index) {
case 0:
return document.getName();
case 1:
return document.getPrimaryCategory();
case 2:
if (document.getLanguage()!=null) {
return Language.getLanguage(document.getLanguage()).getName();
} else {
return null;
}
case 3:
if (document instanceof Feed) {
return "Feed";
} else {
return "Site";
}
}
return null;
}
public void sortByColumn(int index, boolean ascending) {
Collections.sort(documentList, new DocumentComparator(index, ascending));
fireContentsChanged(this, 0, documentList.size());
}
private static class DocumentComparator implements Comparator {
private boolean ascending;
private int column;
DocumentComparator(int column, boolean ascending) {
this.column = column;
this.ascending = ascending;
}
public int compare(Object o1, Object o2) {
Document d1 = (Document) o1;
Document d2 = (Document) o2;
String l1;
String l2;
int result = 0;
switch (column) {
case 0:
result = d1.getRawName().compareToIgnoreCase(d2.getRawName());
break;
case 1:
String c1 = d1.getPrimaryCategory();
if (c1 == null) {
c1 = "";
}
String c2 = d2.getPrimaryCategory();
if (c2 == null) {
c2 = "";
}
result = c1.compareToIgnoreCase(c2);
break;
case 2:
l1 = Language.getLanguage(d1.getLanguage()).getName();
l2 = Language.getLanguage(d2.getLanguage()).getName();
result = l1.compareTo(l2);
break;
case 3:
if (d1 instanceof Feed && d2 instanceof Site) {
result = -1;
} else if (d1 instanceof Site && d2 instanceof Feed) {
result = 1;
} else {
result = 0;
}
break;
}
return (ascending ? result : (result * -1));
}
}
}