EditControl.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:20k
源码类别:
J2ME
开发平台:
Java
- /*
- * EditControl.java
- *
- * Created on April 23, 2010, 10:20 AM
- *
- * To change this template, choose Tools | Template Manager
- * and open the template in the editor.
- */
- package com.framework;
- import java.util.Vector;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Graphics;
- /**
- *
- * @author Tejaswi
- */
- public class EditControl extends UIControl implements TimerInterface {
- long timeHolder;
- final int DELAY = 350;
- final int CHAR_INSERT_DELAY = 1300;
- boolean showCursor = false;
- GTantra font;
- Vector textVector = new Vector();
- int totalHeight = 0;;
- int cursorXPosition = 0;
- int cursorYPosition = 0;
- int cursorLocation = 0;
- int fontHeight;
- int currentSelectedRow;
- int leftPadding = 3;
- private StringBuffer actulText = new StringBuffer("");
- int scrollHeight = 0;
- BaseTimer timeTask,insertCharTask;
- int horScrolled = 0;
- boolean isMultiline = true;
- //--------color related variables---------------
- int cursorColor =0;
- private int scrollBarBgColor = 0xffffff;
- private int scrollBarArrowColol = 0;
- //-------- variables might need to re config. ----------
- private static final int ERROR_WARP = 4;
- private static final int SCROLL_BAR_WIDTH = 13;
- private boolean m_editable = true;
- public static final int SMART_KEY_PAD = 0;
- public static final int QWERTY_KEY_PAD = 1;
- static int keyPadType =SMART_KEY_PAD;
- public static final int NUMERIC = 0;
- public static final int ALPHA_NUMERIC = 1;
- int editType = ALPHA_NUMERIC;
- int oldKeyCode = -1;
- private static final char[][] charArry = {
- {' ', '0'},
- {'.', ',', ''', '?', '!', '"', '_', '-', '(', ')', '@', '/', ':', '1'},
- {'a', 'b', 'c', '2'},
- {'d', 'e', 'f', '3'},
- {'g', 'h', 'i', '4'},
- {'j', 'k', 'l', '5'},
- {'m', 'n', 'o', '6'},
- {'p', 'q', 'r', 's', '7'},
- {'t', 'u', 'v', '8'},
- {'w', 'x', 'y', 'z', '9'}
- };
- int count = 0;
- char currentChar;
- int max_limit = 0;
- /** Creates a new instance of EditControl */
- public EditControl(GTantra font,int width,int height,int max_limit,boolean isMultiline) {
- this.max_limit = max_limit;
- this.isMultiline = isMultiline;
- this.font = font;
- //this.width = width;
- //this.height = height;
- fontHeight = font.getFontHeight();
- textVector.addElement("");
- setWidth(width);
- if(height != -1)
- {
- setHeight(height);
- }else
- resize();
- setCursorPosition();
- }
- public void setSelected(boolean selected) {
- super.setSelected(selected);
- if(!selected)
- {
- if(timeTask != null)
- {
- timeTask.cancel();
- timeTask = null;
- }
- showCursor = false;
- }else
- {
- if(m_editable)
- {
- if(timeTask != null)
- {
- timeTask.cancel();
- timeTask = null;
- }
- timeTask = BaseTimer.schedule(DELAY, DELAY, this,0);
- }
- }
- }
- public void setEditable(boolean editable)
- {
- m_editable = editable;
- }
- public void setHeight(int height) {
- super.setHeight(height);
- reParseText();
- }
- public void setWidth(int width) {
- super.setWidth(width);
- reParseText();
- }
- public String getText()
- {
- return actulText.toString();
- }
- public void setText(String str)
- {
- try
- {
- if(isMultiline)
- {
- textVector.removeAllElements();
- actulText.delete(0, actulText.length());
- actulText.append(str);
- cursorLocation = str.length();
- reParseText();
- }else
- {
- actulText.delete(0, actulText.length());
- actulText.append(str);
- cursorLocation = str.length();
- setScrollHeight();
- setCursorPosition();
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- //-------------------------------------------------------
- private void drawCursor(Graphics g) {
- if (showCursor && m_editable && insertCharTask == null) {
- g.setColor(cursorColor);
- g.drawLine(cursorXPosition, cursorYPosition - (fontHeight >> 1), cursorXPosition, cursorYPosition + (fontHeight >> 1));
- }
- }
- private void drawScrollBar(Graphics g)
- {
- if(totalHeight > height)
- {
- g.setColor(scrollBarBgColor);
- // g.fillRect(width - SCROLL_BAR_WIDTH, 0, SCROLL_BAR_WIDTH, SCROLL_BAR_WIDTH);
- // g.fillRect(width - SCROLL_BAR_WIDTH, height - SCROLL_BAR_WIDTH, SCROLL_BAR_WIDTH, SCROLL_BAR_WIDTH);
- // g.drawRect(width - SCROLL_BAR_WIDTH, 0, SCROLL_BAR_WIDTH, height);
- g.fillRect(width - SCROLL_BAR_WIDTH, 0, SCROLL_BAR_WIDTH, height);
- int arrowPadding = 2;
- g.setColor(scrollBarArrowColol);
- int scrollBarArrowWidth =SCROLL_BAR_WIDTH -(arrowPadding << 1);
- if(scrollHeight > 0 )
- drawFilledTriangle(g, width - scrollBarArrowWidth - arrowPadding , (scrollBarArrowWidth >> 1) + arrowPadding , true,scrollBarArrowWidth );
- if(scrollHeight + height < totalHeight)
- drawFilledTriangle(g, width - scrollBarArrowWidth - arrowPadding ,height- (scrollBarArrowWidth >> 1) - arrowPadding , false,scrollBarArrowWidth );
- }
- }
- public static void drawFilledTriangle(Graphics g, int startPtX, int startPtY, boolean arrowDirection, int arrowWidth) {
- int wdth = arrowWidth;
- /**vertical arrow is to be drawn*/
- int i = 0;
- boolean var = true;
- while (var == true) {
- /**up arrow is to be drawn*/
- if (arrowDirection == true) {
- g.drawLine(startPtX + i, startPtY - i, startPtX + wdth - i, startPtY - i);
- /**down arrow is to be drawn*/
- } else {
- g.drawLine(startPtX + i, startPtY + i, startPtX + wdth - i, startPtY + i);
- }
- if (wdth - i == arrowWidth >> 1) {
- var = false;
- }
- i++;
- }
- }
- int clipX, clipY,clipWidth,clipHeight;
- private void setClip(Graphics g,int _x, int _y,int _width,int _height)
- {
- clipX = g.getClipX();
- clipY = g.getClipY();
- clipWidth = g.getClipWidth();
- clipHeight = g.getClipHeight();
- g.setClip(_x, _y, _width, _height);
- }
- private void resumeSetClip(Graphics g)
- {
- g.setClip(clipX, clipY, clipWidth, clipHeight);
- }
- private void resize()
- {
- setHeight(fontHeight + 4);
- }
- public static void setKeyPadType(int keyType) {
- keyPadType = keyType;
- }
- public void setCursorColor(int cursorColor) {
- this.cursorColor = cursorColor;
- }
- public void paint(Graphics g) {
- if(textVector.size() == 0 && textVector.elementAt(0).toString().length() == 0 && isMultiline)
- {
- reParseText();
- }
- if(insertCharTask != null)
- {
- g.setColor(0x00FF00);
- int _y = (height - fontHeight) >> 1;
- if(isMultiline)
- {
- _y = currentSelectedRow * fontHeight;
- }
- g.fillRect(cursorXPosition, _y, font.getCharWidth(currentChar) + 1 , font.getCharHeight(currentChar) + 1);
- }
- if(isMultiline)
- {
- g.translate(0, -scrollHeight);
- for (int i = 0; i < textVector.size(); i++) {
- String string = textVector.elementAt(i).toString();
- if(i == currentSelectedRow && insertCharTask != null && getCursorLocationInRow() <= string.length())
- {
- StringBuffer buffer = new StringBuffer(string );
- buffer.insert(getCursorLocationInRow(), currentChar);
- font.drawString(g, buffer.toString(), leftPadding, i * fontHeight, GTantra.TEXT_LEFT);
- }else
- font.drawString(g, string, leftPadding, i * fontHeight, GTantra.TEXT_LEFT);
- }
- drawCursor(g);
- g.translate(0, scrollHeight);
- }else{
- g.translate(-horScrolled,0);
- StringBuffer buffer = new StringBuffer(actulText.toString());
- if(insertCharTask != null)
- {
- buffer.insert(cursorLocation, currentChar);
- }
- font.drawString(g, buffer.toString(), leftPadding, (height - fontHeight) >> 1, GTantra.TEXT_LEFT);
- drawCursor(g);
- g.translate(horScrolled,0);
- }
- // if(insertCharTask != null)
- // {
- // g.setColor(0x00FF00);
- // int _y = (height - fontHeight) >> 1;
- // if(isMultiline)
- // {
- // _y = currentSelectedRow * fontHeight;
- // }
- // g.fillRect(cursorXPosition, _y, font.getCharWidth(currentChar) +1, font.getCharHeight(currentChar)+1);
- // font.drawString(g, currentChar+"", cursorXPosition, _y , GTantra.TEXT_LEFT);
- // }
- // resumeSetClip(g);
- drawScrollBar(g);
- }
- public String[] divideString(String text, GTantra font, int width)
- {
- if(width <= 0)
- {
- return new String[]{text};
- }
- String str[] = Util.divideString(text,font,width);
- if(str.length <= 1)
- return str;
- String strRet[] = new String[str.length - 1];
- System.arraycopy(str, 1, strRet, 0, strRet.length);
- str = null;
- return strRet;
- }
- public boolean keyPressed(int keycode) {
- int gameKey = DisplayManager.getInst().getGameAction(keycode);
- if(!m_editable)
- return false;
- showCursor = true;
- if ((gameKey == Canvas.LEFT || gameKey == Canvas.RIGHT || gameKey == Canvas.UP || gameKey == Canvas.DOWN) && isMightGameKey(keycode)) {
- int tmp = cursorLocation;
- if(insertCharTask != null)
- {
- task(1);
- }
- moveCursor(gameKey);
- if(tmp == cursorLocation)
- {
- return false;
- }
- return true;
- } else if (keycode == KeyEvent.BACK_KEY) {
- if (cursorLocation > 0) {
- cursorLocation--;
- actulText.deleteCharAt(cursorLocation);
- reParseText();
- setCursorPosition();
- setScrollHeight();
- return true;
- }
- } else if(gameKey != Canvas.FIRE || !isMightGameKey(keycode) ){
- if(max_limit > 0 && actulText.length() >= max_limit)
- {
- return true;
- }
- if(editType == NUMERIC)
- {
- if(keycode >= DisplayManager.KEY_NUM0 && keycode <= DisplayManager.KEY_NUM9)
- {
- char ch = (char) keycode;
- actulText.insert(cursorLocation, ch);
- cursorLocation++;
- reParseText();
- setCursorPosition();
- setScrollHeight();
- }
- }
- else if(keyPadType == QWERTY_KEY_PAD)
- {
- char ch = (char) keycode;
- actulText.insert(cursorLocation, ch);
- cursorLocation++;
- reParseText();
- setCursorPosition();
- setScrollHeight();
- }else
- {
- if(keycode != -1 && keycode != oldKeyCode && insertCharTask != null)
- {
- task(1);
- oldKeyCode = -1;
- }
- if(insertCharTask != null)
- {
- insertCharTask.cancel();
- insertCharTask = null;
- }else
- oldKeyCode = keycode;
- insertCharTask = BaseTimer.schedule(CHAR_INSERT_DELAY, this,1);
- if(keycode == Canvas.KEY_STAR)
- {
- currentChar = '*' ;
- task(1);
- }else if(keycode == Canvas.KEY_POUND)
- {
- currentChar = '#' ;
- task(1);
- }else
- {
- currentChar = charArry[keycode - DisplayManager.KEY_NUM0][count];
- }
- count++;
- if(count >= charArry[keycode - DisplayManager.KEY_NUM0].length )
- {
- count = 0;
- }
- }
- return true;
- }
- return false;
- }
- private void reParseText() {
- if(isMultiline){
- int extraWidth = 0;
- if(totalHeight > height)
- extraWidth = SCROLL_BAR_WIDTH;
- String arry[] = divideString(actulText.toString(), font, width - (leftPadding << 1) - ERROR_WARP - extraWidth);
- textVector.removeAllElements();
- for (int i = 0; i < arry.length; i++) {
- textVector.addElement(arry[i]);
- }
- totalHeight = textVector.size() * fontHeight;
- if(textVector.size() == 1)
- {
- totalHeight = height;
- }
- // String currentText = (textVector.elementAt(currentSelectedRow).toString());
- // if(currentSelectedRow >= textVector.size())
- // {
- // currentSelectedRow--;
- // setCursorPosition();
- //
- // }else if(getCursorLocationInRow() > currentText.length()){
- // currentSelectedRow++;
- // }
- setAutoCurrentRow();
- setCursorPosition();
- }
- }
- private void setAutoCurrentRow()
- {
- if(isMultiline)
- {
- int count = 0;
- for (int i = 0; i < textVector.size(); i++) {
- String line = textVector.elementAt(i).toString();
- count += line.length();
- if(cursorLocation <= count)
- {
- currentSelectedRow = i;
- setScrollHeight();
- break;
- }
- }
- }
- }
- private void setScrollHeight()
- {
- if(isMultiline){
- int _y = currentSelectedRow * fontHeight;
- if(totalHeight > height)
- {
- if(_y + fontHeight >= height + scrollHeight )
- {
- scrollHeight = _y + fontHeight - height;
- }else if(_y < scrollHeight){
- scrollHeight = _y;
- }
- }else{
- scrollHeight = 0;
- }
- }else{
- int allowedWidth = width - (leftPadding << 1 ) - leftPadding;
- if(allowedWidth <= 0)
- {
- horScrolled = 0;
- return;
- }
- int subStringWidth = font.getStringWidth(actulText.toString().substring(0, cursorLocation));
- if(subStringWidth < horScrolled )
- {
- horScrolled = subStringWidth;
- }else if(subStringWidth - horScrolled >= allowedWidth )
- {
- horScrolled = subStringWidth - allowedWidth;
- }
- if(horScrolled < 0)
- horScrolled = 0;
- }
- }
- private void moveCursor(int direction) {
- if (direction == Canvas.LEFT) {
- if (cursorLocation > 0) {
- cursorLocation--;
- }
- } else if (direction == Canvas.RIGHT) {
- if (cursorLocation < actulText.length()) {
- cursorLocation++;
- }
- } else if (direction == Canvas.DOWN && isMultiline) {
- if (currentSelectedRow < textVector.size() - 1) {
- String downString = textVector.elementAt(currentSelectedRow + 1).toString();
- currentSelectedRow++;
- if(font.getStringWidth(downString) < cursorXPosition)
- {
- setCursorLocationInRow(downString.length());
- }else{
- setCursorLocationInRow(getCharLocationOnWidth(downString,cursorXPosition));
- }
- if(cursorLocation > actulText.length())
- {
- cursorLocation = actulText.length();
- }
- }
- } else if (direction == Canvas.UP && isMultiline) {
- if (currentSelectedRow > 0) {
- String upString = textVector.elementAt(currentSelectedRow - 1).toString();
- currentSelectedRow--;
- if(font.getStringWidth(upString) < cursorXPosition)
- {
- setCursorLocationInRow(upString.length());
- }else{
- setCursorLocationInRow(getCharLocationOnWidth(upString,cursorXPosition));
- }
- if(cursorLocation < 0)
- {
- cursorLocation = 0;
- }
- }
- }
- setAutoCurrentRow();
- setCursorPosition();
- setScrollHeight();
- }
- private int getCharLocationOnWidth(String str,int width)
- {
- int tmp_width = 0;
- int char_loc = 0;
- if(width >= font.getStringWidth(str))
- char_loc = str.length();
- else{
- while(true)
- {
- tmp_width = font.getStringWidth(str.substring(0, char_loc));
- if(tmp_width > width || char_loc >= str.length())
- {
- char_loc--;
- break;
- }
- char_loc++;
- }
- }
- return char_loc ;
- }
- private boolean isMightGameKey(int keycode) {
- if (!(keycode >= 65 && keycode <= 65 + 26) && !(keycode >= 97 && keycode <= 97 + 26) && !(keycode >= Canvas.KEY_NUM0 && keycode <= Canvas.KEY_NUM9)) {
- return true;
- } else {
- return false;
- }
- }
- private void setCursorPosition() {
- if(isMultiline)
- {
- String currentRowText = textVector.elementAt(currentSelectedRow).toString();
- if(getCursorLocationInRow() >= 0 && getCursorLocationInRow() <= currentRowText.length())
- {
- cursorXPosition = leftPadding + font.getStringWidth(textVector.elementAt(currentSelectedRow).toString().substring(0, getCursorLocationInRow()));
- cursorYPosition = (fontHeight >> 1) + (currentSelectedRow * fontHeight);
- }
- }else{
- cursorXPosition = leftPadding + font.getStringWidth(actulText.toString().substring(0, cursorLocation));
- cursorYPosition = (height >> 1);
- }
- }
- private void setCursorLocationInRow(int location)
- {
- int chars = 0;
- for (int idx = 0; idx < currentSelectedRow; idx++) {
- chars += textVector.elementAt(idx).toString().length();
- }
- cursorLocation = location + chars;
- }
- private int getCursorLocationInRow()
- {
- int chars = 0;
- for (int idx = 0; idx < currentSelectedRow; idx++) {
- chars += textVector.elementAt(idx).toString().length();
- }
- return (cursorLocation - chars);
- }
- public void task(int data) {
- if(data == 1)
- {
- actulText.insert(cursorLocation, currentChar);
- cursorLocation++;
- reParseText();
- setCursorPosition();
- setScrollHeight();
- count = 0;
- if(insertCharTask != null)
- {
- insertCharTask.cancel();
- insertCharTask = null;
- }
- }else
- {
- showCursor = !showCursor;
- DisplayManager.getInst().invalidate();
- }
- }
- }