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.lcdui.Text;
030: import com.sun.midp.chameleon.CLayer;
031: import javax.microedition.lcdui.*;
032: import com.sun.midp.chameleon.skins.ScreenSkin;
033: import com.sun.midp.chameleon.skins.TitleSkin;
034:
035: /**
036: * A basic "title" layer. This layer holds a screens title information.
037: */
038: public class TitleLayer extends CLayer {
039:
040: /** The text to draw as the title */
041: protected String title;
042:
043: /** This is the anchor position for the title */
044: protected int titlex, titley, titlew, titleh;
045:
046: /**
047: * Construct a new TitleLayer. This will construct a new layer using
048: * the background image and color settings as defined in the TitleSkin.
049: */
050: public TitleLayer() {
051: super (TitleSkin.IMAGE_BG, TitleSkin.COLOR_BG);
052: }
053:
054: /**
055: * The TitleLayer overrides the initialize method in order to locate
056: * the title layer in the window at the position and dimensions defined
057: * in the TitleSkin.
058: */
059: protected void initialize() {
060: super .initialize();
061:
062: setAnchor();
063: }
064:
065: /**
066: * Sets the anchor constraints for rendering operation.
067: */
068: public void setAnchor() {
069: bounds[X] = 0;
070: bounds[Y] = 0;
071: bounds[W] = ScreenSkin.WIDTH;
072: bounds[H] = TitleSkin.HEIGHT;
073: titlex = 0;
074: }
075:
076: /**
077: * Set the title of this title layer.
078: *
079: * @param title the text to draw as the title
080: * @return true if visability of layer was changed
081: */
082: public boolean setTitle(String title) {
083: boolean oldVisible = this .visible;
084: this .title = title;
085:
086: setDirty();
087: this .visible = (title != null);
088:
089: // force a re-calc of the text anchor location
090: titlex = 0;
091: return (oldVisible != this .visible);
092: }
093:
094: /**
095: * Get the title of this layer.
096: *
097: * @return the title of this layer, or null if this TitleLayer has no
098: * title set.
099: */
100: public String getTitle() {
101: return title;
102: }
103:
104: /**
105: * Draw the body of this layer. This method uses settings in the
106: * TitleSkin to render the title at the correct location using
107: * specified font and color values.
108: *
109: * @param g the Graphics to draw to
110: */
111: protected void paintBody(Graphics g) {
112: String title = this .title;
113: if (title == null) {
114: return;
115: }
116:
117: if (titlex == 0) {
118: // anchor isn't set yet
119: titlew = TitleSkin.FONT.stringWidth(title);
120: if (titlew > (ScreenSkin.WIDTH - (2 * TitleSkin.MARGIN))) {
121: titlew = ScreenSkin.WIDTH - (2 * TitleSkin.MARGIN);
122: }
123:
124: switch (TitleSkin.TEXT_ALIGN_X) {
125: case Graphics.HCENTER:
126: titlex = (ScreenSkin.WIDTH - titlew) / 2;
127: break;
128: case Graphics.RIGHT:
129: titlex = (ScreenSkin.WIDTH - TitleSkin.MARGIN - titlew);
130: break;
131: case Graphics.LEFT:
132: default:
133: titlex = TitleSkin.MARGIN;
134: break;
135: }
136:
137: // We center the title vertically in the
138: // space provided
139: titleh = TitleSkin.FONT.getHeight();
140: if (titleh < TitleSkin.HEIGHT) {
141: titley = (TitleSkin.HEIGHT - titleh) / 2;
142: } else {
143: titley = 0;
144: }
145: }
146:
147: g.translate(titlex, titley);
148: Text.drawTruncStringShadowed(g, title, TitleSkin.FONT,
149: TitleSkin.COLOR_FG, TitleSkin.COLOR_FG_SHD,
150: TitleSkin.TEXT_SHD_ALIGN, titlew);
151: g.translate(-titlex, -titley);
152: }
153:
154: /**
155: * Update bounds of layer
156: * @param layers - current layer can be dependant on this parameter
157: */
158: public void update(CLayer[] layers) {
159: super.update(layers);
160: setAnchor();
161: }
162:
163: }
|