001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.kvem.midp.pim;
028:
029: import com.sun.kvem.midp.pim.formats.VCard30Format;
030: import javax.microedition.pim.Contact;
031: import javax.microedition.pim.PIM;
032:
033: /**
034: * Implementation of a PIM contact.
035: *
036: */
037: public class ContactImpl extends AbstractPIMItem implements Contact {
038: /**
039: * Constructs a Contact list.
040: * @param list handle for Contact List implementation
041: */
042: public ContactImpl(AbstractPIMList list) {
043: super (list, PIM.CONTACT_LIST);
044: if (list != null && !(list instanceof ContactListImpl)) {
045: throw new RuntimeException("Wrong list passed");
046: }
047: }
048:
049: /**
050: * Constructs a Contact list from a handler and base Contact
051: * record.
052: * @param list Contact List implementation handler
053: * @param base Contact record template
054: */
055: ContactImpl(AbstractPIMList list, Contact base) {
056: super (list, base);
057: if (!(list instanceof ContactListImpl)) {
058: throw new RuntimeException("Wrong list passed");
059: }
060: }
061:
062: /**
063: * Gets preferred index for requested field.
064: * @param field requested element
065: * @return preferred index for requested field
066: */
067: public int getPreferredIndex(int field) {
068: int indices = countValues(field);
069: for (int i = 0; i < indices; i++) {
070: int attributes = getAttributes(field, i);
071: if ((attributes & ATTR_PREFERRED) != 0) {
072: return i;
073: }
074: }
075: return -1;
076: }
077:
078: /**
079: * Gets encoding format for this contact record.
080: * @return handle for encoding format
081: */
082: PIMFormat getEncodingFormat() {
083: return new VCard30Format();
084: }
085:
086: /**
087: * Ensures valid field.
088: * @param field identifier to validate
089: * @return <code>true</code> if contact field identifier is
090: * supported
091: */
092: static boolean isValidPIMField(int field) {
093: switch (field) {
094: case Contact.ADDR:
095: case Contact.BIRTHDAY:
096: case Contact.CLASS:
097: case Contact.EMAIL:
098: case Contact.FORMATTED_ADDR:
099: case Contact.FORMATTED_NAME:
100: case Contact.NAME:
101: case Contact.NICKNAME:
102: case Contact.NOTE:
103: case Contact.ORG:
104: case Contact.PHOTO:
105: case Contact.PHOTO_URL:
106: case Contact.PUBLIC_KEY:
107: case Contact.PUBLIC_KEY_STRING:
108: case Contact.REVISION:
109: case Contact.TEL:
110: case Contact.TITLE:
111: case Contact.UID:
112: case Contact.URL:
113: return true;
114: default:
115: return false;
116: }
117: }
118:
119: /**
120: * Adds a binary value to the current Contact.
121: * @param field identifier for current element
122: * @param attributes property to insert
123: * @param value binary value to be recorded
124: * @param offset index into value array
125: * @param length size of data to copy
126: */
127: public void addBinary(int field, int attributes, byte[] value,
128: int offset, int length) {
129: super .addBinary(field, attributes, value, offset, length);
130: if (field == PUBLIC_KEY) {
131: // remove any values from PUBLIC_KEY_STRING
132: while (countValues(PUBLIC_KEY_STRING) > 0) {
133: removeValue(PUBLIC_KEY_STRING, 0);
134: }
135: }
136: }
137:
138: /**
139: * Adds a string to the current Contact.
140: * @param field identifier for current element
141: * @param attributes property to insert
142: * @param value string to be recorded
143: */
144: public void addString(int field, int attributes, String value) {
145: super .addString(field, attributes, value);
146: if (field == PUBLIC_KEY_STRING) {
147: // remove any values from PUBLIC_KEY
148: while (countValues(PUBLIC_KEY) > 0) {
149: removeValue(PUBLIC_KEY, 0);
150: }
151: }
152: }
153:
154: /**
155: * Gets the revision field identifier.
156: * @return revision field identifier
157: */
158: protected int getRevisionField() {
159: return REVISION;
160: }
161:
162: /**
163: * Gets the UID field identifier.
164: * @return UID field identifier
165: */
166: protected int getUIDField() {
167: return UID;
168: }
169:
170: /**
171: * Converts the Contact to a printable format.
172: * @return formatted Contact record
173: */
174: protected String toDisplayableString() {
175: return "Contact[" + formatData() + "]";
176: }
177:
178: }
|