📄 addressbook.java
字号:
package mujmail;/*MujMail - Simple mail client for J2MECopyright (C) 2006 Nguyen Son Tung <n.sontung@gmail.com>Copyright (C) 2008 David Hauzar <david.hauzar.mujmail@gmail.com> This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */import mujmail.util.Functions;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.util.Enumeration;import java.util.Hashtable;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.StringItem;import javax.microedition.lcdui.TextField;import javax.microedition.rms.RecordEnumeration;import javax.microedition.rms.RecordStore;//#ifdef MUJMAIL_TOUCH_SCRimport mujmail.pointer.MujMailPointerEventProducer;import mujmail.pointer.MujMailPointerEventListener;//#endif/** * Makes possible to administer, display and search the contacts. */public class AddressBook extends Canvas implements Runnable { /** Flag signals if we want to print debug prints */ private static final boolean DEBUG = false; public //to flip recipients when Insert Recipients from sendMail Vector getAddresses() { return addresses; } public void setAddresses(Vector addresses) { this.addresses = addresses; } public //to map contacts marked as recipients Hashtable getEmailHash() { return emailHash; } public void setEmailHash(Hashtable emailHash) { this.emailHash = emailHash; } /** * Represents one contact stored in database / vector addresses. */ public static class Contact { String name; private String email; String notes; int DBIndex; public String getName() { return name; } public Contact(String name, String email, String notes) { this.name = name; this.email = email; this.notes = notes; } //#ifdef MUJMAIL_SYNC public String toString() { return "Name: " + this.name + "\n" + "Email: " + this.getEmail() + "\n" + "Notes: " + this.notes + "\n\n"; } public static Contact parseContact(String contactStr) { String name, email, notes; name = contactStr.substring(contactStr.indexOf("Name: ") + 6, contactStr.indexOf('\n')); contactStr = contactStr.substring(contactStr.indexOf("\n") + 1); email = contactStr.substring(contactStr.indexOf("Email: ") + 7, contactStr.indexOf('\n')); contactStr = contactStr.substring(contactStr.indexOf("\n") + 1); notes = contactStr.substring(contactStr.indexOf("Notes: ") + 7, contactStr.indexOf('\n')); return new Contact(name, email, notes); } //#endif public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } //to confirm a pressed key after a given period defined in keyTimer.schedule() private class KeyConfirmer extends TimerTask { public void run() { if (keyMajor != -1) { if (!shift) { input.insert(textCur, KEYS[keyMajor].charAt(keyMinor)); } else { input.insert(textCur, KEYS[keyMajor].charAt(KEYS[keyMajor].length() - 1)); } textCur++; keyMajor = -1; inputChanged = true; } repaint(); } } //it refreshes the canvas in a given period defined in curTimer.schedule() to have the blinking textcursor efect private class CursorShow extends TimerTask { public void run() { textCurShow = !textCurShow; repaint(); } } static final byte MODE_DEFAULT = 0; static final byte MODE_EDIT = 1; //when editing a contact static final byte MODE_SENDMAIL_BROWSE = 2; //when selecting a contact to be paste in SendMail byte mode = MODE_DEFAULT; MujMail mujMail; Form cntForm, viewForm; //a form to create a contact; to view contact's info Command mark, done, back, delete, edit, add, delAll, view, sendMail; Command cfBack, cfSave; //for contact editing form Command vBack; //for contact viewing form Command flipRcps; //to flip recipients when Insert Recipients from sendMail private Vector addresses; //a container to store contacts Hashtable nameHash; //store first letters of some names for faster finding of a contact's name boolean busy, btnHidden; Hashtable marked; //to map contacts marked as recipients private Hashtable emailHash; //to map contacts's emails that are in the addressbook, preventing multiple entries having the same email int cur; Displayable nextDisplay; //a displayable that should be displayed(MailForm, SendMail) after some action (saving a contact, adding recipients) Image img_search = Functions.getIcon("search.png"); //input key reading stuff final String[] KEYS = {" 0", "._,'?!\"*1", "abc2", "def3", "ghi4", "jkl5", "mno6", "pqrs7", "tuv8", "wxyz9"}; Timer keyTimer; byte keyMajor = -1; byte keyMinor; boolean inputChanged, shift; StringBuffer input; //text cursors stuff Timer curTimer; byte textCur; boolean textCurShow = true; int recipientChoice = 0; String recipientChoiceStr = "To:"; private final PointerEventListener pointerEventListener = new PointerEventListener(); //#ifdef MUJMAIL_TOUCH_SCR private final MujMailPointerEventProducer pointerEventProducer; //#endif public AddressBook(MujMail mujMail) { this.mujMail = mujMail; addresses = new Vector(); nextDisplay = this; marked = new Hashtable(); emailHash = new Hashtable(); nameHash = new Hashtable(); input = new StringBuffer(); cfSave = new Command(Lang.get(Lang.BTN_SAVE), Command.OK, 1); cfBack = new Command(Lang.get(Lang.BTN_BACK), Command.BACK, 2); vBack = new Command(Lang.get(Lang.BTN_BACK), Command.BACK, 1); mark = new Command(Lang.get(Lang.BTN_AD_MARK), Command.OK, 2); done = new Command(Lang.get(Lang.BTN_AD_DONE), Command.ITEM, 1); back = new Command(Lang.get(Lang.BTN_BACK), Command.BACK, 3); sendMail = new Command(Lang.get(Lang.BTN_AD_SEND_MAIL), Command.OK, 2); add = new Command(Lang.get(Lang.BTN_AD_ADD_NEW), Command.ITEM, 3); edit = new Command(Lang.get(Lang.BTN_EDIT), Command.ITEM, 4); delete = new Command(Lang.get(Lang.BTN_DELETE), Command.ITEM, 5); view = new Command(Lang.get(Lang.BTN_AD_VIEW), Command.ITEM, 6); delAll = new Command(Lang.get(Lang.BTN_CLEAR), Command.ITEM, 7); flipRcps = new Command(Lang.get(Lang.BTN_AD_FLIPRCP), Command.ITEM,8); addCommand(sendMail); addCommand(view); addCommand(back); addCommand(edit); addCommand(add); addCommand(delete); addCommand(delAll); setCommandListener(mujMail); Thread t = new Thread(this); t.start(); t.setPriority(Thread.MAX_PRIORITY); //#ifdef MUJMAIL_TOUCH_SCR pointerEventProducer = new MujMailPointerEventProducer(pointerEventListener, getWidth(), getHeight()); //#endif } public void run() { busy = true; try { RecordStore ADRS = RecordStore.openRecordStore("AddressBook", true); try { if (DEBUG) System.out.println("DEBUG AddressBook.run() - loading AddressBook"); if (ADRS.getNumRecords() > 0) { int id, sizeOfRecord; byte[] data = new byte[50]; DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data)); RecordEnumeration enumeration = ADRS.enumerateRecords(null, null, false); getAddresses().ensureCapacity(enumeration.numRecords()); while (enumeration.hasNextElement()) { try { id = enumeration.nextRecordId(); sizeOfRecord = ADRS.getRecordSize(id); if (sizeOfRecord > data.length) { data = new byte[sizeOfRecord + 20]; inputStream = new DataInputStream(new ByteArrayInputStream(data)); } ADRS.getRecord(id, data, 0); inputStream.reset(); Contact contact = new Contact( inputStream.readUTF(), inputStream.readUTF(), inputStream.readUTF() ); contact.DBIndex = id; getAddresses().addElement(contact); getEmailHash().put(contact.getEmail(), contact); } catch (Exception exp) { //try another one } } if (inputStream != null) { inputStream.close(); } data = null; } if (DEBUG) System.out.println("DEBUG AddressBook.run() - loading AddressBook done"); } catch (Exception ex) { mujMail.alert.setAlert(this, this, Lang.get(Lang.ALRT_AD_LOAD) + Lang.get(Lang.FAILED), MyAlert.DEFAULT, AlertType.ERROR); } catch (Error er) { mujMail.alert.setAlert(this, this, Lang.get(Lang.ALRT_AD_LOAD) + Lang.get(Lang.FAILED), MyAlert.DEFAULT, AlertType.ERROR); } ADRS.closeRecordStore(); } catch (Exception ex) { mujMail.alert.setAlert(this, this, Lang.get(Lang.ALRT_AD_LOAD) + Lang.get(Lang.FAILED), MyAlert.DEFAULT, AlertType.ERROR); } Functions.sort(getAddresses(), Functions.SRT_ORDER_INC, Functions.SRT_CNT_NAME); initHash(); busy = false; } public int getSelectedIndex() { return cur; } public void setSelectedIndex(int i) { if (!addresses.isEmpty()) { cur = (cur + i + getAddresses().size()) % getAddresses().size(); } repaint(); } protected synchronized void keyPressed(int keyCode) { switch (getGameAction(keyCode)) { case UP: if (keyCode != KEY_NUM2) { pointerEventListener.up(); return; } break; case DOWN: if (keyCode != KEY_NUM8) { pointerEventListener.down(); return; } break; case RIGHT: if (keyCode != KEY_NUM6) { pointerEventListener.right(); return; } break; case LEFT: if (keyCode != KEY_NUM4) { pointerEventListener.left(); return; } break; case FIRE: if (keyCode != KEY_NUM5) { pointerEventListener.fire(); return; } break; } if (keyCode == '#') {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -