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   *  Contributor(s):
21   *    3GLab
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  	// XXX actually the label for this should be an empty String
36  	// but the implementation is free to show a label
37  	// so the label should be set to "" and on the Command render
38  	// we should check if it was the dismiss command and
39  	// display a predefined label...
40  	public static final Command DISMISS_COMMAND = new Command("OK", Command.OK, 0);
41  	int time;
42  	Gauge indicator;
43  
44  	// this is for alertListener
45  	static Displayable nextDisplayable;
46  	static CommandListener defaultListener = new CommandListener()
47  	{
48  		public void commandAction(Command cmd, Displayable d)
49  		{
50  			// XXX if nextDisplayable == null
51  			// then it means that this Alert was
52  			// setted current when there was not a previous
53  			// Displayable (ie immediately after MIDlet start)
54  			// in that particular case the initial state should 
55  			// be restored
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 		// validate the gauge against the restrictrions
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 			// if the command vector is empty then
177 			// there is no default command
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 	    // XXX stop timeout thread!
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 }