package net.sf.jpluck.apps.jpluckx;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;
import javax.swing.AbstractListModel;
import net.sf.jpluck.conversion.Conversion;
import net.sf.jpluck.conversion.ConversionAdapter;
import net.sf.jpluck.jxl.Document;
import net.sf.jpluck.jxl.JXL;
public class AutoUpdate extends AbstractListModel {
private List fileList = new ArrayList();
private Logger logger = Logger.getLogger("autoupdate");
private Timer timer = new Timer();
public AutoUpdate(int intervalInMinutes) {
long period = 1000 * intervalInMinutes;
timer.schedule(new UpdateTask(), 0, period);
}
public Object getElementAt(int index) {
return fileList.get(index);
}
public int getSize() {
return fileList.size();
}
public void add(File file) {
fileList.add(file);
}
public void cancel() {
timer.cancel();
}
public File get(int idx) {
return (File) fileList.get(idx);
}
public void remove(File file) {
int idx = fileList.indexOf(file);
fireIntervalRemoved(this, idx, idx);
}
private class UpdateTask extends TimerTask {
private Conversion conversion;
public void run() {
if (conversion != null) {
return;
}
for (Iterator it = fileList.iterator(); it.hasNext();) {
File file = (File) it.next();
try {
JXL jxl = new JXL(file);
Document[] documents = jxl.getDocumentsToConvert();
conversion = new Conversion(documents);
conversion.addConversionListener(new ConversionAdapter() {
public void conversionFinished() {
conversion = null;
}
});
Thread thread = new Thread(conversion);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
} catch (Exception e) {
logger.warning(file.getAbsolutePath() + ": " + e.getClass() + ": " + e.getMessage());
}
}
}
}
}