📄 stringwrapper.java
字号:
package mome.ext;import java.util.Enumeration;import javax.microedition.lcdui.Font;/** * @author Sergio Morozov * @version 0.2 */public class StringWrapper implements Enumeration{ private int width; private Font font = Font.getDefaultFont(); private boolean truncate = false; private char[] src; private int srcLength; private int pos = 0; private int l2 = 0; /* * (non-Javadoc) * * @see java.util.Enumeration#hasMoreElements() */ public StringWrapper(int width) { this(width, null); } public StringWrapper(int width, Font font) { this(width, font, false); } public StringWrapper(int width, Font font, String str) { this(width, font, str, false); } public StringWrapper(int width, boolean truncate) { this(width, null, truncate); } public StringWrapper(int width, Font font, boolean truncate) { this.setFont(font); this.setWidth(width); this.setTruncate(truncate); } public StringWrapper(int width, Font font, String str, boolean truncate) { this(width, font, truncate); this.setString(str); } public void restart() { this.pos = 0; if (this.src != null) { this.l2 = font.charsWidth(this.src, 0, this.srcLength) - this.width; } else this.srcLength = 0; } /** * @return the font * @since 0.2 */ public Font getFont() { return this.font; } /** * @param font * the font to set * @since 0.2 */ public void setFont(Font font) { this.font = font == null ? Font.getDefaultFont() : font; } public void setWidth(int width) { if (width <= 0) throw new IllegalArgumentException("width not >= 0: " + width); this.width = width; this.restart(); } public int getWidth() { return this.width; } public void setString(String str) { if (str == null) throw new NullPointerException("str"); this.srcLength = str.length(); this.src = str.toCharArray(); this.restart(); } public String getString() { return String.valueOf(this.src); } /** * @return the truncate * @since 0.2 */ public boolean isTruncate() { return this.truncate; } /** * @param truncate * the truncate to set * @since 0.2 */ public void setTruncate(boolean truncate) { this.truncate = truncate; this.restart(); } public boolean hasMoreElements() { return pos < srcLength; } /* * (non-Javadoc) * * @see java.util.Enumeration#nextElement() */ public Object nextElement() { String res = null; if (this.pos < this.srcLength) { int i = (this.srcLength * this.width + pos * l2) / (this.width + l2) + 1; if (i > this.srcLength) i = this.srcLength; int l = font.charsWidth(this.src, pos, i - pos) - this.width; if (i < this.srcLength) { if (l > 0) { while (i > 0 && (l -= font.charWidth(this.src[i--])) > 0); } else { while (i < this.srcLength && (l += font.charWidth(this.src[i++])) <= 0); if (l > 0) l -= font.charWidth(this.src[i--]); } l2 -= this.width + l; } res = String.valueOf(src, this.pos, i - this.pos); this.pos = this.truncate ? this.srcLength : i; } return res; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -