Creating and sending events

The events are created in the EventCreationForm, which contains fields for the event title, time, and duration. The EventCreationForm also includes a Command for opening the ContactsListForm for browsing the device's contacts list and selecting a recipient for the event message. The message is sent in a separate thread by using an operations queue.

Figure: Creating a new event

To implement the EventCreationForm class:

  1. Create the EventCreationForm.java class file.

  2. Import the required packages, initialize the UI elements and Commands, and create a CommandListener.

    import java.io.ByteArrayOutputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.Date;
    import java.util.TimeZone;
    import javax.microedition.lcdui.*;
    import javax.microedition.pim.*;
    
    // Form used to create a new event and send it to a contact
    class EventCreationForm extends Form implements CommandListener {
        private final Command sendCommand, exitCommand, findCommand;
        private final TextField number, duration, topic;
        private final DateField dateTime;
        private final EventSharingMIDlet parent;
    
        public EventCreationForm(EventSharingMIDlet parent, int port) {
            super("Create an event");
            this.parent = parent;
    
            // init UI
            sendCommand = new Command("Send", Command.SCREEN, 1);
            findCommand = new Command("Find Contacts", Command.SCREEN, 2);
            exitCommand = new Command("Exit", Command.EXIT, 1);
    
            number = new TextField("Dest. number", "", 20, TextField.PHONENUMBER);
            dateTime = new DateField("Date", DateField.DATE_TIME, TimeZone.getDefault());
            dateTime.setDate(new Date());
            duration = new TextField("Duration (min)", "30", 24, TextField.NUMERIC);
            topic = new TextField("Subject", "", 24, TextField.ANY);
    
            // create the form
            append(number);
            append(dateTime);
            append(duration);
            append(topic);
    
            addCommand(findCommand);
            addCommand(sendCommand);
            addCommand(exitCommand);
            setCommandListener(this);
        }
    
        void setTargetPhoneNumber(String phoneNumber) {
            number.setString(phoneNumber);
        }
    
        public void commandAction(Command cmd, Displayable displayable) {
            if (cmd == sendCommand) {
                // do a sanity check and then
                // send the message in a separate thread
                parent.enqueueOperation(new SendEventMessageOperation(number.getString(),
                    dateTime.getDate(), duration.getString(), topic.getString()));
            } else if (cmd == exitCommand) {
                parent.notifyDestroyed();
            } else if (cmd == findCommand) {
                parent.showContactsList();
            }
        }
  3. Create an inner class for handling the message sending operation to the destination number via a request to EventSharingMIDlet . For better application flow, do this in a separate thread by implementing the Operation interface. Once the fields are filled and the send command is given, validate the field contents.

        private class SendEventMessageOperation implements Operation {
            private final String number, duration, topic;
            private final Date date;
    
            SendEventMessageOperation(String number, Date date, String duration, String topic) {
                this.number = number;
                this.date = date;
                this.duration = duration;
                this.topic = topic;
            }
    
            public void execute() {
                int length = 0;
                try {
                    length = Integer.parseInt(duration);
                } catch (NumberFormatException e) {
                    parent.showMessage("Duration not valid", EventCreationForm.this);
                    return;
                }
                if (length <= 0) {
                    parent.showMessage("Duration needs to be positive", EventCreationForm.this);
                } else if (number == null || number.length() == 0) {
                    parent.showMessage("Number not entered", EventCreationForm.this);
                } else if (topic == null || topic.length() == 0) {
                    parent.showMessage("Subject not entered", EventCreationForm.this);
  4. Continuing in the inner class, create a new Event with the EventList.createEvent method, which imports the contents into the first EventList database and serializes it as a vCalendar message.

                } else {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    EventList eventList = null;
                    try {
                        PIM pim = PIM.getInstance();
                        String listNames[] = pim.listPIMLists(PIM.EVENT_LIST);
                        if (listNames.length > 0) {
                            eventList = (EventList) pim.openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE, listNames[0]);
                            Event newEvent = eventList.createEvent();
                            if (eventList.isSupportedField(Event.SUMMARY)) {
                                newEvent.addString(Event.SUMMARY, PIMItem.ATTR_NONE, topic);
                            }
                            if (eventList.isSupportedField(Event.START)) {
                                newEvent.addDate(Event.START, PIMItem.ATTR_NONE, date.getTime());
                            }
                            if (eventList.isSupportedField(Event.END)) {
                                newEvent.addDate(Event.END, PIMItem.ATTR_NONE, date.getTime() + 60 * 1000 * length);
                            }
                            // let's check that VCALENDAR/1.0 is supported
                            String supportedFormats[] = PIM.getInstance().supportedSerialFormats(PIM.EVENT_LIST);
                            for (int i = 0; i < supportedFormats.length; i++) {
                                if (supportedFormats[i].equals("VCALENDAR/1.0")) {
                                    PIM.getInstance().toSerialFormat(newEvent, out, "UTF-8", "VCALENDAR/1.0");
                                    break;
                                }
                            }
                            if (out.size() == 0) {
                                parent.showMessage("VCALENDAR/1.0 not supported", EventCreationForm.this);
                            }
                            // let's add the event locally
                            // an advanced version should wait for an ack
                            eventList.importEvent(newEvent);
                            newEvent.commit();
                        } else {
                            parent.showMessage("No Event list available", EventCreationForm.this);
                        }
                    } catch (PIMException e) {
                            parent.showMessage("PIMException: " + e.getMessage(), EventCreationForm.this);
                    } catch (SecurityException e) {
                            parent.showMessage("SecurityException: " + e.getMessage(), EventCreationForm.this);
                    } catch (UnsupportedEncodingException e) {
                            // should not happen since UTF-8 is mandatory
                    } finally {
                        try {
                            if (eventList != null) {
                                eventList.close();
                            }
                        } catch (PIMException e) {
                            // ignore, we are closing anyway
                        }
                    }
                    // out could be empty if there is no support
                    // for VCALENDAR/1.0
                    if (out.size() > 0) {
                        parent.sendMessage(number, out.toByteArray());
                    }
                }
            }
        }
    }

Now that you have implemented the functionality for creating and sending events, implement the functionality for displaying the contacts list.