001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.pim;
033:
034: import java.util.Enumeration;
035: import java.util.Vector;
036:
037: import javax.microedition.lcdui.Command;
038: import javax.microedition.lcdui.CommandListener;
039: import javax.microedition.lcdui.Display;
040: import javax.microedition.lcdui.Displayable;
041: import javax.microedition.lcdui.List;
042: import javax.microedition.midlet.MIDlet;
043: import javax.microedition.pim.Contact;
044: import javax.microedition.pim.ContactList;
045: import javax.microedition.pim.Event;
046: import javax.microedition.pim.EventList;
047: import javax.microedition.pim.PIM;
048: import javax.microedition.pim.PIMException;
049: import javax.microedition.pim.PIMItem;
050: import javax.microedition.pim.PIMList;
051: import javax.microedition.pim.ToDo;
052: import javax.microedition.pim.ToDoList;
053:
054: /**
055: * Demonstrate the use of JSR 75 PIM APIs
056: */
057: public class ItemSelectionScreen extends List implements
058: CommandListener {
059: private final Command selectCommand = new Command("Select",
060: Command.ITEM, 1);
061: private final Command backCommand = new Command("Back",
062: Command.BACK, 1);
063: private final Command removeCommand = new Command("Delete",
064: Command.SCREEN, 3);
065: private final Command newCommand = new Command("New",
066: Command.SCREEN, 2);
067: private final PIMDemo midlet;
068: private final Displayable caller;
069: private final int listType;
070: private final PIMList list;
071: private final Vector itemList = new Vector();
072:
073: public ItemSelectionScreen(PIMDemo midlet, Displayable caller,
074: int listType, PIMList list) throws PIMException {
075: super ("Select a PIM item", List.IMPLICIT);
076: this .midlet = midlet;
077: this .caller = caller;
078: this .listType = listType;
079: this .list = list;
080:
081: populateList();
082:
083: addCommand(backCommand);
084: addCommand(newCommand);
085: setCommandListener(this );
086: }
087:
088: void populateList() {
089: new Thread(new Runnable() {
090: public void run() {
091: synchronized (ItemSelectionScreen.this ) {
092: try {
093: deleteAll();
094: removeCommand(selectCommand);
095: removeCommand(removeCommand);
096: itemList.removeAllElements();
097:
098: for (Enumeration items = list.items(); items
099: .hasMoreElements();) {
100: PIMItem item = (PIMItem) items
101: .nextElement();
102: int fieldCode = 0;
103:
104: switch (listType) {
105: case PIM.CONTACT_LIST:
106: fieldCode = Contact.FORMATTED_NAME;
107:
108: break;
109:
110: case PIM.EVENT_LIST:
111: fieldCode = Event.SUMMARY;
112:
113: break;
114:
115: case PIM.TODO_LIST:
116: fieldCode = ToDo.SUMMARY;
117:
118: break;
119: }
120:
121: String label = getDisplayedField(item);
122:
123: if (label == null) {
124: label = "<Incomplete data>";
125: }
126:
127: append(label, null);
128: itemList.addElement(item);
129: }
130:
131: if (size() > 0) {
132: setSelectCommand(selectCommand);
133: addCommand(removeCommand);
134: }
135: } catch (PIMException e) {
136: midlet.reportException(e,
137: ItemSelectionScreen.this );
138: }
139: }
140: }
141: }).start();
142: }
143:
144: public void commandAction(Command command, Displayable displayable) {
145: if (command == backCommand) {
146: Display.getDisplay(midlet).setCurrent(caller);
147: } else if (command == selectCommand) {
148: try {
149: PIMItem item = (PIMItem) itemList
150: .elementAt(getSelectedIndex());
151: Displayable screen = new ItemDisplayScreen(midlet,
152: this , item);
153: Display.getDisplay(midlet).setCurrent(screen);
154: } catch (Exception e) {
155: midlet.reportException(e, this );
156: }
157: } else if (command == removeCommand) {
158: new Thread(new Runnable() {
159: public void run() {
160: synchronized (ItemSelectionScreen.this ) {
161: try {
162: PIMItem item = (PIMItem) itemList
163: .elementAt(getSelectedIndex());
164:
165: switch (listType) {
166: case PIM.CONTACT_LIST:
167: ((ContactList) list)
168: .removeContact((Contact) item);
169:
170: break;
171:
172: case PIM.EVENT_LIST:
173: ((EventList) list)
174: .removeEvent((Event) item);
175:
176: break;
177:
178: case PIM.TODO_LIST:
179: ((ToDoList) list)
180: .removeToDo((ToDo) item);
181:
182: break;
183: }
184: } catch (Exception e) {
185: midlet.reportException(e,
186: ItemSelectionScreen.this );
187: }
188: }
189: }
190: }).start();
191: populateList();
192: } else if (command == newCommand) {
193: new Thread(new Runnable() {
194: public void run() {
195: try {
196: PIMItem item = null;
197:
198: switch (listType) {
199: case PIM.CONTACT_LIST:
200: item = ((ContactList) list).createContact();
201:
202: break;
203:
204: case PIM.EVENT_LIST:
205: item = ((EventList) list).createEvent();
206:
207: break;
208:
209: case PIM.TODO_LIST:
210: item = ((ToDoList) list).createToDo();
211:
212: break;
213: }
214:
215: int fieldCode = getDisplayedFieldCode();
216: item
217: .addString(fieldCode,
218: PIMItem.ATTR_NONE, "");
219:
220: Displayable screen = new ItemDisplayScreen(
221: midlet, ItemSelectionScreen.this , item);
222: Display.getDisplay(midlet).setCurrent(screen);
223: } catch (Exception e) {
224: midlet.reportException(e,
225: ItemSelectionScreen.this );
226: }
227: }
228: }).start();
229: }
230: }
231:
232: int getDisplayedFieldCode() {
233: int fieldCode = 0;
234:
235: switch (listType) {
236: case PIM.CONTACT_LIST:
237: fieldCode = Contact.FORMATTED_NAME;
238:
239: break;
240:
241: case PIM.EVENT_LIST:
242: fieldCode = Event.SUMMARY;
243:
244: break;
245:
246: case PIM.TODO_LIST:
247: fieldCode = ToDo.SUMMARY;
248:
249: break;
250: }
251:
252: return fieldCode;
253: }
254:
255: void fixDisplayedField(PIMItem item) {
256: int fieldCode = getDisplayedFieldCode();
257:
258: if (listType == PIM.CONTACT_LIST) {
259: boolean defined = false;
260:
261: if (item.countValues(fieldCode) != 0) {
262: String s = item.getString(fieldCode, 0);
263:
264: if ((s != null) && (s.trim().length() > 0)) {
265: defined = true;
266: }
267: }
268:
269: if (!defined) {
270: // try to fill in the values from NAME
271: if (item.countValues(Contact.NAME) != 0) {
272: String[] a = item.getStringArray(Contact.NAME, 0);
273:
274: if (a != null) {
275: StringBuffer sb = new StringBuffer();
276:
277: if (a[Contact.NAME_GIVEN] != null) {
278: sb.append(a[Contact.NAME_GIVEN]);
279: }
280:
281: if (a[Contact.NAME_FAMILY] != null) {
282: if (sb.length() > 0) {
283: sb.append(" ");
284: }
285:
286: sb.append(a[Contact.NAME_FAMILY]);
287: }
288:
289: String s = sb.toString().trim();
290:
291: if (s.length() > 0) {
292: if (item.countValues(fieldCode) == 0) {
293: item.addString(fieldCode,
294: Contact.ATTR_NONE, s);
295: } else {
296: item.setString(fieldCode, 0,
297: Contact.ATTR_NONE, s);
298: }
299: }
300: }
301: }
302: }
303: }
304: }
305:
306: String getDisplayedField(PIMItem item) {
307: int fieldCode = getDisplayedFieldCode();
308: fixDisplayedField(item);
309:
310: String fieldValue = null;
311:
312: if (item.countValues(fieldCode) != 0) {
313: String s = item.getString(fieldCode, 0);
314:
315: if ((s != null) && (s.trim().length() != 0)) {
316: fieldValue = s;
317: }
318: }
319:
320: return fieldValue;
321: }
322: }
|