KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > store > AbstractCalendarStore


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.calendar.store;
19
20 import javax.swing.event.EventListenerList JavaDoc;
21
22 import org.columba.calendar.model.api.IComponentInfo;
23 import org.columba.calendar.model.api.IComponentInfoList;
24 import org.columba.calendar.store.api.IStoreListener;
25 import org.columba.calendar.store.api.StoreEvent;
26 import org.columba.calendar.store.api.StoreException;
27
28 public abstract class AbstractCalendarStore {
29
30     private EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
31
32     public AbstractCalendarStore() {
33         super();
34
35         // register interest on store changes
36
// TODO the dependency should be the other way around
37
addStorageListener(StoreEventDelegator.getInstance());
38
39     }
40
41     public abstract IComponentInfo get(Object JavaDoc id) throws StoreException;
42
43     public abstract void add(IComponentInfo basicModel) throws StoreException;
44
45     public void modify(Object JavaDoc id, IComponentInfo basicModel) throws StoreException {
46         if (id == null)
47             throw new IllegalArgumentException JavaDoc("id == null");
48         if (basicModel == null)
49             throw new IllegalArgumentException JavaDoc("basicModel == null");
50
51         fireItemChanged(id);
52     }
53
54     public void remove(Object JavaDoc id) throws StoreException {
55         if (id == null)
56             throw new IllegalArgumentException JavaDoc("id == null");
57         fireItemRemoved(id);
58     }
59
60     public abstract IComponentInfoList getComponentInfoList()
61             throws StoreException;
62
63     public abstract boolean exists(Object JavaDoc id) throws StoreException;
64
65     /** ********************** event ****************************** */
66
67     /**
68      * Adds a listener.
69      */

70     public void addStorageListener(IStoreListener l) {
71         listenerList.add(IStoreListener.class, l);
72     }
73
74     /**
75      * Removes a previously registered listener.
76      */

77     public void removeStorageListener(IStoreListener l) {
78         listenerList.remove(IStoreListener.class, l);
79     }
80
81     /**
82      * Propagates an event to all registered listeners notifying them of a item
83      * addition.
84      */

85     protected void fireItemAdded(Object JavaDoc uid) {
86
87         StoreEvent e = new StoreEvent(this, uid);
88         // Guaranteed to return a non-null array
89
Object JavaDoc[] listeners = listenerList.getListenerList();
90
91         // Process the listeners last to first, notifying
92
// those that are interested in this event
93
for (int i = listeners.length - 2; i >= 0; i -= 2) {
94             if (listeners[i] == IStoreListener.class) {
95                 ((IStoreListener) listeners[i + 1]).itemAdded(e);
96             }
97         }
98     }
99
100     /**
101      * Propagates an event to all registered listeners notifying them of a item
102      * removal.
103      */

104     protected void fireItemRemoved(Object JavaDoc uid) {
105
106         StoreEvent e = new StoreEvent(this, uid);
107         // Guaranteed to return a non-null array
108
Object JavaDoc[] listeners = listenerList.getListenerList();
109
110         // Process the listeners last to first, notifying
111
// those that are interested in this event
112
for (int i = listeners.length - 2; i >= 0; i -= 2) {
113             if (listeners[i] == IStoreListener.class) {
114                 ((IStoreListener) listeners[i + 1]).itemRemoved(e);
115             }
116         }
117     }
118
119     /**
120      * Propagates an event to all registered listeners notifying them of a item
121      * change.
122      */

123     protected void fireItemChanged(Object JavaDoc uid) {
124
125         StoreEvent e = new StoreEvent(this, uid);
126         // Guaranteed to return a non-null array
127
Object JavaDoc[] listeners = listenerList.getListenerList();
128
129         // Process the listeners last to first, notifying
130
// those that are interested in this event
131
for (int i = listeners.length - 2; i >= 0; i -= 2) {
132             if (listeners[i] == IStoreListener.class) {
133                 ((IStoreListener) listeners[i + 1]).itemChanged(e);
134             }
135         }
136     }
137 }
138
Popular Tags