KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > microedition > lcdui > Alert


1
2 /*
3  * MicroEmulator
4  * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
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  * Contributor(s):
21  * 3GLab
22  */

23  
24 package javax.microedition.lcdui;
25
26
27 public class Alert extends Screen
28 {
29
30     public static final int FOREVER = -2;
31
32     ImageStringItem alertContent;
33     AlertType type;
34     static final Command OK = new Command("OK", Command.OK, 0);
35     int time;
36
37
38     CommandListener alertListener = new CommandListener()
39     {
40
41         public void commandAction(Command cmd, Displayable d)
42         {
43             currentDisplay.clearAlert();
44         }
45
46     };
47
48
49     public Alert(String title)
50     {
51         this(title, null, null, null);
52     }
53
54
55     public Alert(String title, String alertText, Image alertImage, AlertType alertType)
56     {
57         super(title);
58         setTimeout(getDefaultTimeout());
59         this.alertContent = new ImageStringItem(null, alertImage, alertText);
60         setType(alertType);
61         super.setCommandListener(alertListener);
62     }
63
64
65     public void addCommand(Command cmd)
66     {
67         throw new IllegalStateException("Alert does not accept commands");
68     }
69
70
71     public int getDefaultTimeout()
72     {
73         return Alert.FOREVER;
74     }
75
76
77     public String getString()
78     {
79         return alertContent.getText();
80     }
81
82
83     public int getTimeout()
84     {
85         return time;
86     }
87
88
89   public AlertType getType()
90   {
91     return type;
92   }
93   
94   
95   public void setType(AlertType type)
96     {
97         this.type = type;
98     }
99
100
101     public void setCommandListener(CommandListener l)
102     {
103         throw new IllegalStateException("Alert does not accept listeners");
104     }
105
106
107     public Image getImage()
108     {
109     return alertContent.getImage();
110     }
111
112
113     public void setImage(Image img)
114     {
115     if (img.isMutable()) {
116       throw new IllegalArgumentException("Image cannot be mutable");
117     }
118     alertContent.setImage(img);
119     repaint();
120     }
121
122
123     public void setString(String str)
124     {
125         alertContent.setText(str);
126         repaint();
127     }
128
129
130     public void setTimeout(int time)
131     {
132     if (time != FOREVER && time <= 0) {
133       throw new IllegalArgumentException();
134     }
135         this.time = time;
136     super.removeCommand(OK);
137         if (getTimeout() == Alert.FOREVER) {
138             super.addCommand(OK);
139         }
140
141     }
142
143
144     int getHeight()
145     {
146         return alertContent.getHeight();
147     }
148
149
150     int paintContent(Graphics g)
151     {
152         return alertContent.paint(g);
153     }
154
155
156     void showNotify()
157     {
158         super.showNotify();
159
160     viewPortY = 0;
161     }
162
163
164     int traverse(int gameKeyCode, int top, int bottom)
165     {
166         Font f = Font.getDefaultFont();
167
168         if (gameKeyCode == 1 && top != 0) {
169             return -f.getHeight();
170         }
171         if (gameKeyCode == 6 && bottom < getHeight()) {
172             return f.getHeight();
173         }
174
175         return 0;
176     }
177
178 }
179
Popular Tags