TickerTextControl.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:6k
源码类别:
J2ME
开发平台:
Java
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.framework;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- /**
- *
- * @author swaroop
- */
- public class TickerTextControl extends UIControl implements TimerInterface{
- String text[];
- GTantra font;
- int tickerX = 0;
- int stringWidth;
- boolean always = false;
- int pallet = 0;
- int titlePallet = 0;
- int allignment = GTantra.TEXT_HCENTER;
- int charHeight;
- private Image icon;
- private int iconAllignment;
- private static final int PADDING = 2;
- BaseTimer timer;
- public void setIcon(Image icon,int allignment)
- {
- this.icon = icon;
- this.iconAllignment = allignment;
- resize();
- }
- public void setPallet(int pallet) {
- this.pallet = pallet;
- }
- public void setAlways(boolean always) {
- this.always = always;
- }
- public TickerTextControl(String str,GTantra font) {
- if(str == null)
- str = "";
- this.font = font;
- text = new String[]{str};
- charHeight =Util.getMaxCharHeight(font, str);
- resize();
- }
- public void setWidth(int width) {
- super.setWidth(width);
- parse();
- }
- public void resize()
- {
- width = font.getStringWidth(text[0]) + (PADDING << 1);
- height = charHeight + (PADDING << 1);
- if(icon != null)
- {
- width += PADDING + icon.getWidth();
- if(icon.getHeight() + (PADDING << 1) > height)
- {
- height = icon.getHeight() + (PADDING << 1);
- }
- }
- parse();
- }
- public void resizeNSetBgImage(Image image)
- {
- setBgImage(Util.resizeImage(image, width + (PADDING << 1), height + (PADDING << 1)));
- }
- public void parse()
- {
- String str = text[0];
- int pWidth = width - (PADDING);
- if(icon != null)
- {
- pWidth -= (icon.getWidth() + (PADDING << 1));
- }
- String data = Util.getEllipsisString(text[0] , pWidth,font);
- if(data != null)
- {
- text = new String[2];
- text[0] = str;
- text[1] = data;
- }else
- {
- text = new String[]{str};
- }
- stringWidth = font.getStringWidth( text[0]);
- charHeight =Util.getMaxCharHeight(font, str);
- }
- public void setSelected(boolean selected) {
- parse();
- if(selected && text.length > 1 && (!isSelected()) && !always)
- {
- tickerX = 0;
- super.setSelected(selected);
- }
- else
- super.setSelected(selected);
- checkTickerStatus();
- }
- public void showNotify() {
- if(always)
- {
- tickerX = 0;
- }
- checkTickerStatus();
- super.showNotify();
- }
- private void checkTickerStatus()
- {
- if(text.length > 1)
- {
- if( (always || selected))
- {
- if(timer == null)
- timer = BaseTimer.schedule(150, 150, this , 0);
- }else if(timer != null)
- {
- timer.cancel();
- timer = null;
- }
- }else if(timer != null)
- {
- timer.cancel();
- timer = null;
- }
- }
- public String getText()
- {
- return text[0];
- }
- public void hideNotify() {
- if(timer != null)
- {
- timer.cancel();
- timer = null;
- }
- super.hideNotify();
- }
- public Image getIcon() {
- return icon;
- }
- public void paint(Graphics g) {
- font.setCurrentPallete(pallet);
- if (text.length == 1) {
- drawText(g,text[0]);
- } else {
- if (isSelected() || (always)) {
- int startX = (PADDING);
- int pWidth = width - (PADDING << 1) ;
- if(icon != null)
- {
- if(iconAllignment == Graphics.LEFT)
- {
- startX += (PADDING << 1) + icon.getWidth();
- pWidth -= ((PADDING << 1) + icon.getWidth());
- }else
- {
- pWidth -= ((PADDING << 1) + icon.getWidth());
- }
- }
- Util.setClipNSave(startX, 0, pWidth, height, g);
- font.drawString(g, text[0], tickerX, (height - charHeight) >> 1, GTantra.TEXT_LEFT);
- Util.restoreSetClip(g);
- } else {
- drawText(g,text[1]);
- }
- }
- if(icon != null)
- {
- if(iconAllignment == Graphics.LEFT)
- {
- g.drawImage(icon, PADDING, (height - icon.getHeight()) >> 1, 0);
- }else if(iconAllignment == Graphics.RIGHT)
- {
- g.drawImage(icon, width - icon.getWidth() - PADDING, (height - icon.getHeight()) >> 1, 0);
- }
- }
- }
- private void drawText(Graphics g,String str)
- {
- int startOffset = PADDING;
- int strWidth = font.getStringWidth(str);
- if(icon != null && allignment == iconAllignment)
- {
- startOffset += icon.getWidth() + PADDING;
- }
- if( allignment == Graphics.HCENTER)
- {
- font.drawString(g, str, (width - strWidth) >> 1, (height - charHeight) >> 1, GTantra.TEXT_LEFT);
- }else if(allignment == Graphics.LEFT)
- {
- font.drawString(g, str, startOffset, (height - charHeight) >> 1, GTantra.TEXT_LEFT);
- }else if(allignment == Graphics.RIGHT)
- {
- font.drawString(g, str, width - strWidth - startOffset, (height - charHeight) >> 1, GTantra.TEXT_LEFT);
- }
- }
- public void setText(String str) {
- String data = Util.getEllipsisString(str, width, font);
- if (data == null) {
- text = new String[]{str};
- } else {
- text = new String[2];
- text[0] = str;
- text[1] = data;
- }
- checkTickerStatus();
- }
- public void setAllignment(int allignment) {
- this.allignment = allignment;
- }
- public void task(int data) {
- tickerX -= 2;
- if (stringWidth + tickerX < 0) {
- tickerX = width - 2;
- }
- DisplayManager.getInst().invalidate();
- }
- }