?? softbuttoncontrol.java
字號:
// 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)
/**
//MenuBar文檔生成日期:2006.02.10
//
//(1)概述:
//類名稱:SoftButtonControl
//類說明:
// 提供在Canvas上繪制二級菜單最下面的狀態欄或者左右軟鍵的能力,通過一個標志即可切換這兩種風格。
*
//所在子系統:MenuBar
//
//系統總描述:
我們提供的MenuBar J2ME版本 就是這么一種概念:
一個可以下載到手機(例如Nokia7610已經確實可以下載安裝并運行)的Java應用程序。
他模仿Opera Mini的界面風格以及操縱模式,未來試圖向ucweb學習底邊狀態欄的繪制。
這種風格我們稱之為二級菜單,甚至多級菜單。它可以在小小的手機屏幕上展示如何提供盡可能多的菜單命令。
//(2)歷史記錄:
//創建人: 鄭昀(2006.02.10)
//聯系我: Google Talk >> zhengyun@gmail.com
//Blogs: http://blog.csdn.net/zhengyun_ustc/以及http://www.cnblogs.com/zhengyun_ustc
//(3)版權聲明:
//我這個版本的MenuBar,代碼您可以借鑒,但不得用于商業用途,除非得到本人的授權。
////////////////////////////////////////////////////////////////////*/
package com.ultrapower.gui;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
//import bluegammon.Device;
/**
* A custom item for softbuttons. Has functionality for
* setting one <code>Command</code> per softbutton. This
* also binds the backbutton if any of the command's type
* is <code>Command.BACK</code>.
*
* @author Peter Andersson
*/
public class SoftButtonControl
{
/** Displayable that is used for drawing this control */
protected Displayable m_displayable;
/** Left command */
protected Command m_leftCommand;
/** Right command */
protected Command m_rightCommand;
/** Back command */
protected Command m_backCommand;
/** Command listener */
protected CommandListener m_listener;
/** Enabled/disabled flag for left command */
protected boolean m_leftCommandEnabled = true;
/** Enabled/disabled flag for right command */
protected boolean m_rightCommandEnabled = true;
/*
* SoftButton的兩種界面風格。
* 如果是false,則代表最下面一行都是狀態欄;
* 如果是true,則代表屏幕左右下角各繪制一個button。
*/
protected boolean m_bSoftbuttonOrStatusbar = false;
/** String of left softbutton */
protected char[] m_left;
/** String of right softbutton */
protected char[] m_right;
/** Font used when drawing softbutton strings */
protected Font m_font;
/** Biggest text width amongst the commands */
protected int m_maxWidth;
/** 繪制底邊狀態欄的最大寬度 */
protected int m_statusbarWidth;
protected int m_canvasHeight;
/*
* 左右軟按鈕的邊框顏色
*/
protected static final int COL_BORDER = 0x88440000;
/*
* 左右軟按鈕的繪制底色,0xbb440000是棕褐色
*/
protected static final int COL_BG = 0x55440000;
/*
* 左右軟按鈕的字體顏色,0xffffff是白色
*/
protected static final int COL_COMMAND = 0xffffff;
/*
* 禁用時左右軟按鈕的字體顏色,0x888888是灰色
*/
protected static final int COL_DISABLED_COMMAND = 0x888888;
/*
* 對話框的確定按鈕背景透明色,0x65ee0000是粉紅色
*/
protected int m_buttonBackgroundColor = 0x65110000;
/*
* 邊框的顏色,0xdd0000是紅色
*/
protected int m_borderColor = 0xd6ffff;//0xdd0000;
/*
* 對話框內文本的字體顏色,0xffffff是白色
*/
protected int m_textColor = 0xffffff;
protected int[] m_transpBuf;
protected static int[] m_rgbButtonData;
protected int m_fontHeight;
/**
* Initializes this softbutton control.
*
* @param d The displayable that this control draw on.
* @param font The font used for rendering softbuttons.
* @param leftCommand The left softbutton command or null.
* @param rightCommand The right softbutton command or null.
* @param bSoftbuttonOrStatusbar 最后一個參數指繪制SoftButton的界面風格,是左右兩個軟鍵呢,還是一個長條的狀態欄
*/
public void init(Displayable d, Font font, Command leftCommand, Command rightCommand,
boolean bSoftbuttonOrStatusbar)
{
System.out.println("SoftButton::init!");
m_displayable = d;
m_font = font;
m_fontHeight = m_font.getHeight();
m_statusbarWidth = m_displayable.getWidth();
m_canvasHeight = m_displayable.getHeight();
m_bSoftbuttonOrStatusbar = bSoftbuttonOrStatusbar;
setRightCommand(rightCommand);
setLeftCommand(leftCommand);
if(!m_bSoftbuttonOrStatusbar)
{
// 創建按鈕的透明背景色rgb緩沖區 (4 lines)
// zhengyun 20051220
System.out.println("創建按鈕的透明背景色rgb緩沖區!");
if (m_rgbButtonData == null || m_rgbButtonData.length != m_statusbarWidth * 4)
{
m_rgbButtonData = new int[m_statusbarWidth * 4];
for (int i = 0; i < m_rgbButtonData.length; i++)
{
m_rgbButtonData[i] = m_buttonBackgroundColor;
}
}
}
}
/**
* Returns the command listener.
* @return The command listener.
*/
public CommandListener getCommandListener()
{
return m_listener;
}
/**
* Sets the commandlistener that will be reported upon
* softkey presses.
* @param listener The listener.
*/
public void setCommandListener(CommandListener listener)
{
m_listener = listener;
}
/**
* Returns the command assigned to left softbutton.
* @return The left comamand.
*/
public Command getLeftCommand()
{
return m_leftCommand;
}
/**
* Sets the left softbutton command.
* @param c The command.
*/
public void setLeftCommand(Command c)
{
if (m_backCommand == m_leftCommand)
{
m_backCommand = null;
}
m_leftCommand = c;
if (m_leftCommand != null)
{
m_left = m_leftCommand.getLabel().toCharArray();
if (m_leftCommand.getCommandType() == Command.BACK)
{
m_backCommand = c;
}
enable(c, true);
}
calcWidth();
}
/**
* Returns the command assigned to right softbutton.
* @return The right comamand.
*/
public Command getRightCommand()
{
return m_rightCommand;
}
/**
* Sets the right softbutton command.
* @param c The command.
*/
public void setRightCommand(Command c)
{
if (m_backCommand == m_rightCommand)
{
m_backCommand = null;
}
m_rightCommand = c;
if (m_rightCommand != null)
{
m_right = m_rightCommand.getLabel().toCharArray();
if (m_rightCommand.getCommandType() == Command.BACK)
{
m_backCommand = c;
}
enable(c, true);
}
calcWidth();
}
/**
* Enables/disables a command.
* @param c The command.
* @param enable True for enable, false for disable.
*/
public void enable(Command c, boolean enable)
{
if (c == m_leftCommand)
{
m_leftCommandEnabled = enable;
}
if (c == m_rightCommand)
{
m_rightCommandEnabled = enable;
}
}
/**
* Call this from the displayable when a key is pressed to
* activate this control.
*
* @param keyCode The keycode reported in <code>Displayable</code>'s
* <code>keyPressed</code> method.
*/
public void keyPressed(int keyCode)
{
if (keyCode == Device.KEYCODE_LEFT_SOFT)
{
if (m_leftCommand != null && m_listener != null && m_leftCommandEnabled)
{
m_listener.commandAction(m_leftCommand, m_displayable);
}
}
else if (keyCode == Device.KEYCODE_RIGHT_SOFT)
{
if (m_rightCommand != null && m_listener != null && m_rightCommandEnabled)
{
m_listener.commandAction(m_rightCommand, m_displayable);
}
}
else if (keyCode == Device.KEYCODE_BACK)
{
if (m_backCommand != null && m_listener != null &&
(m_backCommand == m_leftCommand && m_leftCommandEnabled ||
m_backCommand == m_rightCommand && m_rightCommandEnabled))
{
m_listener.commandAction(m_backCommand, m_displayable);
}
}
}
/**
* Call this from the displayable a repaint is necessary to
* draw this control
*
* @param g The graphics context reported in <code>Displayable</code>'s
* <code>paint</code> method.
*/
public void paint(Graphics g)
{
System.out.println("SoftButton::paint!");
int w = m_displayable.getWidth();
int h = m_displayable.getHeight();
if(!m_bSoftbuttonOrStatusbar)
{
int y = m_canvasHeight - m_fontHeight - 2;
{
/*
* zhengyun 20051220 added
* 添加OK按鈕上的背景色
*/
System.out.println("添加OK按鈕上的背景色!");
g.setColor(m_borderColor);
g.drawRect(1, y, m_statusbarWidth - 1, m_fontHeight - 1);
g.setColor(m_textColor);
g.drawRect(2, y + 1, m_statusbarWidth - 2, m_fontHeight - 2);
// 繪制編輯框內的背景色,稍微淺色點
for (int yAlternative = y + 1; yAlternative < y + m_fontHeight - 1; yAlternative += 4)
{
int nTemp = y + m_fontHeight - yAlternative;
nTemp = 4>nTemp ? nTemp:4;
// public void drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int
// width, int height, boolean processAlpha).
g.drawRGB(m_rgbButtonData, 0, m_statusbarWidth - 4,
3, yAlternative, m_statusbarWidth - 4, nTemp, true);
}
}
}
g.setFont(m_font);
if (m_leftCommand != null)
{
paintCommand(g, m_left, w, h, m_leftCommandEnabled, false);
}
if (m_rightCommand != null)
{
paintCommand(g, m_right, w, h, m_rightCommandEnabled, true);
}
}
/**
* Calculate softbutton width.
*/
protected void calcWidth()
{
int twl = m_left == null ? 0: m_font.charsWidth(m_left,0,m_left.length);
int twr = m_right == null ? 0: m_font.charsWidth(m_right,0,m_right.length);
/*
* Math.max(twr, twl)在CLDC1.0上不能使用!
* 所以必須自己用if/else判斷
* zhengyun 20051216
*/
int mw = twr>twl ? twr:twl;
if (m_maxWidth != mw)
{
m_maxWidth = mw;
recalcTransparantBuffer();
}
}
/**
* Paint a softbutton command.
* @param g Graphics context to draw to.
* @param text Text of command.
* @param w Width of softbutton.
* @param h Height of softbutton.
* @param enabled True if softbutton is enabled, false otherwise.
* @param rightAlign True if softbutton is to the right, false for left.
*/
protected void paintCommand(Graphics g, char[] text,
int w, int h, boolean enabled, boolean rightAlign)
{
System.out.println("SoftButton::paintCommand");
int x = 0;
if (rightAlign)
{
x = w - m_maxWidth - 2;
}
if(m_bSoftbuttonOrStatusbar)
{
g.drawRGB(m_transpBuf, 0, m_maxWidth+2,
x, h-m_fontHeight-1, m_maxWidth+2, m_fontHeight+1, true);
}
g.setColor(enabled ? COL_COMMAND : COL_DISABLED_COMMAND);
x += m_maxWidth / 2;
x += rightAlign ? 2 : 1;
g.drawChars(text,0,text.length, x, h, Graphics.BOTTOM | Graphics.HCENTER);
}
/**
* Calculate transparant background.
*/
protected void recalcTransparantBuffer()
{
System.out.println("SoftButton::recalcTransparantBuffer>>m_statusbarWidth="
+ m_statusbarWidth + ";m_fontHeight=" + m_fontHeight);
if(!m_bSoftbuttonOrStatusbar)
{
// zhengyun modified 20060215
m_transpBuf = new int[m_statusbarWidth * (m_fontHeight + 1)];//這個矩陣是為了底邊整條狀態欄
for (int i = m_statusbarWidth; i < m_transpBuf.length; i++)
{
m_transpBuf[i] = COL_BG;
}
for (int i = 0; i < m_statusbarWidth; i++)
{
m_transpBuf[i] = COL_BORDER;
}
for (int i = 0; i < m_fontHeight + 1; i++)
{
m_transpBuf[(i * (m_statusbarWidth))] = COL_BORDER;
m_transpBuf[(i * (m_statusbarWidth)) + m_statusbarWidth - 1] = COL_BORDER;
}
}
else
{
m_transpBuf = new int[(m_maxWidth+2) * (m_font.getHeight()+1)]; // 這個矩陣的計算是為了特定兩個軟鍵
for (int i = m_maxWidth+2; i < m_transpBuf.length; i++)
{
m_transpBuf[i] = COL_BG;
}
for (int i = 0; i < m_maxWidth+2; i++)
{
m_transpBuf[i] = COL_BORDER;
}
for (int i = 0; i < m_font.getHeight()+1; i++)
{
m_transpBuf[(i * (m_maxWidth+2))] = COL_BORDER;
m_transpBuf[(i * (m_maxWidth+2)) + m_maxWidth+1] = COL_BORDER;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -