📄 textpane.java
字号:
package com.jmobilecore.ui.core;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
/**
* @author Greg Gridin
*/
public class TextPane extends Component {
/**
* The <code>TextPaneText</code> represents content of this <code>TextPane</code>.
* @see #getText()
* @see #setText(String)
*/
protected TextPaneText textPane;
/**
* Constructs a new <code>TextPane</code> with the specified string of text,
* left justified.
* @param text the string that the textpane presents.
* A <code>null</code> value
* will be accepted without causing a NullPointerException
* to be thrown.
* Unlike text for labels this text can be multiline.
*/
public TextPane(String text) {
this(text, Style.TEXT_FONT);
}
/**
* Constructs a new <code>TextPane</code> with the specified string of text,
* left justified.
* @param text the string that the textpane presents.
* A <code>null</code> value
* will be accepted without causing a NullPointerException
* to be thrown.
* Unlike text for labels this text can be multiline.
* @param font the <code>TextPane</code> font
*/
public TextPane(String text, Font font) {
textPane = new TextPaneText(text, font, ScreenCanvas.WIDTH);
super.setFont(font);
}
/**
* Gets the text of this text pane.
* @return the text of this text pane, or <code>null</code> if
* the text has been set to <code>null</code>.
* @see #setText(String)
*/
public String getText() {
return new String(textPane.buffer);
}
/**
* Sets the text for this text pane to the specified value.
* @param text the text that this text pane displays.
* @see #getText()
*/
public void setText(String text) {
textPane.setText(text, font, ScreenCanvas.WIDTH);
setHeight();
invalidate();
}
/**
* Sets the font of this component.
* @param _font the font to become this component's font;
* if this parameter is <code>null</code> then this
* component font won't be changed
*/
public void setFont(Font _font) {
textPane.setText(getText(), _font, ScreenCanvas.WIDTH);
super.setFont(_font);
}
/**
* Paints the <code>TextPane</code> to the screen.
*
* @param g The Graphics object to render to.
*/
public void paint(Graphics g) {
paintBackground(g);
prepareForeground(g);
final int fHeight = font.getHeight();
int y = screenY + Style.V_GAP;
final int numLines = textPane.getNumLines();
int strlen, offset;
for (int i = 0; i < numLines; i++) {
offset = textPane.lineBreaks[i * 2];
strlen = textPane.lineBreaks[i * 2 + 1] - offset;
if (strlen != 0) {
if (alignment == CENTER)
g.drawChars(textPane.buffer, offset, strlen, getWidth() / 2, y, Graphics.TOP | Graphics.HCENTER);
else if (alignment == RIGHT)
g.drawChars(textPane.buffer, offset, strlen, getWidth() - Style.H_GAP, y, Graphics.TOP | Graphics.RIGHT);
else
g.drawChars(textPane.buffer, offset, strlen, Style.H_GAP, y, Graphics.TOP | Graphics.LEFT);
}
y += fHeight;
}
}
/**
* Calculates the text pane height
*/
protected void setHeight() {
this.height = (font.getHeight()) * textPane.maxLine + Style.V_GAP;
}
}
class TextPaneText {
public static final int PAGE_LINES = 1;
/**
* The text to display
*/
protected char[] buffer;
/**
* Array of pointers to line breaks
*/
protected int[] lineBreaks;
/**
* The number of lines
*/
protected int maxLine;
/**
* Constructs a new <code>TextPaneText</code> with the text, font for specified
* max width.
* @param text the string that the <code>TextPaneText</code> presents.
* @param font the font for the text
* @param maxWidth the width of the <code>TextPane</code>. Usually it is width of the screen
*/
public TextPaneText(String text, Font font, int maxWidth) {
setText(text, font, maxWidth);
}
/**
* Adds line pointer. Increases <code>lineBreaks</code> array if necessary
* @param startPos the starting position of the line in <code>buffer</code> array.
* @param endPos the ending position of the line in <code>buffer</code> array.
*/
protected void addLine(int startPos, int endPos) {
if (maxLine * 2 >= lineBreaks.length) {
int temp [] = new int[lineBreaks.length * 2 + 2];
System.arraycopy(lineBreaks, 0, temp, 0, lineBreaks.length);
lineBreaks = temp;
}
lineBreaks[maxLine * 2] = startPos;
lineBreaks[maxLine * 2 + 1] = endPos;
maxLine++;
}
/**
* Sets a new text, font for specified max width.
* @param _text the string that the <code>TextPaneText</code> presents.
* @param _font the font for the text
* @param _maxWidth the width of the <code>TextPane</code>. Usually it is width of the screen
*/
public void setText(String _text, Font _font, int _maxWidth) {
clear();
if (_text == null || _font == null || _maxWidth <= 0) return;
int start = 0, end = 0, lastSpace = -1;
int curWidth = Style.H_GAP;
char ch;
final int length = _text.length();
reset(_text);
for (int pos = 0; pos < length; pos++) {
ch = buffer[pos];
if (ch == '\n') {
addLine(start, pos);
curWidth = Style.H_GAP;
buffer[pos] = ' ';
lastSpace = start = pos + 1;
continue;
}
if (ch == ' ') {
lastSpace = pos;
if (start == pos) {
start++;
continue;
}
}
curWidth += _font.charWidth(ch);
if (curWidth >= _maxWidth - Style.H_GAP) {
end = (lastSpace > start) ? lastSpace + 1 : pos;
addLine(start, end);
curWidth = Style.H_GAP;
lastSpace = start = end;
pos = end - 1;
}
}
addLine(start, length);
}
/**
* Get number of lines
* @return the number of lines for the <code>TextPaneText</code>
*/
public int getNumLines() {
return maxLine;
}
/**
* Get specified line
* @param num the number of the line in <code>TextPaneText</code>.
*
* @return the text for <code>num</code>-th line
*/
public String getLine(int num) {
if (num >= maxLine)
return null;
int offset = lineBreaks[num * 2];
int strlen = lineBreaks[num * 2 + 1] - offset;
return new String(buffer, offset, strlen);
}
/**
* Clears the <code>TextPaneText</code>
*/
public void clear() {
buffer = null;
lineBreaks = null;
maxLine = 0;
}
/**
* Sets the <code>TextPaneText</code> to the specified string
* @param text the new text.
*/
private void reset(String text) {
buffer = text.toCharArray();
lineBreaks = new int[PAGE_LINES * 2];
maxLine = 0;
}
} // class TextPaneText
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -