TextControl.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:5k
源码类别:
J2ME
开发平台:
Java
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.framework;
- import javax.microedition.lcdui.Graphics;
- /**
- *
- * @author santoshsalunke
- */
- public class TextControl extends UIControl {
- String[] data;
- GTantra font;
- int alignment;
- private int fontPallet;
- private int titlePallet=0;
- private int verticalPadding = 3;
- int charHeight;
- GTantra selectionFont;
- public TextControl(String text, int width, GTantra font, int align,boolean justify) {
- if(justify)
- {
- data = Util.divideString(text, font, width - 10);
- }else
- {
- data = Util.divideString(text, font, width - ( (getBorderThickness() + 2) * 2));
- }
- alignment = align;
- this.font = font;
- setWidth(width);
- for (int i = 0; i <data.length && justify; i++) {
- data[i] = getJustifiedText(data[i]);
- }
- charHeight = Util.getMaxCharHeight(font,data[0]);
- int k = (charHeight + verticalPadding) * (data.length - 1);
- setHeight(k);
- }
- public void setTitlePallet(int titlePallet) {
- this.titlePallet = titlePallet;
- }
- public void setAlignment(int alignment) {
- this.alignment = alignment;
- }
- public void setPallet(int pallet) {
- this.fontPallet = pallet;
- }
- public GTantra getFont() {
- return font;
- }
- public int getCharHeight() {
- return charHeight;
- }
- public String[] getTextArry()
- {
- return data;
- }
- String getJustifiedText(String str)
- {
- int stringWidth = font.getStringWidth(str);
- int spaceCharWidth = font.getSpaceCharactorWidth();
- int totalSpaceChars = (getWidth() - stringWidth - 10) / spaceCharWidth;
- totalSpaceChars -= spaceCharWidth;
- String array[] = Util.split(str, " ");
- if(array.length == 1)
- return str;
- int spaceForOne = totalSpaceChars / (array.length - 1);
- int assigned = 0;
- for (int i = 0; i < array.length - 1; i++) {
- array[i] += " ";
- if(i == array.length - 2)
- {
- array[i] += getSpaceString(totalSpaceChars - assigned);
- }else{
- array[i] += getSpaceString(spaceForOne);
- assigned += spaceForOne;
- }
- }
- String mainString = "";
- for (int i = 0; i < array.length; i++) {
- mainString += array[i];
- }
- return mainString;
- }
- String getSpaceString(int cnt)
- {
- String str = "";
- for (int i = 0; i < cnt; i++) {
- str += " ";
- }
- return str;
- }
- public void setText(String text){
- data = Util.divideString(text, font, width);
- }
- public String getText(){
- return data[0];
- }
- public void paint(Graphics g) {
- try {
- GTantra tmpFont = font;
- if(isSelected() && selectionFont != null)
- {
- tmpFont = selectionFont;
- }
- int tempY = 2;
- int tempX = 2;
- font.setCurrentPallete(fontPallet);
- if (alignment == GTantra.TEXT_HCENTER) {
- tempX = width >> 1;
- } else if (alignment == GTantra.TEXT_VCENTER_HCENTER) {
- tempX = width >> 1;
- tempY =( height - ((data.length - 1 ) * (charHeight + verticalPadding))) >>1;
- }
- if (selected && bgColor != (-1)) {
- g.setColor(bgColor);
- g.fillRect(0, 0, width, height - 2);
- }
- for (int i = 1; i < data.length; i++) {
- int transY = g.getTranslateY();
- if( (transY + tempY + font.getFontHeight() < 0))
- {
- tempY += (font.getFontHeight()+1);
- continue;
- }
- if(transY + tempY > DisplayManager.getInst().getHeight())
- {
- break;
- }
- if(alignment == GTantra.TEXT_HCENTER || alignment == GTantra.TEXT_VCENTER_HCENTER)
- {
- tmpFont.drawString(g, data[i], tempX, tempY, GTantra.TEXT_HCENTER);
- }else{
- tmpFont.drawString(g, data[i], tempX, tempY, GTantra.TEXT_LEFT);
- }
- tempY += (charHeight+verticalPadding);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void setSelectionFont(GTantra selectionFont) {
- this.selectionFont = selectionFont;
- }
- public void setVerticalPadding(int verticalPadding) {
- this.verticalPadding = verticalPadding;
- }
- }