Library Batch Action beaTlet

A LibraryBatchAction is an action that processes each element in the library. It is meant to be fairly quick, which is why it is not persisted. If, for example, you wanted to change an attribute in the same way for each song, this would be the right way to do it.

To create your own LibraryBatchAction you need to subclass com.tagtraum.beatunes.action.standard.LibraryBatchAction and provide an implementation for the LibraryBatchAction.EachSongProcessor interface. A good way to do this is to create an inner class in your main action class. To demonstrate how this is done, check out the sample code for a PrintEachNameInLibrary-action. All it does, is to write each song name in the library to the beaTunes log. As a nice side-effect, this also shows you how to log stuff.

Sample Library Batch Action

The code is pretty self-explanatory:

from javax.swing import Action
from org.slf4j import LoggerFactory
from com.tagtraum.beatunes.action.standard import LibraryBatchAction

# An action that allows to do something (print the name) for each song in the library.
# The corresponding menu item can be found in the 'Tools' menu.
class PrintEachNameInLibrary(LibraryBatchAction):

    # Inner class that implements the
    # com.tagtraum.beatunes.action.standard.LibraryBatchAction.EachSongProcessor
    # interface. Its process method is called for each song.
    class SongPrinter(LibraryBatchAction.EachSongProcessor):

        log = LoggerFactory.getLogger("PrintEachNameInLibrary.py")

        # Called once, before processing starts.
        def startProcessing(self, count):
            self.__class__.log.info("We are expecting to print %s names. Let's start!" % (count))

        # Called for each song.
        def process(self, song, index):
            # print to beaTunes log file
            self.__class__.log.info("Song: %s" % (song.getName()))

        # Called once all songs were processed.
        def finishProcessing(self):
            self.__class__.log.info("Done!")

        # Message to be shown in progress dialog.
        def getProgressDialogMessage(self, song):
            return "<html>Printing <i>%s</i> ...</html>" % (song.getName())

        # Title for progress dialog.
        def getProgressDialogTitle(self):
            return "Printing Song Names ..."

    # Unique id
    def getId(self):
        return "Jython.PrintEachNameInLibrary"

    # Is called by beaTunes as part of the lifecycle after instantiation.
    # At this point all other plugins are instantiated and registered.
    # We use this to set the menu item's (i.e. action's) name.
    def init(self):
        self.putValue(Action.NAME, "Print all Song Names")

    # We need to ask the user, whether he really wants to do this.
    # How we ask is defined here.
    def getConfirmationMessage(self):
        return "Do you really want to print all the song names in this library to the log?";

    # Factory method that creates the processor for each song.
    def createEachSongProcessor(self):
        return PrintEachNameInLibrary.SongPrinter();

Curious to see more examples? Check out the playlist exporter sample code.