001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.chameleon.layers;
028:
029: import com.sun.midp.chameleon.*;
030:
031: import javax.microedition.lcdui.*;
032: import java.util.*;
033:
034: import com.sun.midp.chameleon.skins.TickerSkin;
035: import com.sun.midp.chameleon.skins.ScreenSkin;
036: import com.sun.midp.chameleon.skins.SoftButtonSkin;
037:
038: /**
039: *
040: */
041: public class TickerLayer extends CLayer {
042: protected String text;
043:
044: protected int textLoc;
045: protected int textLen;
046:
047: /**
048: * A Timer which will handle firing repaints of the TickerPainter
049: */
050: protected Timer tickerTimer;
051:
052: /**
053: * A TimerTask which will repaint the Ticker on a repeated basis
054: */
055: protected TickerPainter tickerPainter;
056:
057: public TickerLayer() {
058: super (TickerSkin.IMAGE_BG, TickerSkin.COLOR_BG);
059: }
060:
061: public void toggleAlert(boolean alertUp) {
062: if (alertUp && TickerSkin.IMAGE_AU_BG != null) {
063: super .setBackground(TickerSkin.IMAGE_AU_BG, 0);
064: } else if (!alertUp) {
065: super .setBackground(TickerSkin.IMAGE_BG,
066: TickerSkin.COLOR_BG);
067: }
068: }
069:
070: protected void initialize() {
071: super .initialize();
072:
073: setAnchor();
074: tickerTimer = new Timer();
075: }
076:
077: public void setAnchor() {
078: bounds[X] = 0;
079: bounds[W] = ScreenSkin.WIDTH;
080: bounds[H] = TickerSkin.HEIGHT;
081: switch (TickerSkin.ALIGN) {
082: case (Graphics.TOP):
083: bounds[Y] = 0;
084: break;
085: case (Graphics.BOTTOM):
086: default:
087: bounds[Y] = ScreenSkin.HEIGHT - SoftButtonSkin.HEIGHT
088: - bounds[H];
089: }
090: }
091:
092: /**
093: * Set the ticker of this ticker layer.
094: * @param text the text to be displayed on the ticker
095: * @return * @return true if visability of layer was changed
096: */
097: public boolean setText(String text) {
098: boolean oldVisable = super .visible;
099: synchronized (this ) {
100: this .text = text;
101: super .visible = (text != null && text.trim().length() > 0);
102: textLoc = bounds[X] + bounds[W];
103: textLen = (text == null) ? 0 : TickerSkin.FONT
104: .stringWidth(text);
105: setDirty();
106: }
107: return (oldVisable != super .visible);
108: }
109:
110: public String getText() {
111: return text;
112: }
113:
114: protected void paintBody(Graphics g) {
115: synchronized (this ) {
116: if (text == null) {
117: return;
118: }
119: g.setFont(TickerSkin.FONT);
120: if (TickerSkin.COLOR_FG_SHD != TickerSkin.COLOR_FG) {
121: int x = textLoc;
122: int y = TickerSkin.TEXT_ANCHOR_Y;
123: switch (TickerSkin.TEXT_SHD_ALIGN) {
124: case (Graphics.TOP | Graphics.LEFT):
125: x -= 1;
126: y -= 1;
127: break;
128: case (Graphics.TOP | Graphics.RIGHT):
129: x += 1;
130: y -= 1;
131: break;
132: case (Graphics.BOTTOM | Graphics.LEFT):
133: x -= 1;
134: y += 1;
135: break;
136: case (Graphics.BOTTOM | Graphics.RIGHT):
137: default:
138: x += 1;
139: y += 1;
140: break;
141: }
142: g.setColor(TickerSkin.COLOR_FG_SHD);
143: g.drawString(text, x, y, Graphics.TOP
144: | TickerSkin.DIRECTION);
145: }
146: g.setColor(TickerSkin.COLOR_FG);
147: g.drawString(text, textLoc, TickerSkin.TEXT_ANCHOR_Y,
148: Graphics.TOP | TickerSkin.DIRECTION);
149:
150: if (tickerPainter == null) {
151: startTicker();
152: }
153: }
154: }
155:
156: public void startTicker() {
157: if (!visible) {
158: return;
159: }
160:
161: stopTicker();
162: tickerPainter = new TickerPainter();
163: tickerTimer.schedule(tickerPainter, 0, TickerSkin.RATE);
164: }
165:
166: /**
167: * Stop the ticking of the ticker.
168: */
169: public void stopTicker() {
170: if (tickerPainter == null) {
171: return;
172: }
173: tickerPainter.cancel();
174: tickerPainter = null;
175: }
176:
177: /**
178: * Update bounds of layer
179: *
180: * @param layers - current layer can be dependant on this parameter
181: */
182: public void update(CLayer[] layers) {
183: super .update(layers);
184: setAnchor();
185: }
186:
187: /**
188: * A special helper class to repaint the Ticker
189: * if one has been set
190: */
191: private class TickerPainter extends TimerTask {
192: /**
193: * Repaint the ticker area of this Screen
194: */
195: public final void run() {
196: if (!visible) {
197: stopTicker();
198: } else {
199: requestRepaint();
200: switch (TickerSkin.DIRECTION) {
201: case Graphics.RIGHT:
202: textLoc += TickerSkin.SPEED;
203: if (textLoc >= (bounds[W] + textLen)) {
204: textLoc = -(textLen);
205: }
206: break;
207: case Graphics.LEFT:
208: default:
209: textLoc -= TickerSkin.SPEED;
210: if (textLoc <= -(textLen)) {
211: textLoc = bounds[X] + bounds[W];
212: }
213: }
214: }
215: }
216: }
217:
218: }
|