package org.midi.j2me.util;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
/**
* 字符串换行及翻页
*
*/
public class StringSplitor {
/** 字符串绘制的宽高,起始x,y坐标 */
public int layoutWidth, layoutHeight, layoutX, layoutY;
/** 每行的间隔 */
private int rowGap;
/** 字符串文本 */
private String text;
/** 字体高度 */
private int fontHeight;
/** 文本的总行数 */
private int totalRowCount;
/** 文本被分割后存在Vector中 */
private Vector row;
/** 字体 */
private Font font = null;
/** 当前行 */
private int currRow = 0;
public boolean isChange = false;
/** 一行的高度 */
private int rowHeight = 0;
private int moveRowCount;
/** 屏幕可以显示的行数 */
private int screenRowCount = 0;
/** 前景色 */
private int fgColor = -1;
/**
* 构造函数 <br>
* Str:布局中的字符串 <br>
* X:布局的左上角位置 <br>
* Y:布局的右上角位置 <br>
* LayoutX:布局顶点X <br>
* LayoutY:布局顶点Y <br>
* LayoutWidth:布局宽度 <br>
* LayoutHeight:布局高度 <br>
* Gap:各行之间的空隙
*/
public StringSplitor(String text, int layoutX, int layoutY,
int layoutWidth, int layoutHeight, int rowGap, int fgColor) {
this.text = text;
this.layoutX = layoutX;
this.layoutY = layoutY;
this.layoutWidth = layoutWidth;
this.layoutHeight = layoutHeight;
this.rowGap = rowGap;
this.fgColor = fgColor;
}
/**
* 分割
*
* @param font
*/
public void split(Font font) {
this.font = font;
fontHeight = font.getHeight();
row = Dialogue.divideString(this.text, layoutWidth, font);
row = Dialogue.divideString(row, layoutWidth, font);
totalRowCount = row.size();
if (layoutHeight % (fontHeight + rowGap) == 0) {
screenRowCount = (layoutHeight / (fontHeight + rowGap));
} else {
screenRowCount = (layoutHeight / (fontHeight + rowGap)) - 1;
}
moveRowCount = totalRowCount - screenRowCount;
rowHeight = layoutHeight / (moveRowCount);
}
/**
* 画出字符串
*/
public void draw(Graphics g, int x, int y) {
int i1 = g.getClipX();
int j1 = g.getClipY();
int k1 = g.getClipWidth();
int l1 = g.getClipHeight();
g.setClip(layoutX, layoutY, layoutWidth, layoutHeight);
g.setFont(font);
for (int i = currRow; i < row.size(); i++) {
String s = (String) row.elementAt(i);
g.setColor(this.fgColor);
g.drawString(s, x, y + (i - currRow) * (fontHeight + rowGap),
Graphics.TOP | Graphics.LEFT);
}
if (moveRowCount >= 1) {
drawScrollBar(g);
}
g.setClip(i1, j1, k1, l1);
isChange = false;
}
/**
* 画滚动条
*
* @param g
*/
private void drawScrollBar(Graphics g) {
g.setColor(21, 111, 72);
g.drawLine(layoutX + layoutWidth - 2, layoutY, layoutX + layoutWidth
- 2, layoutY + layoutHeight);
int curY = layoutY + (currRow * layoutHeight) / (moveRowCount);
if (curY + rowHeight >= (layoutY + layoutHeight)) {
curY = layoutY + layoutHeight - rowHeight;
}
g.fillRect(layoutX + layoutWidth - 4, curY, 4, rowHeight);
}
public boolean isNext() {
if (currRow < moveRowCount - 1)
return true;
else
return false;
}
public void next() {
if (currRow < totalRowCount - 1) {
currRow++;
isChange = true;
}
}
public boolean isPrev() {
if (currRow > 0)
return true;
else
return false;
}
public void prev() {
if (currRow > 0) {
currRow--;
isChange = true;
}
}
public boolean isNextPage() {
if (currRow < totalRowCount - screenRowCount)
return true;
else
return false;
}
public void nextPage() {
if (currRow < totalRowCount - 1) {
currRow = Math.min(totalRowCount, currRow + screenRowCount);
isChange = true;
}
}
public boolean isPrevPage() {
if (currRow > 0)
return true;
else
return false;
}
public void prevPage() {
if (currRow > 0) {
currRow = Math.max(0, currRow - screenRowCount);
isChange = true;
}
}
public void setFgColor(int fgColor) {
this.fgColor = fgColor;
}
public void setFont(Font font) {
this.font = font;
}
}