001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc.
003: *
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: * * Neither the name of Sun Microsystems, Inc. nor the names of its
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
023: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
024: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package loginscreenexample;
034:
035: import java.io.IOException;
036: import javax.microedition.lcdui.Alert;
037: import javax.microedition.lcdui.Command;
038: import javax.microedition.lcdui.CommandListener;
039: import javax.microedition.lcdui.Display;
040: import javax.microedition.lcdui.Displayable;
041: import javax.microedition.lcdui.Ticker;
042: import javax.microedition.midlet.*;
043: import org.netbeans.microedition.lcdui.LoginScreen;
044: import org.netbeans.microedition.lcdui.SplashScreen;
045: import org.netbeans.microedition.lcdui.WaitScreen;
046: import org.netbeans.microedition.util.SimpleCancellableTask;
047:
048: /**
049: * @author devil
050: * http://www.netbeans.org/kb/60/mobility/loginscreen.html
051: */
052: public class LoginScreenExample extends MIDlet implements
053: CommandListener {
054:
055: private boolean midletPaused = false;
056: //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
057: private Command exitCommand;
058: private Ticker ticker;
059: private SimpleCancellableTask task;
060: private LoginScreen loginScreen;
061: private Alert alertSuccess;
062: private WaitScreen waitScreen;
063: private Alert alertFailure;
064: private SplashScreen splashScreen;
065:
066: //</editor-fold>//GEN-END:|fields|0|
067:
068: /**
069: * The LoginScreenExample constructor.
070: */
071: public LoginScreenExample() {
072: }
073:
074: //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
075: //</editor-fold>//GEN-END:|methods|0|
076:
077: //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
078: /**
079: * Initilizes the application.
080: * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
081: */
082: private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
083: // write pre-initialize user code here
084: //GEN-LINE:|0-initialize|1|0-postInitialize
085: // write post-initialize user code here
086: }//GEN-BEGIN:|0-initialize|2|
087:
088: //</editor-fold>//GEN-END:|0-initialize|2|
089:
090: //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
091: /**
092: * Performs an action assigned to the Mobile Device - MIDlet Started point.
093: */
094: public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
095: // write pre-action user code here
096: switchDisplayable(null, getSplashScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction
097: // write post-action user code here
098: }//GEN-BEGIN:|3-startMIDlet|2|
099:
100: //</editor-fold>//GEN-END:|3-startMIDlet|2|
101:
102: //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
103: /**
104: * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
105: */
106: public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
107: // write pre-action user code here
108: //GEN-LINE:|4-resumeMIDlet|1|4-postAction
109: // write post-action user code here
110: }//GEN-BEGIN:|4-resumeMIDlet|2|
111:
112: //</editor-fold>//GEN-END:|4-resumeMIDlet|2|
113:
114: //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
115: /**
116: * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
117: * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
118: * @param nextDisplayable the Displayable to be set
119: */
120: public void switchDisplayable(Alert alert,
121: Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
122: // write pre-switch user code here
123: Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
124: if (alert == null) {
125: display.setCurrent(nextDisplayable);
126: } else {
127: display.setCurrent(alert, nextDisplayable);
128: }//GEN-END:|5-switchDisplayable|1|5-postSwitch
129: // write post-switch user code here
130: }//GEN-BEGIN:|5-switchDisplayable|2|
131:
132: //</editor-fold>//GEN-END:|5-switchDisplayable|2|
133:
134: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">//GEN-BEGIN:|18-getter|0|18-preInit
135: /**
136: * Returns an initiliazed instance of task component.
137: * @return the initialized component instance
138: */
139: public SimpleCancellableTask getTask() {
140: if (task == null) {//GEN-END:|18-getter|0|18-preInit
141: // write pre-init user code here
142: task = new SimpleCancellableTask();//GEN-BEGIN:|18-getter|1|18-execute
143: task
144: .setExecutable(new org.netbeans.microedition.util.Executable() {
145: public void execute() throws Exception {//GEN-END:|18-getter|1|18-execute
146: login();
147: }//GEN-BEGIN:|18-getter|2|18-postInit
148: });//GEN-END:|18-getter|2|18-postInit
149: // write post-init user code here
150: }//GEN-BEGIN:|18-getter|3|
151: return task;
152: }
153:
154: //</editor-fold>//GEN-END:|18-getter|3|
155:
156: //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
157: /**
158: * Called by a system to indicated that a command has been invoked on a particular displayable.
159: * @param command the Command that was invoked
160: * @param displayable the Displayable where the command was invoked
161: */
162: public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
163: // write pre-action user code here
164: if (displayable == loginScreen) {//GEN-BEGIN:|7-commandAction|1|31-preAction
165: if (command == LoginScreen.LOGIN_COMMAND) {//GEN-END:|7-commandAction|1|31-preAction
166: // write pre-action user code here
167: switchDisplayable(null, getWaitScreen());//GEN-LINE:|7-commandAction|2|31-postAction
168: // write post-action user code here
169: } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|34-preAction
170: // write pre-action user code here
171: exitMIDlet();//GEN-LINE:|7-commandAction|4|34-postAction
172: // write post-action user code here
173: }//GEN-BEGIN:|7-commandAction|5|27-preAction
174: } else if (displayable == splashScreen) {
175: if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|5|27-preAction
176: // write pre-action user code here
177: switchDisplayable(null, getLoginScreen());//GEN-LINE:|7-commandAction|6|27-postAction
178: // write post-action user code here
179: }//GEN-BEGIN:|7-commandAction|7|17-preAction
180: } else if (displayable == waitScreen) {
181: if (command == WaitScreen.FAILURE_COMMAND) {//GEN-END:|7-commandAction|7|17-preAction
182: // write pre-action user code here
183: switchDisplayable(getAlertFailure(), getLoginScreen());//GEN-LINE:|7-commandAction|8|17-postAction
184: // write post-action user code here
185: } else if (command == WaitScreen.SUCCESS_COMMAND) {//GEN-LINE:|7-commandAction|9|16-preAction
186: // write pre-action user code here
187: switchDisplayable(getAlertSuccess(), getLoginScreen());//GEN-LINE:|7-commandAction|10|16-postAction
188: // write post-action user code here
189: }//GEN-BEGIN:|7-commandAction|11|7-postCommandAction
190: }//GEN-END:|7-commandAction|11|7-postCommandAction
191: // write post-action user code here
192: }//GEN-BEGIN:|7-commandAction|12|
193:
194: //</editor-fold>//GEN-END:|7-commandAction|12|
195:
196: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: waitScreen ">//GEN-BEGIN:|13-getter|0|13-preInit
197: /**
198: * Returns an initiliazed instance of waitScreen component.
199: * @return the initialized component instance
200: */
201: public WaitScreen getWaitScreen() {
202: if (waitScreen == null) {//GEN-END:|13-getter|0|13-preInit
203: // write pre-init user code here
204: waitScreen = new WaitScreen(getDisplay());//GEN-BEGIN:|13-getter|1|13-postInit
205: waitScreen.setTitle("waitScreen");
206: waitScreen.setCommandListener(this );
207: waitScreen.setText("Please Wait ...");
208: waitScreen.setTask(getTask());//GEN-END:|13-getter|1|13-postInit
209: // write post-init user code here
210: }//GEN-BEGIN:|13-getter|2|
211: return waitScreen;
212: }
213:
214: //</editor-fold>//GEN-END:|13-getter|2|
215:
216: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|25-getter|0|25-preInit
217: /**
218: * Returns an initiliazed instance of splashScreen component.
219: * @return the initialized component instance
220: */
221: public SplashScreen getSplashScreen() {
222: if (splashScreen == null) {//GEN-END:|25-getter|0|25-preInit
223: // write pre-init user code here
224: splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|25-getter|1|25-postInit
225: splashScreen.setTitle("splashScreen");
226: splashScreen.setCommandListener(this );
227: splashScreen.setText(" Login Screen Example");//GEN-END:|25-getter|1|25-postInit
228: // write post-init user code here
229: }//GEN-BEGIN:|25-getter|2|
230: return splashScreen;
231: }
232:
233: //</editor-fold>//GEN-END:|25-getter|2|
234:
235: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|33-getter|0|33-preInit
236: /**
237: * Returns an initiliazed instance of exitCommand component.
238: * @return the initialized component instance
239: */
240: public Command getExitCommand() {
241: if (exitCommand == null) {//GEN-END:|33-getter|0|33-preInit
242: // write pre-init user code here
243: exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|33-getter|1|33-postInit
244: // write post-init user code here
245: }//GEN-BEGIN:|33-getter|2|
246: return exitCommand;
247: }
248:
249: //</editor-fold>//GEN-END:|33-getter|2|
250:
251: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loginScreen ">//GEN-BEGIN:|29-getter|0|29-preInit
252: /**
253: * Returns an initiliazed instance of loginScreen component.
254: * @return the initialized component instance
255: */
256: public LoginScreen getLoginScreen() {
257: if (loginScreen == null) {//GEN-END:|29-getter|0|29-preInit
258: // write pre-init user code here
259: loginScreen = new LoginScreen(getDisplay());//GEN-BEGIN:|29-getter|1|29-postInit
260: loginScreen.setLabelTexts("Username:", "Password:");
261: loginScreen.setTitle("loginScreen");
262: loginScreen.setTicker(getTicker());
263: loginScreen.addCommand(LoginScreen.LOGIN_COMMAND);
264: loginScreen.addCommand(getExitCommand());
265: loginScreen.setCommandListener(this );
266: loginScreen.setBGColor(-3355444);
267: loginScreen.setFGColor(0);
268: loginScreen.setUseLoginButton(false);//GEN-END:|29-getter|1|29-postInit
269: // write post-init user code here
270: }//GEN-BEGIN:|29-getter|2|
271: return loginScreen;
272: }
273:
274: //</editor-fold>//GEN-END:|29-getter|2|
275:
276: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alertFailure ">//GEN-BEGIN:|37-getter|0|37-preInit
277: /**
278: * Returns an initiliazed instance of alertFailure component.
279: * @return the initialized component instance
280: */
281: public Alert getAlertFailure() {
282: if (alertFailure == null) {//GEN-END:|37-getter|0|37-preInit
283: // write pre-init user code here
284: alertFailure = new Alert("alert",
285: "Wrong username or password", null, null);//GEN-BEGIN:|37-getter|1|37-postInit
286: alertFailure.setTimeout(Alert.FOREVER);//GEN-END:|37-getter|1|37-postInit
287: // write post-init user code here
288: }//GEN-BEGIN:|37-getter|2|
289: return alertFailure;
290: }
291:
292: //</editor-fold>//GEN-END:|37-getter|2|
293:
294: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alertSuccess ">//GEN-BEGIN:|39-getter|0|39-preInit
295: /**
296: * Returns an initiliazed instance of alertSuccess component.
297: * @return the initialized component instance
298: */
299: public Alert getAlertSuccess() {
300: if (alertSuccess == null) {//GEN-END:|39-getter|0|39-preInit
301: // write pre-init user code here
302: alertSuccess = new Alert("Logged In",
303: "Successfuly logged in", null, null);//GEN-BEGIN:|39-getter|1|39-postInit
304: alertSuccess.setTimeout(Alert.FOREVER);//GEN-END:|39-getter|1|39-postInit
305: // write post-init user code here
306: }//GEN-BEGIN:|39-getter|2|
307: return alertSuccess;
308: }
309:
310: //</editor-fold>//GEN-END:|39-getter|2|
311:
312: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ticker ">//GEN-BEGIN:|41-getter|0|41-preInit
313: /**
314: * Returns an initiliazed instance of ticker component.
315: * @return the initialized component instance
316: */
317: public Ticker getTicker() {
318: if (ticker == null) {//GEN-END:|41-getter|0|41-preInit
319: // write pre-init user code here
320: ticker = new Ticker("");//GEN-LINE:|41-getter|1|41-postInit
321: // write post-init user code here
322: //#ifdef LoginScreenServletExample
323: //# ticker.setString("The sample is in online mode. Use username/password john/peanuts for successfull connection.");
324: //#else
325: ticker
326: .setString("The sample is in offline mode. Use username/password test/test for successfull connection.");
327: //#endif
328:
329: }//GEN-BEGIN:|41-getter|2|
330: return ticker;
331: }
332:
333: //</editor-fold>//GEN-END:|41-getter|2|
334:
335: /**
336: * Returns a display instance.
337: * @return the display instance.
338: */
339: public Display getDisplay() {
340: return Display.getDisplay(this );
341: }
342:
343: /**
344: * Exits MIDlet.
345: */
346: public void exitMIDlet() {
347: switchDisplayable(null, null);
348: destroyApp(true);
349: notifyDestroyed();
350: }
351:
352: /**
353: * Called when MIDlet is started.
354: * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
355: */
356: public void startApp() {
357: if (midletPaused) {
358: resumeMIDlet();
359: } else {
360: initialize();
361: startMIDlet();
362: }
363: midletPaused = false;
364: }
365:
366: /**
367: * Called when MIDlet is paused.
368: */
369: public void pauseApp() {
370: midletPaused = true;
371: }
372:
373: /**
374: * Called to signal the MIDlet to terminate.
375: * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
376: */
377: public void destroyApp(boolean unconditional) {
378: }
379:
380: private void login() throws Exception {
381: //#ifdef LoginScreenServletExample
382: //# //URL
383: //# String url = "http://localhost:8080/LoginScreenExample/" + "?username=" + getLoginScreen().getUsername() + "&password=" + getLoginScreen().getPassword();
384: //#endif
385:
386: //#ifdef LoginScreenServletExample
387: //# //Connect to the server
388: //# HttpConnection hc = (HttpConnection) Connector.open(url);
389: //# //Authentication
390: //# if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
391: //# hc.close();
392: //# return;
393: //# }
394: //# //Closing time...
395: //# hc.close();
396: //# //Take action based on login value
397: //#endif
398:
399: //#ifndef LoginScreenServletExample
400: //if the username/password starts with "test" then it's OK
401: if (getLoginScreen().getUsername().startsWith("test")
402: && getLoginScreen().getPassword().startsWith("test")) {
403: return;
404: }
405: //#endif
406: //throw exception because the login didn't pass
407: throw new Exception("The login was not successful");
408: }
409: }
|