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

📄 ui.java

📁 国外的j2me播放器软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package no.auc.one.portableplayer.userinterface;

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

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

import javax.microedition.lcdui.*;

import org.apache.log4j.*;

// XXX Really UserInterfaceController.
//     Should control the other user interface elements.
public class UI implements CommandListener, MenuEventListener {
    private static Logger LOG = Logger.getLogger("UI");
    
	//Commands
	private Command homeCommand = null;
	private Command playCommand = null;
    private Command showLogCommand = null;
	
    // Menu & elements
    Menu oneppMenu;
    MenuElementContainer rootMenuContainer;
    MediaContentMenuElementContainer meBrowse;
    MenuElementContainer meSetup;
    MenuElement meSetupBack;
    MenuElementComboShift meSetupRepeat;
    MenuElementComboShift meSetupShuffle;
    MenuElementComboShift meSetupHostingPort;
    MenuElementComboShift meServers;
    MenuElement meServersRefresh;
    MenuElementComboShift meRenderers;
    MenuElement meRenderersRefresh;
    MenuElement meExit;

    PlayScreen ps = null;
    
	Main m = null;
	private static UI ref;
    LibraryMgr lib = LibraryMgr.getInstance();
    Settings oneppSettings = Settings.getInstance();
    
    static MediaServer currentMS = null;
    
	//Constructor
	private UI(Main m) throws Exception {
        if (m == null) {
            // XXX Should throw some more appropriate Exception class
            throw new Exception("Main has not been specified");
        }
        
        System.out.println("UI::ctor 1");
        this.m = m;

        LOG.debug("UI::ctor");
        
        Display d = Display.getDisplay(m);
        Logger.getLogCanvas().setPreviousScreen(oneppMenu);
        
        showLogCommand = new Command("Show Log", Command.SCREEN, 1);

        initializeMenu();
        
        ps = new PlayScreen("Now Playing", oneppMenu, d);
	}
	
	public static UI getInstance() {
        if (ref == null) {
            throw new RuntimeException( // XXX Shouldn't be RuntimeException ;)
                "ref is null meaning that Main has not been specified yet.");
        } else {
            return ref;
        }
    }
    
    public static UI getInstance(Main m) throws Exception {
        if (ref == null) {
            ref = new UI(m);
        }
        
        return ref;
    }

    public Menu getMainMenu() {
        return oneppMenu;
    }

/*    
    public void setOwner(Main m) {
        this.m = m;
    }
*/
    
    // XXX EVIL method... to be removed later (using Settings instead)
    public Main getOwner() {
        return m;
    }
    
    public PlayScreen getPlayScreen() {
        return ps;
    }

    public void show() throws Exception {
    }
    
    private void initializeMenu() {
        try {
            oneppMenu = new Menu();

		    homeCommand = new Command("Home", Command.BACK, 1);
		    playCommand = new Command("Play Screen", Command.SCREEN, 1);
	        oneppMenu.setCommandListener(this);
        
		    oneppMenu.addCommand(homeCommand);
		    oneppMenu.addCommand(playCommand);
            oneppMenu.addCommand(showLogCommand);

            rootMenuContainer = new MenuElementContainer("ROOTMENU", null);
            oneppMenu.setCurrentContainer(rootMenuContainer);
            
            meBrowse = new MediaContentMenuElementContainer(
                new ContentContainer("0", "0", "Browse"),
                null); /*need a cds!*/

            // TODO Add servers and renderers to settings too
            meServersRefresh = new MenuElement("Refresh", this);
            meServers = new MenuElementComboShift(
                "Media Server", 
                this,
                new MenuElement[]{ meServersRefresh });
            
            meRenderersRefresh = new MenuElement("Refresh", this);
            meRenderers = new MenuElementComboShift(
                "Media Renderer",
                this,
                new MenuElement[]{
                    meRenderersRefresh});
            
            meSetup = new MenuElementContainer("Settings", this);
            
            meSetupRepeat = new MenuElementComboShift("Repeat", this);
            if (oneppSettings.getRepeatSetting() == true) {
		        meSetupRepeat.addChild(
    		    	new MenuElement[]{
    		    	    new MenuElement("On", this),
    		    	    new MenuElement("Off", this)});
            } else {
                meSetupRepeat.addChild(
		    	    new MenuElement[]{
				        new MenuElement("Off", this),
				        new MenuElement("On", this)});
            }
            
            meSetupShuffle = new MenuElementComboShift("Shuffle", this);
            if (oneppSettings.getShuffleSetting() == true) {
		        meSetupShuffle.addChild(
		    	    new MenuElement[]{
				        new MenuElement("On"),
				        new MenuElement("Off")});
            } else {
		        meSetupShuffle.addChild(
		    	    new MenuElement[]{
				        new MenuElement("Off"),
				        new MenuElement("On")});
            }
	    
            meSetupHostingPort = new MenuElementComboShift(
                "Hosting port number", 
                this);
            if (oneppSettings.getHostingPortSetting() == -1) {
                meSetupHostingPort.addChild(
	                new MenuElement[]{
	        		     new MenuElement("Auto"),
	        		     new MenuElement("Manual")});
            } else {
                meSetupHostingPort.addChild(
                    new MenuElement[]{
                        new MenuElement("Manual"),
                        new MenuElement("Auto")});
            }
		   
            meSetupBack = new MenuElement("Back", this);

            meSetupRepeat = new MenuElementComboShift(
                "Repeat",
                this,
                new MenuElement[]{
                    new MenuElement("Off"),
                    new MenuElement("On")});
        
            meSetupShuffle = new MenuElementComboShift(
                "Shuffle",
                this,
                new MenuElement[]{
                    new MenuElement("Off"),
                    new MenuElement("On")});

            meSetupBack = new MenuElement("Back");
            
            meSetup.addChild(
                new MenuElementBase[]{
                    meSetupBack, 
                    meSetupRepeat, 
                    meSetupShuffle,
                    meSetupHostingPort});
            
            meExit = new MenuElement("Exit", this);
            
            rootMenuContainer.addChild(new MenuElementBase[]{
                meBrowse,
                // meSetup,
                meServers,
                meRenderers,
                meExit
            });
        } catch (NullPointerException npe) {
            LOG.fatal("NullPointerException during initialization of UI");
            LOG.fatal(npe);
        } catch (Exception e) {
            LOG.fatal("Exception occured finding MS or MR.");
            LOG.fatal(e);
        }
    }

    public Thread updateMediaServerMenu() throws IOException, InterruptedException {
        return updateMediaServerMenu(true);
    }
		
    public Thread updateMediaServerMenu(boolean showAlert) throws IOException, InterruptedException {
        MediaServerMenuUpdate msupdateThread = null;
        
        try {
            System.out.println("updateMediaServerMenu");
            meServers.removeAllChildren();
            
            msupdateThread = new MediaServerMenuUpdate(
                showAlert);
            msupdateThread.start();
        } catch (Exception e) {
            System.out.println("Exception occured while updating MS menu");
            e.printStackTrace();
        }
        
        return msupdateThread;
    }
	
    private class MediaServerMenuUpdate extends Thread implements CancelAction {
        MediaServer servers = null;
        WaitingAlert mua = null;
        boolean showAlert = true;
        
        public MediaServerMenuUpdate(boolean showAlert) {
            this.showAlert = showAlert;
        }
       
        public void run() {
            try {
                if (showAlert) {
                    System.out.println("Creating MUA");
                    mua = new WaitingAlert(
                        "Updating media servers",
                        Display.getDisplay(m),
                        (CancelAction)this,
                        this);
                    mua.start();
                }
				
                System.out.println("Calling oneppSettings.updateMSList");
				oneppSettings.updateMediaServerList();
                System.out.println("Starting to enum MS");
                for(Enumeration e = oneppSettings.getMediaServerList();
                    e.hasMoreElements();)
                {
                    MediaServer ms = (MediaServer)e.nextElement();
                    LOG.debug("MS Name: " + ms.getFriendlyName());
					meServers.addChild(
                        new MediaServerMenuElement(
                            ms,
                            UI.getInstance()));
                }
            } catch (Exception e) {
                System.out.println("Exception occured while updating MS menu");
                e.printStackTrace();
            } finally {
                LOG.debug("Finished updating media server menu");
                servers = null;
                meServers.addChild(meServersRefresh);
                
                if (showAlert) {
                    Display d = Display.getDisplay(m);
                    if (d.getCurrent() == mua) {
                        LOG.debug("Current displayable is MUA");
                        d.setCurrent(mua.getPrevDisplayable());
                    } else {
                        LOG.debug("Current displayable is " + d.getCurrent());
                    }
                }
            }
            
			oneppMenu.repaint();
        }

        public void cancel() throws IOException {
            oneppSettings.cancelUpdateMediaServerList();
        }
    }
    
    public Thread updateMediaRendererMenu() throws IOException, InterruptedException {
        return updateMediaRendererMenu(true);
    }
        
    public Thread updateMediaRendererMenu(boolean showAlert) throws IOException, InterruptedException {
        meRenderers.removeAllChildren();

        MediaRendererMenuUpdate mrupdateThread = new MediaRendererMenuUpdate(
            showAlert);
        
        mrupdateThread.start();
        LOG.debug("updateMediaRendererMenu waiting for thread to finish");
        
        return mrupdateThread;
    }

    private class MediaRendererMenuUpdate extends Thread implements CancelAction {
        WaitingAlert mua = null;
		MediaRenderer renderers = null;
        boolean showAlert = true;
        
        public MediaRendererMenuUpdate(boolean showAlert) {
            this.showAlert = showAlert;
        }

        public void run() {
            try {
                if (showAlert) {
                    mua = new WaitingAlert(
                        "Updating media renderers",
                        Display.getDisplay(m),
                        (CancelAction)this,
                        this);
                    mua.start();
                }
				

⌨️ 快捷键说明

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