⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediacontentmenuelementcontainer.java

📁 国外的j2me播放器软件
💻 JAVA
字号:
package no.auc.one.portableplayer.userinterface;

import java.io.IOException;
import java.util.Enumeration;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Image;

import org.apache.log4j.Logger;

import no.auc.one.portableplayer.communication.mediaserver.ContentDirectoryService;
import no.auc.one.portableplayer.librarymanager.*;
import no.auc.one.portableplayer.settings.Settings;
import no.auc.one.portableplayer.utils.CancelAction;

public final class MediaContentMenuElementContainer 
    extends MenuElementContainer
{
    private static Logger LOG = Logger.getLogger("UI");

    private ContentDirectoryService cds = null;
    private ContentContainer content;
    private boolean playing = false;
    private boolean displayingChildren = false;
    
    public MediaContentMenuElementContainer(
        ContentContainer container,
        ContentDirectoryService cds)
    {
        super(container.toString());

        this.content = container;
        this.cds = cds;
    }
    
    /**
     * @return Name of the container concatinated with -> if content is 
     *         available, or (empty) if it is empty.
     */
    public String toString() {
        String text = content.getName();

        if (content.totalLength() > 0 || content.totalLength() == -1) {
            if (content != null && content.getResourceUri() != null) {
                text = "P! " + text + " ->";
            } else {
                text += " ->";
            }
        } else {
            text += " (empty)";
        }
        
        return text;
    }
    
    public boolean isPlaying() {
        return playing;
    }
    
    public void setPlaying(boolean b) {
        playing = b;
    }

    public ContentDirectoryService getContentDirectoryService() {
        return cds;
    }
    
    public void setContentDirectoryService(ContentDirectoryService cds) {
        this.cds = cds;
    }
    
    public void navigateLeft() {
        displayingChildren = false;
        
        if (childrenLength() > 0) {
            removeAllChildren();
        }
        
        if (content != null) {
            content.removeAllContent();
        }

        super.navigateLeft();
    }
    
    public void navigateUp() {
        if (currentChildIndex > 0) {
            MenuElementBase me = (MenuElementBase)children.elementAt(
                    currentChildIndex);

            if (me != null) {
                if (me instanceof MediaContentMenuElementContainer) {
                    MediaContentMenuElementContainer mce = (MediaContentMenuElementContainer)me;
                    mce.setPlaying(mce.isPlaying());
                } else if (me instanceof MediaInformationMenuElement) {
                    MediaInformationMenuElement mie = (MediaInformationMenuElement)me;
                    mie.setPlaying(mie.isPlaying());
                }
            }
            
            super.navigateUp();            
        }

/*
        System.out.println(
            "curlength=" + content.currentLength() + 
            ", totlength=" + content.totalLength() + 
            ", currentChildIndex=" + currentChildIndex + 
            ", topIndex=" + topIndex + 
            ", markerPos=" + markerPosition);
*/
        
        return;
    }
    
    public void navigateDown() {
        if (currentChildIndex >= (content.totalLength() - 1)) {
            return;
        }
        
        if (currentChildIndex + 1 >= content.currentLength() &&
            currentChildIndex + 1 < content.totalLength() && 
            content.totalLength() > content.currentLength()) 
         {
            if (cds == null) {
                System.out.println("CDS is null");
                return;
            }
            
            fetchMoreElements();
         }
        
        MenuElementBase me = (MenuElementBase)children.elementAt(
                currentChildIndex);

        if (me != null) {
            if (me instanceof MediaContentMenuElementContainer) {
                MediaContentMenuElementContainer mce = (MediaContentMenuElementContainer)me;
                mce.setPlaying(mce.isPlaying());
            } else if (me instanceof MediaInformationMenuElement) {
                MediaInformationMenuElement mie = (MediaInformationMenuElement)me;
                mie.setPlaying(mie.isPlaying());
            }
        }
            
        super.navigateDown();

        playing = false;
/*
        System.out.println(
            "curlength=" + content.currentLength() + 
            ", totlength=" + content.totalLength() + 
            ", currentChildIndex=" + currentChildIndex + 
            ", topIndex=" + topIndex + 
            ", markerPos=" + markerPosition);
*/            
    }        
        
    /* (non-Javadoc)
     * @see no.auc.one.portableplayer.userinterface.MenuElementContainer#invokeAction()
     */
    public void invokeAction() {
        if (displayingChildren) {
            super.invokeAction();
        } else {
            System.out.println("Action invoked for container(" + this + ")");
            String resourceUri = content.getResourceUri();
            if (resourceUri != null) {
                try {
                    System.out.println("ResourceURI: " + resourceUri);
    
                    Display d = Display.getDisplay(UI.getInstance().getOwner());
                    InvokeMediaAction ima = new InvokeMediaAction(
                            !isPlaying() ? InvokeMediaAction.PLAYTRACK : InvokeMediaAction.PAUSE,
                            content);
                    WaitingAlert wa = new WaitingAlert(
                        !isPlaying() ? "Playing album: " + content : "Pause rendering",
                        d,
                        ima,
                        Thread.currentThread(),
                        false);                        
                    d.setCurrent(wa, d.getCurrent());
                    long start = System.currentTimeMillis();
                    ima.start();
                    ima.join();
                    playing = !playing;
                    if (System.currentTimeMillis() - start < 1000) {
                        wa.setTimeout(1000);
                    } else {
                        wa.setTimeout(1);
                    }                    
                } catch (InterruptedException ioe) {
                    System.err.println("InterruptedException while playing content");
                }
            } else {
                System.out.println("ResourceURI is null :/");
            }
        }
    }
    
    public boolean prepareNavigateRight() {
        if (cds == null) {
            System.out.println("CDS is null");
            return false;
        }
        
        playing = false;
        displayingChildren = true;

        fetchMoreElements();
        LOG.debug("Finished browsing");

        return (content.totalLength() > 0);
    }

    public void removeAllChildren() {
        content.removeAllContent();

        super.removeAllChildren();
    }
    
    private void fetchMoreElements() {
        WaitingAlert wa = null;
        Display d = Display.getDisplay(UI.getInstance().getOwner());
        
        try {
            FetchMediaElements fme = new FetchMediaElements();

            wa = new WaitingAlert(
                "Downloading media information. Please wait...", 
                d,
                fme,
                Thread.currentThread(),
                false);
            wa.start();
            
            long start = System.currentTimeMillis();
            fme.start();
            fme.join();
            
            if (System.currentTimeMillis() - start < 1000) {
                wa.setTimeout(1000);
            } else {
                wa.setTimeout(1);
            }
        } catch (InterruptedException e) {
        }
    }

    private final class FetchMediaElements extends Thread implements CancelAction {
        public synchronized void run() {
            try {
                int maxCurrentMenuElements = 
                    UI.getInstance().getMainMenu().getMaxCurrentMenuElements();
                int startIndex = content.currentLength();
                
                cds.browseDirectChildren(
                    content, 
                    startIndex,
                    maxCurrentMenuElements + 5 > 10 ? 
                    maxCurrentMenuElements + 5 : 10);
            
                MediaContent[] mc = content.getMediaContent();
                for (int i = startIndex; i < content.currentLength(); i++) {
                    if (mc[i] instanceof ContentContainer) {
                        addChild(
                            new MediaContentMenuElementContainer(
                                (ContentContainer)mc[i], 
                                cds));
                    } else if (mc[i] instanceof TrackInformation) {
                        addChild(
                            new MediaInformationMenuElement(
                                (TrackInformation)mc[i],
                                cds));
                    } else if (mc[i] == null) {
                        System.out.println("mc[i] == null");
                    } else {
                        System.out.println(
                            "Unknown type of media encountered: " + mc[i]);
                    }
                }
            } catch (java.io.IOException ioe) {
                LOG.fatal("IOException getting elements of container");
                LOG.fatal(ioe);
            } catch (ArrayIndexOutOfBoundsException ae) {
                LOG.fatal("ArrayIndex wrong");
                LOG.fatal(ae);
            } catch (Exception e) {
                LOG.fatal("Exception getting elements of container");
                LOG.fatal(e);
                e.printStackTrace();
            }
        }

        public void cancel() throws IOException {
        }
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -