FINAL: CÓDIGO-FONTE DA MIDLET Para fins de conferência, seguem os códigos-fontes alterados. Este código-fonte encontra-se no arquivo chamado ColetorMIDlet.java: package coletor; import br.edu.estacio.produto; import br.edu.estacio.produtopersistencia; import java.util.vector; import javax.microedition.lcdui.*; import javax.microedition.midlet.midlet; /** * Classe visual para cadastro ou edição de produtos. * @author Estácio (Prof. Michel Masiero de Aquino). */ public class ColetorMIDlet extends MIDlet implements CommandListener { private final ProdutoPersistencia produtopersistencia; private Produto produto; private final Vector produtovector; private boolean midletpaused = false; private Command saircommand; private Command salvarcommand; private Command listarcommand; private Command voltarcommand; private Command editarcommand; private Form produtoform; private TextField nometextfield; private TextField quantidadetextfield; private TextField codigotextfield; private List produtolist; private Alert produtoalert; public ColetorMIDlet() { this.produtopersistencia = new ProdutoPersistencia(); this.produtovector = new Vector(); private void initialize() { public void startmidlet() { public void resumemidlet() { public void switchdisplayable(alert alert, Displayable nextdisplayable) { Display display = getdisplay(); if (alert == null) { display.setcurrent(nextdisplayable); else { display.setcurrent(alert, nextdisplayable); public void commandaction(command command, Displayable displayable) { if (displayable == produtoform) { if (command == listarcommand) { if (true == this.produtopersistencia.abrirrecordstore()) { this.produtopersistencia.iniciarnavagacaoproduto(); this.produtovector.removeallelements(); this.getprodutolist().deleteall(); while (true == this.produtopersistencia.irparaproximonavegavel()) { this.produtovector.addelement(this.produtopersistencia.getproduto()); this.getprodutolist().append(this.produtopersistencia.getproduto().getnome(), null); this.produtopersistencia.fecharrecordstore();
switchdisplayable(null, getprodutolist()); else if (command == saircommand) { exitmidlet(); else if (command == salvarcommand) { if (true == this.produtopersistencia.abrirrecordstore()) { String codigo = this.codigotextfield.getstring().trim(); String nome = this.nometextfield.getstring().trim(); String quantidade = this.quantidadetextfield.getstring().trim(); if ((0 < codigo.length()) && (0 < nome.length()) && (0 < quantidade.length())) { if (null == this.produto) { this.produto = new Produto(); this.produto.setcodigo(codigo); this.produto.setnome(nome); try { this.produto.setquantidade(integer.parseint(quantidade)); catch(numberformatexception nfex) { this.produto.setquantidade(0); boolean salvo = this.produtopersistencia.salvarproduto(this.produto); this.produto = null; this.produtopersistencia.fecharrecordstore(); this.codigotextfield.setstring(""); this.nometextfield.setstring(""); this.quantidadetextfield.setstring(""); this.getprodutoalert().setstring((true == salvo)? "Salvo com sucesso!" : "Erro ao salvar!"); else { this.getprodutoalert().setstring("preencha os campos antes de salvar!"); switchdisplayable(getprodutoalert(), getprodutoform()); else if (displayable == produtolist) { if (command == List.SELECT_COMMAND) { produtolistaction(); else if (command == editarcommand) { if (-1 < this.getprodutolist().getselectedindex()) { Produto produtoselecionado = (Produto)this.produtoVector.elementAt(this.getProdutoList().getSelectedIndex()); this.produto = new Produto(); this.produto.setid(produtoselecionado.getid()); this.produto.setcodigo(produtoselecionado.getcodigo()); this.produto.setnome(produtoselecionado.getnome()); this.produto.setquantidade(produtoselecionado.getquantidade()); this.codigotextfield.setstring(this.produto.getcodigo()); this.nometextfield.setstring(this.produto.getnome()); this.quantidadetextfield.setstring(integer.tostring(this.produto.getquantidade())); else if (command == voltarcommand) { public Command getsaircommand() { if (saircommand == null) { saircommand = new Command("Sair", Command.EXIT, 0); return saircommand; public Form getprodutoform() { if (produtoform == null) { produtoform = new Form("Cadastro de Produtos", new Item[]{getCodigoTextField(), getnometextfield(), getquantidadetextfield()); produtoform.addcommand(getsaircommand()); produtoform.addcommand(getsalvarcommand()); produtoform.addcommand(getlistarcommand()); produtoform.setcommandlistener(this); return produtoform;
public Command getsalvarcommand() { if (salvarcommand == null) { salvarcommand = new Command("Salvar", Command.SCREEN, 0); return salvarcommand; public Command getlistarcommand() { if (listarcommand == null) { listarcommand = new Command("Listar", Command.SCREEN, 0); return listarcommand; public Command getvoltarcommand() { if (voltarcommand == null) { voltarcommand = new Command("Voltar", Command.BACK, 0); return voltarcommand; public Command geteditarcommand() { if (editarcommand == null) { editarcommand = new Command("Editar", Command.SCREEN, 0); return editarcommand; public List getprodutolist() { if (produtolist == null) { produtolist = new List("Listagem de Produtos", Choice.IMPLICIT); produtolist.addcommand(getvoltarcommand()); produtolist.addcommand(geteditarcommand()); produtolist.setcommandlistener(this); return produtolist; public void produtolistaction() { String selectedstring = getprodutolist().getstring(getprodutolist().getselectedindex()); public Alert getprodutoalert() { if (produtoalert == null) { produtoalert = new Alert("Informa\u00E7\u00E3o"); produtoalert.settimeout(alert.forever); return produtoalert; public TextField getcodigotextfield() { if (codigotextfield == null) { codigotextfield = new TextField("C\u00F3digo:", null, 32, TextField.ANY); return codigotextfield; public TextField getnometextfield() { if (nometextfield == null) { nometextfield = new TextField("Descri\u00E7\u00E3o:", null, 32, TextField.ANY); return nometextfield; public TextField getquantidadetextfield() { if (quantidadetextfield == null) { quantidadetextfield = new TextField("Quantidade:", null, 4, TextField.NUMERIC); return quantidadetextfield; public Display getdisplay() { return Display.getDisplay(this);
public void exitmidlet() { switchdisplayable(null, null); destroyapp(true); notifydestroyed();