1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package javax.microedition.lcdui;
25
26 import org.microemu.device.DeviceFactory;
27
28
29 public class Alert extends Screen
30 {
31 public static final int FOREVER = -2;
32
33 ImageStringItem alertContent;
34 AlertType type;
35
36
37
38
39
40 public static final Command DISMISS_COMMAND = new Command("OK", Command.OK, 0);
41 int time;
42 Gauge indicator;
43
44
45 static Displayable nextDisplayable;
46 static CommandListener defaultListener = new CommandListener()
47 {
48 public void commandAction(Command cmd, Displayable d)
49 {
50
51
52
53
54
55
56 ((Alert) d).currentDisplay.setCurrent(nextDisplayable);
57 }
58 };
59
60 public Alert(String title)
61 {
62 this(title, null, null, null);
63 }
64
65
66 public Alert(String title, String alertText, Image alertImage, AlertType alertType)
67 {
68 super(title);
69 super.setUI(DeviceFactory.getDevice().getUIFactory().createAlertUI(this));
70
71 setTimeout(getDefaultTimeout());
72 this.alertContent = new ImageStringItem(null, alertImage, alertText);
73 setType(alertType);
74 super.addCommand(Alert.DISMISS_COMMAND);
75 super.setCommandListener(defaultListener);
76 }
77
78
79 public void addCommand(Command cmd)
80 {
81 if (cmd == Alert.DISMISS_COMMAND) {
82 return;
83 } else {
84 super.addCommand(cmd);
85 super.removeCommand(Alert.DISMISS_COMMAND);
86 }
87 }
88
89 public void removeCommand(Command cmd) {
90 if (cmd == Alert.DISMISS_COMMAND) {
91 return;
92 } else {
93 super.removeCommand(cmd);
94 if (getCommands().size() == 0) {
95 super.addCommand(Alert.DISMISS_COMMAND);
96 }
97 }
98 }
99
100 public int getDefaultTimeout()
101 {
102 return Alert.FOREVER;
103 }
104
105
106 public String getString()
107 {
108 return alertContent.getText();
109 }
110
111
112 public int getTimeout()
113 {
114 return time;
115 }
116
117
118 public AlertType getType()
119 {
120 return type;
121 }
122
123
124 public void setType(AlertType type)
125 {
126 this.type = type;
127 repaint();
128 }
129
130
131 public void setCommandListener(CommandListener l)
132 {
133 if (l == null)
134 l = defaultListener;
135 super.setCommandListener(l);
136 }
137
138
139 public Image getImage()
140 {
141 return alertContent.getImage();
142 }
143
144
145 public void setImage(Image img)
146 {
147 if (img.isMutable()) {
148 img = Image.createImage(img);
149 }
150 alertContent.setImage(img);
151 repaint();
152 }
153
154 public Gauge getIndicator() {
155 return indicator;
156 }
157
158 public void setIndicator(Gauge indicator) {
159 if (indicator == null) {
160 if (this.indicator != null)
161 this.indicator.setOwner(null);
162 this.indicator = null;
163 repaint();
164 return;
165 }
166
167
168 if (indicator.getLayout() != 0 ||
169 indicator.getLabel() != null ||
170 indicator.prefHeight != -1 ||
171 indicator.prefWidth != -1 ||
172 indicator.commandListener != null ||
173 indicator.isInteractive() ||
174 indicator.getOwner() != null ||
175 !indicator.commands.isEmpty()) {
176
177
178 throw new IllegalArgumentException(
179 "This gauge cannot be added to an Alert");
180 }
181 indicator.setOwner(this);
182 this.indicator = indicator;
183 repaint();
184 }
185
186
187 public void setString(String str)
188 {
189 alertContent.setText(str);
190 repaint();
191 }
192
193
194 public void setTimeout(int time)
195 {
196 if (time != FOREVER && time <= 0) {
197 throw new IllegalArgumentException();
198 }
199
200 if (time != FOREVER && getCommands().size() > 1)
201 time = FOREVER;
202
203 this.time = time;
204 }
205
206
207 private int getContentHeight()
208 {
209 return alertContent.getHeight();
210 }
211
212
213 int paintContent(Graphics g)
214 {
215 return alertContent.paint(g);
216 }
217
218
219 void showNotify()
220 {
221 super.showNotify();
222 viewPortY = 0;
223 }
224
225
226 int traverse(int gameKeyCode, int top, int bottom)
227 {
228 Font f = Font.getDefaultFont();
229
230 if (gameKeyCode == 1 && top != 0) {
231 return -f.getHeight();
232 }
233 if (gameKeyCode == 6 && bottom < getContentHeight()) {
234 return f.getHeight();
235 }
236
237 return 0;
238 }
239
240 }