[It-codfisc-j2me-loginfo] CodiceFiscale/src/uk/co/marcoratto/j2me/codicefiscale PrefsForm.java, NO
Brought to you by:
marcoratto
Update of /cvsroot/it-codfisc-j2me/CodiceFiscale/src/uk/co/marcoratto/j2me/codicefiscale In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv29531 Modified Files: CodiceFiscaleMIDlet.java FileComuniCsvSingleton.java FileProvinceCsvSingleton.java SavedForm.java Added Files: PrefsForm.java Log Message: Updated --- NEW FILE: PrefsForm.java --- /* * Copyright (C) 2007 Marco Ratto * * This file is part of the project it-codfisc-j2me. * * it-codfisc-j2me is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * any later version. * * it-codfisc-j2me is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with it-codfisc-j2me; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package uk.co.marcoratto.j2me.codicefiscale; import java.io.*; import java.util.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.wireless.messaging.*; import javax.microedition.rms.*; import uk.co.marcoratto.j2me.i18n.I18N; import uk.co.marcoratto.j2me.utils.*; import uk.co.marcoratto.j2me.codicefiscale.*; public class PrefsForm extends Form implements CommandListener { private final CodiceFiscaleMIDlet parent; private Command prefsCommandCancel = new Command(I18N.getInstance().translate("back"), Command.BACK, 1); private Command prefsCommandConfirm = new Command(I18N.getInstance().translate("prefs.button.confirm"), Command.STOP, 1); private ChoiceGroup provincia = new ChoiceGroup(I18N.getInstance().translate("prefs.label.province"), ChoiceGroup.POPUP); private Store store; public PrefsForm(CodiceFiscaleMIDlet parent) { super(I18N.getInstance().translate("prefs.title")); this.parent = parent; // init UI provincia.deleteAll(); Provincia[] elenco = FileProvinceCsvSingleton.getInstance().getElenco(); for (int j=0; j<elenco.length; j++) { provincia.append(elenco[j].getName(), null); } append(provincia); addCommand(prefsCommandCancel); addCommand(prefsCommandConfirm); // create the form setCommandListener(this); try { store = new Store(parent.RS_PREFS, true); String s = store.read(1); if (s != null) { provincia.setSelectedIndex(Integer.parseInt(s, 10), true); } } catch (Exception e) { // Ignore } finally { try { store.close(); } catch (Exception e) { // Ignore } } } public void commandAction(Command cmd, Displayable displayable) { if (cmd == prefsCommandCancel) { parent.mainMenu(); } else if (cmd == prefsCommandConfirm) { try { store = new Store(parent.RS_PREFS, true); store.deleteAll(); } catch (Exception e) { // Ignore } finally { try { store.close(); } catch (Exception e) { // Ignore } } try { store = new Store(parent.RS_PREFS, true); store.insert(this.provincia.getSelectedIndex()); } catch (Exception e) { // Ignore } finally { try { store.close(); } catch (Exception e) { // Ignore } } this.parent.showMessage(I18N.getInstance().translate("alert.prefs_saved")); } } } Index: FileComuniCsvSingleton.java =================================================================== RCS file: /cvsroot/it-codfisc-j2me/CodiceFiscale/src/uk/co/marcoratto/j2me/codicefiscale/FileComuniCsvSingleton.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileComuniCsvSingleton.java 6 Sep 2007 08:49:38 -0000 1.2 --- FileComuniCsvSingleton.java 7 Jan 2008 18:07:20 -0000 1.3 *************** *** 52,56 **** v = (Vector) this.elencoComuni.get(siglaProvincia); } else { ! System.out.println("AmbienteSingleton.getInstance():Devo leggere i comuni della provincia " + siglaProvincia); InputStream is = null; try { --- 52,56 ---- v = (Vector) this.elencoComuni.get(siglaProvincia); } else { ! System.out.println("FileComuniCsvSingleton.getInstance():Devo leggere i comuni della provincia " + siglaProvincia); InputStream is = null; try { *************** *** 62,66 **** if (b[0] == 10) { String s = str.toString(); - // System.out.println("AmbienteSingleton.getInstance():" + s); Comune comune = new Comune(); comune.setName(s.substring(0, s.indexOf(","))); --- 62,65 ---- Index: FileProvinceCsvSingleton.java =================================================================== RCS file: /cvsroot/it-codfisc-j2me/CodiceFiscale/src/uk/co/marcoratto/j2me/codicefiscale/FileProvinceCsvSingleton.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileProvinceCsvSingleton.java 6 Sep 2007 08:49:38 -0000 1.2 --- FileProvinceCsvSingleton.java 7 Jan 2008 18:07:20 -0000 1.3 *************** *** 27,53 **** private static FileProvinceCsvSingleton istanza = null; ! Provincia seq[]; private FileProvinceCsvSingleton() { } public static FileProvinceCsvSingleton getInstance() { if (istanza == null) { - try { istanza = new FileProvinceCsvSingleton(); ! istanza.init(); ! } catch (Exception e) { ! System.out.println(e); ! } } return istanza; } ! protected void init() throws Exception { ! Vector table = new Vector(0, 1); ! System.out.println("AmbienteSingleton.getInstance():Devo leggere l'elenco delle province"); ! InputStream is = null; try { - is = this.getClass().getResourceAsStream("/data/province.csv"); StringBuffer str = new StringBuffer(); byte b[] = new byte[1]; --- 27,53 ---- private static FileProvinceCsvSingleton istanza = null; ! private Provincia seq[]; private FileProvinceCsvSingleton() { + System.out.println("FileProvinceCsvSingleton()"); } public static FileProvinceCsvSingleton getInstance() { if (istanza == null) { istanza = new FileProvinceCsvSingleton(); ! istanza.init(); } return istanza; } ! public Provincia[] getElenco() { ! return seq; ! } ! ! private void init() { ! System.out.println("FileProvinceCsvSingleton.load(): start"); ! InputStream is = this.getClass().getResourceAsStream("/data/province.csv"); ! Vector table = new Vector(0, 1); try { StringBuffer str = new StringBuffer(); byte b[] = new byte[1]; *************** *** 64,68 **** str.append(new String(b)); } ! } int num = table.size(); seq = new Provincia[num]; --- 64,68 ---- str.append(new String(b)); } ! } int num = table.size(); seq = new Provincia[num]; *************** *** 82,89 **** } } - - public Provincia[] getElenco() { - return seq; - } - } --- 82,84 ---- Index: SavedForm.java =================================================================== RCS file: /cvsroot/it-codfisc-j2me/CodiceFiscale/src/uk/co/marcoratto/j2me/codicefiscale/SavedForm.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SavedForm.java 6 Sep 2007 08:49:38 -0000 1.1 --- SavedForm.java 7 Jan 2008 18:07:20 -0000 1.2 *************** *** 35,42 **** private final CodiceFiscaleMIDlet parent; ! private Command savedCommandBack = new Command(I18N.getInstance().translate("back"), Command.BACK, 0); ! private Command commandDelete = new Command(I18N.getInstance().translate("saved.button.delete"), Command.STOP, 2); ! private Command commandDeleteAll = new Command(I18N.getInstance().translate("saved.button.deleteAll"), Command.STOP, 2); ! private Command commandSelect = new Command(I18N.getInstance().translate("saved.button.select"), Command.STOP, 2); private Store store; --- 35,42 ---- private final CodiceFiscaleMIDlet parent; ! private Command savedCommandBack = new Command(I18N.getInstance().translate("back"), Command.BACK, 1); ! private Command commandDelete = new Command(I18N.getInstance().translate("saved.button.delete"), Command.STOP, 1); ! private Command commandDeleteAll = new Command(I18N.getInstance().translate("saved.button.deleteAll"), Command.STOP, 1); ! // private Command commandSelect = new Command(I18N.getInstance().translate("saved.button.select"), Command.OK, 1); private Store store; *************** *** 47,51 **** // init UI addCommand(savedCommandBack); - addCommand(commandSelect); addCommand(commandDelete); addCommand(commandDeleteAll); --- 47,50 ---- *************** *** 55,59 **** try { ! store = new Store(parent.RECORD_STORE, false); for (int i=1; i <= store.size(); i++) { String temp = store.read(i); --- 54,58 ---- try { ! store = new Store(parent.RS_LIST, false); for (int i=1; i <= store.size(); i++) { String temp = store.read(i); *************** *** 73,112 **** public void commandAction(Command cmd, Displayable displayable) { if (cmd == savedCommandBack) { parent.mainMenu(); return; - } else if (cmd == commandSelect) { - String[] fields = Utility.split(this.getString(this.getSelectedIndex()), ';'); - if (fields == null) { - parent.showMessage(I18N.getInstance().translate("alert.saved.record_error")); - return; - } - try { - parent.cf = new CodiceFiscale(); - parent.cf.setCognome(fields[1]); - parent.cf.setNome(fields[2]); - parent.cf.setSesso(fields[3]); - parent.cf.setDataNascita(fields[4], fields[5], fields[6]); - parent.cf.setProvinciaLuogoNascita(fields[7]); - parent.cf.setLuogoNascita(fields[8]); - parent.cf.setCodiceLuogoNascita(fields[9]); - parent.cf.setIndiceSesso(Integer.parseInt(fields[10], 10)); - parent.cf.setIndiceProvinciaLuogoNascita(Integer.parseInt(fields[11], 10)); - parent.cf.setIndiceLuogoNascita(Integer.parseInt(fields[12], 10)); - parent.cf.calcola(); - if (fields[0].equalsIgnoreCase(parent.cf.getCodiceFiscale()) == false) { - parent.cf = null; - parent.showMessage(I18N.getInstance().translate("alert.saved.codice_fiscale_error")); - } else { - parent.showMessage(I18N.getInstance().translate("alert.record_readed")); - } - } catch (Exception e) { - e.printStackTrace(); - parent.cf = null; - parent.showMessage(I18N.getInstance().translate("alert.saved.codice_fiscale_exception")); - } } else if (cmd == commandDelete) { try { ! store = new Store(parent.RECORD_STORE, false); store.delete(this.getSelectedIndex()+1); delete(this.getSelectedIndex()); --- 72,82 ---- public void commandAction(Command cmd, Displayable displayable) { + System.out.println("commandAction(Command, Displayable):" + cmd.getCommandType()); if (cmd == savedCommandBack) { parent.mainMenu(); return; } else if (cmd == commandDelete) { try { ! store = new Store(parent.RS_LIST, false); store.delete(this.getSelectedIndex()+1); delete(this.getSelectedIndex()); *************** *** 121,125 **** } else if (cmd == commandDeleteAll) { try { ! store = new Store(parent.RECORD_STORE, false); store.deleteAll(); this.deleteAll(); --- 91,95 ---- } else if (cmd == commandDeleteAll) { try { ! store = new Store(parent.RS_LIST, false); store.deleteAll(); this.deleteAll(); *************** *** 132,136 **** --- 102,142 ---- } } + } else { + if (this.getSelectedIndex() >= 0) { + this.loadData(this.getSelectedIndex()); + } } } + + private void loadData(int idx) { + String[] fields = Utility.split(this.getString(idx), ';'); + if (fields == null) { + parent.showMessage(I18N.getInstance().translate("alert.saved.record_error")); + return; + } + try { + parent.cf = new CodiceFiscale(); + parent.cf.setCognome(fields[1]); + parent.cf.setNome(fields[2]); + parent.cf.setSesso(fields[3]); + parent.cf.setDataNascita(fields[4], fields[5], fields[6]); + parent.cf.setProvinciaLuogoNascita(fields[7]); + parent.cf.setLuogoNascita(fields[8]); + parent.cf.setCodiceLuogoNascita(fields[9]); + parent.cf.setIndiceSesso(Integer.parseInt(fields[10], 10)); + parent.cf.setIndiceProvinciaLuogoNascita(Integer.parseInt(fields[11], 10)); + parent.cf.setIndiceLuogoNascita(Integer.parseInt(fields[12], 10)); + parent.cf.calcola(); + if (fields[0].equalsIgnoreCase(parent.cf.getCodiceFiscale()) == false) { + parent.cf = null; + parent.showMessage(I18N.getInstance().translate("alert.saved.codice_fiscale_error")); + } else { + parent.showMessage(I18N.getInstance().translate("alert.record_readed")); + } + } catch (Exception e) { + e.printStackTrace(); + parent.cf = null; + parent.showMessage(I18N.getInstance().translate("alert.saved.codice_fiscale_exception")); + } + } } Index: CodiceFiscaleMIDlet.java =================================================================== RCS file: /cvsroot/it-codfisc-j2me/CodiceFiscale/src/uk/co/marcoratto/j2me/codicefiscale/CodiceFiscaleMIDlet.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** CodiceFiscaleMIDlet.java 6 Sep 2007 08:49:38 -0000 1.2 --- CodiceFiscaleMIDlet.java 7 Jan 2008 18:07:20 -0000 1.3 *************** *** 40,43 **** --- 40,44 ---- private SavedForm savedForm; + private PrefsForm prefsForm; private Store store; *************** *** 53,57 **** private TextBox messageBox; ! final static String RECORD_STORE = "CodiceFiscaleRS"; /** A choice group with available operations. */ --- 54,59 ---- private TextBox messageBox; ! final static String RS_LIST = "CodiceFiscaleListRS"; ! final static String RS_PREFS = "CodiceFiscalePrefsRS"; /** A choice group with available operations. */ *************** *** 88,91 **** --- 90,95 ---- public CodiceFiscaleMIDlet() { + FileProvinceCsvSingleton.getInstance(); + // Alert form alert = new Alert(I18N.getInstance().translate("alert.title"), "", null, AlertType.ERROR); *************** *** 106,110 **** public void startApp() { // Load Singletons... ! FileComuniCsvSingleton.getInstance().getElenco(FileProvinceCsvSingleton.getInstance().getElenco()[0].getCode()); // Menu --- 110,130 ---- public void startApp() { // Load Singletons... ! int idxProv = 0; ! try { ! store = new Store(RS_PREFS, true); ! String s = store.read(1); ! if (s != null) { ! idxProv = Integer.parseInt(s, 10); ! } ! } catch (Exception e) { ! // Ignore ! } finally { ! try { ! store.close(); ! } catch (Exception e) { ! // Ignore ! } ! } ! FileComuniCsvSingleton.getInstance().getElenco(FileProvinceCsvSingleton.getInstance().getElenco()[idxProv].getCode()); // Menu *************** *** 121,124 **** --- 141,145 ---- menu.append(I18N.getInstance().translate("menu.send.sms"), null); menu.append(I18N.getInstance().translate("menu.saved"), null); + menu.append(I18N.getInstance().translate("menu.preferences"), null); menu.append(I18N.getInstance().translate("menu.help"), null); menu.append(I18N.getInstance().translate("menu.info"), null); *************** *** 215,223 **** dataNascita.setDate(new Date()); sex.setSelectedIndex(0, true); ! provincia.setSelectedIndex(0, true); ! Provincia[] elenco = FileProvinceCsvSingleton.getInstance().getElenco(); ! siglaProvincia.setString(elenco[0].getCode()); ! this.loadComuni(elenco[0].getCode()); ! codiceComune.setString(FileComuniCsvSingleton.getInstance().getElenco(siglaProvincia.getString())[0].getCode()); } else if (c == verifyCommandBack) { mainMenu(); --- 236,242 ---- dataNascita.setDate(new Date()); sex.setSelectedIndex(0, true); ! ! this.loadProvince(); ! } else if (c == verifyCommandBack) { mainMenu(); *************** *** 243,252 **** break; case 4: ! displayHelp(); break; case 5: displayInfo(); break; ! case 6: displayAbout(); break; --- 262,274 ---- break; case 4: ! displayPrefs(); break; case 5: + displayHelp(); + break; + case 6: displayInfo(); break; ! case 7: displayAbout(); break; *************** *** 322,330 **** ); - this.loadProvince(); provincia.setItemCommandListener(this); formCalculate.setCommandListener(this); } if (this.cf != null) { System.out.println("CodiceFiscale non nullo."); --- 344,352 ---- ); provincia.setItemCommandListener(this); formCalculate.setCommandListener(this); } + if (this.cf != null) { System.out.println("CodiceFiscale non nullo."); *************** *** 342,349 **** System.out.println("CodiceFiscale nullo."); dataNascita.setDate(new Date()); ! Provincia[] elenco = FileProvinceCsvSingleton.getInstance().getElenco(); ! siglaProvincia.setString(elenco[0].getCode()); ! this.loadComuni(elenco[0].getCode()); ! codiceComune.setString(FileComuniCsvSingleton.getInstance().getElenco(siglaProvincia.getString())[0].getCode()); } --- 364,374 ---- System.out.println("CodiceFiscale nullo."); dataNascita.setDate(new Date()); ! ! this.loadProvince(); ! ! // Provincia[] elenco = FileProvinceCsvSingleton.getInstance().getElenco(); ! // siglaProvincia.setString(elenco[0].getCode()); ! // this.loadComuni(elenco[0].getCode()); ! // codiceComune.setString(FileComuniCsvSingleton.getInstance().getElenco(siglaProvincia.getString())[0].getCode()); } *************** *** 366,374 **** int size = 0; try { ! store = new Store(RECORD_STORE, true); size = store.size(); } catch (Exception e) { } finally { - if (store != null) { try { store.close(); --- 391,398 ---- int size = 0; try { ! store = new Store(RS_LIST, true); size = store.size(); } catch (Exception e) { } finally { try { store.close(); *************** *** 376,381 **** // Ignore } - store = null; - } } if (size < 1) { --- 400,403 ---- *************** *** 387,390 **** --- 409,417 ---- } + private void displayPrefs() { + prefsForm = new PrefsForm(this); + Display.getDisplay(this).setCurrent(prefsForm); + } + public void destroyApp(boolean b) { notifyDestroyed(); *************** *** 399,416 **** } ! private void loadProvince() { ! provincia.deleteAll(); ! Provincia[] elenco = FileProvinceCsvSingleton.getInstance().getElenco(); ! for (int j=0; j<elenco.length; j++) { ! provincia.append(elenco[j].getName(), null); ! } } ! private void loadComuni(String siglaProvincia) { ! comune.deleteAll(); ! Comune[] elenco = FileComuniCsvSingleton.getInstance().getElenco(siglaProvincia); ! for (int j=0; j<elenco.length; j++) { ! comune.append(elenco[j].getName(), null); ! } } --- 426,447 ---- } ! void loadProvince() { ! // provincia.deleteAll(); ! // Provincia[] elenco = FileProvinceCsvSingleton.getInstance().getElenco(); ! // for (int j=0; j<elenco.length; j++) { ! // provincia.append(elenco[j].getName(), null); ! // } ! ProcessProvince process = new ProcessProvince(this); ! process.start(); } ! void loadComuni(String siglaProvincia) { ! // comune.deleteAll(); ! // Comune[] elenco = FileComuniCsvSingleton.getInstance().getElenco(siglaProvincia); ! // for (int j=0; j<elenco.length; j++) { ! // comune.append(elenco[j].getName(), null); ! // } ! ProcessComuni process = new ProcessComuni(this, siglaProvincia); ! process.start(); } *************** *** 506,510 **** comune.getSelectedIndex(); try { ! store = new Store(RECORD_STORE, true); int idx = store.find(';', cf.getCodiceFiscale(), 0); if (idx == 0) { --- 537,541 ---- comune.getSelectedIndex(); try { ! store = new Store(RS_LIST, true); int idx = store.find(';', cf.getCodiceFiscale(), 0); if (idx == 0) { *************** *** 518,522 **** showMessage(I18N.getInstance().translate("alert.record_save_exception")); } finally { - if (store != null) { try { store.close(); --- 549,552 ---- *************** *** 525,531 **** } store = null; - } } } } --- 555,648 ---- } store = null; } } } + class ProcessProvince implements Runnable { + private CodiceFiscaleMIDlet MIDlet; + + public ProcessProvince(CodiceFiscaleMIDlet MIDlet) { + this.MIDlet = MIDlet; + } + + public void run() { + try { + transmit (); + } catch (Exception error) { + System.err.println(error.toString()); + } + } + + public void start() { + Thread thread = new Thread(this); + + try { + thread.start(); + } catch (Exception error) { + } + } + + private void transmit() throws IOException { + Store store = null; + this.MIDlet.provincia.deleteAll(); + Provincia[] elenco = FileProvinceCsvSingleton.getInstance().getElenco(); + for (int j=0; j<elenco.length; j++) { + this.MIDlet.provincia.append(elenco[j].getName(), null); + } + int idxProv = 0; + try { + store = new Store(this.MIDlet.RS_PREFS, true); + String s = store.read(1); + if (s != null) { + idxProv = Integer.parseInt(s, 10); + } + } catch (Exception e) { + // Ignore + } finally { + try { + store.close(); + } catch (Exception e) { + // Ignore + } + } + this.MIDlet.provincia.setSelectedIndex(idxProv, true); + this.MIDlet.siglaProvincia.setString(elenco[idxProv].getCode()); + this.MIDlet.loadComuni(elenco[idxProv].getCode()); + } + } + + class ProcessComuni implements Runnable { + private CodiceFiscaleMIDlet MIDlet; + private String siglaProvincia; + + public ProcessComuni(CodiceFiscaleMIDlet MIDlet, String siglaProvincia) { + this.MIDlet = MIDlet; + this.siglaProvincia = siglaProvincia; + } + + public void run() { + try { + transmit (); + } catch (Exception error) { + System.err.println(error.toString()); + } + } + + public void start() { + Thread thread = new Thread(this); + try { + thread.start(); + } catch (Exception error) { + } + } + + private void transmit() throws IOException { + this.MIDlet.comune.deleteAll(); + Comune[] elenco = FileComuniCsvSingleton.getInstance().getElenco(siglaProvincia); + for (int j=0; j<elenco.length; j++) { + this.MIDlet.comune.append(elenco[j].getName(), null); + } + this.MIDlet.codiceComune.setString(elenco[0].getCode()); + } + } + |