/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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.
*/
package org.jampa.gui.runnables;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.jampa.controllers.Controller;
import org.jampa.gui.translations.Messages;
import org.jampa.model.disk.DirectoryItem;
import org.jampa.model.disk.FileItem;
/**
* Class to implement the progress dialog during library scan.
* @author jerem
*
*/
public class DatabaseAdder implements IRunnableWithProgress {
private DirectoryItem _rootItem;
public DatabaseAdder(DirectoryItem rootItem) {
_rootItem = rootItem;
}
private void internalAddDirectory(IProgressMonitor monitor, DirectoryItem directoryItem) {
// First add the files.
FileItem item;
Iterator<FileItem> iter = directoryItem.getFileList().iterator();
while (iter.hasNext()) {
if (monitor.isCanceled())
return;
item = iter.next();
monitor.subTask(Messages.getString("Controller.ScanningDirectory") + item.getLabel()); //$NON-NLS-1$
Controller.getInstance().getHSQLController().insertFileToLibrary(item.getFileName());
monitor.worked(1);
}
// then the directories.
Iterator<DirectoryItem> dirIter = directoryItem.getDirectoryList().iterator();
while (dirIter.hasNext()) {
internalAddDirectory(monitor, dirIter.next());
}
}
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
int nbFiles = _rootItem.countAllFileItems();
monitor.beginTask(Messages.getString("Controller.ScanningLibrary"), nbFiles); //$NON-NLS-1$
internalAddDirectory(monitor, _rootItem);
monitor.done();
}
}