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

📄 bloglines.java

📁 J2me应用开发经典例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * the listener receives the specified timeout choice. If the user already has
   * a popup open, a call to this method makes current popup go away and the
   * listener to current popup is reported with the timeout choice. After this,
   * specified popup is presented. Thus, it is the responsibility of the server
   * or proxy layer to buffer multiple incoming popups. To check if a popup is
   * currently displayed, use <code>Bloglines.getCurrentPopup()</code> or
   * <code>Bloglines..isShowingPopup()</code>
   * 
   * @param text      Character array with text presented in popup.
   * @param altTexts  Array of character arrays with alternatives presented in popup. If
   *                  this argument is null, no alternatives are presented.
   * @param timeOutInSeconds  The timeout in seconds. If this argument is set to zero,
   *    			  the popup is displayed until the a user makes a choice. If no 
   * 				  choices are given and the timeout is zero, this popup will not show.
   * @param defaultChoice  The default selection when the popup appears.
   * @param timeOutChoice  The choice reported if the popup times out or is overridden by
   *		          another popup.
   * @param listener  The listener to this popup.
   */
  public synchronized static Popup showPopup(Display display, char[] text, char[][] altTexts,
      int timeOutInSeconds, int defaultChoice, int timeOutChoice,
      PopupListener listener)
  {
	  System.out.println("showPopup Enter");
	  
    if (getCanvas() == null)
      return null;
	
    if (getCanvas().getPopup() != null && getCanvas().getPopup().isActive())
    {
		Popup p = getCanvas().getPopup();
        getPopupListener().selectedChoice(p.getTimeOutChoice(), true);
        p.dispose();
        m_popupCache = p;
    }

	Popup p = getPopupInstance();
    p.init(display, text, altTexts, (byte) timeOutInSeconds, (byte) defaultChoice,
        (byte) timeOutChoice, getPopupListener(), getCanvas().getWidth(),
        getCanvas().getHeight());
	
    m_popupListener = listener;
    getCanvas().setPopup(p);
	/*display.setCurrent(
			new Alert(
					"提示", 
					"showPopup>>3",
							null, AlertType.ERROR));*/
    getCanvas().repaint();
    return p;
  }
  /*
   * @param clickEventID 点击确认按钮之后要产生的事件ID,zhengyun added
   */
  public synchronized static TextFieldPopup showTextField(
		  Display display, 
		  char[] text, 
		  char[][] altTexts,
	      int timeOutInSeconds, 
	      int defaultChoice, 
	      int timeOutChoice,
		  int clickEventID,
	      PopupListener listener)
	  {
		  System.out.println("showTextField Enter");
		  
	    if (getCanvas() == null)
	      return null;
		
		if (getCanvas().getTextField() != null && getCanvas().getTextField().isActive())
	    {
			TextFieldPopup p = getCanvas().getTextField();
	        getPopupListener().selectedChoice(p.getTimeOutChoice(), true);
	        p.dispose();
			m_popupTextFieldCache = p;
	    }

		TextFieldPopup p = getTextFieldInstance();
	    p.init(display, text, altTexts, (byte) timeOutInSeconds, (byte) defaultChoice,
	        (byte) timeOutChoice, clickEventID, getPopupListener(), getCanvas().getWidth(),
	        getCanvas().getHeight());
		
	    m_popupListener = listener;
	    getCanvas().setTextField(p);
	    getCanvas().repaint();
		
	    return p;
	  }

  /**
   * Returns a popup instance from cache.
   * @return	A popup instance
   */
  protected synchronized static Popup getPopupInstance()
  {
    if (m_popupCache == null)
    {
      m_popupCache = new Popup();
    }
    return m_popupCache;
  }
  protected synchronized static TextFieldPopup getTextFieldInstance()
  {
    if (m_popupTextFieldCache == null)
    {
		m_popupTextFieldCache = new TextFieldPopup();
    }
    return m_popupTextFieldCache;
  }

  /**
   * Returns a popuplistener that can be used for dispatching
   * popup events, used internally only.
   * @return A popuplistener dispatching events.
   */
  public synchronized static PopupListener getPopupListener()
  {
    return getInstance();
  }

  /**
   * Returns a commandlistener that can be used for dispatching
   * user string input, used internally only.
   * @return A command listener for reporting user string input.
   */
  protected synchronized static CommandListener getInputCommandListener()
  {
    return getInstance();
  }

  /**
   * Returns an instance of <code>Bloglines</code>,
   * used internally only for listener implementations.
   * @return  A bluegammon instance
   */
  private static Bloglines getInstance()
  {
    return m_inst;
  }
  
  /**
   * Initializes this class.
   * @param bg		The bluegammon midlet.
   * @param display	The display.
   */
  public synchronized static void init(BloglinesMobile bg, Display display)
  {
	  try
	  {
	    if (m_inst == null)
	    {
	      // Creates singleton listeners
	      m_inst = new Bloglines();
	    }
	    // Initialize device facade
		Device.init(bg, display);
	    // Initialize the RMS persistence facade
	    RmsFacade.init(NBR_OF_KEYS);
		{
			byte[] bUsingProxy = RmsFacade.get(Bloglines.PROXY_CMWAP);
			if (bUsingProxy == null)
			{
				/*
				 * 这说明是用户第一次运行本程序,那么我们需要设置用户默认要使用cmwap代理
				 * 否则将使用cmnet
				 * zhengyun added 20051230
				 */
				RmsFacade.setBoolean(Bloglines.PROXY_CMWAP, true);
			}
		}
		
	    // Initialize the menu control canvas
		MenuCanvas.getInstance(display).initShow();
		setCanvas(MenuCanvas.getInstance(display));
	
	    // Initialize generic string input handling
	    m_textBox = new TextBox(null, null, 1, TextField.ANY);
	    m_textBox.addCommand(OK);
	    m_textBox.addCommand(CANCEL);
	    m_textBox.setCommandListener(getInputCommandListener());
	  }
	  catch(Exception exc)
	  {
		  display.setCurrent(
					new Alert(
							"初始化错误", 
							"Bloglines.init:"
									+ exc.getMessage() + "/" + exc.getClass(),
									null, AlertType.ERROR));
	  }
  }

  // PopupListener implementation
  public void selectedChoice(byte choice, boolean timeOut)
  {
	  System.out.println("enter Bloglines::selectedChoice");
    // Just forward to registered listener and repaint
    if (m_popupListener != null)
      m_popupListener.selectedChoice(choice, timeOut);
    if (getCanvas() != null)
      getCanvas().repaint();
	
	/*
	 * zhengyun added 20051224
	 * 用getTextFieldInstance().getText()就可以得到当前用户输入的文字
	 */
	//System.out.println("用户最终输入的字符串是=" + getTextFieldInstance().getText());
	
	try
	{
		// 必须转换为byte
		byte mail = (byte)MenuCanvas.ACTION_INPUTUSERMAIL;
		byte pass = (byte)MenuCanvas.ACTION_INPUTPASSWORD;
		byte set_mail = (byte)MenuCanvas.ACTION_SETTINGS_INPUTUSERMAIL;
		byte set_pass = (byte)MenuCanvas.ACTION_SETTINGS_INPUTPASSWORD;
		/*
		 * 为了确定用户输入的字符串,不得已在这里加了几句话
		 * zhengyun added 20051226
		 */
		if(choice == mail || choice == pass
				|| choice == set_mail || choice == set_pass)
		{
			// 拿到用户输入的字符串
			String sText = getTextFieldInstance().getText().trim();
			System.out.println("用户输入:" + sText);
			if(sText.length() > 0)
			{
				System.out.println("模拟用户按键FIRE");
				if(choice == mail || choice == set_mail)
				{
					// 保存用户名到RMS中
					setUserMail(sText);
				}
				else if(choice == pass || choice == set_pass)
				{
					// 保存用户密码到RMS中
					setUserPassword(sText);					
				}
				
				if(choice == mail || choice == pass)
				{
					// 只有在主界面上的登录bloglines菜单项上,
					//  才模拟点击 当前菜单项,从而激发下一步动作
					MenuCanvas.getInstance(Device.getDisplay()).getMenu().keyPressed(
							Device.KEYCODE_FIRE_SOFT);
				}
			}
		}
	}
	catch(Exception exc)
	{
		System.out.println("Bloglines::selectedChoice Exception!" + exc.getMessage()
				+ "/" + exc.getClass());
	}
  }

  // CommandListener implementation for input textbox
  public void commandAction(Command c, Displayable d)
  {
    if (d == m_textBox)
    {
      synchronized (getInputLock())
      {
        if (c != CANCEL &&
            m_textBox.getString().length() > 0 &&
            m_inputHandler != null)
        {
          final String input = m_textBox.getString();
		  
		  /*
		   * 实践发现,这里的声明Thread.start不能像原来的new Thread(new Runnable()....start();
		   * 一句话了事,好像这种语句在nokia上跑不起来!
		   * 切记!
		   * zhengyun 20051216
		   */
		  Thread thread;
          (thread = new Thread(new Runnable()
          {
            public void run()
            {
              m_inputHandler.handleStringInput(input);
            }
          })).start();
		  
        }
        setCanvas(getCanvas());
      }
    }
  }

  // Prevent external construction
  private Bloglines() {}
}

⌨️ 快捷键说明

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