Source code: javax/microedition/lcdui/Alert.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Contributor(s):
20 * 3GLab
21 */
22
23 package javax.microedition.lcdui;
24
25
26 public class Alert extends Screen
27 {
28
29 public static final int FOREVER = -2;
30
31 ImageStringItem alertContent;
32 AlertType type;
33 static final Command OK = new Command("OK", Command.OK, 0);
34 int time;
35
36
37 CommandListener alertListener = new CommandListener()
38 {
39
40 public void commandAction(Command cmd, Displayable d)
41 {
42 currentDisplay.clearAlert();
43 }
44
45 };
46
47
48 public Alert(String title)
49 {
50 this(title, null, null, null);
51 }
52
53
54 public Alert(String title, String alertText, Image alertImage, AlertType alertType)
55 {
56 super(title);
57 setTimeout(getDefaultTimeout());
58 this.alertContent = new ImageStringItem(null, alertImage, alertText);
59 setType(alertType);
60 super.setCommandListener(alertListener);
61 }
62
63
64 public void addCommand(Command cmd)
65 {
66 throw new IllegalStateException("Alert does not accept commands");
67 }
68
69
70 public int getDefaultTimeout()
71 {
72 return Alert.FOREVER;
73 }
74
75
76 public String getString()
77 {
78 return alertContent.getText();
79 }
80
81
82 public int getTimeout()
83 {
84 return time;
85 }
86
87
88 public AlertType getType()
89 {
90 return type;
91 }
92
93
94 public void setType(AlertType type)
95 {
96 this.type = type;
97 }
98
99
100 public void setCommandListener(CommandListener l)
101 {
102 throw new IllegalStateException("Alert does not accept listeners");
103 }
104
105
106 public Image getImage()
107 {
108 return alertContent.getImage();
109 }
110
111
112 public void setImage(Image img)
113 {
114 if (img.isMutable()) {
115 throw new IllegalArgumentException("Image cannot be mutable");
116 }
117 alertContent.setImage(img);
118 repaint();
119 }
120
121
122 public void setString(String str)
123 {
124 alertContent.setText(str);
125 repaint();
126 }
127
128
129 public void setTimeout(int time)
130 {
131 if (time != FOREVER && time <= 0) {
132 throw new IllegalArgumentException();
133 }
134 this.time = time;
135 super.removeCommand(OK);
136 if (getTimeout() == Alert.FOREVER) {
137 super.addCommand(OK);
138 }
139
140 }
141
142
143 int getHeight()
144 {
145 return alertContent.getHeight();
146 }
147
148
149 int paintContent(Graphics g)
150 {
151 return alertContent.paint(g);
152 }
153
154
155 void showNotify()
156 {
157 super.showNotify();
158
159 viewPortY = 0;
160 }
161
162
163 int traverse(int gameKeyCode, int top, int bottom)
164 {
165 Font f = Font.getDefaultFont();
166
167 if (gameKeyCode == 1 && top != 0) {
168 return -f.getHeight();
169 }
170 if (gameKeyCode == 6 && bottom < getHeight()) {
171 return f.getHeight();
172 }
173
174 return 0;
175 }
176
177 }