Package org.rssowl.contrib.podcast.ui.media

Source Code of org.rssowl.contrib.podcast.ui.media.MediaController

/*   **********************************************************************  **
**   Copyright notice                                                       **
**                                                                          **
**   (c) 2005-2006 RSSOwl Development Team                                  **
**   http://www.rssowl.org/                                                 **
**                                                                          **
**   All rights reserved                                                    **
**                                                                          **
**   This program and the accompanying materials are made available under   **
**   the terms of the Eclipse Public License v1.0 which accompanies this    **
**   distribution, and is available at:                                     **
**   http://www.rssowl.org/legal/epl-v10.html                               **
**                                                                          **
**   A copy is found in the file epl-v10.html and important notices to the  **
**   license from the team is found in the textfile LICENSE.txt distributed **
**   in this package.                                                       **
**                                                                          **
**   This copyright notice MUST APPEAR in all copies of the file!           **
**                                                                          **
**   Contributors:                                                          **
**     Christophe Bouhier - podcast plugin                         **
**                                                                          **
**  **********************************************************************  */

package org.rssowl.contrib.podcast.ui.media;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Display;
import org.rssowl.contrib.podcast.core.download.Download;
import org.rssowl.contrib.podcast.core.download.DownloadEvent;
import org.rssowl.contrib.podcast.core.download.IDownloadListener;
import org.rssowl.contrib.podcast.core.net.INetTaskListener;
import org.rssowl.contrib.podcast.core.net.NetTaskEvent;
import org.rssowl.contrib.podcast.core.process.IInstructionListener;
import org.rssowl.contrib.podcast.core.process.InstructionEvent;
import org.rssowl.contrib.podcast.core.process.InstructionService;
import org.rssowl.contrib.podcast.model.IPersonalBookMark;
import org.rssowl.contrib.podcast.model.IXFile;

/**
* A file control class. This class defines a list selection listener.
*
* @author <a href="mailto:christophe@kualasoft.com">Christophe Bouhier </a>
* @author <a href="mailto:andreas.schaefer@madplanet.com">Andreas Schaefer </a>
* @version 1.1
*/
public class MediaController implements ActionListener,
    IInstructionListener, IDownloadListener, INetTaskListener {

  private MediaView mView;

  // CB TODO migrate the file view.
// protected ID3Control mID3Controller = new ID3Control();

  public MediaController() {
 
  }

  public void setView(MediaView pView) {
    mView = pView;
    // CB TODO, ADD Dispose listener.
    InstructionService.getInstance().getNotifier().addListener(this);
    initializeUI();
  }

  // CB Remove later if no external controllers are set.
  // public void setControllers(ID3Control pID3Controller) {
  // mID3Controller = pID3Controller;
  // initialize();
  // }

  public void initialize() {
  }

  public void initializeUI() {

// mFileFilterItemsAction = new FileViewSelector();
//
// mView.mFileFilterMenu.setText(Messages.getString("fileControl.view"));
//
// mView.mFileViewItemsMenu.addListener(SWT.Selection,
// mFileFilterItemsAction);
// mView.mFileViewItemsMenu.setText(mFileFilterItemsAction.getName());

    mView.setViewer(MediaView.MEDIA_VIEW);

//    mView.mMediaViewer.mTableViewer
//        .addSelectionChangedListener(new FileSelection());
//    setMarkingActionStatus(false);
  }

  public MediaView getView() {
    return mView;
  }

  // CB TODO, the input.
  public void setMediaView(IPersonalBookMark pBookMark) {

    // mFeed = pFeed;

    // Check this feed as the RSS model might not be available.
    // TODO, move assertion

    if (pBookMark != null) {
      mView.setInput(pBookMark);
//      setMarkingActionStatus(true);
    } else {
      mView.clean();
//      setActionStatus(false, null);
//      setMarkingActionStatus(false);
    }
  }
 
 
  // CB TODO, remove selection listener,
//  private class FileSelection implements ISelectionChangedListener {
//    public void selectionChanged(SelectionChangedEvent event) {
//      // if the selection is empty clear the label
//      if (event.getSelection().isEmpty()) {
//        return;
//      }
//      if (event.getSelection() instanceof IStructuredSelection) {
//        IStructuredSelection selection = (IStructuredSelection) event
//            .getSelection();
//        mView.mSelection = selection.getFirstElement();
//        mView.mSelectionArray = selection.toArray();
//
//        if (mView.mSelection instanceof IXFile) {
//          IXFile lFile = (IXFile) mView.mSelection;
//          setActionStatus(true, lFile);
//        }
//        if (mView.mSelection instanceof IXItem) {
//          setActionStatus(false, null);
//        }
//      }
//    }
//  };

//  public void setActionStatus(boolean pStatus, IXFile pFile) {
//    ((IBaseActionCondition) mFileID3RewriteTagsAction).setEnabled(pStatus,
//        pFile);
//    ((IBaseActionCondition) mFileDownloadAction).setEnabled(pStatus, pFile);
//    ((IBaseActionCondition) mFilePlayAction).setEnabled(pStatus, pFile);
//    ((IBaseActionCondition) mFileOpenAction).setEnabled(pStatus, pFile);
//    ((IBaseActionCondition) mFileDeleteSelectedAction).setEnabled(pStatus,
//        pFile);
//    ((IBaseActionCondition) mFileAddSelectedToPlayerAction).setEnabled(
//        pStatus, pFile);
//    ((IBaseActionCondition) mFileID3ViewAction).setEnabled(pStatus, pFile);
//
//    // mView.mFileID3EditMenu.setEnabled(pStatus);
//    // mView.mFileID3EditPopupMenu.setEnabled(pStatus);
//  }


//  public class FileViewSelector extends BaseAction {
//
//    public FileViewSelector() {
//      init(Messages.getString("fileControl.items"), true);
//    }
//
//    public void handleEvent(Event event) {
//      if (mView.mFileViewItemsMenu.getSelection()) {
//        if (mView.mCurrentView != MediaView.ITEM_VIEW) {
//          mView.setViewer(MediaView.ITEM_VIEW);
//          mView.mItemViewer.mTreeViewer
//              .addSelectionChangedListener(new FileSelection());
//
//        }
//      } else {
//        if (mView.mCurrentView != MediaView.MEDIA_VIEW) {
//          mView.setViewer(MediaView.MEDIA_VIEW);
//          mView.mMediaViewer.mTableViewer
//              .addSelectionChangedListener(new FileSelection());
//        }
//      }
//      if (mFeed != null) {
//        mView.setInput(mFeed);
//      }
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }

//  private class FileID3ViewAction extends BaseAction implements
//      IBaseActionCondition {
//    public FileID3ViewAction() {
//      init(Messages.getString("id3control.mp3view"), false);
//    }
//
//    public void handleEvent(Event e) {
//    }
//
//
//    public void setEnabled(boolean pEnabled, Object pConditionObject) {
//      if (conditionMet(pConditionObject)) {
//        mView.mFileID3ViewMenu.setEnabled(true);
//        mView.mFileID3ViewPopupMenu.setEnabled(true);
//      } else {
//        mView.mFileID3ViewMenu.setEnabled(false);
//        mView.mFileID3ViewPopupMenu.setEnabled(false);
//      }
//    }
//
//    public boolean conditionMet(Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setEnabled(boolean pEnabled, Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }

//  private class FileID3RewriteAction extends BaseAction implements
//      IBaseActionCondition {
//
//    public FileID3RewriteAction() {
//      init(Messages.getString("fileControl.rewrite"), false);
//    }
//
//    public void handleEvent(Event e) {
//      Object[] lFileSelection = getSelectedFiles();
//      if (lFileSelection.length != 0) {
//        ID3Logic.getInstance().ID3Rewrite(lFileSelection);
//      }
//    }
//
//    public void setEnabled(boolean pEnabled, Object pConditionObject) {
//      mView.mFileID3RewriteMenu
//          .setEnabled(conditionMet(pConditionObject));
//      mView.mFileID3RewritePopupMenu
//          .setEnabled(conditionMet(pConditionObject));
//    }
//
//    public boolean conditionMet(Object pConditionObject) {
//      boolean lEnable = false;
//      if (pConditionObject instanceof IXFile) {
//        IXFile lFile = (IXFile) pConditionObject;
//        lEnable = ID3Logic.getInstance().supportsID3(lFile);
//      }
//      return lEnable;
//    }
//
//    public boolean conditionMet(Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setEnabled(boolean pEnabled, Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }
//
//  private class FileDownloadAction extends BaseAction implements
//      IBaseActionCondition {
//    public FileDownloadAction() {
//      init(Messages.getString("downloadscontrol.download"), false);
//    }
//
//    public void handleEvent(Event e) {
//      Object[] lFiles = getSelectedFiles();
//      for (int j = 0; j < lFiles.length; j++) {
//        IXFile lFile = (IXFile) lFiles[j];
//        if (lFile instanceof IXPersonalEnclosure) {
//          IXPersonalEnclosure lEnclosure = (IXPersonalEnclosure) lFile;
//          DownloadLogic.getInstance().addDownload(lEnclosure);
//        }
//      }
//    }
//
//    public void setEnabled(boolean pEnabled, Object pConditionObject) {
//      mView.mFileDownloadMenu.setEnabled(conditionMet(pConditionObject));
//      mView.mFileDownloadPopupMenu
//          .setEnabled(conditionMet(pConditionObject));
//    }
//
//    public boolean conditionMet(Object pConditionObject) {
//      boolean lEnable = false;
//      if (pConditionObject instanceof IXPersonalEnclosure) {
//        IXPersonalEnclosure lEnclosure = (IXPersonalEnclosure) pConditionObject;
//        try {
//
//          // Check if we are downloading.
//          if (DownloadLogic.getInstance().getDownload(lEnclosure) == null) {
//            if (!lEnclosure.isLocal() || lEnclosure.isIncomplete()) {
//              mView.mFileDownloadMenu.setText(Messages
//                  .getString("fileControl.download")
//                  + lEnclosure.getName());
//              lEnable = true;
//            }
//          }
//        } catch (JPodderException e) {
//          e.printStackTrace();
//        }
//      } else {
//        mView.mFileDownloadMenu.setText(Messages
//            .getString("fileControl.download"));
//      }
//      return lEnable;
//    }
//
//    public boolean conditionMet(Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setEnabled(boolean pEnabled, Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }

//  private class MarkAllEnclosuresAction extends BaseAction {
//    public MarkAllEnclosuresAction() {
//      init(Messages.getString("feedcontrol.markAll"), true);
//    }
//
//    public void handleEvent(Event e) {
//      Iterator it = mFeed.getMerged(false).iterator();
//      while (it.hasNext()) {
//        IXFile file = (IXFile) it.next();
//        file.setMarked(true);
//      }
//      mView.update();
//      StatusController.getInstance().updateSummary();
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }
//
//  private class UnmarkAllEnclosuresAction extends BaseAction {
//    public UnmarkAllEnclosuresAction() {
//      init(Messages.getString("feedcontrol.clearAll"), true);
//    }
//
//    public void handleEvent(Event e) {
//      Iterator it = mFeed.getMerged(false).iterator();
//      while (it.hasNext()) {
//        IXFile file = (IXFile) it.next();
//        file.setMarked(false);
//      }
//      mView.update();
//      StatusController.getInstance().updateSummary();
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }
//
//  private class DeleteMarkedAction extends BaseAction {
//
//    public DeleteMarkedAction() {
//      init(Messages.getString("fileControl.delete"), true);
//    }
//
//    public void handleEvent(Event e) {
//      int count = mFeed.getMergedCount(true); // Only count files on
//      // the disc.
//      if (count == 0) {
//        MessageDialog.openInformation(UILauncher.getInstance()
//            .getShell(), Messages
//            .getString("fileControl.noSelection"), Messages
//            .getString("fileControl.noSelection.title"));
//        return;
//      }
//
//      boolean confirmed = MessageDialog.openConfirm(UILauncher
//          .getInstance().getShell(),
//          Messages.getString("fileControl.confirmDelete",
//              new Integer(count).toString()), Messages
//              .getString("fileControl.confirmDelete.title"));
//      if (confirmed) {
//        int deleteLocalCount = 0;
//        Iterator it = mFeed.getMerged(false).iterator();
//        while (it.hasNext()) {
//          IXFile lFile = (IXFile) it.next();
//          if (lFile.isMarked()) { // Marking is context sensitive.
//            // Delete the track from the player first.
//            if (PlayerLogic.getInstance().deleteTrack(lFile)) {
//              lFile.setInPlayer(false);
//            }
//
//            // Delete the local file.
//            if (lFile.getFile() != null && lFile.getFile().exists()) {
//              if (lFile.getFile().delete()) {
//                lFile.setLocal(false);
//                deleteLocalCount++;
//                it.remove();
//              }
//            }
//            lFile.setMarked(false);
//          }
//          mView.update(lFile);
//        }
//
//        // Rebuild the merge model and refresh the table after
//        // succesfull
//        // deletion of the files.
//        mFeed.getMerged(true);
//        StatusController.getInstance().updateFeedBar(mFeed);
//      }
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }
//
//  private class DeleteSelectedAction extends BaseAction implements
//      IBaseActionCondition {
//
//    public DeleteSelectedAction() {
//      init(Messages.getString("fileControl.delete.selection"), false);
//    }
//
//    public void handleEvent(Event e) {
//
//      Object[] lFiles = getSelectedFiles();
//      if (lFiles == null || lFiles.length == 0) {
//        return;
//      }
//      boolean confirmed = MessageDialog.openConfirm(UILauncher
//          .getInstance().getShell(), Messages.getString(
//          "fileControl.confirmDelete.selection", new Integer(
//              lFiles.length).toString()), Messages
//          .getString("fileControl.confirmDelete.title"));
//      if (confirmed) {
//        removeFiles(lFiles);
//        StatusController.getInstance().updateFeedBar(mFeed);
//      }
//    }
//
//    public boolean conditionMet(Object pConditionObject) {
//      boolean lEnable = false;
//      if (pConditionObject instanceof IXFile) {
//        IXFile lFile = (IXFile) pConditionObject;
//        if (lFile.isLocal()) {
//          lEnable = true;
//        }
//      } else {
//        mView.mFileDownloadMenu.setText(Messages
//            .getString("fileControl.download"));
//      }
//      return lEnable;
//    }
//
//    public void setEnabled(boolean pEnabled, Object pConditionObject) {
//
//      boolean lStatus;
//      if (conditionMet(pConditionObject)) {
//        lStatus = true;
//      } else {
//        lStatus = false;
//      }
//      mView.mFileDeleteSelectedMenu.setEnabled(lStatus);
//      mView.mFileDeleteSelectedPopupMenu.setEnabled(lStatus);
//    }
//
//    public boolean conditionMet(Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setEnabled(boolean pEnabled, Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }

//  protected void removeFiles(Object[] pFiles) {
//    for (int index = 0; index < pFiles.length; index++) {
//      IXFile file = (IXFile) pFiles[index];
//      if (PlayerLogic.getInstance().deleteTrack(file)) {
//        file.setInPlayer(false);
//      }
//      if (file.getFile() != null && file.getFile().exists()) {
//        if (file.getFile().delete()) {
//          file.setLocal(false);
//        } else {
//          mLog.warn("Delete file error: " + file.getFile().getName());
//        }
//      }
//      mView.getModelPreparator().updateModel();
//      mView.update();
//    }
//  }

//  private class MarkedToPlayerAction extends BaseAction {
//
//    public MarkedToPlayerAction() {
//      init(Messages.getString("fileControl.addMarked"), true);
//    }
//
//    public void handleEvent(Event e) {
//
//      int count = mFeed.getMergedCount(true);
//      if (count == 0) {
//        MessageDialog.openInformation(UILauncher.getInstance()
//            .getShell(), Messages
//            .getString("fileControl.noSelection"), Messages
//            .getString("fileControl.noSelection.title"));
//        return;
//      }
//      boolean confirmed = MessageDialog.openConfirm(UILauncher
//          .getInstance().getShell(), Messages
//          .getString("fileControl.addMissing"), Messages
//          .getString("fileControl.addMissing.tittle"));
//      if (confirmed) {
//        Iterator it = mFeed.getMerged(false).iterator();
//        while (it.hasNext()) {
//          IXFile lFile = (IXFile) it.next();
//          boolean incomplete = false;
//          if (lFile instanceof IXPersonalEnclosure) {
//            try {
//              incomplete = ((IXPersonalEnclosure) lFile)
//                  .isIncomplete();
//            } catch (Exception e1) {
//              // File is incomplete
//            }
//          }
//          if (!incomplete && lFile.isMarked() && lFile.isLocal()
//              && !lFile.getInPlayer()) {
//            // Tasks.storeInPlayer(file);
//            PlayerLogic.getInstance().addTrack(
//                lFile.getPersonalBookMark().getTitle(), lFile);
//            mView.update(lFile);
//          }
//        }
//      }
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }

//  private class SyncSelectedWithPlayerAction extends BaseAction implements
//      IBaseActionCondition {
//
//    public SyncSelectedWithPlayerAction() {
//      init(Messages.getString("fileControl.addSelected"), false);
//    }
//
//    public void handleEvent(Event e) {
//      Object[] lFiles = getSelectedFiles();
//      for (int index = 0; index < lFiles.length; index++) {
//        IXFile lFile = (IXFile) lFiles[index];
//        if (PlayerLogic.getInstance().addTrack(mFeed.getTitle(), lFile)) {
//          lFile.setInPlayer(true);
//          mView.update(lFile);
//        }
//      }
//      StatusController.getInstance().updateFeedBar(mFeed);
//    }
//
//    public boolean conditionMet(Object pConditionObject) {
//      if (pConditionObject instanceof IXFile) {
//        IXFile lFileWrapper = (IXFile) pConditionObject;
//        IPlayer lPlayer = PlayerLogic.getInstance().getBestPlayer(
//            lFileWrapper);
//        if (lPlayer instanceof NoPlayer) {
//          return false;
//        } else {
//          if (lFileWrapper.isLocal()) {
//            return true;
//          }
//        }
//      }
//      return false;
//    }
//
//    public void setEnabled(boolean pEnabled, Object pConditionObject) {
//      boolean lStatus;
//      if (conditionMet(pConditionObject)) {
//        lStatus = true;
//      } else {
//        lStatus = false;
//      }
//      mView.mFileAddSelectedToPlayerMenu.setEnabled(lStatus);
//      mView.mFileAddSelectedToPlayerPopupMenu.setEnabled(lStatus);
//
//    }
//
//    public boolean conditionMet(Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setEnabled(boolean pEnabled, Object[] pConditionObjects) {
//      // Not implemented yet.
//      throw new IllegalArgumentException();
//    }
//
//    public void setControls(Widget[] pControls) {
//      // TODO Auto-generated method stub
//
//    }
//  }

  public IXFile getSelectedFile() {
    if (mView.mSelection instanceof IXFile) {
      return (IXFile) mView.mSelection;
    } else
      return null;
  }

  public Object[] getSelectedFiles() {

    if (mView.mSelectionArray == null) {
      return null;
    }
    ArrayList lFiles = new ArrayList();
    for (int i = 0; i < mView.mSelectionArray.length; i++)
      if (mView.mSelectionArray[i] instanceof IXFile) {
        lFiles.add(mView.mSelectionArray[i]);
      }
    return lFiles.toArray();
  }

  public void setSelectedFile(IXFile pFile) {
    mView.setSelected(pFile);
  }

  public void instructionSucceeded(InstructionEvent e) {
    if (mView == null || mView.getView().isDisposed()) {
      return;
    }

    if (e.getSubject() instanceof IXFile) {
      IXFile lFile = (IXFile) e.getSubject();
      if (e.getInstructionType() == InstructionEvent.INSTRUCTION_INSPECT
          || e.getInstructionType() == InstructionEvent.INSTRUCTION_MARK
          || e.getInstructionType() == InstructionEvent.INSTRUCTION_COLLECT_ENCL){
// FIXME, check the current bookmark before
//          && mFeed.equals(lFile.getBookMark())) {
        mView.update(lFile); // DOESN'T CALL.
      }
    }
// FIXME, the below update is for a collection.
    //    if (e.getSubject() instanceof IXPersonalFeed) {
//      IXPersonalFeed lFeed = (IXPersonalFeed) e.getSubject();
//      if (e.getTask() == XFeedEvent.INSTRUCTION_COLLECT) {
//        setFileView(lFeed);
//      }
//    }
  }

  public void instructionFailed(InstructionEvent e) {
    if (mView == null || mView.getView().isDisposed()) {
      return;
    }

  }

  public void instructionInfo(InstructionEvent e) {
    if (mView == null || mView.getView().isDisposed()) {
      return;
    }
  }

  public void actionPerformed(ActionEvent arg0) {

    // CB TODO, migrate the code which updates the files view of ongoing
    // downloads.
//    if (mFeed == null || mView == null || mView.getView().isDisposed()) {
//      return;
//    }
//
//    Iterator it = DownloadLogic.getInstance().getDownloadIterator();
//    while (it.hasNext()) {
//      Download lDownload = (Download) it.next();
//      if (lDownload.getEnclosure().getPersonalBookMark().equals(mFeed)) {
//        switch (lDownload.getState()) {
//        case DownloadLogic.DOWNLOADING: {
//          mView.updateDownload(lDownload.getEnclosure());
//        }
//        }
//      }
//    }
  }

  /**
   * Listen for network events.
   */
  public void netActionPerformed(final NetTaskEvent event) {
    // Do some state updating an a UI update.
    if (mView == null && mView.getView().isDisposed()) {
      return;
    }

    final Download lDownload = (Download) event.getSource();
    Display.getDefault().asyncExec(new Runnable() {
      public void run() {
        if (event.getNetEvent() == NetTaskEvent.DOWNLOAD_SUCCESS) {
        }
        if (event.getNetEvent() == NetTaskEvent.DOWNLOAD_STATUS_CHANGED) {
        }
        if (event.getNetEvent() == NetTaskEvent.DOWNLOAD_FAILED) {
        }
        mView.update(lDownload.getAttachment());
      }
    });
  }

  public void modelChanged(DownloadEvent pEvent) {
  }
}
TOP

Related Classes of org.rssowl.contrib.podcast.ui.media.MediaController

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.