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 javax.microedition.lcdui;
028:
029: /**
030: * Look and feel implementation for <code>StringItem</code> using
031: * platform widget.
032: */
033: class StringItemLFImpl extends ItemLFImpl implements StringItemLF {
034:
035: /**
036: * Creates a look&feel for a <code>StringItem</code>.
037: *
038: * @param strItem The <code>StringItem</code> associated with this
039: * look&feel
040: */
041: StringItemLFImpl(StringItem strItem) {
042:
043: super (strItem);
044:
045: this .strItem = strItem;
046:
047: // when no commands are added, the actual appearance
048: // is PLAIN; the actual appearance will be the same
049: // as appearance set in StringItem if a command is added
050: // to this StringItem
051: appearanceMode = Item.PLAIN;
052: }
053:
054: // *****************************************************
055: // Public methods (StringItemLF interface impl)
056: // *****************************************************
057:
058: /**
059: * Notifies L&F of a string change in the corresponding
060: * <code>StringItem</code>.
061: *
062: * @param str the new string set in the <code>StringItem</code>
063: */
064: public void lSetText(String str) {
065: // Only update native resource if it exists.
066: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
067: setContent0(nativeId, str, appearanceMode);
068: }
069:
070: lRequestInvalidate(true, true);
071: }
072:
073: /**
074: * Notifies L&F of a font change in the corresponding
075: * <code>StringItem</code>.
076: *
077: * @param font the new font set in the <code>StringItem</code>
078: */
079: public void lSetFont(Font font) {
080: // Only update native resource if it exists.
081: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
082: setFont0(nativeId, font.getFace(), font.getStyle(), font
083: .getSize());
084: }
085:
086: lRequestInvalidate(true, true);
087: }
088:
089: /**
090: * Gets default font to render text in StringItem if it was not
091: * set by the application.
092: * @return - the font to render text if it was not set by the app
093: */
094: public Font getDefaultFont() {
095: return Theme.curContentFont;
096: }
097:
098: /**
099: * Notifies L&F of a command addition in the corresponding
100: * <code>StringItem</code>.
101: *
102: * @param cmd the newly added command
103: * @param i the index of the added command in the <code>StringItem</code>'s
104: * commands[] array
105: */
106: public void lAddCommand(Command cmd, int i) {
107: super .lAddCommand(cmd, i);
108:
109: if ((strItem.numCommands >= 1)
110: && (appearanceMode == Item.PLAIN)) {
111: appearanceMode = strItem.appearanceMode == Item.BUTTON ? Item.BUTTON
112: : Item.HYPERLINK;
113: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
114: setContent0(nativeId, strItem.str, appearanceMode);
115: }
116: lRequestInvalidate(true, true);
117: }
118: }
119:
120: /**
121: * Notifies L&F of a command removal in the corresponding
122: * <code>StringItem</code>.
123: *
124: * @param cmd the newly removed command
125: * @param i the index of the removed command in the
126: * <code>StringItem</code>'s commands[] array
127: */
128: public void lRemoveCommand(Command cmd, int i) {
129: super .lRemoveCommand(cmd, i);
130:
131: // restore the value of the original appearanceMode
132: if (strItem.numCommands < 1) {
133: appearanceMode = Item.PLAIN;
134: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
135: setContent0(nativeId, strItem.str, appearanceMode);
136: }
137: lRequestInvalidate(true, true);
138: }
139: }
140:
141: // *****************************************************
142: // Package private methods
143: // *****************************************************
144:
145: /**
146: * Determine if this <code>Item</code> should have a newline before it.
147: *
148: * @return <code>true</code> if it should have a newline before
149: */
150: boolean equateNLB() {
151: String label = strItem.label;
152: String str = strItem.str;
153:
154: // If label starts with a \n, put this StringItem on a newline
155: if (label != null && label.length() > 0) {
156: if (label.charAt(0) == '\n') {
157: return true;
158: }
159: } else if (str != null && str.length() > 0) {
160: // If there is no label and our content starts with a \n,
161: // this StringItem starts on a newline
162: if (str.charAt(0) == '\n') {
163: return true;
164: }
165: } else {
166: // empty StringItem
167: return false;
168: }
169:
170: if ((strItem.layout & Item.LAYOUT_2) == Item.LAYOUT_2) {
171: return ((strItem.layout & Item.LAYOUT_NEWLINE_BEFORE) == Item.LAYOUT_NEWLINE_BEFORE);
172: }
173:
174: // LAYOUT_2 was not set, hence we need to provide backward
175: // compatibility with MIDP1.0 where any StringItem with a
176: // non-null label would go on a new line.
177: return label != null && label.length() > 0;
178: }
179:
180: /**
181: * Determine if this <code>Item</code> should have a newline after it.
182: *
183: * @return <code>true</code> if it should have a newline after
184: */
185: boolean equateNLA() {
186:
187: String label = item.label;
188: String str = strItem.str;
189:
190: // If content ends with a \n, there is a newline after
191: // this StringItem
192: if (str != null && str.length() > 0) {
193: if (str.charAt(str.length() - 1) == '\n') {
194: return true;
195: }
196: } else if (label != null && label.length() > 0) {
197: // If there is no content and our label ends with a \n,
198: // there is a newline after this StringItem
199: if (label.charAt(label.length() - 1) == '\n') {
200: return true;
201: }
202: } else {
203: // empty StringItem
204: return false;
205: }
206:
207: if ((strItem.layout & Item.LAYOUT_2) == Item.LAYOUT_2) {
208: return ((item.layout & Item.LAYOUT_NEWLINE_AFTER) == Item.LAYOUT_NEWLINE_AFTER);
209: }
210: return false;
211: }
212:
213: /**
214: * Called by event delivery to notify an <code>ItemLF</code> in current
215: * <code>FormLF</code> of a change in its peer state.
216: * Handle special gesture of default command.
217: *
218: * @param hint <code>-1</code> signals that user performed the
219: * special gesture of default command
220: *
221: * @return always <code>false</code> so <code>ItemStateListener</code>
222: * will not be notified
223: */
224: boolean uCallPeerStateChanged(int hint) {
225: // activate default command if hint is -1
226: if (hint == -1) {
227:
228: Command defaultCommand;
229: ItemCommandListener commandListener;
230:
231: synchronized (Display.LCDUILock) {
232:
233: defaultCommand = strItem.defaultCommand;
234: commandListener = strItem.commandListener;
235: }
236:
237: if (defaultCommand != null && commandListener != null) {
238:
239: // Protect from any unexpected application exceptions
240: try {
241: synchronized (Display.calloutLock) {
242: commandListener.commandAction(defaultCommand,
243: strItem);
244: }
245: } catch (Throwable thr) {
246: Display.handleThrowable(thr);
247: }
248: }
249: }
250:
251: // Indicate to Form to not notify ItemStateListener
252: return false;
253: }
254:
255: /**
256: * Create native resource for current <code>StringItem</code>.
257: * Override function in <code>ItemLFImpl</code>.
258: *
259: * @param ownerId Owner screen's native resource id
260: */
261: void createNativeResource(int ownerId) {
262: nativeId = createNativeResource0(ownerId, strItem.label,
263: strItem.layout, strItem.str, appearanceMode,
264: strItem.font);
265: }
266:
267: /**
268: * KNI function that create native resource for current
269: * <code>StringItem</code>.
270: *
271: * @param ownerId Owner screen's native resource id
272: * (<code>MidpDisplayable *</code>)
273: * @param label label to be used for this <code>Item</code>
274: * @param layout layout directive associated with this <code>Item</code>
275: * @param text text to be used for this <code>StringItem</code>
276: * @param appearanceMode should be <code>PLAIN</code>,
277: * <code>HYPERLINK</code> or <code>BUTTON</code>
278: * @param font font face to be used for rendering <code>StringItem</code>
279: * content
280: *
281: * @return native resource id (<code>MidpItem *</code>) of this
282: * <code>StringItem</code>
283: */
284: private native int createNativeResource0(int ownerId, String label,
285: int layout, String text, int appearanceMode, Font font);
286:
287: /**
288: * KNI function that sets text on the native resource corresponding
289: * to the current <code>StringItem</code>.
290: *
291: * @param nativeId native resource id for this item
292: * @param text new text set on the current <code>StringItem</code>
293: * @param appearanceMode the appearance mode of the text passed in
294: */
295: private native void setContent0(int nativeId, String text,
296: int appearanceMode);
297:
298: /**
299: * KNI function that sets font on the native resource corresponding
300: * to the current <code>StringItem</code>.
301: *
302: * @param nativeId native resource id for this item
303: * @param face face of the new font
304: * @param style style of the new font
305: * @param size size of the new font
306: */
307: private native void setFont0(int nativeId, int face, int style,
308: int size);
309:
310: /**
311: * The <code>StringItem</code> associated with this view.
312: */
313: private StringItem strItem;
314:
315: /**
316: * The appearance mode.
317: * The actual appearance of a <code>StringItem</code> could be different to
318: * the one set in <code>StringItem</code>. A <code>StringItem</code>
319: * created with <code>PLAIN</code> appearance will look like a
320: * <code>HYPERLINK</code> if commands were added.
321: */
322: private int appearanceMode;
323: }
|