📄 addressbook.java
字号:
//saves a given contact do DB. public Contact saveContactToDB(Contact contact) throws MyException { //for the sake of simplicity we just try to determine if there's any charakter before the @ if (contact.getEmail() == null || contact.getEmail().indexOf("@") < 1) { throw new MyException(MyException.VARIOUS_BAD_EMAIL); } if ((getEmailHash().containsKey(contact.getEmail())) && mode != MODE_EDIT) { throw new MyException(MyException.VARIOUS_AB_MULTIPLE_ENTRIES); } MyException exception = null; RecordStore ADRS = Functions.openRecordStore("AddressBook", true); try { ByteArrayOutputStream byteStream; DataOutputStream outputStream; byteStream = new ByteArrayOutputStream(); outputStream = new DataOutputStream(byteStream); if (mode == MODE_EDIT) { contact.DBIndex = ((Contact) getAddresses().elementAt(cur)).DBIndex; } //if the name is not presented or invalid, try to guest it from the email address if (contact.name == null || contact.name.length() == 0) { contact.name = contact.getEmail().substring(0, contact.getEmail().indexOf("@")); } outputStream.writeUTF(contact.name); outputStream.writeUTF(contact.getEmail()); outputStream.writeUTF(contact.notes); outputStream.flush(); /*buggy setRecord() method not only on the WTK emulator if (mode != MODE_EDIT) contact.DBIndex = ADRS.addRecord( byteStream.toByteArray(), 0, byteStream.size() ); else ADRS.setRecord(contact.DBIndex, byteStream.toByteArray(), 0, byteStream.size() ); */ int oldIndex = contact.DBIndex; contact.DBIndex = ADRS.addRecord(byteStream.toByteArray(), 0, byteStream.size()); if (mode == MODE_EDIT) { ADRS.deleteRecord(oldIndex); } outputStream.close(); byteStream.close(); } catch (Exception ex) { exception = new MyException(MyException.DB_CANNOT_SAVE_CONTACT); } Functions.closeRecordStore(ADRS); if (exception != null) { throw exception; } return contact; } //edits a selected contact public void edit(int index) { if (0 <= index && index < getAddresses().size()) { Contact contact = (Contact) getAddresses().elementAt(index); mode = MODE_EDIT; cntForm = createCntForm(); ((TextField) cntForm.get(0)).setString(contact.name); ((TextField) cntForm.get(1)).setString(contact.getEmail()); ((TextField) cntForm.get(2)).setString(contact.notes); mujMail.getDisplay().setCurrent(cntForm); } } //creates a Contact form so we can add or edit a contact private Form createCntForm() { TextField name, email, notes; cntForm = new Form(Lang.get(Lang.AD_CONTACT_INFO)); name = new TextField(Lang.get(Lang.AD_NAME), "", 50, TextField.ANY); if ( Properties.textFieldMailIncorrect ) { email = new TextField(Lang.get(Lang.AD_EMAIL), "@", 512, TextField.ANY); } else { email = new TextField(Lang.get(Lang.AD_EMAIL), "@", 512, TextField.EMAILADDR); } notes = new TextField(Lang.get(Lang.AD_NOTES), "", 1000, TextField.ANY); cntForm.append(name); cntForm.append(email); cntForm.append(notes); cntForm.addCommand(cfBack); cntForm.addCommand(cfSave); cntForm.setCommandListener(mujMail); return cntForm; } //if its called from mailForm and a mail address is given, then the string MailFormAddress is the email address //so after saving the email address, we can return display focus to MailForm; otherwise string MailFormAddress is null public void showCntForm(String MailFormAddress) { cntForm = createCntForm(); nextDisplay = mujMail.getDisplay().getCurrent(); mujMail.getDisplay().setCurrent(cntForm); if (MailFormAddress != null) { //was is call by MailForm? ((TextField) cntForm.get(0)).setString(MailFormAddress); } } //creates a Hashtable for faster searching using the first letter of each name as index //something like the indexes in the real life phonebook private void initHash() { nameHash.clear(); if (getAddresses().isEmpty()) { return; } char firstLetter = ((Contact) getAddresses().firstElement()).name.charAt(0); firstLetter = Character.toLowerCase(firstLetter); nameHash.put(new Character(firstLetter), new Integer(0)); int size = getAddresses().size(); char cntNameFL; //first letter of a contact's name for (int i = 1; i < size; i++) { cntNameFL = ((Contact) getAddresses().elementAt(i)).name.charAt(0); cntNameFL = Character.toLowerCase(cntNameFL); if (firstLetter != cntNameFL) { firstLetter = cntNameFL; nameHash.put(new Character(firstLetter), new Integer(i)); } } } //returns index of the most matched contact public int search(String name) { if (name == null || name.length() == 0) { return -1; } name.toLowerCase(); Integer i = (Integer) nameHash.get(new Character(name.charAt(0))); //get the closest index if (i == null) //if its first letter was never indexed { return -1; } int size = getAddresses().size(), index = i.intValue(); String contactName = null; //lets find its correct position (index) while (index < size) { contactName = ((Contact) getAddresses().elementAt(index)).name.toLowerCase(); if (contactName.charAt(0) != name.charAt(0)) //has different first letter { return -1; } //not found if (contactName.compareTo(name) < 0) //name should be after the contactName { index++; } else { break; } //found or name lies before contactName } if (contactName.startsWith(name)) //now check if it really matches { return index; } else { return -1; } } //views a contact info public void view(int index) { if (0 <= index && index < getAddresses().size()) { viewForm = new Form(Lang.get(Lang.AD_CONTACT_INFO)); Contact contact = (Contact) getAddresses().elementAt(index); viewForm.append(new StringItem(Lang.get(Lang.AD_NAME), contact.name)); viewForm.append(new StringItem(Lang.get(Lang.AD_EMAIL),contact.getEmail())); viewForm.append(new StringItem(Lang.get(Lang.AD_NOTES), contact.notes)); viewForm.addCommand(vBack); viewForm.setCommandListener(mujMail); mujMail.getDisplay().setCurrent(viewForm); } } //sends a selected contact a mail public void sendMail(int index) { if (0 <= index && index < getAddresses().size()) { Contact contact = (Contact) getAddresses().elementAt(index); mujMail.sendMail.to.setString("\"" + contact.name + "\" <" + contact.getEmail() + ">"); mujMail.getDisplay().setCurrent(mujMail.sendMail); } } //is called when a Form wants to add emails from the addressbook public void addEmails(Form form) { nextDisplay = form; mode = MODE_SENDMAIL_BROWSE; addCommand(mark); addCommand(done); addCommand(flipRcps); mujMail.getDisplay().setCurrent(this); } //is called when the user confirms adding emails and wants to paste all those emails addresses to a form; public void pasteEmails() { StringBuffer emailBf = new StringBuffer(); Contact contact; //probably old - and doesn't work TextField tfield = mujMail.sendMail.getSelectedItem() == null? mujMail.sendMail.to: mujMail.sendMail.getSelectedItem(); //newer switch(recipientChoice) { case 0: tfield = mujMail.sendMail.to; break; case 1: tfield = mujMail.sendMail.cc; break; case 2: tfield = mujMail.sendMail.bcc; break; } for (Enumeration e = marked.elements(); e.hasMoreElements(); ) { contact = (Contact) e.nextElement(); if (tfield.getString().indexOf(contact.getEmail()) == -1) { emailBf.append("\"" + contact.name + "\" <" + contact.getEmail() + ">, "); } } // if ((tfield == mujMail.sendMail.cc || tfield == mujMail.sendMail.bcc) && emailBf.length() > 0 ) mujMail.sendMail.addBc(); if (tfield != mujMail.sendMail.body) { tfield.setString(emailBf.toString() + tfield.getString()); } else { //tfield.setString(tfield.getString() + emailBf.toString()); tfield.insert(emailBf.toString(), tfield.getCaretPosition()); } back(); } //is called when we want to flip recipients if inserting public void flipRecipients() { recipientChoice++; if (recipientChoice > 2) recipientChoice = 0; switch(recipientChoice) { case 0: recipientChoiceStr = "To:"; break; case 1: recipientChoiceStr = "Cc:"; break; case 2: recipientChoiceStr = "Bcc:"; break; } //System.out.println("flipRecipients " + recipientChoice); } //is called when the user un/mark a contact in the addressbook public void markEmail(int index) { if (index < getAddresses().size()) { Contact contact = (Contact) getAddresses().elementAt(index); if (!marked.containsKey(contact.email)) { marked.put(contact.getEmail(), contact); } else { marked.remove(contact.getEmail()); } repaint(); } } public String getContactName(String email) { if (email == null) { return null; } if (!emailHash.containsKey(email)) { return null; } return ((Contact) getEmailHash().get(email)).name; } //#ifdef MUJMAIL_TOUCH_SCR private class PointerEventListener extends MujMailPointerEventListener.MujMailPointerEventListenerAdapter { //#else//# private class PointerEventListener { //#endif public void left() { textCur = (byte) ((textCur - 1 + input.length() + 1) % (input.length() + 1)); cancelTimers(); repaint(); } public void right() { textCur = (byte) ((textCur + 1) % (input.length() + 1)); cancelTimers(); repaint(); } public void up() { setSelectedIndex(-1); repaint(); } public void down() { setSelectedIndex(1); repaint(); } public void fire() { if (mode != MODE_SENDMAIL_BROWSE) { sendMail(cur); } else { markEmail(cur); } repaint(); } public void slash() { shift = !shift; } public void star() { input.deleteCharAt(textCur - 1); textCur--; inputChanged = true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -