KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > context > semantic > SemanticContext


1 package org.columba.core.context.semantic;
2
3 import java.util.logging.Logger JavaDoc;
4
5 import javax.swing.event.EventListenerList JavaDoc;
6
7 import org.columba.core.context.base.api.IAttributeType;
8 import org.columba.core.context.base.api.IStructureType;
9 import org.columba.core.context.base.api.IStructureValue;
10 import org.columba.core.context.base.api.MULTIPLICITY;
11 import org.columba.core.context.base.api.IAttributeType.BASETYPE;
12 import org.columba.core.context.semantic.api.IContextEvent;
13 import org.columba.core.context.semantic.api.IContextListener;
14 import org.columba.core.context.semantic.api.ISemanticContext;
15 import org.columba.core.main.MainInterface;
16
17 public class SemanticContext implements ISemanticContext {
18
19     private static final Logger JavaDoc LOG = Logger
20             .getLogger("org.columba.core.context.semantic.SemanticContext");
21
22     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
23
24     private IStructureType type;
25
26     private IStructureValue value;
27
28     public SemanticContext() {
29         super();
30
31         initContext();
32     }
33
34     // initialize context
35
private void initContext() {
36
37         // <context>
38
// <core>
39
// <identity>
40
// </identity>
41
// <datetime>
42
// </datetime>
43
// </core>
44
// </context>
45
type = MainInterface.contextFactory.createStructure("context",
46                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
47
48         // identity definition
49
IStructureType identity = type.addChild(ISemanticContext.CONTEXT_NODE_IDENTITY, ISemanticContext.CONTEXT_NAMESPACE_CORE);
50         // MULTIPLICITY.ZERO_TO_ONE is default
51
IAttributeType emailAddress = identity.addAttribute(ISemanticContext.CONTEXT_ATTR_EMAIL_ADDRESS,
52                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
53         identity.addAttribute(ISemanticContext.CONTEXT_ATTR_DISPLAY_NAME, ISemanticContext.CONTEXT_NAMESPACE_CORE);
54         identity.addAttribute(ISemanticContext.CONTEXT_ATTR_FIRST_NAME, ISemanticContext.CONTEXT_NAMESPACE_CORE);
55         identity.addAttribute(ISemanticContext.CONTEXT_ATTR_LAST_NAME, ISemanticContext.CONTEXT_NAMESPACE_CORE);
56         identity.addAttribute(ISemanticContext.CONTEXT_ATTR_WEBSITE, ISemanticContext.CONTEXT_NAMESPACE_CORE);
57
58         // date time timezone definition
59
IStructureType dateTime = type.addChild(ISemanticContext.CONTEXT_NODE_DATE_TIME, ISemanticContext.CONTEXT_NAMESPACE_CORE);
60         IAttributeType date = dateTime.addAttribute(ISemanticContext.CONTEXT_ATTR_DATE, ISemanticContext.CONTEXT_NAMESPACE_CORE);
61         IAttributeType timeZone = dateTime.addAttribute(ISemanticContext.CONTEXT_ATTR_TIME_ZONE,
62                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
63         date.setBaseType(BASETYPE.DATE);
64
65         // date range (start time, end time) definition
66
IStructureType dateRange = type.addChild(ISemanticContext.CONTEXT_NODE_DATERANGE,
67                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
68         IAttributeType startStart = dateRange.addAttribute(ISemanticContext.CONTEXT_ATTR_STARTDATE,
69                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
70         startStart.setBaseType(BASETYPE.DATE);
71         IAttributeType endDate = dateRange.addAttribute(ISemanticContext.CONTEXT_ATTR_ENDDATE,
72                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
73         endDate.setBaseType(BASETYPE.DATE);
74
75         // document definition
76
IStructureType document = type.addChild("document", ISemanticContext.CONTEXT_NAMESPACE_CORE);
77         document.addAttribute("author", ISemanticContext.CONTEXT_NAMESPACE_CORE);
78         document.addAttribute("title", ISemanticContext.CONTEXT_NAMESPACE_CORE);
79         document.addAttribute("summary", ISemanticContext.CONTEXT_NAMESPACE_CORE);
80         document.addAttribute("body", ISemanticContext.CONTEXT_NAMESPACE_CORE);
81
82         // locale definition
83
IStructureType locale = type.addChild("locale", ISemanticContext.CONTEXT_NAMESPACE_CORE);
84         locale.addAttribute("language", ISemanticContext.CONTEXT_NAMESPACE_CORE);
85         locale.addAttribute("country", ISemanticContext.CONTEXT_NAMESPACE_CORE);
86         locale.addAttribute("variant", ISemanticContext.CONTEXT_NAMESPACE_CORE);
87
88         // list of attachments
89
IStructureType attachmentList = type.addChild("attachmentList",
90                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
91         IStructureType attachment = attachmentList.addChild("attachment",
92                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
93         attachment.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
94         // single attachment
95
attachment.addAttribute("name", ISemanticContext.CONTEXT_NAMESPACE_CORE);
96         IAttributeType contentType = attachment.addAttribute("content",
97                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
98         contentType.setBaseType(BASETYPE.BINARY);
99
100         // message
101
IStructureType message = type.addChild(ISemanticContext.CONTEXT_NODE_MESSAGE, ISemanticContext.CONTEXT_NAMESPACE_CORE);
102         message.addAttribute(ISemanticContext.CONTEXT_ATTR_SUBJECT, ISemanticContext.CONTEXT_NAMESPACE_CORE);
103         // single sender - re-use identity type
104
IStructureType sender = message.addChild(ISemanticContext.CONTEXT_ATTR_SENDER, ISemanticContext.CONTEXT_NAMESPACE_CORE);
105         sender.addChild(identity);
106         sender.setCardinality(MULTIPLICITY.ONE_TO_ONE);
107         // re-use identity type for recipient list
108
IStructureType recipients = message.addChild(ISemanticContext.CONTEXT_NODE_MESSAGE_RECIPIENTS,
109                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
110         recipients.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
111         recipients.addChild(identity);
112         // message body
113
message.addAttribute(ISemanticContext.CONTEXT_ATTR_BODY_TEXT, ISemanticContext.CONTEXT_NAMESPACE_CORE);
114         message.addAttribute(ISemanticContext.CONTEXT_ATTR_SELECTED_BODYTEXT, ISemanticContext.CONTEXT_NAMESPACE_CORE);
115         IAttributeType arrivalDate = message.addAttribute(ISemanticContext.CONTEXT_ATTR_DATE, ISemanticContext.CONTEXT_NAMESPACE_CORE);
116         arrivalDate.setBaseType(BASETYPE.DATE);
117         // message contains list of attachments
118
message.addChild(attachmentList);
119         
120         IStructureType event = type.addChild(ISemanticContext.CONTEXT_NODE_EVENT, ISemanticContext.CONTEXT_NAMESPACE_CORE);
121         IStructureType dateRangeType = event.addChild(dateRange);
122         dateRangeType.setCardinality(MULTIPLICITY.ONE_TO_ONE);
123         event.addAttribute(ISemanticContext.CONTEXT_ATTR_SUMMARY, ISemanticContext.CONTEXT_NAMESPACE_CORE);
124         event.addAttribute(ISemanticContext.CONTEXT_ATTR_DESCRIPTION, ISemanticContext.CONTEXT_NAMESPACE_CORE);
125         event.addAttribute(ISemanticContext.CONTEXT_ATTR_LOCATION, ISemanticContext.CONTEXT_NAMESPACE_CORE);
126     }
127
128     public IStructureValue createValue() {
129         value = MainInterface.contextFactory.createValue("context",
130                 ISemanticContext.CONTEXT_NAMESPACE_CORE, type);
131
132         return value;
133     }
134
135     public IStructureType getType() {
136         return this.type;
137     }
138
139     public synchronized IStructureValue getValue() {
140         return this.value;
141     }
142
143     public synchronized void setValue(IStructureValue value) {
144         this.value = value;
145         
146         // notify all listeners
147
fireContextChangedEvent(value);
148     }
149     
150     public void addContextListener(IContextListener l) {
151         listenerList.add(IContextListener.class, l);
152
153     }
154
155     public void removeContextListener(IContextListener l) {
156         listenerList.remove(IContextListener.class, l);
157     }
158
159     protected void fireContextChangedEvent(IStructureValue value) {
160
161         IContextEvent e = new ContextEvent(this, value);
162         // Guaranteed to return a non-null array
163
Object JavaDoc[] listeners = listenerList.getListenerList();
164
165         // Process the listeners last to first, notifying
166
// those that are interested in this event
167
for (int i = listeners.length - 2; i >= 0; i -= 2) {
168             if (listeners[i] == IContextListener.class) {
169                 ((IContextListener) listeners[i + 1]).contextChanged(e);
170             }
171         }
172     }
173
174     
175
176 }
177
Popular Tags