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.VCalendar10Format;
030: import java.util.Date;
031: import java.util.Enumeration;
032: import javax.microedition.pim.Event;
033: import javax.microedition.pim.FieldEmptyException;
034: import javax.microedition.pim.PIM;
035: import javax.microedition.pim.RepeatRule;
036:
037: /**
038: * Implementation of a PIM Event.
039: *
040: */
041: public class EventImpl extends AbstractPIMItem implements Event {
042: /** Rule for repeating events. */
043: private RepeatRule repeatRule = null;
044:
045: /**
046: * Constructs an event implementation handler.
047: * @param list handler for event list
048: */
049: public EventImpl(AbstractPIMList list) {
050: super (list, PIM.EVENT_LIST);
051: if (list != null && !(list instanceof EventListImpl)) {
052: throw new RuntimeException("Wrong list passed");
053: }
054: }
055:
056: /**
057: * Constructs an event list from a base entry.
058: * @param list event list handler
059: * @param base event template
060: */
061: EventImpl(AbstractPIMList list, Event base) {
062: super (list, base);
063: if (!(list instanceof EventListImpl)) {
064: throw new RuntimeException("Wrong list passed");
065: }
066: this .repeatRule = base.getRepeat();
067: }
068:
069: /**
070: * Gets the repeat rule, if any.
071: * @return repeat rule, which could be <code>null</code>
072: * @see #setRepeat
073: */
074: public RepeatRule getRepeat() {
075: if (repeatRule == null) {
076: return null;
077: }
078: // clone the repeat rule
079: int[] fields = repeatRule.getFields();
080: RepeatRule newRule = new RepeatRule();
081: for (int i = 0; i < fields.length; i++) {
082: int field = fields[i];
083: switch (field) {
084: case RepeatRule.COUNT:
085: case RepeatRule.DAY_IN_MONTH:
086: case RepeatRule.DAY_IN_WEEK:
087: case RepeatRule.DAY_IN_YEAR:
088: case RepeatRule.FREQUENCY:
089: case RepeatRule.INTERVAL:
090: case RepeatRule.MONTH_IN_YEAR:
091: case RepeatRule.WEEK_IN_MONTH:
092: newRule.setInt(field, repeatRule.getInt(field));
093: break;
094: case RepeatRule.END:
095: newRule.setDate(field, repeatRule.getDate(field));
096: break;
097: }
098: }
099: Enumeration dates = repeatRule.getExceptDates();
100: while (dates.hasMoreElements()) {
101: Date date = (Date) dates.nextElement();
102: newRule.addExceptDate(date.getTime());
103: }
104: return newRule;
105: }
106:
107: /**
108: * Updates the repeat rule.
109: * @param value the new repeat rule
110: * @see #getRepeat
111: */
112: public void setRepeat(RepeatRule value) {
113: this .repeatRule = value;
114: setModified(true);
115: }
116:
117: /**
118: * Gets the encoding format handler.
119: * @return the PIM format handler
120: */
121: PIMFormat getEncodingFormat() {
122: return new VCalendar10Format();
123: }
124:
125: /**
126: * Ensures the PIM field identifier is supported.
127: * @param field the property identifier
128: * @return <code>true</code> if the field is supported
129: */
130: static boolean isValidPIMField(int field) {
131: switch (field) {
132: case Event.ALARM:
133: case Event.CLASS:
134: case Event.END:
135: case Event.LOCATION:
136: case Event.NOTE:
137: case Event.REVISION:
138: case Event.START:
139: case Event.SUMMARY:
140: case Event.UID:
141: return true;
142: default:
143: return false;
144: }
145: }
146:
147: /**
148: * Gets the revision field identifier.
149: * @return revision field identifier
150: */
151: protected int getRevisionField() {
152: return REVISION;
153: }
154:
155: /**
156: * Gets the UID field identifier.
157: * @return UID field identifier
158: */
159: protected int getUIDField() {
160: return UID;
161: }
162:
163: /**
164: * Sets default values for this item.
165: */
166: protected void setDefaultValues() {
167: super .setDefaultValues();
168: if (repeatRule != null) {
169: try {
170: repeatRule.getInt(RepeatRule.FREQUENCY);
171: } catch (FieldEmptyException e) {
172: repeatRule.setInt(RepeatRule.FREQUENCY,
173: RepeatRule.DAILY);
174: }
175: try {
176: repeatRule.getInt(RepeatRule.INTERVAL);
177: } catch (FieldEmptyException e) {
178: repeatRule.setInt(RepeatRule.INTERVAL, 1);
179: }
180: }
181: }
182:
183: /**
184: * Converts the Event record to a printable format.
185: * @return formatted Event record
186: */
187: protected String toDisplayableString() {
188: StringBuffer sb = new StringBuffer("Event[");
189: String data = formatData();
190: sb.append(data);
191: RepeatRule rule = getRepeat();
192: if (rule != null) {
193: if (data.length() > 0) {
194: sb.append(", ");
195: }
196: sb.append("Rule=[");
197: int[] fields = rule.getFields();
198: for (int i = 0; i < fields.length; i++) {
199: if (i != 0) {
200: sb.append(",");
201: }
202: int field = fields[i];
203: switch (field) {
204: case RepeatRule.FREQUENCY:
205: sb.append("Frequency=");
206: switch (rule.getInt(field)) {
207: case RepeatRule.DAILY:
208: sb.append("Daily");
209: break;
210: case RepeatRule.WEEKLY:
211: sb.append("Weekly");
212: break;
213: case RepeatRule.MONTHLY:
214: sb.append("Monthly");
215: break;
216: case RepeatRule.YEARLY:
217: sb.append("Yearly");
218: break;
219: default:
220: sb.append("<Unknown: " + rule.getInt(field)
221: + ">");
222: }
223: break;
224: case RepeatRule.END:
225: sb.append("End="
226: + PIMHandler.getInstance().composeDateTime(
227: rule.getDate(field)));
228: break;
229: case RepeatRule.COUNT:
230: sb.append("Count=" + rule.getInt(field));
231: break;
232: case RepeatRule.INTERVAL:
233: sb.append("Interval=" + rule.getInt(field));
234: break;
235: case RepeatRule.DAY_IN_WEEK:
236: sb.append("DayInWeek=0x"
237: + Integer.toHexString(rule.getInt(field)));
238: break;
239: case RepeatRule.DAY_IN_MONTH:
240: sb.append("DayInMonth=" + rule.getInt(field));
241: break;
242: case RepeatRule.DAY_IN_YEAR:
243: sb.append("DayInYear=" + rule.getInt(field));
244: break;
245: case RepeatRule.WEEK_IN_MONTH:
246: sb.append("WeekInMonth=0x"
247: + Integer.toHexString(rule.getInt(field)));
248: break;
249: case RepeatRule.MONTH_IN_YEAR:
250: sb.append("MonthInYear=0x"
251: + Integer.toHexString(rule.getInt(field)));
252: break;
253: default:
254: sb.append("<Unknown: " + field + "="
255: + rule.getInt(field));
256:
257: }
258: }
259: sb.append("]");
260: }
261: sb.append("]");
262: return sb.toString();
263: }
264:
265: }
|