Question

I'm currently developing a J2ME application event manager. I'm using PIM but I don't have enough knowledge on this. Can someone help me or give me references on how to set in displaying, editing and deleting records?

Here's what I've done so far.

        import javax.microedition.midlet.MIDlet;
        import javax.microedition.lcdui.Displayable;
        import javax.microedition.lcdui.Display;
        import javax.microedition.lcdui.Form;
        import javax.microedition.lcdui.CommandListener;
        import javax.microedition.lcdui.TextField;
        import javax.microedition.lcdui.DateField;
        import javax.microedition.lcdui.Command;
        import javax.microedition.lcdui.Alert;

        import javax.microedition.pim.PIM;
        import javax.microedition.pim.PIMItem;
        import javax.microedition.pim.EventList;
        import javax.microedition.pim.Event;
        import javax.microedition.pim.PIMException;

        import java.util.Date;


        public class AddCalendarEvent extends MIDlet implements CommandListener {

            private Display display;
            private Form addEventForm;
            private Command cmdAddEvent;
            private Command cmdExit;
            private TextField summaryField;
            private DateField startDateField;
            private DateField endDateField;
            private TextField noteField;
            private TextField locationField;

            public AddCalendarEvent() {
                if(checkPIMSupport() == false) {
                    exitMIDlet();
                }

                initializeComponents();
            }

            public void initializeComponents() {
                display = Display.getDisplay(this);
                addEventForm = new Form("Add event");
                cmdAddEvent = new Command("Add event", Command.SCREEN, 0);
                addEventForm.addCommand(cmdAddEvent);

                cmdExit = new Command("Exit", Command.EXIT, 0);
                addEventForm.addCommand(cmdExit);

                addEventForm.setCommandListener(this);        

                try {
                    EventList eventList = (EventList)PIM.getInstance().openPIMList(
                            PIM.EVENT_LIST, PIM.READ_WRITE);
                    if(eventList.isSupportedField(Event.SUMMARY) == true) {
                        summaryField = new TextField("Summary", null, 20, 
                                                    TextField.ANY);
                        addEventForm.append(summaryField);
                    } else {
                        eventList.close();
                        throw new Exception("Summary field is not supported");
                    }

                    if(eventList.isSupportedField(Event.START) == true) {
                        startDateField = new DateField("Start date", 
                                                    DateField.DATE_TIME);
                        startDateField.setDate(new Date());
                        addEventForm.append(startDateField);
                    }

                    if(eventList.isSupportedField(Event.END) == true) {
                        endDateField = new DateField("End date", DateField.DATE_TIME);
                        endDateField.setDate(new Date());
                        addEventForm.append(endDateField);
                    }

                    if(eventList.isSupportedField(Event.NOTE) == true) {
                        noteField = new TextField("Note", null, 20, TextField.ANY);
                        addEventForm.append(noteField);
                    }

                    if(eventList.isSupportedField(Event.LOCATION) == true) {
                        locationField = new TextField("Location", null, 20, 
                                                    TextField.ANY);
                        addEventForm.append(locationField);
                    }            

                    eventList.close();

                } catch(PIMException pimExc) {
                }
                catch(SecurityException secExc) {

                }
                catch(Exception exc) {

                    exitMIDlet();
                }
            }

            private boolean checkPIMSupport() {
                String propValue = System.getProperty("microedition.pim.version");
                if(propValue != null) {
                    return true;
                } else {
                    return false;
                }
            }

            private void addEvent() {
                try {
                    EventList eventList = (EventList)PIM.getInstance().openPIMList(
                            PIM.EVENT_LIST, PIM.READ_WRITE);            

                    Event event = eventList.createEvent();

                    if(eventList.isSupportedField(Event.SUMMARY) == true) {
                        String summary = summaryField.getString();
                        event.addString(Event.SUMMARY, PIMItem.ATTR_NONE, summary);
                    } else {
                        eventList.close();
                        throw new Exception("Summary field for event is not supported");
                    }

                    if(eventList.isSupportedField(Event.START) == true) {
                        long startDate = startDateField.getDate().getTime();
                        event.addDate(Event.START, PIMItem.ATTR_NONE, startDate);
                    }

                    if(eventList.isSupportedField(Event.END) == true) {
                        long endDate = endDateField.getDate().getTime();
                        event.addDate(Event.END, PIMItem.ATTR_NONE, endDate);
                    }

                    if(eventList.isSupportedField(Event.NOTE) == true) {
                        String note = noteField.getString();
                        event.addString(Event.NOTE, PIMItem.ATTR_NONE, note);
                    }

                    if(eventList.isSupportedField(Event.LOCATION) == true) {
                        String location = locationField.getString();
                        event.addString(Event.LOCATION, PIMItem.ATTR_NONE, location);
                    }      

                    event.commit();
                    eventList.close();

                    showAlert("Info", "Event was successfully added.");

                } catch(PIMException pimExc) {
                    showAlert("PIMException", pimExc.getMessage());
                }
                catch(SecurityException secExc) {
                    showAlert("SecurityException", secExc.getMessage());
                }
                catch(Exception exc) {
                    showAlert("Exception", exc.getMessage());
                }   
            }

            private void showAlert(String title, String message) {
                Alert alert = new Alert(title);
                alert.setString(message);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }

            public void startApp() {
                display.setCurrent(addEventForm);
            }

            public void pauseApp() {
            }
            public void destroyApp(boolean unconditional) {
            }

            private void exitMIDlet() {
                notifyDestroyed();
            }

            public void commandAction(Command command, Displayable displayable) {
                if(command == cmdAddEvent) {
                    addEvent();
                }

                if(command == cmdExit) {
                    exitMIDlet();
                }
            }
        }
Was it helpful?

Solution

Study the specification and tutorials suggested in jsr75 tag wiki .

This one looks like the best match to your issues: Getting Started With the PIM APIs

...This article provides a code-intensive introductory tutorial to the PIM APIs; it:

  • Introduces JSR 75
  • Describes the javax.microedition.pim optional package
  • Provides details about the PIM APIs
  • Offers a taste of the effort involved in using them
  • Provides code that you can adapt to the needs of your own wireless applications

...

Above tutorial also lists some resources recommended for further study in section For more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top