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: * Platform widget look and feel of <code>ImageItem</code>.
031: */
032: class ImageItemLFImpl extends ItemLFImpl implements ImageItemLF {
033:
034: /**
035: * Creates look & feel for an <code>ImageItem</code>.
036: *
037: * @param imageItem the <code>ImageItem</code> associated with this
038: * look & feel
039: */
040: ImageItemLFImpl(ImageItem imageItem) {
041: super (imageItem);
042:
043: ImageData imageData = null;
044: if (imageItem != null) {
045: Image image = imageItem.immutableImg;
046: if (image != null) {
047: imageData = image.getImageData();
048: }
049: }
050:
051: this .imgItem = imageItem;
052:
053: this .itemImageData = imageData;
054:
055: // when no commands are added, the actual appearance
056: // is PLAIN; the actual appearance will be the same
057: // as appearance set in ImageItem if a command is added
058: // to this ImageItem
059: appearanceMode = Item.PLAIN;
060: }
061:
062: // *****************************************************
063: // Public methods (ImageItemLF impl)
064: // *****************************************************
065:
066: /**
067: * Notifies L&F of an image change in the corresponding
068: * <code>ImageItem</code>.
069: *
070: * @param img the new image set in the <code>ImageItem</code>
071: */
072: public void lSetImage(Image img) {
073: // Only update native resource if it exists.
074: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
075: ImageData imageData = null;
076: if (img != null) {
077: imageData = img.getImageData();
078: }
079: setContent0(nativeId, imageData, imgItem.altText,
080: appearanceMode);
081: }
082: lRequestInvalidate(true, true);
083: }
084:
085: /**
086: * Notifies L&F of an alternative text change
087: * in the corresponding <code>ImageItem</code>.
088: *
089: * @param altText the new alternative text set in the
090: * <code>ImageItem</code>
091: */
092: public void lSetAltText(String altText) {
093: // Only update native resource if it exists.
094: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
095: setContent0(nativeId, itemImageData, altText,
096: appearanceMode);
097: }
098: lRequestInvalidate(true, true);
099: }
100:
101: /**
102: * Notifies L&F of a command addition in the corresponding
103: * <code>ImageItem</code>.
104: *
105: * @param cmd the newly added command
106: * @param i the index of the added command in the <code>ImageItem</code>'s
107: * commands[] array
108: */
109: public void lAddCommand(Command cmd, int i) {
110: super .lAddCommand(cmd, i);
111:
112: if ((imgItem.numCommands >= 1)
113: && (appearanceMode == Item.PLAIN)) {
114: appearanceMode = imgItem.appearanceMode == Item.BUTTON ? Item.BUTTON
115: : Item.HYPERLINK;
116: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
117: setContent0(nativeId, itemImageData, imgItem.altText,
118: appearanceMode);
119: }
120: lRequestInvalidate(true, true);
121: }
122: }
123:
124: /**
125: * Notifies L&F of a command removal in the corresponding
126: * <code>ImageItem</code>.
127: *
128: * @param cmd the newly removed command
129: * @param i the index of the removed command in the
130: * <code>ImageItem</code>'s commands[] array
131: */
132: public void lRemoveCommand(Command cmd, int i) {
133: super .lRemoveCommand(cmd, i);
134:
135: // restore the value of the original appearanceMode
136: if (imgItem.numCommands < 1) {
137: appearanceMode = Item.PLAIN;
138: if (nativeId != DisplayableLFImpl.INVALID_NATIVE_ID) {
139: setContent0(nativeId, itemImageData, imgItem.altText,
140: appearanceMode);
141: }
142: lRequestInvalidate(true, true);
143: }
144: }
145:
146: // *****************************************************
147: // Package private methods
148: // *****************************************************
149: /**
150: * Create native resource for current <code>ImageItem</code>.
151: * Override function in <code>ItemLFImpl</code>.
152: *
153: * @param ownerId Owner screen's native resource id
154: */
155: void createNativeResource(int ownerId) {
156: nativeId = createNativeResource0(ownerId, imgItem.label,
157: imgItem.layout, itemImageData, imgItem.altText,
158: appearanceMode);
159: }
160:
161: // *****************************************************
162: // Private methods
163: // *****************************************************
164:
165: /**
166: * Determine if this <code>Item</code> should have a newline before it.
167: *
168: * @return <code>true</code> if it should have a newline before
169: */
170: boolean equateNLB() {
171: // MIDP1.0 already had an ability to set LAYOUT_NEWLINE_BEFORE.
172: // Hence there is no need to check for LAYOUT_2 (as in StringItem)
173: if (super .equateNLB()) {
174: return true;
175: }
176:
177: // LAYOUT_NEWLINE_BEFORE is not set but LAYOUT_2 is set
178: // which means that items could be positioned side by side
179: if ((imgItem.layout & Item.LAYOUT_2) == Item.LAYOUT_2) {
180: return false;
181: }
182:
183: // LAYOUT_2 was not set, hence we need to provide backward
184: // compatibility with MIDP1.0 where any ImageItem with a
185: // non-null label would go on a new line.
186: return imgItem.label != null && imgItem.label.length() > 0;
187: }
188:
189: /**
190: * Called by event delivery to notify an ItemLF in current FormLF
191: * of a change in its peer state.
192: * Handle special gesture of default command.
193: *
194: * @param hint <code>-1</code> signals that user performed
195: * the special gesture of default command
196: *
197: * @return always <code>false</code> so <code>ItemStateListener</code>
198: * will not be notified
199: */
200: boolean uCallPeerStateChanged(int hint) {
201: // activate default command if hint is -1
202: if (hint == -1) {
203:
204: Command defaultCommand;
205: ItemCommandListener commandListener;
206:
207: synchronized (Display.LCDUILock) {
208:
209: defaultCommand = imgItem.defaultCommand;
210: commandListener = imgItem.commandListener;
211: }
212:
213: if (defaultCommand != null && commandListener != null) {
214:
215: // Protect from any unexpected application exceptions
216: try {
217: synchronized (Display.calloutLock) {
218: commandListener.commandAction(defaultCommand,
219: imgItem);
220: }
221: } catch (Throwable thr) {
222: Display.handleThrowable(thr);
223: }
224: }
225: }
226:
227: // Indicate to Form to not notify ItemStateListener
228: return false;
229: }
230:
231: /**
232: * KNI function that creates native resource for current
233: * <code>ImageItem</code>.
234: *
235: * @param ownerId Owner screen's native resource id
236: * (<code>MidpDisplayable *</code>)
237: * @param label label to be used for this <code>Item</code>
238: * @param layout layout directive associated with this <code>Item</code>
239: * @param imageData ImageData to be used for this <code>ImageItem</code>
240: * @param altText alternative text to be used for this
241: * <code>ImageItem</code>
242: * @param appearanceMode should be <code>PLAIN</code>,
243: * <code>HYPERLINK</code> or <code>BUTTON</code>
244: *
245: * @return native resource id (<code>MidpItem *</code>) of this
246: * <code>ImageItem</code>
247: */
248: private native int createNativeResource0(int ownerId, String label,
249: int layout, ImageData imageData, String altText,
250: int appearanceMode);
251:
252: /**
253: * KNI function that sets image on the native resource corresponding
254: * to the current <code>ImageItem</code>.
255: *
256: * @param nativeId native resource id for this item
257: * @param imgData <code>ImageData</code> instance associated with
258: * a new <code>Image</code> set on the current
259: * <code>ImageItem</code>
260: * @param text new alternative text set on the current
261: * <code>ImageItem</code>
262: * @param appearanceMode the actual appearance mode to be used
263: */
264: private native void setContent0(int nativeId, ImageData imageData,
265: String text, int appearanceMode);
266:
267: /**
268: * The <code>ImageItem</code> associated with this view.
269: */
270: private final ImageItem imgItem;
271:
272: /**
273: * The <code>ImageData</code> associated with the item.
274: */
275: private final ImageData itemImageData;
276:
277: /**
278: * Appearance mode.
279: * The actual appearance of an <code>ImageItem</code> could be different to
280: * the one set in <code>ImageItem</code>. An <code>ImageItem</code>
281: * created with <code>PLAIN</code> appearance will look like a
282: * <code>HYPERLINK</code> if commands were added.
283: */
284: private int appearanceMode;
285:
286: } // ImageItemLFImpl
|