You Are Here:

Community: Wiki

This page was last modified on 7 October 2009, at 16:57.

Alert

From Forum Nokia Wiki

Reviewer Approved   

The following code creates a simple file navigator helps :

- navigate files and directories

- rename files and directories

- delete files and directories

It illustrates using of high-level GUI such as : Form, Alert, List, TextField. Also FileConnectivity API used to.

I test my application on S40_SDK_3rd_Edition_Feature_Pack_2 emulator. Minimal requared heap memory - 64Kb. I try to start with 32Kb - but application crashes with OutOfMemoryError. I try to resolve this by using System.gc(), but it helps only for system types. KVM (kilobyte virtual machine) has no posibilities to unload classes and resolve memory fragmentation issues.

There is 2 classes : RootMIDlet as midlet and RenameForm as form for renaming.

RootMIDlet illustrated below :

package player;
 
import java.io.IOException;
import java.util.Enumeration;
import java.util.Stack;
 
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Ticker;
 
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
 
public class RootMIDlet extends MIDlet implements CommandListener {
private Display disp = Display.getDisplay(this);
private Command backCommand;
private Command openCommand;
private Command renameCommand;
private Command deleteCommand;
private Command exitCommand;
 
private static List list;
private Stack rootDir = new Stack();
private RenameForm form;
public void startApp() {
String title = "Multiple list";
Ticker ticker = new Ticker(title);
list = new List(title, Choice.IMPLICIT, new String[0], null);
 
backCommand = new Command("Back", Command.BACK, 1);
openCommand = new Command("Open", Command.ITEM, 1);
renameCommand = new Command("Rename", Command.ITEM, 1);
deleteCommand = new Command("Delete", Command.ITEM, 1);
exitCommand = new Command("Exit", Command.ITEM, 1);
 
list.addCommand(backCommand);
list.addCommand(openCommand);
list.addCommand(renameCommand);
list.addCommand(deleteCommand);
list.addCommand(exitCommand);
 
list.setCommandListener(this);
list.setTicker(ticker);
 
disp.setCurrent(list);
 
form = RenameForm.getInstance();
form.initForm(list, null, disp);
 
getRootList();
}
 
protected void getRootList() {
String driveString;
try {
Enumeration drives = FileSystemRegistry.listRoots();
list.deleteAll();
 
while (drives.hasMoreElements()) {
driveString = drives.nextElement().toString();
list.append(driveString, null);
}
drives = null;
} catch (SecurityException se) {
form.createExc("SecurityException", se);
} catch (Exception e) {
form.createExc("Exception", e);
}
}
 
protected void getFileList(String path) {
String filename;
try {
FileConnection fc = (FileConnection)
Connector.open("file:///" + path, Connector.READ);
Enumeration filelist = fc.list("*", true); //also hidden
fc.close();
 
list.deleteAll();
 
while (filelist.hasMoreElements()) {
filename = (String) filelist.nextElement();
list.append(filename, null);
}
filelist = null;
} catch (IOException ioe) {
form.createExc("IOException", ioe);
rootDir.pop();
} catch (SecurityException se) {
form.createExc("SecurityException", se);
rootDir.pop();
} catch (Exception e) {
form.createExc("Exception", e);
rootDir.pop();
}
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
}
 
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
this.notifyDestroyed();
} else if (c == backCommand) {
back();
System.gc();//free memory if possible
} else if (c == List.SELECT_COMMAND || c == openCommand) {
openFolder();
System.gc();//free memory if possible
} else if (c == renameCommand) {
rename();
System.gc();//free memory if possible
} else if (c == deleteCommand) {
delete();
System.gc();//free memory if possible
}
}
 
private void back() {
if (rootDir.size() == 1) {
rootDir.pop();
getRootList();
} else if (rootDir.size() > 1) {
rootDir.pop();
getFileList((String) rootDir.peek());
} else {
System.out.println("Unexpected stack size.");
}
}
 
private void openFolder() {
int selected = list.getSelectedIndex();
if (selected > -1) {
String str = list.getString(selected);
if (str.charAt(str.length() - 1) == '/') {
if (rootDir.size() > 0) {
String peek = (String) rootDir.peek();
StringBuffer sb =
new StringBuffer(peek.length() + str.length());
sb.append(peek)
.append(str);
rootDir.addElement(sb.toString());
} else {
rootDir.addElement(str);
}
getFileList((String) rootDir.peek());
}
}
}
 
private void rename() {
if (rootDir.size() > 0) {
form.initForm(list, (String) rootDir.peek(), disp);
disp.setCurrent(form);
}
}
 
private void delete() {
if (rootDir.size() > 0) {
int selected = list.getSelectedIndex();
if (selected > -1) {
String str = list.getString(selected);
String peek = (String) rootDir.peek();
StringBuffer sb =
new StringBuffer(peek.length() + str.length() + 8);
sb.append("file:///")
.append(peek)
.append(str);
try {
FileConnection fc = (FileConnection)
Connector.open(sb.toString(), Connector.READ_WRITE);
fc.delete();
fc.close();
list.delete(selected);
form.createConf("Deleting",
"Deleting process completed successfully!");
} catch (IOException ioe) {
form.createExc("IOException", ioe);
rootDir.pop();
} catch (SecurityException se) {
form.createExc("SecurityException", se);
rootDir.pop();
} catch (Exception e) {
form.createExc("Exception", e);
rootDir.pop();
}
}
}
}
}

RenameForm illustrated below :

package player;
 
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
 
public class RenameForm extends Form implements ItemCommandListener {
 
private static RenameForm instance = null;
private Command saveCommand;
private TextField textfield;
private List list;
private int selected = -1;
private String path;
private Display d;
private Alert alert = new Alert(null);
 
public static RenameForm getInstance() {
if (instance == null) {
instance = new RenameForm();
}
return instance;
}
 
private RenameForm() {
super("Renaming form");
textfield = new TextField("New name:", "", 255, TextField.ANY);
saveCommand = new Command("Save", Command.OK, 1);
 
textfield.addCommand(saveCommand);
textfield.setItemCommandListener(this);
 
this.append(textfield);
}
 
public void initForm(List fileList, String curPath, Display disp) {
this.list = fileList;
this.selected = list.getSelectedIndex();
this.path = curPath;
this.d = disp;
if (selected > -1) {
textfield.setString(list.getString(selected));
}
}
 
public void commandAction(Command c, Item item) {
if (c == saveCommand) {
try {
String oldName = list.getString(selected);
StringBuffer sb =
new StringBuffer(path.length() + oldName.length() + 8);
sb.append("file:///")
.append(path)
.append(oldName);
FileConnection fc = (FileConnection)
Connector.open(sb.toString(), Connector.READ_WRITE);
String str = textfield.getString();
if (oldName.equals(str) || oldName.equals(str + '/')) {
createWarning("Renaming", "This name already in use!");
} else {
if (fc.isDirectory()) {
if (str.charAt(str.length() - 1) == '/') {
fc.rename(str.substring(0, str.length() - 1));
list.set(selected, str, null);
} else {
fc.rename(str);
list.set(selected, str + '/', null);
}
} else {
fc.rename(str);
list.set(selected, str, null);
}
createConf("Renaming", "Renaming process completed successfully!");
}
fc.close();
} catch (IOException ioe) {
createExc("IOException", ioe);
} catch (SecurityException se) {
createExc("SecurityException", se);
} catch (Exception e) {
createExc("Exception", e);
}
}
}
 
public void createConf(String title, String text) {
alert.setTitle(title);
alert.setString(text);
alert.setType(AlertType.CONFIRMATION);
d.setCurrent(alert, list);
}
 
public void createExc(String title, Exception e) {
alert.setTitle(title);
alert.setString(e.getMessage());
alert.setType(AlertType.ERROR);
d.setCurrent(alert, list);
}
 
public void createWarning(String title, String text) {
alert.setTitle(title);
alert.setString(text);
alert.setType(AlertType.WARNING);
d.setCurrent(alert);
}
}

Please inform me if something is wrong or uneffective :) --Pavel4096 12:10, 3 May 2008 (EEST)

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia