Package org.jampa.gui.runnables

Source Code of org.jampa.gui.runnables.DiskItemAdder

/*
* 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 java.util.List;

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;
import org.jampa.model.disk.IDiskItem;

public class DiskItemAdder implements IRunnableWithProgress {
 
  private String _playlistName;
  private List<IDiskItem> _items;
  private FileItem _firstFileItem = null;
  private int _insertionIndex;
 
  public DiskItemAdder(String playlistName, List<IDiskItem> items, int insertionIndex) {
    _playlistName = playlistName;
    _items = items;
    _insertionIndex = insertionIndex;
  }

  private void internalAddItemsToPlaylist(IProgressMonitor monitor, String playlistName, List<IDiskItem> items) {
    IDiskItem item;
    Iterator<IDiskItem> iter = items.iterator();
    while (iter.hasNext()) {
      if (monitor.isCanceled())
        return;
      item = iter.next();
      if (item instanceof DirectoryItem) {
        // First add the files.
        FileItem fileItem;
        Iterator<FileItem> fileIter = ((DirectoryItem) item).getFileList().iterator();
        while (fileIter.hasNext()) {
          if (monitor.isCanceled())
            return;
          fileItem = fileIter.next();
         
          if (_firstFileItem == null)
            _firstFileItem = fileItem;
         
          monitor.subTask(Messages.getString("PlaylistController.AddToPlaylist") + fileItem.getLabel()); //$NON-NLS-1$
          Controller.getInstance().getPlaylistController().addFileToPlaylist(playlistName, fileItem, false, false, _insertionIndex);
          if (_insertionIndex != -1) {
            _insertionIndex++;
          }
          monitor.worked(1);       
        }
        internalAddItemsToPlaylist(monitor, playlistName, ((DirectoryItem) item).getDirectoryListAsDiskItem());
      } else if (item instanceof FileItem) {
        if (_firstFileItem == null)
          _firstFileItem = (FileItem) item;
       
        monitor.subTask(Messages.getString("PlaylistController.AddToPlaylist") + item.getLabel()); //$NON-NLS-1$
        Controller.getInstance().getPlaylistController().addFileToPlaylist(playlistName, (FileItem) item, false, false, _insertionIndex);
        if (_insertionIndex != -1) {
          _insertionIndex++;
        }
        monitor.worked(1);
      }
    }
  }
 
  public FileItem getFirstFileItem() {
    return _firstFileItem;
  }
 
  @Override
  public void run(IProgressMonitor monitor) throws InvocationTargetException,  InterruptedException {
    int nbFiles = 0;
    IDiskItem item;
    Iterator<IDiskItem> iter = _items.iterator();
    while (iter.hasNext()) {
      item = iter.next();
      if (item instanceof DirectoryItem) {
        nbFiles += ((DirectoryItem) item).countAllFileItems();
      } else if (item instanceof FileItem) {
        nbFiles++;
      }
    }
    monitor.beginTask(Messages.getString("PlaylistController.AddToPlaylistMain"), nbFiles); //$NON-NLS-1$
   
    internalAddItemsToPlaylist(monitor, _playlistName, _items);
   
    monitor.done();
  }

}
TOP

Related Classes of org.jampa.gui.runnables.DiskItemAdder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.