Application memo en j2me pour palm treo 500

Soyez le premier à donner votre avis sur cette source.

Vue 12 627 fois - Téléchargée 689 fois

Description

Bonjour,

Je débute en J2ME et je viens de m'acheter un Palm Treo 500 qui tourne sous Windows Mobile 6. Pour une raison que j'ignore, Palm n'a pas porté sous Windows Mobile l'application "memo" qui est celle que j'utilisais le plus sur mes "anciens Palm".

J'ai donc réalisé une petite appli J2ME qui permet de saisir des mémos, de modifier et de faire des recherches parmi les mémos. Les mémos sont stockés en Record Store.

Source / Exemple :


package memo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

	public class Screen extends MIDlet {
				
	    /** A generic way of indicating whether startApp() has previously been called. */
	    private Display display;
	    private Screen midlet;
	    private List memoList;
	    private Vector memoMap;
	    private RecordStore memoStore;
	    private int nbMemo = 0;
	    private String lastSearchString = "";
	    
	    /** Priority of commands relative to others of the same type. */
	    private static final int COMMAND_PRIORITY = 2;
	    private static final int COMMAND_PRIORITY_1 = 1;
	    
	    /** Command specified to initiate the termination of the MIDlet. */

	    private static final Command CMD_EXIT =
	            new Command("Exit", Command.EXIT, COMMAND_PRIORITY);

	    /** Command specified to initiate the launch of the TextEditor screen. */
	    private static final Command CMD_NEW =
	            new Command("New", Command.ITEM, COMMAND_PRIORITY_1);
	    
	    private static final Command CMD_EDIT =
            new Command("Edit", Command.ITEM, COMMAND_PRIORITY);
	        
	    private static final Command CMD_SEARCH =
            new Command("Search", Command.ITEM, COMMAND_PRIORITY);
	
	    private static final Command CMD_DONE =
            new Command("Done", Command.ITEM, COMMAND_PRIORITY);
	    
	    private static final Command CMD_DELETE =
            new Command("Delete", Command.ITEM, COMMAND_PRIORITY);
	   
	    
	    /*

  • Update an existing memo into the RecordStore
  • /
public void updateMemo(Memo memo) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); try { os.writeUTF(memo.getText()); os.close(); } catch (IOException e) { System.out.println(e); } byte[] data = baos.toByteArray(); try { memoStore.setRecord(memo.getId(), data, 0, data.length); } catch (Exception e) { System.err.println(e); } } /*
  • Stores a Memo object into the RecordStore.
  • Returns the Id of the created Record.
  • /
public int storeMemo(Memo memo) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); int Id = -1; try { os.writeUTF(memo.getText()); os.close(); } catch (IOException e) { System.out.println(e); } byte[] data = baos.toByteArray(); try { Id = memoStore.addRecord(data, 0, data.length); } catch (Exception e) { System.err.println(e); } return Id; } /*
  • reads the record #Id from the RecordStore and fill a Memo object with these
  • bytes.
  • Returns the created Memo object
  • /
public Memo getMemo(int Id) { Memo memo = null; try { byte[] bytes = memoStore.getRecord(Id); DataInputStream is = new DataInputStream(new ByteArrayInputStream(bytes)); String texte = is.readUTF(); memo = new Memo(texte,Id); //memo = new Memo("Default text "+Id); } catch (Exception e) { System.err.println(e); } return memo; } /*
  • Deletes the record #Id from the RecordStore
  • /
public void deleteMemo(int Id) { try { memoStore.deleteRecord(Id); } catch (Exception e) { System.err.println(e); } } public void startApp() { if (display == null) { // First time we've been called. display=Display.getDisplay(this); midlet = this; // create the List of all memos memoMap = new Vector(); // create the List of Memo titles being diplayed on the main screen memoList = new List("memo",List.IMPLICIT); // Open and read the record Store memoStore = null; try { RecordEnumeration recordEnum = null; memoStore = RecordStore.openRecordStore(Memo.MEMO_STORE_NAME, true,RecordStore.AUTHMODE_ANY,true); recordEnum = memoStore.enumerateRecords(null,null,false); while (recordEnum.hasPreviousElement()) { int Id = recordEnum.previousRecordId(); // System.out.println("Reading Record Id = "+Id+ " from Record Store"); Memo memo = getMemo(Id); memoMap.addElement(memo); memoList.append(memo.getTitle(),null); nbMemo++; } } catch (RecordStoreException e) { System.err.println(e); } /*
  • Creates the menu of the memo application
  • /
memoList.addCommand(CMD_EXIT); memoList.addCommand(CMD_NEW); memoList.addCommand(CMD_EDIT); memoList.addCommand(CMD_SEARCH); memoList.addCommand(CMD_DELETE); /*
  • Process the actions associated with the menus
  • /
memoList.setCommandListener(new CommandListener() { public void commandAction(Command c, Displayable d) { /*
  • Creation of a new memo
  • /
if (c == CMD_NEW) { // create a new memo object. The Id is not known yet. It will be // known only after the memo has been inserted into the Record Store Memo memo = new Memo("",-1); int Id = storeMemo(memo); memo.setId(Id); // System.out.println("Creating memo Id = "+Id); memoMap.addElement(memo); nbMemo++; memoList.append(memo.getTitle(),null); memoList.setSelectedIndex(nbMemo-1,true); /*
  • Switch to the screen allowing the user to enter the text of the
  • memo
  • /
TextEditor textEditor = new TextEditor(midlet,""); display.setCurrent(textEditor); /*
  • Edit an existing memo
  • /
} else if (c == CMD_EDIT) { if (nbMemo > 0) { int i = memoList.getSelectedIndex(); Memo memo = (Memo)memoMap.elementAt(i); TextEditor textEditor = new TextEditor(midlet,memo.getText()); display.setCurrent(textEditor); } } else if (c == CMD_DELETE) { /*
  • Delete a memo
  • /
if (nbMemo > 0) { int index = memoList.getSelectedIndex(); Memo memo = (Memo)memoMap.elementAt(index); deleteMemo(memo.getId()); memoMap.removeElementAt(index); memoList.delete(index); nbMemo--; display.setCurrent(memoList); } } else if (c == CMD_SEARCH) { /*
  • Create a search box to entering the box to search
  • /
SearchBox searchEditor = new SearchBox(midlet,lastSearchString); display.setCurrent(searchEditor); } else if (c == CMD_DONE) { /*
  • The system was displaying only the list of memo containing
  • a string to search. The Done command restores the normal
  • diplay and menus.
  • /
display.setCurrent(memoList); memoList.removeCommand(CMD_DONE); memoList.removeCommand(CMD_EDIT); memoList.removeCommand(CMD_DELETE); memoList.addCommand(CMD_EXIT); memoList.addCommand(CMD_NEW); memoList.addCommand(CMD_EDIT); memoList.addCommand(CMD_SEARCH); memoList.addCommand(CMD_DELETE); /*
  • Rebuild the full memo list
  • /
memoList.deleteAll(); memoMap.removeAllElements(); nbMemo = 0; try { RecordEnumeration recordEnum = null; recordEnum = memoStore.enumerateRecords(null,null,false); while (recordEnum.hasPreviousElement()) { int Id = recordEnum.previousRecordId(); System.out.println("Reading Record Id = "+Id+ " from Record Store"); Memo memo = getMemo(Id); memoMap.addElement(memo); memoList.append(memo.getTitle(),null); nbMemo++; } } catch (RecordStoreException e) { System.err.println(e); } display.setCurrent(memoList); } else if (c == CMD_EXIT) { /*
  • exit the program
  • /
destroyApp(true); notifyDestroyed(); } } }); display.setCurrent(memoList); } } /**
  • This must be defined but no implementation is required because the MIDlet
  • only responds to user interaction.
  • /
public void pauseApp() { } /**
  • No further implementation is required because the MIDlet holds no
  • resources that require releasing.
*
  • @param unconditional is ignored.
  • /
public void destroyApp(boolean unconditional) { try { memoStore.closeRecordStore(); } catch (Exception e) { System.err.println(e); } } /**
  • A convenience method for exiting.
  • /
public void exitRequested(){ destroyApp(false); /*
  • notifyDestroyed() tells the scheduler that this MIDlet is now in a
  • destroyed state and is ready for disposal.
  • /
notifyDestroyed(); } /*
  • This method is called by the TextEditor object when the user has selected either
  • OK or CANCEL menu.
  • It updates the text of the memo object with the text entered by the user
  • /
public void textEditorDone(String s) { if (s != null) { int index = memoList.getSelectedIndex(); Memo memo = (Memo)memoMap.elementAt(index); memo.setText(s); memoList.delete(index); memoList.insert(index, memo.getTitle(),null); updateMemo(memo); } display.setCurrent(memoList); } /*
  • This method is called by the SearchBox object when the user has selected either
  • OK or CANCEL menu.
  • It replaces the list of memos being displayed by a new list containing only those
  • memos containing the searched text.
  • Additionally, it creates a new set a menus (mainly, a DONE menu allowing to come
  • back to the full memo list
  • /
public void searchBoxDone(String s) { if (s != null) { // afficher uniquement la liste des notes corresondant à la recherche lastSearchString = s; memoList.deleteAll(); memoMap.removeAllElements(); nbMemo = 0; try { RecordEnumeration recordEnum = null; recordEnum = memoStore.enumerateRecords(null,null,false); while (recordEnum.hasPreviousElement()) { int Id = recordEnum.previousRecordId(); System.out.println("Reading Record Id = "+Id+ " from Record Store"); Memo memo = getMemo(Id); if (memo.contains(s)) { memoMap.addElement(memo); memoList.append(memo.getTitle(),null); nbMemo++; } } } catch (RecordStoreException e) { System.err.println(e); } memoList.removeCommand(CMD_NEW); memoList.removeCommand(CMD_EXIT); memoList.removeCommand(CMD_SEARCH); memoList.addCommand(CMD_DONE); display.setCurrent(memoList); } } }

Conclusion :


En revanche, je n'ai pas trouvé le moyen de synchroniser les mémos avec un PC (en utilisant Active Sync). Si vous vous sentez de modifier le source et de le remettre sur le site, n'hésitez pas ...

Thierry

Codes Sources

A voir également

Ajouter un commentaire

Commentaires

Messages postés
26
Date d'inscription
mercredi 5 mars 2003
Statut
Membre
Dernière intervention
11 mai 2011
2
Si tu as un window mobile 6. Veux-tu m'expliquer pourquoi tu n'as pas fait ton logiciel en C# ???
Messages postés
43
Date d'inscription
dimanche 16 mai 2004
Statut
Membre
Dernière intervention
25 décembre 2011

Pour un débutant J2ME, je dis bravo pour cette gestion via RMS.

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.