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 javax.swing.event.EventListenerList;
021:
022: import org.columba.calendar.model.api.IComponentInfo;
023: import org.columba.calendar.model.api.IComponentInfoList;
024: import org.columba.calendar.store.api.IStoreListener;
025: import org.columba.calendar.store.api.StoreEvent;
026: import org.columba.calendar.store.api.StoreException;
027:
028: public abstract class AbstractCalendarStore {
029:
030: private EventListenerList listenerList = new EventListenerList();
031:
032: public AbstractCalendarStore() {
033: super ();
034:
035: // register interest on store changes
036: // TODO the dependency should be the other way around
037: addStorageListener(StoreEventDelegator.getInstance());
038:
039: }
040:
041: public abstract IComponentInfo get(Object id) throws StoreException;
042:
043: public abstract void add(IComponentInfo basicModel)
044: throws StoreException;
045:
046: public void modify(Object id, IComponentInfo basicModel)
047: throws StoreException {
048: if (id == null)
049: throw new IllegalArgumentException("id == null");
050: if (basicModel == null)
051: throw new IllegalArgumentException("basicModel == null");
052:
053: fireItemChanged(id);
054: }
055:
056: public void remove(Object id) throws StoreException {
057: if (id == null)
058: throw new IllegalArgumentException("id == null");
059: fireItemRemoved(id);
060: }
061:
062: public abstract IComponentInfoList getComponentInfoList()
063: throws StoreException;
064:
065: public abstract boolean exists(Object id) throws StoreException;
066:
067: /** ********************** event ****************************** */
068:
069: /**
070: * Adds a listener.
071: */
072: public void addStorageListener(IStoreListener l) {
073: listenerList.add(IStoreListener.class, l);
074: }
075:
076: /**
077: * Removes a previously registered listener.
078: */
079: public void removeStorageListener(IStoreListener l) {
080: listenerList.remove(IStoreListener.class, l);
081: }
082:
083: /**
084: * Propagates an event to all registered listeners notifying them of a item
085: * addition.
086: */
087: protected void fireItemAdded(Object uid) {
088:
089: StoreEvent e = new StoreEvent(this , uid);
090: // Guaranteed to return a non-null array
091: Object[] listeners = listenerList.getListenerList();
092:
093: // Process the listeners last to first, notifying
094: // those that are interested in this event
095: for (int i = listeners.length - 2; i >= 0; i -= 2) {
096: if (listeners[i] == IStoreListener.class) {
097: ((IStoreListener) listeners[i + 1]).itemAdded(e);
098: }
099: }
100: }
101:
102: /**
103: * Propagates an event to all registered listeners notifying them of a item
104: * removal.
105: */
106: protected void fireItemRemoved(Object uid) {
107:
108: StoreEvent e = new StoreEvent(this , uid);
109: // Guaranteed to return a non-null array
110: Object[] listeners = listenerList.getListenerList();
111:
112: // Process the listeners last to first, notifying
113: // those that are interested in this event
114: for (int i = listeners.length - 2; i >= 0; i -= 2) {
115: if (listeners[i] == IStoreListener.class) {
116: ((IStoreListener) listeners[i + 1]).itemRemoved(e);
117: }
118: }
119: }
120:
121: /**
122: * Propagates an event to all registered listeners notifying them of a item
123: * change.
124: */
125: protected void fireItemChanged(Object uid) {
126:
127: StoreEvent e = new StoreEvent(this , uid);
128: // Guaranteed to return a non-null array
129: Object[] listeners = listenerList.getListenerList();
130:
131: // Process the listeners last to first, notifying
132: // those that are interested in this event
133: for (int i = listeners.length - 2; i >= 0; i -= 2) {
134: if (listeners[i] == IStoreListener.class) {
135: ((IStoreListener) listeners[i + 1]).itemChanged(e);
136: }
137: }
138: }
139: }
|