ImageItempublic class ImageItem extends Item An item that can contain an image.
Each ImageItem object contains a reference to an
{@link Image} object.
This Image may be mutable or immutable. If the
Image is mutable, the
effect is as if snapshot of its contents is taken at the time the
ImageItem
is constructed with this Image and when
setImage is called with an Image .
The snapshot is used whenever the contents of the
ImageItem are to be
displayed. Even if the application subsequently draws into the
Image , the
snapshot is not modified until the next call to
setImage . The snapshot is
not updated when the container of the
ImageItem becomes current or
becomes visible on the display. (This is because the application does not
have control over exactly when Displayables and Items
appear and disappear
from the display.)
The value null may be specified for the image
contents of an ImageItem .
If
this occurs (and if the label is also null ) the
ImageItem will occupy no
space on the screen.
ImageItem contains layout directives that were
originally defined in
MIDP 1.0. These layout directives have been moved to the
{@link Item} class and now apply to all items. The declarations are left
in ImageItem for source compatibility purposes.
The altText parameter specifies a string to be
displayed in place of the
image if the image exceeds the capacity of the display. The
altText
parameter may be null . |
Fields Summary |
---|
public static final int | LAYOUT_DEFAULTSee {@link Item#LAYOUT_DEFAULT}.
Value 0 is assigned to LAYOUT_DEFAULT . | public static final int | LAYOUT_LEFTSee {@link Item#LAYOUT_LEFT}.
Value 1 is assigned to LAYOUT_LEFT . | public static final int | LAYOUT_RIGHTSee {@link Item#LAYOUT_RIGHT}.
Value 2 is assigned to LAYOUT_RIGHT . | public static final int | LAYOUT_CENTERSee {@link Item#LAYOUT_CENTER}.
Value 3 is assigned to LAYOUT_CENTER . | public static final int | LAYOUT_NEWLINE_BEFORESee {@link Item#LAYOUT_NEWLINE_BEFORE}.
Value 0x100 is assigned to
LAYOUT_NEWLINE_BEFORE . | public static final int | LAYOUT_NEWLINE_AFTERSee {@link Item#LAYOUT_NEWLINE_AFTER}.
Value 0x200 is assigned to
LAYOUT_NEWLINE_AFTER . | private Image | imgThe snapshot of the Image of this ImageItem;
If the Image of this ImageItem was set to a mutable Image
this variable is updated with a new snapshot each time setImage() is
called. | private Image | mutImgIf the ImageItem was created with a mutable image or its Image
was set to a mutable image, that mutable image is stored in
the mutImg variable so that ImageItem.getImage() could return it. | private String | altTextThe alternate text of this ImageItem | private int | vertPadVertical padding used for this ImageItem so that it is padded to
occupy whole number of lines. | private int | appearanceModeThe appearance hint | private int | originalAppearanceModeThe original appearance hint before switching | private static final int | HYPERLINK_PADThe hyperlink pad used between Hyperlink Border and ImageItem | private static final Image | HYPERLINK_IMGThe hyperlink Image to display with the Hyperlink | private static final Image | VERTICAL_HYPERLINK_IMGThe Vertical hyperlink Image to display with the Hyperlink |
Constructors Summary |
---|
public ImageItem(String label, Image img, int layout, String altText)Creates a new ImageItem with the given label, image, layout
directive, and alternate text string. Calling this constructor is
equivalent to calling
ImageItem(label, image, layout, altText, PLAIN);
|
super(label);
synchronized (Display.LCDUILock) {
setImageImpl(img);
setLayoutImpl(layout);
this.altText = altText;
}
| public ImageItem(String label, Image image, int layout, String altText, int appearanceMode)Creates a new ImageItem object with the given label, image,
layout directive, alternate text string, and appearance mode.
Either label or alternative text may be present or null .
The appearanceMode parameter
(see Appearance Modes)
is a hint to the platform of the application's intended use
for this ImageItem . To provide hyperlink- or
button-like behavior,
the application should associate a default Command with this
ImageItem and add an
ItemCommandListener to this
ImageItem .
Here is an example showing the use of an
ImageItem as a button:
ImageItem imgItem =
new ImageItem("Default: ", img,
Item.LAYOUT_CENTER, null,
Item.BUTTON);
imgItem.setDefaultCommand(
new Command("Set", Command.ITEM, 1);
// icl is ItemCommandListener
imgItem.setItemCommandListener(icl);
|
this(label, image, layout, altText);
synchronized (Display.LCDUILock) {
switch (appearanceMode) {
case Item.PLAIN:
case Item.HYPERLINK:
case Item.BUTTON:
this.appearanceMode = appearanceMode;
break;
default:
throw new IllegalArgumentException();
}
}
|
Methods Summary |
---|
void | addCommandImpl(Command cmd)Adds a context sensitive Command to the image item.
synchronized (Display.LCDUILock) {
super.addCommandImpl(cmd);
if ((numCommands >= 1) && (appearanceMode == Item.PLAIN)) {
// restore the value of the original appearanceMode
// if it is a button
// otherwise simple change the appearanceMode
// to a hyperlink
this.appearanceMode =
originalAppearanceMode == Item.BUTTON ?
Item.BUTTON : Item.HYPERLINK;
invalidate();
}
} // synchronized
| void | callKeyPressed(int keyCode)Called by the system to signal a key press
if (keyCode != Display.KEYCODE_SELECT) {
return;
}
if (getCommandCount() == 0 || commandListener == null) {
return;
}
ItemCommandListener cl = null;
Command defaultCmd = null;
synchronized (Display.LCDUILock) {
cl = commandListener;
defaultCmd = defaultCommand;
} // synchronized
// SYNC NOTE: The call to the listener must occur outside
// of the lock
if (cl != null) {
try {
// SYNC NOTE: We lock on calloutLock around any calls
// into application code
synchronized (Display.calloutLock) {
if (defaultCmd != null) {
cl.commandAction(defaultCmd, this);
} else {
// REMINDER : Needs HI decision
// either call the first command
// from the command list or
// invoke the menu
}
}
} catch (Throwable thr) {
Display.handleThrowable(thr);
}
}
| int | callMinimumHeight()Get the minimum height of this Item
return callPreferredHeight(-1);
| int | callMinimumWidth()Get the minimum width of this Item
return callPreferredWidth(-1);
| void | callPaint(Graphics g, int width, int height)Paint this ImageItem
int labelHeight = super.paintLabel(g, width);
if (img == null) {
return;
}
int x = 0;
int y = labelHeight;
int l = getLayout() & ImageItem.LAYOUT_CENTER;
if (l == ImageItem.LAYOUT_CENTER) {
x = width / 2;
if ((numCommands >= 1) &&
(this.appearanceMode == Item.BUTTON)) {
x += BUTTON_BORDER + BUTTON_PAD;
y += BUTTON_BORDER + BUTTON_PAD;
} else if ((numCommands >= 1) &&
(this.appearanceMode == Item.HYPERLINK)) {
x += VERTICAL_HYPERLINK_IMG.getWidth() + HYPERLINK_PAD;
y += HYPERLINK_IMG.getHeight() + HYPERLINK_PAD;
}
g.drawImage(img, x, y,
Graphics.TOP | Graphics.HCENTER);
} else if (l == ImageItem.LAYOUT_RIGHT) {
x = width;
if ((numCommands >= 1) &&
(this.appearanceMode == Item.BUTTON)) {
x -= (BUTTON_BORDER + BUTTON_PAD);
y += BUTTON_BORDER + BUTTON_PAD;
} else if ((numCommands >= 1) &&
(this.appearanceMode == Item.HYPERLINK)) {
x -= VERTICAL_HYPERLINK_IMG.getWidth() + HYPERLINK_PAD;
y += HYPERLINK_IMG.getHeight() + HYPERLINK_PAD;
}
g.drawImage(img, x, y,
Graphics.TOP | Graphics.RIGHT);
} else {
// use x = 0;
if ((numCommands >= 1) &&
(this.appearanceMode == Item.BUTTON)) {
x += BUTTON_BORDER + BUTTON_PAD;
y += BUTTON_BORDER + BUTTON_PAD;
} else if ((numCommands >= 1) &&
(this.appearanceMode == Item.HYPERLINK)) {
x += VERTICAL_HYPERLINK_IMG.getWidth() + HYPERLINK_PAD;
y += HYPERLINK_IMG.getHeight() + HYPERLINK_PAD;
}
g.drawImage(img, x, y,
Graphics.TOP | Graphics.LEFT);
}
// draw the button border or hyperlink image
if ((numCommands >= 1) &&
(this.appearanceMode == Item.BUTTON)) {
int w = img.getWidth();
int h = img.getHeight();
// y = labelHeight + vertPad/2;
y = labelHeight;
if (l == ImageItem.LAYOUT_CENTER) {
x = (width / 2) - (w / 2);
} else if (l == ImageItem.LAYOUT_RIGHT) {
x = width - w - 2 * (BUTTON_BORDER + BUTTON_PAD);
} else {
x = 0;
}
w += 2 * (BUTTON_BORDER + BUTTON_PAD);
h += 2 * (BUTTON_BORDER + BUTTON_PAD);
drawButtonBorder(g, x, y, w, h, hasFocus);
} else if ((numCommands >= 1) &&
(this.appearanceMode == Item.HYPERLINK)) {
// NOTE: We test to see if the width of the Image
// in the ImageItem is
// wider than the width of the hyperlink image,
// if so, we re-draw
// the image offset to the right as many times as necessary
// to fill the width of the Image in the ImageItem.
int imageItemWidth = img.getWidth();
int imageItemHeight = img.getHeight();
y = labelHeight;
if (l == ImageItem.LAYOUT_CENTER) {
drawTop_BottomBorder(g,
width / 2 - imageItemWidth / 2
- HYPERLINK_PAD - VERTICAL_HYPERLINK_IMG.getWidth(),
width / 2 + imageItemWidth / 2 + 2 * HYPERLINK_PAD,
y, y + HYPERLINK_IMG.getHeight()
+ imageItemHeight + 2 * HYPERLINK_PAD);
drawLeft_RightBorder(g, y,
y + imageItemHeight + (2 * HYPERLINK_PAD),
width / 2 - imageItemWidth / 2
- VERTICAL_HYPERLINK_IMG.getHeight(),
width / 2 + imageItemWidth / 2 + 2 * HYPERLINK_PAD);
} else if (l == ImageItem.LAYOUT_RIGHT) {
drawTop_BottomBorder(g,
width - imageItemWidth
- 2 * HYPERLINK_PAD - VERTICAL_HYPERLINK_IMG.getWidth(),
width - VERTICAL_HYPERLINK_IMG.getWidth(),
y, y + HYPERLINK_IMG.getHeight()
+ imageItemHeight + 2 * HYPERLINK_PAD);
drawLeft_RightBorder(g, y,
y + imageItemHeight + 2 * HYPERLINK_PAD,
width - imageItemWidth
- HYPERLINK_PAD - VERTICAL_HYPERLINK_IMG.getHeight(),
width - VERTICAL_HYPERLINK_IMG.getWidth());
} else {
drawTop_BottomBorder(g, 0,
imageItemWidth + 2 * HYPERLINK_PAD,
y, y + HYPERLINK_IMG.getHeight()
+ imageItemHeight + 2 * HYPERLINK_PAD);
drawLeft_RightBorder(g, y,
y + imageItemHeight + 2 * HYPERLINK_PAD,
0, HYPERLINK_IMG.getHeight()
+ imageItemWidth + 2 * HYPERLINK_PAD);
}
}// end if HYPERLINK
| int | callPreferredHeight(int w)Get the preferred height of this Item
if (img == null) {
return getLabelHeight(w);
}
if (numCommands >= 1) {
if (this.appearanceMode == Item.BUTTON) {
return img.getHeight() + getLabelHeight(w) +
(BUTTON_BORDER + BUTTON_PAD) * 2;
} else if (this.appearanceMode == Item.HYPERLINK) {
return img.getHeight() + getLabelHeight(w) +
(HYPERLINK_IMG.getHeight() + HYPERLINK_PAD) * 2;
}
}
return img.getHeight() + getLabelHeight(w);
| int | callPreferredWidth(int h)Get the preferred width of this Item
if (img == null) {
return getLabelWidth();
}
if (numCommands >= 1) {
if (this.appearanceMode == Item.BUTTON) {
return img.getWidth() + (BUTTON_BORDER + BUTTON_PAD) * 2;
} else if (this.appearanceMode == Item.HYPERLINK) {
return img.getWidth() +
(VERTICAL_HYPERLINK_IMG.getWidth() + HYPERLINK_PAD) * 2;
}
}
int labelWidth = getLabelWidth();
int imageW = img.getWidth();
return (labelWidth > imageW ? labelWidth : imageW);
| private void | drawLeft_RightBorder(Graphics g, int start, int end, int x1, int x2)Draw Left and Right Border for showing Hyperlink ImageItem
for (int y = start; y < end; y += VERTICAL_HYPERLINK_IMG.getHeight()) {
// draw the left border
g.drawImage(VERTICAL_HYPERLINK_IMG, x1, y,
Graphics.TOP | Graphics.LEFT);
// draw the right border
g.drawImage(VERTICAL_HYPERLINK_IMG, x2, y,
Graphics.TOP | Graphics.LEFT);
}
| private void | drawTop_BottomBorder(Graphics g, int start, int end, int y1, int y2)Draw Top and Bottom Border for showing Hyperlink ImageItem
for (int x = start; x < end; x += HYPERLINK_IMG.getWidth()) {
// draw the top border
g.drawImage(HYPERLINK_IMG, x, y1,
Graphics.TOP | Graphics.LEFT);
// draw the bottom border
g.drawImage(HYPERLINK_IMG, x, y2,
Graphics.TOP | Graphics.LEFT);
}
| public java.lang.String | getAltText()Gets the text string to be used if the image exceeds the device's
capacity to display it.
// SYNC NOTE: return of atomic value, no locking necessary
return altText;
| public int | getAppearanceMode()Returns the appearance mode of the ImageItem .
See Appearance Modes.
return appearanceMode;
| public Image | getImage()Gets the image contained within the ImageItem , or
null if there is no
contained image.
synchronized (Display.LCDUILock) {
return mutImg == null ? img : mutImg;
}
| public int | getLayout()Gets the layout directives used for placing the image.
// NOTE: looks odd, but this method is required for 1.0 compatiblitiy
return super.getLayout();
| void | removeCommandImpl(Command cmd)Removes the context sensitive command from item.
synchronized (Display.LCDUILock) {
super.removeCommandImpl(cmd);
if ((numCommands < 1) && (appearanceMode != Item.PLAIN)) {
// store value of the original appearanceMode
originalAppearanceMode = this.appearanceMode;
// change the appearanceMode to plain
this.appearanceMode = Item.PLAIN;
// size or appearance changed
invalidate();
}
} // synchronized
| public void | setAltText(java.lang.String text)Sets the alternate text of the ImageItem , or
null if no alternate text is provided.
// SYNC NOTE: atomic, no locking necessary
this.altText = text;
| private void | setAltTextImpl(java.lang.String text)Set the alternate text for this ImageItem
this.altText = text;
| public void | setImage(Image img)Sets the Image object contained within the
ImageItem . The image may be
mutable or immutable. If img is
null , the ImageItem is set to be
empty. If img is mutable, the effect is as if a
snapshot is taken of
img's contents immediately prior to the call to
setImage . This
snapshot is used whenever the contents of the
ImageItem are to be
displayed. If img is already the
Image of this ImageItem , the effect
is as if a new snapshot of img's contents is taken. Thus, after
painting into a mutable image contained by an
ImageItem , the
application can call
imageItem.setImage(imageItem.getImage());
|
to refresh the ImageItem's snapshot of its Image.
If the ImageItem is visible on the display when
the snapshot is
updated through a call to setImage , the display is
updated with the new
snapshot as soon as it is feasible for the implementation to so do.
synchronized (Display.LCDUILock) {
setImageImpl(img);
invalidate();
}
| private void | setImageImpl(Image img)Set the Image for this ImageItem
if (img != null && img.isMutable()) {
this.mutImg = img;
this.img = Image.createImage(img); // use immutable copy of img
} else {
this.mutImg = null;
this.img = img;
}
| public void | setLayout(int layout)Sets the layout directives.
// NOTE: looks odd, but this method is required for 1.0 compatiblitiy
super.setLayout(layout);
| boolean | shouldSkipTraverse()Determine if Form should not traverse to this ImageItem
if ((label == null || label.length() == 0) && (img == null)) {
return true;
}
return false;
|
|