001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.store;
019:
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.List;
025: import java.util.logging.Logger;
026:
027: import javax.swing.Timer;
028:
029: import org.columba.calendar.model.api.IComponent;
030: import org.columba.calendar.model.api.IComponentInfo;
031: import org.columba.calendar.model.api.IEvent;
032: import org.columba.calendar.model.api.IEventInfo;
033: import org.columba.calendar.store.api.ICalendarStore;
034: import org.columba.calendar.store.api.IStoreListener;
035: import org.columba.calendar.store.api.StoreEvent;
036: import org.columba.calendar.store.api.StoreException;
037: import org.columba.calendar.ui.base.CalendarHelper;
038: import org.columba.core.base.Mutex;
039:
040: import com.miginfocom.calendar.activity.Activity;
041: import com.miginfocom.calendar.activity.ActivityDepository;
042: import com.miginfocom.calendar.category.CategoryStructureEvent;
043:
044: /**
045: * StoreEventDelegator class
046: * @author fdietz
047: */
048: public class StoreEventDelegator implements IStoreListener,
049: ActionListener {
050:
051: /** JDK 1.4+ logging framework logger, used for logging. */
052: private static final Logger LOG = Logger
053: .getLogger("org.columba.calendar.store.event");
054: private static final int UPDATE_DELAY = 50;
055: private static StoreEventDelegator instance;
056: private Timer timer;
057: private Mutex mutex;
058: private int swap = 0;
059: private List[] itemRemovedList;
060: private List[] itemChangedList;
061: private List[] itemAddedList;
062:
063: /**
064: * StoreEventDelegator method
065: */
066: public StoreEventDelegator() {
067: super ();
068:
069: itemRemovedList = new List[] { new ArrayList(500),
070: new ArrayList(500) };
071: itemChangedList = new List[] { new ArrayList(500),
072: new ArrayList(500) };
073: itemAddedList = new List[] { new ArrayList(500),
074: new ArrayList(500) };
075:
076: mutex = new Mutex();
077:
078: timer = new Timer(UPDATE_DELAY, this );
079: timer.start();
080: }
081:
082: /**
083: * getInstance method
084: * @return instance
085: */
086: public static StoreEventDelegator getInstance() {
087: if (instance == null)
088: instance = new StoreEventDelegator();
089:
090: return instance;
091: }
092:
093: /**
094: * clearAllLists method
095: */
096: private void clearAllLists() {
097: itemAddedList[swap].clear();
098: itemRemovedList[swap].clear();
099: itemChangedList[swap].clear();
100: }
101:
102: /**
103: * processCalendarEvents method
104: */
105: public void processCalendarEvents() {
106: StoreEventComparator instance2 = StoreEventComparator
107: .getInstance();
108: StoreEventComparator storeEventComparator = instance2;
109: StoreEventComparator storeEventComparator2 = storeEventComparator;
110: List list = itemAddedList[swap];
111: if (list.size() > 0) {
112: LOG.info("process item added calendar events");
113:
114: Collections.sort(list, storeEventComparator2);
115:
116: // Process the events
117: for (int i = 0; i < list.size(); i++) {
118: StoreEvent next = (StoreEvent) list.get(i);
119:
120: ICalendarStore store = (ICalendarStore) next
121: .getSource();
122: try {
123: IComponentInfo model = store.get(next.getChanges());
124: // we only update changes for events currently
125: if (model.getType() == IComponent.TYPE.EVENT) {
126: Activity act = CalendarHelper
127: .createActivity((IEventInfo) model);
128:
129: ActivityDepository
130: .getInstance()
131: .addBrokedActivity(
132: act,
133: this ,
134: CategoryStructureEvent.ADDED_CREATED);
135: }
136: } catch (StoreException e) {
137: // TODO Auto-generated catch block
138: e.printStackTrace();
139: }
140: }
141: }
142: if (itemRemovedList[swap].size() > 0) {
143: Collections.sort(itemRemovedList[swap],
144: storeEventComparator2);
145:
146: // Process the events
147: for (int i = 0; i < itemRemovedList[swap].size(); i++) {
148: StoreEvent next = (StoreEvent) itemRemovedList[swap]
149: .get(i);
150: //ICalendarStore store = (ICalendarStore) next.getSource();
151: String activityId = (String) next.getChanges();
152:
153: // remove old activity
154: ActivityDepository.getInstance()
155: .removeBrokedActivityById(activityId);
156: }
157: }
158: if (itemChangedList[swap].size() > 0) {
159: Collections.sort(itemChangedList[swap],
160: storeEventComparator2);
161:
162: // Process the events
163: for (int i = 0; i < itemChangedList[swap].size(); i++) {
164: StoreEvent next = (StoreEvent) itemChangedList[swap]
165: .get(i);
166:
167: ICalendarStore store = (ICalendarStore) next
168: .getSource();
169: try {
170: IComponentInfo model = store.get(next.getChanges());
171: // we only update changes for events currently
172: if (model.getType() == IComponent.TYPE.EVENT) {
173:
174: String activityId = model.getId();
175: // remove old activity
176: ActivityDepository.getInstance()
177: .removeBrokedActivityById(activityId);
178:
179: // create new activity
180: Activity act = CalendarHelper
181: .createActivity((IEventInfo) model);
182: ActivityDepository
183: .getInstance()
184: .addBrokedActivity(
185: act,
186: this ,
187: CategoryStructureEvent.ADDED_CREATED);
188: }
189: } catch (StoreException e) {
190: // TODO Auto-generated catch block
191: e.printStackTrace();
192: }
193: }
194: }
195: }
196:
197: /* (non-Javadoc)
198: * @see org.columba.calendar.store.api.IStoreListener#itemAdded(org.columba.calendar.store.api.StoreEvent)
199: */
200: public void itemAdded(StoreEvent e) {
201: LOG.info(e.toString());
202:
203: mutex.lock();
204:
205: itemAddedList[1 - swap].add(e);
206:
207: mutex.release();
208: }
209:
210: /* (non-Javadoc)
211: * @see org.columba.calendar.store.api.IStoreListener#itemRemoved(org.columba.calendar.store.api.StoreEvent)
212: */
213: public void itemRemoved(StoreEvent e) {
214: LOG.info(e.toString());
215:
216: mutex.lock();
217:
218: itemRemovedList[1 - swap].add(e);
219:
220: mutex.release();
221: }
222:
223: /* (non-Javadoc)
224: * @see org.columba.calendar.store.api.IStoreListener#itemChanged(org.columba.calendar.store.api.StoreEvent)
225: */
226: public void itemChanged(StoreEvent e) {
227: LOG.info(e.toString());
228:
229: mutex.lock();
230:
231: itemChangedList[1 - swap].add(e);
232:
233: mutex.release();
234: }
235:
236: /* (non-Javadoc)
237: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
238: */
239: public void actionPerformed(ActionEvent e) {
240:
241: // process all events collected until now
242: mutex.lock();
243:
244: swap = 1 - swap;
245:
246: mutex.release();
247:
248: processCalendarEvents();
249:
250: clearAllLists();
251: }
252: }
|