FileChooser com LWUIT

O LWUIT é um framework desenvolvido pela Sun Microsystems para facilitar o desenvolvimento de interface gráficas em celulares. Ele é bem parecido com o Swing para desktop.

Fiquei conhecendo esse framework graças ao Antônio Marin Neto em seu blog, que aborda características do LWUIT como Recursos, Estabilidade, Desempenho e outros. Eu achei o LWUIT muito fácil de usar e com vários recursos, embora o tempo para iniciar um programa feito nele é um pouco grande. Nada que um loading screen não resolva.

A criação do FileChooser veio da necessidade de trabalhar com arquivos em um projeto que estou desenvolvendo para um Wiki móvel. Em breve disponibilizarei os fontes do projeto como open source para o pessoal poder contribuir.

O primeiro passo foi utilizar a JSR 75 para manipular os arquivos. Para verificar se o dispositivo tem suporte a essa JSR, você pode utilizar o seguinte código:

[cc lang="java"]
public static boolean isJSR75Available() {
// Verifica se a JSR 75 está disponível
return System.getProperty("microedition.io.file.FileConnection.version") != null;
}
[/cc]
Os próximos código terão referências às classes Language e ExceptionHandler. A primeira foi utilizada para localização utilizando o editor de resources do LWUIT e a segunda é responsável por processar exceções. Posteriormente eu explico o funcionamento dessas duas classes. Para funcionar o código sem elas, basta você realizar o tratamento de erros da sua maneira e substituir a chamada de Language.getLocalizedString("...") por alguma outra String.

Agora vamos criar uma classe utilitária (FileUtil) para trabalhar com arquivos. É necessário pegar o separador de arquivos, qual é a string de diretório que representa o nível superior e o prefixo para acessar arquivos.
[cc lang="java"]
public final static String FILE_SEPARATOR = (System.getProperty("file.separator") != null) ? System.getProperty("file.separator") : "/";
public final static String TOP_DIR = "..";
public final static String FILE_PREFIX = "file:///";
[/cc]
Para verificar se o caminho passado é uma raiz, utilizamos um workaround.
[cc lang="java"]
public static boolean isRoot(String path) {
// Operação sobre o path ao invés de verificar na lista de roots
// Verifica se o primeiro separador de arquivo é o último caractere
if (path != null && path.indexOf(FileUtil.FILE_SEPARATOR) != path.length() - 1) {
return false;
}
return true;
}
[/cc]
Depois é só utilizarmos o Connector.open para obter um FileConnection e executar as operações em arquivos.

Com isso temos o seguinte arquivo:
[cc lang="java"]
package br.com.thiagovespa.mobwiki.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

/**
* Classe para manipulação de arquivos
*
* @author Thiago Galbiatti Vespa
*
*/
public final class FileUtil {
public final static String FILE_SEPARATOR = (System
.getProperty("file.separator") != null) ? System
.getProperty("file.separator") : "/";

public final static String TOP_DIR = "..";
public final static String FILE_PREFIX = "file:///";

/**
* Verifica se o caminho é uma raiz baseado na string passada, pode resultar
* em resultado falso caso seja passada um caminho inválido
*
* @param path
* caminho a ser verificado
* @return verdadeiro se o formato da string for raiz
*/
public static boolean isRoot(String path) {
// Operação sobre o path ao invés de verificar na lista de roots
// Verifica se o primeiro separador de arquivo é o último caractere
if (path != null
&& path.indexOf(FileUtil.FILE_SEPARATOR) != path.length() - 1) {
return false;
}
return true;
}

/**
* Cria um diretório
*
* @param path
* diretório a ser criado
* @return true se o diretorio foi criado, false caso contrário
*/
public static boolean createDir(String path) {
FileConnection newDir = null;
try {
newDir = (FileConnection) Connector.open(FileUtil.FILE_PREFIX
+ path + FileUtil.FILE_SEPARATOR, Connector.READ_WRITE);
if (!newDir.exists()) {
newDir.mkdir();
return true;
} else {
ExceptionHandler.handleException(new IOException(Language
.getLocalizedString("dirAlreadyExist")));
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
} catch (SecurityException e) {
ExceptionHandler.handleException(e, Language
.getLocalizedString("securityExceptionDesc"));
} finally {
if (newDir != null) {
try {
newDir.close();
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
}
return false;
}

/**
* Cria um novo arquivo
*
* @param path
* arquivo a ser criado
* @return true se o arquivo foi criado, false caso contrário
*/
public static boolean createNewFile(String path, String content) {
FileConnection newFile = null;
DataOutputStream dos = null;
try {
newFile = (FileConnection) Connector.open(FileUtil.FILE_PREFIX
+ path, Connector.READ_WRITE);
if (!newFile.exists()) {
newFile.create();
dos = newFile.openDataOutputStream();
dos.writeUTF(content);
dos.flush();
return true;
} else {
ExceptionHandler
.handleException(new IOException(Language
.getLocalizedString("file")
+ path
+ Language
.getLocalizedString("already exists!")));
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
} catch (SecurityException e) {
ExceptionHandler.handleException(e, Language
.getLocalizedString("securityExceptionDesc"));
} finally {
try {
if (newFile != null) {
newFile.close();
}
if (dos != null) {
dos.close();
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
return false;
}

/**
* Atualiza arquivo
*
* @param path
* arquivo a ser atualizado
* @return true se o arquivo foi atualizado, false caso contrário
*/
public static boolean updateFile(String path, String content) {
FileConnection file = null;
DataOutputStream dos = null;
try {
file = (FileConnection) Connector.open(FileUtil.FILE_PREFIX + path,
Connector.READ_WRITE);
if (!file.exists()) {
// Cria se não existe
file.create();
} else {
// Trunca se já existe
file.truncate(0);
}
dos = file.openDataOutputStream();
dos.writeUTF(content);
dos.flush();
return true;
} catch (IOException e) {
ExceptionHandler.handleException(e);
} catch (SecurityException e) {
ExceptionHandler.handleException(e, Language
.getLocalizedString("securityExceptionDesc"));
} finally {
try {
if (file != null) {
file.close();
}
if (dos != null) {
dos.close();
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
return false;
}

public static boolean existsFile(String path) {
FileConnection file = null;
try {
file = (FileConnection) Connector.open(FileUtil.FILE_PREFIX + path,
Connector.READ);
if (file.exists()) {
return true;
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
} catch (SecurityException e) {
ExceptionHandler.handleException(e, Language
.getLocalizedString("securityExceptionDesc"));
} finally {
try {
if (file != null) {
file.close();
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
return false;
}

/**
* Lê um arquivo
*
* @param path
* arquivo a ser lido
* @return conteúdo do arquivo
*/
public static String loadFile(String path) {
FileConnection file = null;
DataInputStream dis = null;
String retVal = null;
try {
file = (FileConnection) Connector.open(FileUtil.FILE_PREFIX + path,
Connector.READ);
if (file.exists()) {
dis = file.openDataInputStream();
retVal = dis.readUTF();
} else {
ExceptionHandler.handleException(new IOException("File " + path
+ " do not exists!"));
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
} catch (SecurityException e) {
ExceptionHandler.handleException(e, Language
.getLocalizedString("securityExceptionDesc"));
} finally {
try {
if (file != null) {
file.close();
}
if (dis != null) {
dis.close();
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
return retVal;
}

/**
* Lê um diretório
*
* @param path
* diretório a ser lido
* @return lista
*/
public static String[] loadDir(String path, String pattern) {
FileConnection dir = null;
String[] retVal = null;
Vector files = new Vector();
try {
dir = (FileConnection) Connector.open(FileUtil.FILE_PREFIX + path,
Connector.READ);
if (dir.exists()) {
Enumeration en = dir.list(pattern, true);
while (en.hasMoreElements()) {
String object = (String) en.nextElement();
files.addElement(object);
}
if (files != null) {
retVal = new String[files.size()];
files.copyInto(retVal);
}
} else {
ExceptionHandler.handleException(new IOException("File " + path
+ " do not exists!"));
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
} catch (SecurityException e) {
ExceptionHandler.handleException(e, Language
.getLocalizedString("securityExceptionDesc"));
} finally {
try {
if (dir != null) {
dir.close();
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
return retVal;
}

/**
* Remove um diretório
*
* @param path
* diretório a ser removido
* @return true se removido e false caso contrário
*/
public static boolean deleteDir(String path) {
FileConnection dir = null;
try {
dir = (FileConnection) Connector.open(FileUtil.FILE_PREFIX + path,
Connector.READ_WRITE);
if (dir.exists()) {
Enumeration en = dir.list("*", true);
while (en.hasMoreElements()) {
String object = (String) en.nextElement();
if (object.endsWith(FileUtil.FILE_SEPARATOR)) {
deleteDir(path + object);
} else {
deleteFile(path + object);
}
}
dir.delete();
dir.close();
return true;
} else {
ExceptionHandler.handleException(new IOException(Language
.getLocalizedString("file")
+ path + Language.getLocalizedString("doNotExist")));
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
} catch (SecurityException e) {
ExceptionHandler.handleException(e, Language
.getLocalizedString("securityExceptionDesc"));
} finally {
try {
if (dir != null) {
dir.close();
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
return false;
}

/**
* Remove um arquivo
*
* @param path
* arquivo a ser removido
* @return true se removido e false caso contrário
*/
public static boolean deleteFile(String path) {
FileConnection file = null;
try {
file = (FileConnection) Connector.open(FileUtil.FILE_PREFIX + path,
Connector.READ_WRITE);
if (file.exists()) {
file.delete();
file.close();
return true;
} else {
ExceptionHandler.handleException(new IOException(Language
.getLocalizedString("file")
+ path + Language.getLocalizedString("doNotExist")));
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
} catch (SecurityException e) {
ExceptionHandler.handleException(e, Language
.getLocalizedString("securityExceptionDesc"));
} finally {
try {
if (file != null) {
file.close();
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
return false;
}

}[/cc]
No próximo post a gente parte para a parte gráfica do FileChooser.

Até a próxima!

Sobre: Thiago Galbiatti Vespa

Thiago Galbiatti Vespa é mestre em Ciências da Computação e Matemática Computacional pela USP e bacharel em Ciências da Computação pela UNESP. Coordenador de projetos do JavaNoroeste, membro do JCP (Java Community Process), consultor Oracle, arquiteto de software de empresas de médio e grande porte, palestrante de vários eventos e colaborador de projetos open source. Possui as certificações: Oracle Certified Master, Java EE 5 Enterprise Architect – Step 1, 2 and 3; Oracle WebCenter Portal 11g Certified Implementation Specialist; Oracle Service Oriented Architecture Infrastructure Implementation Certified Expert; Oracle Certified Professional, Java EE 5 Web Services Developer; Oracle Certified Expert, NetBeans Integrated Development Environment 6.1 Programmer; Oracle Certified Professional, Java Programmer; Oracle Certified Associate, Java SE 5/SE 6