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

📄 menucanvas.java

📁 J2me应用开发经典例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind. 
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. 
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)

package com.ultrapower.gui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.ultrapower.Bloglines;
import com.ultrapower.Resources;
import com.ultrapower.RmsFacade;
import com.ultrapower.gui.menu.BinaryPageItem;
import com.ultrapower.gui.menu.DefaultMenuPainter;
import com.ultrapower.gui.menu.ItemAction;
import com.ultrapower.gui.menu.Menu;
import com.ultrapower.gui.menu.MenuListener;
import com.ultrapower.gui.menu.MenuPage;
import com.ultrapower.gui.menu.PageItem;
import com.ultrapower.gui.popup.Popup;
import com.ultrapower.gui.popup.TextFieldPopup;
import com.ultrapower.math.Float;
import com.ultrapower.model.SerializeSub;
import com.ultrapower.model.SerializeSubs;
import com.ultrapower.model.ThreadGetItems;
import com.ultrapower.model.ThreadListSubs;


/**
 * The <code>MenuCanvas</code> is the handler of the Bloglines
 * menu. There is only one menu per game, thus the singleton pattern.
 * 
 * @see bluegammon.gui.menu.Menu
 * @author Peter Andersson
 */
public class MenuCanvas extends PopupCanvas
	implements MenuListener, ItemAction, CommandListener
{
  /** Item property key containing help text */ 
  protected static final int ITEM_HELP = 0;
  /** Action key for starting a local game */
  protected static final int ACTION_LOCAL_START = 1;
  /** Action key for resuming a local game */
  public static final int ACTION_LOCAL_READBLOGITEMS = 2;
  /** Action key for quitting midlet */
  protected static final int ACTION_QUIT = 3;
  /** Action key for showing specific score*/
  protected static final int ACTION_SCORE = 4;
  /** Action key for turning audio on/off */
  protected static final int ACTION_AUDIO = 100;
  /** Action key for turning vibra on/off */
  protected static final int ACTION_VIBRA = 101;
  /** Action key for showing about box */
  protected static final int ACTION_ABOUT = 200;
  /** Action key for "去bloglines检索信息" */
  protected static final int ACTION_LOGIN = 201;
  /** Action key for "输入用户邮件地址名" */
  public static final int ACTION_INPUTUSERMAIL = 202;
  /** Action key for "输入用户登录bloglines密码" */
  public static final int ACTION_INPUTPASSWORD = 203;
  /** Action key for "在设置界面中输入用户邮件地址名" */
  public static final int ACTION_SETTINGS_INPUTUSERMAIL = 204;
  /** Action key for "在设置界面中输入用户登录bloglines密码" */
  public static final int ACTION_SETTINGS_INPUTPASSWORD = 205;
  
  /** Softbutton back command */
  protected static final Command CMD_BACK =
    new Command(Resources.getString(Resources.TXT_C_BACK), Command.BACK, 1);
  /** Softbutton help command */
  protected static final Command CMD_HELP =
    new Command(Resources.getString(Resources.TXT_C_HELP), Command.ITEM, 1);
  
  /*
   * 登录菜单项
   */
  MenuPage m_loginPage = 
      new MenuPage(Resources.getChars(Resources.TXT_T_MYFEEDS), null);
  /*
   * 获取blog订阅源的后台线程
   */
  ThreadListSubs m_listsubs;
  /*
   * 检索某个特定订阅的博客登录条目的后台线程
   */
  ThreadGetItems m_getitems;
  /*
   * 我们用SerializeSubs来保存从bloglines api取回的blog源信息
   */
  public SerializeSubs m_subs;

  /** The menu instance */
  protected Menu m_menu;
  public Menu getMenu()
  {
	  return m_menu;
  }
  /** Softbuttons of the backgammon menu canvas */
  protected SoftButtonControl m_softButtons;
  /** The background image */
  protected Image m_background;
  /** Special page with scores */
  protected MenuPage m_scorePage;
  /** The local resume page item */
  protected PageItem m_resumeGameItem;
  /** The scores item */
  protected PageItem m_scoresItem;
  /** Singleton instance */
  protected static MenuCanvas m_inst = null;
  
  private Display m_display;
  public Display getDisplay()
	{
		return m_display;
	}
  
  /** 
   *  界面上的相片框边缘的填充颜色,0x888833实际显示的就是棕榈色,0x883300则是暗红色
   * */
  protected int m_borderFillRectColor = 0x8cc363;
  
  /**
   * Returns the singleton instance.
   * @return The singleton instance.
   */
  public static MenuCanvas getInstance(Display display) throws Exception
  {
    if (m_inst == null)
    {
      m_inst = new MenuCanvas(display);
    }
    return m_inst;
  }
  
  /**
   * Creates the menu and initiates the gui controls
   */
  protected MenuCanvas(Display display) throws Exception
  {
	  try
	  {
		    m_display = display;
			
		    m_background = Resources.getImage(Resources.IMG_BACKGROUND);
		    
		    m_softButtons = new SoftButtonControl();
			
		    PageItem item;
			
		    // Create menu
		    MenuPage mainPage = 
		      new MenuPage(Resources.getChars(Resources.TXT_T_BLOGLINESBUDDY), null);
			int width = getWidth();
			
			BackgammonMenuPainter painter = new BackgammonMenuPainter(getWidth(), display);
			
			/*m_display.setCurrent(
					new Alert(
							"提示", 
							"开始MenuCanvas!  2",
									null, AlertType.ERROR));
			Thread.sleep(2000);*/
			
			m_menu = 
		      new Menu(mainPage, this, painter, display);
			
		    // Menu titles
		    MenuPage settingsPage = 
		      new MenuPage(Resources.getChars(Resources.TXT_T_SETTINGS), null);
		    m_scorePage = 
		      new MenuPage(Resources.getChars(Resources.TXT_T_STAT), null);
		
		    // Menu main page
		    m_scoresItem = 
		      new PageItem(Resources.getChars(Resources.TXT_I_STAT), null, this, m_scorePage);
		    m_scoresItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_STAT));
		    PageItem settingsItem = 
		      new PageItem(Resources.getChars(Resources.TXT_I_SETTINGS), null, null, settingsPage);
		    settingsItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_SETTINGS));
		
		    //mainPage.addItem(btItem);
			
			/**
			   * 如果在RMS中找到了用户的登录电子邮件地址名和密码,就利用他们来登录bloglines,
			   * 而不要求用户输入用户名密码了
			   * zhengyun added 20051227
			   */
			String usermail = Bloglines.hasSavedUserMail();
			System.out.println("MenuCanvas初始化中得到的usermail=" + usermail);
			String password = Bloglines.hasSavedUserPassword();
			if( usermail.length() < 1 || password.length() < 1)
			{
				// 首先,在这里我们赋予的id是ACTION_INPUTUSERMAIL,表明要求先输入用户的电子邮件地址名
				mainPage.addItem(
				        new PageItem(Resources.getChars(Resources.TXT_I_LOGINBLOGLINES), null, this, null, 
								ACTION_INPUTUSERMAIL));
			}
			else
			{
				// 如果找到了登录信息,那么就直接登录好了
				mainPage.addItem(
				        new PageItem(Resources.getChars(Resources.TXT_I_LOGINBLOGLINES), null, this, null, 
								ACTION_LOGIN));
			}
			
		    //mainPage.addItem(phoneItem);
		    mainPage.addItem(m_scoresItem);
		    mainPage.addItem(settingsItem);
		    mainPage.addItem(
		        new PageItem(Resources.getChars(Resources.TXT_I_ABOUT), null, this, null, 
						ACTION_ABOUT));
		    mainPage.addItem(
		      new PageItem(Resources.getChars(Resources.TXT_I_EXIT), null, this, null,
					  ACTION_QUIT));
		
			PageItem usermailItem = 
				new PageItem(Resources.getChars(Resources.TXT_I_S_SETTINGS_INPUT_USERMAIL), null, this, null, 
						ACTION_SETTINGS_INPUTUSERMAIL);
			PageItem userpassItem = 
				new PageItem(Resources.getChars(Resources.TXT_I_S_SETTINGS_INPUT_USERPASS), null, this, null, 
						ACTION_SETTINGS_INPUTPASSWORD);
		    settingsPage.addItem(usermailItem);
		    settingsPage.addItem(userpassItem);
			
			/*
			 * 下面要设置一个标志:是否走cmwap代理
			 * 默认情况下走代理;如果用户点击,那么切换到不使用代理,而走cmnet
			 */
			PersistenceFlagItem cmwapItem = new PersistenceFlagItem(
			        Bloglines.PROXY_CMWAP,  Resources.getChars(Resources.TXT_I_S_PROXY),
			        Resources.getImage(Resources.IMG_PROXY_ON),
			        Resources.getImage(Resources.IMG_PROXY_OFF), ACTION_AUDIO);
			cmwapItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_S_PROXY));
			settingsPage.addItem(cmwapItem);
		
		    setFullScreenMode(true);
		    
		    // Initiate softbuttons
		    m_softButtons.init(this,
		        Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL),
		        CMD_BACK, CMD_HELP);
		    m_softButtons.setCommandListener(this);
		    m_softButtons.enable(CMD_HELP, false);
		    
		    // Setup menu
		    int menuPadding = 16; // 菜单的边缘宽度
		    m_menu.setLocation(0, menuPadding);
			width = getWidth();
			
			/*
			 * Nokia Series 60 has had a bug with the getHeight() method in full screen mode,
			 *  where it returns 144 instead of 208 (the height of a normal canvas instead of 
			 *  a full screen canvas). 所以,
			 *  在真机测试时也要小心nokia s60的这个bug,
			 *  不要因为屏幕高度获取不正确,导致fillRect与你预想不一致。
			 *  
			 *  zhengyun 20051215
			 */
			int height = getHeight();
			/*m_display.setCurrent(
					new Alert(
							"提示", 
							"开始MenuCanvas>>" + height,
									null, AlertType.ERROR));
			Thread.sleep(2000);*/
			
		    m_menu.setDimensions(width, height - menuPadding * 2);
		    m_menu.setFrameData(10, 20);
		    m_menu.setListener(this);
		    m_menu.start();
	  }
	  catch(Exception exc)
	  {
		  display.setCurrent(
					new Alert(
							"错误", 
							"MenuCanvas.MenuCanvas:"
									+ exc.getMessage() + "/" + exc.getClass(),
									null, AlertType.ERROR));
		  Thread.sleep(2000);
	  }
  }
  
  /**
   * Initializes the states of items and
   * starts the muzak, called on an already
   * initialized menu when it is focused again.
   */
  public void initShow()
  {
	  try
	  {
		    PageItem selItem = m_menu.getSelectedItem();
		    if (selItem != null)
		    {
		      Object helpTxt = selItem.getProperty(ITEM_HELP);
		      m_softButtons.enable(CMD_HELP, helpTxt != null);
		    }
		    //Audio.playSound(Audio.MUSIC);
	  }
	  catch(Exception exc)
	  {
		  m_display.setCurrent(
					new Alert(
							"错误", 
							"initShow:"
									+ exc.getMessage() + "/" + exc.getClass(),
									null, AlertType.ERROR));
	  }
  }

  /**
   * Paints the menu, the possible popup, and the softbuttons.
   * @param g	The graphics context to draw on.
   */
  protected void paint(Graphics g)
  {
	  //System.out.println("enter MenuCanvas::paint");
	  try
	  {
	    //g.setColor(0x888833);
		  /*
		   * m_borderFillRectColor是界面上的相片框边缘的填充颜色
		   */
		  g.setColor(m_borderFillRectColor);
		int height = getHeight();
	    g.fillRect(0, 0, getWidth(), height);
	    g.drawImage(m_background,
	        getWidth() / 2, height / 2, Graphics.VCENTER | Graphics.HCENTER);
	
	    m_menu.paint(g);
	    m_softButtons.paint(g);
	    if (getPopup() != null && getPopup().isActive())
	    {
	      getPopup().paint(g);
	    }
	    else if (getTextField() != null && getTextField().isActive())
	    {
			/*
			 * 由于我们的输入文本对话框和这个原生的Popup对话框是两个体系,
			 * 所以必须在此处特地判断是否要绘制TextFieldPopup
			 * zhengyun 20051226 added
			 */
			getTextField().paint(g);
	    }
	  }
	  catch(Exception exc)
	  {
		  m_display.setCurrent(
					new Alert(
							"错误", 
							"MenuCanvas.paint:"
									+ exc.getMessage() + "/" + exc.getClass(),
									null, AlertType.ERROR));
	  }
  }

  /**
   * Called when user presses a key. Dispatches the keypress to
   * possible popup, menu and softbuttons.
   * @param keyCode	The code of the key that is pressed.
   */
  protected void keyPressed(int keyCode)
  {
    if (getPopup() != null && getPopup().isActive())
    {
      getPopup().keyPressed(keyCode, getGameAction(keyCode));

⌨️ 快捷键说明

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