public void handleActionKey() {
    LibraryFilesTableDataLine line = DATA_MODEL.get(TABLE.getSelectedRow());
    if (line == null || line.getFile() == null) {
      return;
    }
    if (getMediaType().equals(MediaType.getAudioMediaType())
        && MediaPlayer.isPlayableFile(line.getFile())) {
      MediaPlayer.instance()
          .asyncLoadMedia(new MediaSource(line.getFile()), true, false, true, null, getFilesView());
      UXStats.instance().log(UXAction.LIBRARY_PLAY_AUDIO_FROM_FILE);
      return;
    }

    int[] rows = TABLE.getSelectedRows();
    // LibraryTableModel ltm = DATA_MODEL;
    // File file;
    for (int i = 0; i < rows.length; i++) {
      // file = ltm.getFile(rows[i]);
      // if it's a directory try to select it in the library tree
      // if it could be selected return
      //			if (file.isDirectory()
      //				&& LibraryMediator.setSelectedDirectory(file))
      //				return;
    }

    launch(true);
  }
  /**
   * Updates the Table based on the selection of the given table. Perform lookups to remove any
   * store files from the shared folder view and to only display store files in the store view
   */
  void updateTableFiles(DirectoryHolder dirHolder) {
    if (dirHolder == null) {
      return;
    }

    if (dirHolder instanceof MediaTypeSavedFilesDirectoryHolder) {
      MediaType mediaType = ((MediaTypeSavedFilesDirectoryHolder) dirHolder).getMediaType();
      setMediaType(mediaType);

      if (mediaType.equals(MediaType.getAudioMediaType())) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_AUDIO);
      } else if (mediaType == MediaType.getImageMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_PICTURES);
      } else if (mediaType == MediaType.getDocumentMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_DOCUMENTS);
      } else if (mediaType == MediaType.getVideoMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_VIDEOS);
      } else if (mediaType == MediaType.getTorrentMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_TORRENTS);
      } else if (mediaType == MediaType.getProgramMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_APPLICATIONS);
      }
    } else {
      setMediaType(MediaType.getAnyTypeMediaType());
    }
    clearTable();

    List<List<File>> partitionedFiles = split(100, Arrays.asList(dirHolder.getFiles()));

    for (List<File> partition : partitionedFiles) {
      final List<File> fPartition = partition;

      BackgroundExecutorService.schedule(
          new Runnable() {

            @Override
            public void run() {
              for (final File file : fPartition) {
                GUIMediator.safeInvokeLater(
                    new Runnable() {
                      public void run() {
                        addUnsorted(file);
                      }
                    });
              }

              GUIMediator.safeInvokeLater(
                  new Runnable() {
                    public void run() {
                      LibraryMediator.instance().getLibrarySearch().addResults(fPartition.size());
                    }
                  });
            }
          });
    }

    forceResort();
  }
  /**
   * Handles the selection rows in the library window, enabling or disabling buttons and chat menu
   * items depending on the values in the selected rows.
   *
   * @param row the index of the first row that is selected
   */
  public void handleSelection(int row) {
    int[] sel = TABLE.getSelectedRows();
    if (sel.length == 0) {
      handleNoSelection();
      return;
    }

    File selectedFile = getFile(sel[0]);

    //  always turn on Launch, Delete, Magnet Lookup, Bitzi Lookup
    LAUNCH_ACTION.setEnabled(true);
    LAUNCH_OS_ACTION.setEnabled(true);
    DELETE_ACTION.setEnabled(true);

    if (selectedFile != null && !selectedFile.getName().endsWith(".torrent")) {
      CREATE_TORRENT_ACTION.setEnabled(sel.length == 1);
    }

    if (selectedFile != null) {
      SEND_TO_FRIEND_ACTION.setEnabled(sel.length == 1);

      if (getMediaType().equals(MediaType.getAnyTypeMediaType())) {
        boolean atLeastOneIsPlayable = false;

        for (int i : sel) {
          File f = getFile(i);
          if (MediaPlayer.isPlayableFile(f) || hasExtension(f.getAbsolutePath(), "mp4")) {
            atLeastOneIsPlayable = true;
            break;
          }
        }

        SEND_TO_ITUNES_ACTION.setEnabled(atLeastOneIsPlayable);
      } else {
        SEND_TO_ITUNES_ACTION.setEnabled(
            getMediaType().equals(MediaType.getAudioMediaType())
                || hasExtension(selectedFile.getAbsolutePath(), "mp4"));
      }
    }

    if (sel.length == 1 && selectedFile.isFile() && selectedFile.getParentFile() != null) {
      OPEN_IN_FOLDER_ACTION.setEnabled(true);
    } else {
      OPEN_IN_FOLDER_ACTION.setEnabled(false);
    }

    if (sel.length == 1) {
      LibraryMediator.instance().getLibraryCoverArt().setFile(selectedFile);
    }

    //        boolean anyBeingShared = isAnyBeingShared();
    //        WIFI_SHARE_ACTION.setEnabled(!anyBeingShared);
    //        WIFI_UNSHARE_ACTION.setEnabled(!anyBeingShared);
  }
  // inherit doc comment
  protected JPopupMenu createPopupMenu() {
    if (TABLE.getSelectionModel().isSelectionEmpty()) {
      return null;
    }

    JPopupMenu menu = new SkinPopupMenu();

    menu.add(new SkinMenuItem(LAUNCH_ACTION));
    if (getMediaType().equals(MediaType.getAudioMediaType())) {
      menu.add(new SkinMenuItem(LAUNCH_OS_ACTION));
    }
    if (hasExploreAction()) {
      menu.add(new SkinMenuItem(OPEN_IN_FOLDER_ACTION));
    }

    if (areAllSelectedFilesMP4s()) {
      menu.add(DEMUX_MP4_AUDIO_ACTION);
      DEMUX_MP4_AUDIO_ACTION.setEnabled(
          !((DemuxMP4AudioAction) DEMUX_MP4_AUDIO_ACTION).isDemuxing());
    }

    menu.add(new SkinMenuItem(CREATE_TORRENT_ACTION));

    if (areAllSelectedFilesPlayable()) {
      menu.add(createAddToPlaylistSubMenu());
    }

    menu.add(new SkinMenuItem(SEND_TO_FRIEND_ACTION));
    menu.add(new SkinMenuItem(SEND_TO_ITUNES_ACTION));

    menu.addSeparator();
    menu.add(new SkinMenuItem(DELETE_ACTION));
    menu.addSeparator();

    int[] rows = TABLE.getSelectedRows();
    boolean dirSelected = false;
    boolean fileSelected = false;

    for (int i = 0; i < rows.length; i++) {
      File f = DATA_MODEL.get(rows[i]).getFile();
      if (f.isDirectory()) {
        dirSelected = true;
        //				if (IncompleteFileManager.isTorrentFolder(f))
        //					torrentSelected = true;
      } else fileSelected = true;

      if (dirSelected && fileSelected) break;
    }

    DELETE_ACTION.setEnabled(true);
    LibraryFilesTableDataLine line = DATA_MODEL.get(rows[0]);
    menu.add(createSearchSubMenu(line));
    return menu;
  }
 private boolean isSearchResultPlayable() {
   boolean playable = false;
   if (searchResult.getSearchResult() instanceof StreamableSearchResult) {
     playable = ((StreamableSearchResult) searchResult.getSearchResult()).getStreamUrl() != null;
     if (searchResult.getExtension() != null) {
       MediaType mediaType = MediaType.getMediaTypeForExtension(searchResult.getExtension());
       playable =
           mediaType != null && (mediaType.equals(MediaType.getAudioMediaType()))
               || mediaType.equals(MediaType.getVideoMediaType());
     }
   }
   return playable;
 }
  public void scan(String filePath) {
    try {
      MediaType mt = MediaType.getMediaTypeForExtension(FilenameUtils.getExtension(filePath));

      if (mt == null) {
        scanDocument(filePath, true);
      } else if (mt.equals(MediaType.getAudioMediaType())) {
        scanAudio(filePath, true);
      } else if (mt.equals(MediaType.getImageMediaType())) {
        scanPictures(filePath, true);
      } else if (mt.equals(MediaType.getVideoMediaType())) {
        scanVideo(
            filePath,
            true); // until we integrate mplayer for video and research metadata extraction
      } else {
        scanDocument(filePath, true);
      }

    } catch (Throwable e) {
      scanDocument(filePath, true);
      LOG.log(Level.WARNING, "Error scanning file, scanned as document: " + filePath, e);
    }
  }
 /**
  * Note: This is set up for this to work. Polling is not needed though, because updates already
  * generate update events.
  */
 private LibraryPlaylistsTableMediator() {
   super("LIBRARY_PLAYLISTS_TABLE");
   setMediaType(MediaType.getAudioMediaType());
   ThemeMediator.addThemeObserver(this);
 }