📄 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)
package bluegammon.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;
/** 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 static final int COL_BORDER = 0x88440000;
protected static final int COL_BG = 0xbb440000;
protected static final int COL_COMMAND = 0xffffff;
protected static final int COL_DISABLED_COMMAND = 0x888888;
protected int[] m_transpBuf;
/**
* 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.
*/
public void init(Displayable d, Font font, Command leftCommand, Command rightCommand)
{
m_displayable = d;
m_font = font;
setRightCommand(rightCommand);
setLeftCommand(leftCommand);
}
/**
* 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)
{
int w = m_displayable.getWidth();
int h = m_displayable.getHeight();
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);
int mw = Math.max(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)
{
int textH = m_font.getHeight();
int x = 0;
if (rightAlign)
{
x = w - m_maxWidth - 2;
}
g.drawRGB(m_transpBuf, 0, m_maxWidth+2,
x, h-textH-1, m_maxWidth+2, textH+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()
{
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 + -