/*
* 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.library.ILibraryItem;
import org.jampa.model.library.TitleItem;
import org.jampa.utils.Constants;
public class LibraryAdder implements IRunnableWithProgress {
private String _playlistName;
private ILibraryItem _libraryItem;
private ILibraryItem _firstItem;
private int _insertionIndex;
public LibraryAdder(String playlistName, ILibraryItem libraryItem) {
this(playlistName, libraryItem, -1);
}
public LibraryAdder(String playlistName, ILibraryItem libraryItem, int insertionIndex) {
_playlistName = playlistName;
_libraryItem = libraryItem;
_firstItem = null;
_insertionIndex = insertionIndex;
}
private void internalAdd(IProgressMonitor monitor, ILibraryItem item) {
if (monitor.isCanceled())
return;
if (item instanceof TitleItem) {
if (_firstItem == null) {
_firstItem = item;
}
monitor.subTask(Messages.getString("LibraryAdder.AddingItem") + " " + ((TitleItem) item).getName()); //$NON-NLS-1$ //$NON-NLS-2$
Controller.getInstance().getPlaylistController().addFileToPlayList(_playlistName, ((TitleItem) item).getFilePath(), false, false, _insertionIndex);
if (_insertionIndex != -1) {
_insertionIndex++;
}
} else {
ILibraryItem child;
Iterator<ILibraryItem> childrenIter = item.getChildrenList().iterator();
while (childrenIter.hasNext()) {
child = childrenIter.next();
internalAdd(monitor, child);
}
}
monitor.worked(1);
}
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
int nbItems = _libraryItem.getChildCount();
if (_playlistName.equals(Constants.DEFAULT_PLAYLIST_ID)) {
monitor.beginTask(Messages.getString("LibraryAdder.AddingToDefault"), nbItems); //$NON-NLS-1$
} else {
monitor.beginTask(Messages.getString("LibraryAdder.Adding") + " " + _playlistName, nbItems); //$NON-NLS-1$ //$NON-NLS-2$
}
internalAdd(monitor, _libraryItem);
monitor.done();
}
public ILibraryItem getFirstItem() {
return _firstItem;
}
}