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 filebrowserexample;
034:
035: import java.io.IOException;
036: import java.io.InputStream;
037: import javax.microedition.io.file.FileConnection;
038: import javax.microedition.midlet.*;
039: import javax.microedition.lcdui.*;
040: import org.netbeans.microedition.lcdui.SplashScreen;
041: import org.netbeans.microedition.lcdui.pda.FileBrowser;
042:
043: /**
044: * @author devil
045: * Find more info at http://www.netbeans.org/kb/60/mobility/filebrowser.html
046: */
047: public class FileBrowserExample extends MIDlet implements
048: CommandListener {
049:
050: private boolean midletPaused = false;
051:
052: //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
053: private Command backCommand;
054: private Command exitCommand;
055: private TextBox textBox;
056: private FileBrowser fileBrowser;
057: private SplashScreen splashScreen;
058:
059: //</editor-fold>//GEN-END:|fields|0|
060:
061: /**
062: * The FileBrowserExample constructor.
063: */
064: public FileBrowserExample() {
065: }
066:
067: //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
068: //</editor-fold>//GEN-END:|methods|0|
069:
070: //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
071: /**
072: * Initilizes the application.
073: * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
074: */
075: private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
076: // write pre-initialize user code here
077: //GEN-LINE:|0-initialize|1|0-postInitialize
078: // write post-initialize user code here
079: }//GEN-BEGIN:|0-initialize|2|
080:
081: //</editor-fold>//GEN-END:|0-initialize|2|
082:
083: //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
084: /**
085: * Performs an action assigned to the Mobile Device - MIDlet Started point.
086: */
087: public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
088: // write pre-action user code here
089: switchDisplayable(null, getSplashScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction
090: // write post-action user code here
091: }//GEN-BEGIN:|3-startMIDlet|2|
092:
093: //</editor-fold>//GEN-END:|3-startMIDlet|2|
094:
095: //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
096: /**
097: * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
098: */
099: public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
100: // write pre-action user code here
101: //GEN-LINE:|4-resumeMIDlet|1|4-postAction
102: // write post-action user code here
103: }//GEN-BEGIN:|4-resumeMIDlet|2|
104:
105: //</editor-fold>//GEN-END:|4-resumeMIDlet|2|
106:
107: //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
108: /**
109: * 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.
110: * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
111: * @param nextDisplayable the Displayable to be set
112: */
113: public void switchDisplayable(Alert alert,
114: Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
115: // write pre-switch user code here
116: Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
117: if (alert == null) {
118: display.setCurrent(nextDisplayable);
119: } else {
120: display.setCurrent(alert, nextDisplayable);
121: }//GEN-END:|5-switchDisplayable|1|5-postSwitch
122: // write post-switch user code here
123: }//GEN-BEGIN:|5-switchDisplayable|2|
124:
125: //</editor-fold>//GEN-END:|5-switchDisplayable|2|
126:
127: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|23-getter|0|23-preInit
128: /**
129: * Returns an initiliazed instance of backCommand component.
130: * @return the initialized component instance
131: */
132: public Command getBackCommand() {
133: if (backCommand == null) {//GEN-END:|23-getter|0|23-preInit
134: // write pre-init user code here
135: backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|23-getter|1|23-postInit
136: // write post-init user code here
137: }//GEN-BEGIN:|23-getter|2|
138: return backCommand;
139: }
140:
141: //</editor-fold>//GEN-END:|23-getter|2|
142:
143: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|26-getter|0|26-preInit
144: /**
145: * Returns an initiliazed instance of exitCommand component.
146: * @return the initialized component instance
147: */
148: public Command getExitCommand() {
149: if (exitCommand == null) {//GEN-END:|26-getter|0|26-preInit
150: // write pre-init user code here
151: exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|26-getter|1|26-postInit
152: // write post-init user code here
153: }//GEN-BEGIN:|26-getter|2|
154: return exitCommand;
155: }
156:
157: //</editor-fold>//GEN-END:|26-getter|2|
158:
159: //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
160: /**
161: * Called by a system to indicated that a command has been invoked on a particular displayable.
162: * @param command the Command that was invoked
163: * @param displayable the Displayable where the command was invoked
164: */
165: public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
166: // write pre-action user code here
167: if (displayable == fileBrowser) {//GEN-BEGIN:|7-commandAction|1|19-preAction
168: if (command == FileBrowser.SELECT_FILE_COMMAND) {//GEN-END:|7-commandAction|1|19-preAction
169: readFile();
170: switchDisplayable(null, getTextBox());//GEN-LINE:|7-commandAction|2|19-postAction
171: // write post-action user code here
172: } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|27-preAction
173: // write pre-action user code here
174: exitMIDlet();//GEN-LINE:|7-commandAction|4|27-postAction
175: // write post-action user code here
176: }//GEN-BEGIN:|7-commandAction|5|15-preAction
177: } else if (displayable == splashScreen) {
178: if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|5|15-preAction
179: // write pre-action user code here
180: switchDisplayable(null, getFileBrowser());//GEN-LINE:|7-commandAction|6|15-postAction
181: // write post-action user code here
182: }//GEN-BEGIN:|7-commandAction|7|24-preAction
183: } else if (displayable == textBox) {
184: if (command == backCommand) {//GEN-END:|7-commandAction|7|24-preAction
185: // write pre-action user code here
186: switchDisplayable(null, getFileBrowser());//GEN-LINE:|7-commandAction|8|24-postAction
187: // write post-action user code here
188: }//GEN-BEGIN:|7-commandAction|9|7-postCommandAction
189: }//GEN-END:|7-commandAction|9|7-postCommandAction
190: // write post-action user code here
191: }//GEN-BEGIN:|7-commandAction|10|
192:
193: //</editor-fold>//GEN-END:|7-commandAction|10|
194:
195: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|13-getter|0|13-preInit
196: /**
197: * Returns an initiliazed instance of splashScreen component.
198: * @return the initialized component instance
199: */
200: public SplashScreen getSplashScreen() {
201: if (splashScreen == null) {//GEN-END:|13-getter|0|13-preInit
202: // write pre-init user code here
203: splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|13-getter|1|13-postInit
204: splashScreen.setTitle("splashScreen");
205: splashScreen.setCommandListener(this );
206: splashScreen.setText("File Browser Example");//GEN-END:|13-getter|1|13-postInit
207: // write post-init user code here
208: }//GEN-BEGIN:|13-getter|2|
209: return splashScreen;
210: }
211:
212: //</editor-fold>//GEN-END:|13-getter|2|
213:
214: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fileBrowser ">//GEN-BEGIN:|17-getter|0|17-preInit
215: /**
216: * Returns an initiliazed instance of fileBrowser component.
217: * @return the initialized component instance
218: */
219: public FileBrowser getFileBrowser() {
220: if (fileBrowser == null) {//GEN-END:|17-getter|0|17-preInit
221: // write pre-init user code here
222: fileBrowser = new FileBrowser(getDisplay());//GEN-BEGIN:|17-getter|1|17-postInit
223: fileBrowser.setTitle("fileBrowser");
224: fileBrowser.setCommandListener(this );
225: fileBrowser.addCommand(FileBrowser.SELECT_FILE_COMMAND);
226: fileBrowser.addCommand(getExitCommand());//GEN-END:|17-getter|1|17-postInit
227: // write post-init user code here
228: }//GEN-BEGIN:|17-getter|2|
229: return fileBrowser;
230: }
231:
232: //</editor-fold>//GEN-END:|17-getter|2|
233:
234: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textBox ">//GEN-BEGIN:|21-getter|0|21-preInit
235: /**
236: * Returns an initiliazed instance of textBox component.
237: * @return the initialized component instance
238: */
239: public TextBox getTextBox() {
240: if (textBox == null) {//GEN-END:|21-getter|0|21-preInit
241: // write pre-init user code here
242: textBox = new TextBox("textBox", null, 100, TextField.ANY);//GEN-BEGIN:|21-getter|1|21-postInit
243: textBox.addCommand(getBackCommand());
244: textBox.setCommandListener(this );//GEN-END:|21-getter|1|21-postInit
245: // write post-init user code here
246: }//GEN-BEGIN:|21-getter|2|
247: return textBox;
248: }
249:
250: //</editor-fold>//GEN-END:|21-getter|2|
251:
252: /**
253: * Loads text file
254: */
255: private void readFile() {
256: try {
257: FileConnection textFile = fileBrowser.getSelectedFile();
258: getTextBox().setString("");
259: InputStream fis = textFile.openInputStream();
260: byte[] b = new byte[1024];
261: int length = fis.read(b, 0, 1024);
262: fis.close();
263: if (length > 0) {
264: textBox.setString(new String(b, 0, length));
265: }
266: } catch (IOException ex) {
267: ex.printStackTrace();
268: }
269: }
270:
271: /**
272: * Returns a display instance.
273: * @return the display instance.
274: */
275: public Display getDisplay() {
276: return Display.getDisplay(this );
277: }
278:
279: /**
280: * Exits MIDlet.
281: */
282: public void exitMIDlet() {
283: switchDisplayable(null, null);
284: destroyApp(true);
285: notifyDestroyed();
286: }
287:
288: /**
289: * Called when MIDlet is started.
290: * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
291: */
292: public void startApp() {
293: if (midletPaused) {
294: resumeMIDlet();
295: } else {
296: initialize();
297: startMIDlet();
298: }
299: midletPaused = false;
300: }
301:
302: /**
303: * Called when MIDlet is paused.
304: */
305: public void pauseApp() {
306: midletPaused = true;
307: }
308:
309: /**
310: * Called to signal the MIDlet to terminate.
311: * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
312: */
313: public void destroyApp(boolean unconditional) {
314: }
315:
316: }
|