LineEnumeration.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:3k
源码类别:
J2ME
开发平台:
Java
- package com.framework;
- import com.framework.GTantra;
- import java.util.Enumeration;
- import java.util.NoSuchElementException;
- import javax.microedition.lcdui.*;
- //www.astrientlabs.com
- public class LineEnumeration implements Enumeration {
- GTantra m_font;
- String m_txt;
- int m_length,m_width,m_position,m_start;
- String s;
- public LineEnumeration(GTantra font, String text, int width) {
- m_font = font;
- m_txt = text;
- m_length = text.length();
- m_width = width;
- //reseting
- m_position =0;
- m_start = 0;
- }
- public boolean hasMoreElements() {
- return (m_position<(m_length-1));
- }
- public Object nextElement() {
- int maxLength = m_txt.length();
- int next = next();
- if(m_start>=maxLength || next>maxLength)
- return null;
- String s =m_txt.substring(m_start, next);
- m_start = next;
- if((m_txt.length()-1>m_start )&& ((m_txt.charAt(m_start)=='n') ||
- (m_txt.charAt(m_start)==' '))){
- m_position++;
- m_start++;
- }
- return s;
- }
- private int next(){
- int i=getNextWord(m_position);
- int lastBreak = -1;
- String line;
- line= m_txt.substring(m_position, i);
- int lineWidth = m_font.getStringWidth(line);
- while (i<m_length && lineWidth<= m_width){
- if(m_txt.charAt(i)==' ' )
- lastBreak = i;
- else if(m_txt.charAt(i)== 'n'){
- lastBreak =i;
- break;
- }
- if(++i<m_length){
- int val= getNextWord(i);
- if( m_font.getStringWidth(m_txt.substring(m_position, val)) < m_width)
- {
- i = val;
- }
- line = m_txt.substring(m_position, i);
- lineWidth = m_font.getStringWidth(line);
- if(val != i)
- {
- lastBreak =i;
- break;
- }
- }
- }
- if(i==m_length && lineWidth<= m_width)
- m_position = i;
- else if(lastBreak == m_position)
- m_position++;
- else if(lastBreak < m_position)
- m_position =i;
- else
- m_position = lastBreak;
- return m_position;
- }
- private int getNextWord(int startIndex){
- int space = m_txt.indexOf(' ', startIndex);
- int newLine = m_txt.indexOf('n', startIndex);
- if(space ==-1)
- space = m_length;
- if(newLine ==-1)
- newLine = m_length;
- if(space<newLine)
- return checkForBigLine(startIndex,space);
- else
- return checkForBigLine(startIndex,newLine);
- }
- private int checkForBigLine(int startIndex,int endIndex)
- {
- int tmpWidth = m_font.getStringWidth(m_txt.substring(startIndex,endIndex));
- while(tmpWidth >= m_width && startIndex < endIndex){
- tmpWidth = m_font.getStringWidth(m_txt.substring(startIndex,--endIndex));
- }
- return endIndex;
- }
- }