001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.chooser;
033:
034: import javax.microedition.lcdui.*;
035:
036: /**
037: * A Font chooser. This screen can be used to
038: * choose fonts. A form is used to select from
039: * the various choices for size, style, and face.
040: */
041: public class FontChooser extends Form implements ItemStateListener {
042: int face;
043: int style;
044: int size;
045: ChoiceGroup faceChoice;
046: ChoiceGroup styleChoice;
047: ChoiceGroup sizeChoice;
048:
049: /**
050: * Create a new font chooser form.
051: * Create each of the form entries
052: */
053: public FontChooser() {
054: super ("Choose Attributes");
055: faceChoice = new ChoiceGroup("Face", Choice.EXCLUSIVE);
056: faceChoice.append("System", null);
057: faceChoice.append("Monospace", null);
058: faceChoice.append("Proportional", null);
059: styleChoice = new ChoiceGroup("Style", Choice.MULTIPLE);
060: styleChoice.append("Bold", null);
061: styleChoice.append("Italic", null);
062: styleChoice.append("Underlined", null);
063: sizeChoice = new ChoiceGroup("Size", Choice.EXCLUSIVE);
064: sizeChoice.append("Small", null);
065: sizeChoice.append("Medium", null);
066: sizeChoice.append("Large", null);
067:
068: append("Face");
069: append(faceChoice);
070: append("Style");
071: append(styleChoice);
072: append("Size");
073: append(sizeChoice);
074:
075: setItemStateListener(this );
076: }
077:
078: /**
079: * Set the Style of font to display.
080: * @param style the style to select
081: * @see Font#getStyle
082: */
083: public void setStyle(int style) {
084: this .style = style;
085: }
086:
087: /**
088: * Get the style of font currently being displayed.
089: * @return the current style being used for text
090: * @see Font#getStyle
091: */
092: public int getStyle() {
093: return style;
094: }
095:
096: /**
097: * Set the Face of font to display.
098: * @param face the face to select
099: * @see Font#getFace
100: */
101: public void setFace(int face) {
102: this .face = face;
103: }
104:
105: /**
106: * Get the face of font currently being displayed.
107: * @return the current face of the font
108: * @see Font#getFace
109: */
110: public int getFace() {
111: return face;
112: }
113:
114: /**
115: * Set the Size of font to display.
116: * @param size of the font to set
117: * @see Font#getSize
118: */
119: public void setSize(int size) {
120: this .size = size;
121: }
122:
123: /**
124: * Get the size of font currently being displayed.
125: * @return the current size of the font
126: * @see Font#getSize
127: */
128: public int getSize() {
129: return size;
130: }
131:
132: /**
133: * Reflect changes in the item states into the states.
134: * @param item that to which some change occurred
135: */
136: public void itemStateChanged(Item item) {
137: if (item == faceChoice) {
138: int f = faceChoice.getSelectedIndex();
139:
140: switch (f) {
141: case 0:
142: face = Font.FACE_SYSTEM;
143:
144: break;
145:
146: case 1:
147: face = Font.FACE_MONOSPACE;
148:
149: break;
150:
151: case 2:
152: face = Font.FACE_PROPORTIONAL;
153:
154: break;
155: }
156: } else if (item == styleChoice) {
157: style = 0;
158:
159: if (styleChoice.isSelected(0)) {
160: style += Font.STYLE_BOLD;
161: }
162:
163: if (styleChoice.isSelected(1)) {
164: style |= Font.STYLE_ITALIC;
165: }
166:
167: if (styleChoice.isSelected(2)) {
168: style |= Font.STYLE_UNDERLINED;
169: }
170: } else if (item == sizeChoice) {
171: int s = sizeChoice.getSelectedIndex();
172:
173: switch (s) {
174: case 0:
175: size = Font.SIZE_SMALL;
176:
177: break;
178:
179: case 1:
180: size = Font.SIZE_MEDIUM;
181:
182: break;
183:
184: case 2:
185: size = Font.SIZE_LARGE;
186:
187: break;
188: }
189: }
190: }
191: }
|