View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2005 Andres Navarro
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  
21  package javax.microedition.lcdui;
22  
23  public class ImageItem extends Item {
24  
25  	public static final int LAYOUT_DEFAULT = 0;
26  
27  	public static final int LAYOUT_LEFT = 1;
28  
29  	public static final int LAYOUT_RIGHT = 2;
30  
31  	public static final int LAYOUT_CENTER = 3;
32  
33  	public static final int LAYOUT_NEWLINE_BEFORE = 0x100;
34  
35  	public static final int LAYOUT_NEWLINE_AFTER = 0x200;
36  
37  	Image img;
38  
39  	String altText;
40  
41  	private int appearanceMode;
42  
43  	public ImageItem(String label, Image img, int layout, String altText) {
44  		this(label, img, layout, altText, ImageItem.PLAIN);
45  	}
46  
47  	public ImageItem(String label, Image img, int layout, String altText, int appearanceMode) {
48  		super(label);
49  
50  		// may throw IllegalArgumentException
51  		// (that is the intentended behaviour)
52  		setLayout(layout);
53  		if (appearanceMode != ImageItem.PLAIN && appearanceMode != ImageItem.HYPERLINK
54  				&& appearanceMode != ImageItem.BUTTON) {
55  			throw new IllegalArgumentException();
56  		}
57  
58  		setImage(img);
59  		this.altText = altText;
60  		this.appearanceMode = appearanceMode;
61  	}
62  
63  	public String getAltText() {
64  		return altText;
65  	}
66  
67  	public int getAppearanceMode() {
68  		return this.appearanceMode;
69  	}
70  
71  	public Image getImage() {
72  		return img;
73  	}
74  
75  	public int getLayout() {
76  		return super.getLayout();
77  	}
78  
79  	public void setAltText(String text) {
80  		altText = text;
81  	}
82  
83  	public void setImage(Image img) {
84  		if (img != null && img.isMutable()) {
85  			img = Image.createImage(img);
86  		}
87  		this.img = img;
88  		repaint();
89  	}
90  
91  	public void setLayout(int layout) {
92  		super.setLayout(layout);
93  	}
94  
95  	int getHeight() {
96  		if (img == null) {
97  			return super.getHeight();
98  		} else {
99  			return super.getHeight() + img.getHeight();
100 		}
101 	}
102 
103 	int paint(Graphics g) {
104 		super.paintContent(g);
105 
106 		if (img != null) {
107 			g.translate(0, super.getHeight());
108 			if (layout == LAYOUT_DEFAULT || layout == LAYOUT_LEFT) {
109 				g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
110 			} else if (layout == LAYOUT_RIGHT) {
111 				g.drawImage(img, owner.getWidth(), 0, Graphics.RIGHT | Graphics.TOP);
112 			} else if (layout == LAYOUT_CENTER) {
113 				g.drawImage(img, owner.getWidth() / 2, 0, Graphics.HCENTER | Graphics.TOP);
114 			} else {
115 				g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
116 			}
117 			g.translate(0, -super.getHeight());
118 		}
119 
120 		return getHeight();
121 	}
122 
123 	int traverse(int gameKeyCode, int top, int bottom, boolean action) {
124 		Font f = Font.getDefaultFont();
125 
126 		if (gameKeyCode == Canvas.UP) {
127 			if (top > 0) {
128 				if ((top % f.getHeight()) == 0) {
129 					return -f.getHeight();
130 				} else {
131 					return -(top % f.getHeight());
132 				}
133 			} else {
134 				return Item.OUTOFITEM;
135 			}
136 		}
137 		if (gameKeyCode == Canvas.DOWN) {
138 			if (bottom < getHeight()) {
139 				if (getHeight() - bottom < f.getHeight()) {
140 					return getHeight() - bottom;
141 				} else {
142 					return f.getHeight();
143 				}
144 			} else {
145 				return Item.OUTOFITEM;
146 			}
147 		}
148 
149 		return 0;
150 	}
151 
152 }