001: package org.columba.core.context.semantic;
002:
003: import java.util.logging.Logger;
004:
005: import javax.swing.event.EventListenerList;
006:
007: import org.columba.core.context.base.api.IAttributeType;
008: import org.columba.core.context.base.api.IStructureType;
009: import org.columba.core.context.base.api.IStructureValue;
010: import org.columba.core.context.base.api.MULTIPLICITY;
011: import org.columba.core.context.base.api.IAttributeType.BASETYPE;
012: import org.columba.core.context.semantic.api.IContextEvent;
013: import org.columba.core.context.semantic.api.IContextListener;
014: import org.columba.core.context.semantic.api.ISemanticContext;
015: import org.columba.core.main.MainInterface;
016:
017: public class SemanticContext implements ISemanticContext {
018:
019: private static final Logger LOG = Logger
020: .getLogger("org.columba.core.context.semantic.SemanticContext");
021:
022: protected EventListenerList listenerList = new EventListenerList();
023:
024: private IStructureType type;
025:
026: private IStructureValue value;
027:
028: public SemanticContext() {
029: super ();
030:
031: initContext();
032: }
033:
034: // initialize context
035: private void initContext() {
036:
037: // <context>
038: // <core>
039: // <identity>
040: // </identity>
041: // <datetime>
042: // </datetime>
043: // </core>
044: // </context>
045: type = MainInterface.contextFactory.createStructure("context",
046: ISemanticContext.CONTEXT_NAMESPACE_CORE);
047:
048: // identity definition
049: IStructureType identity = type.addChild(
050: ISemanticContext.CONTEXT_NODE_IDENTITY,
051: ISemanticContext.CONTEXT_NAMESPACE_CORE);
052: // MULTIPLICITY.ZERO_TO_ONE is default
053: IAttributeType emailAddress = identity.addAttribute(
054: ISemanticContext.CONTEXT_ATTR_EMAIL_ADDRESS,
055: ISemanticContext.CONTEXT_NAMESPACE_CORE);
056: identity.addAttribute(
057: ISemanticContext.CONTEXT_ATTR_DISPLAY_NAME,
058: ISemanticContext.CONTEXT_NAMESPACE_CORE);
059: identity.addAttribute(ISemanticContext.CONTEXT_ATTR_FIRST_NAME,
060: ISemanticContext.CONTEXT_NAMESPACE_CORE);
061: identity.addAttribute(ISemanticContext.CONTEXT_ATTR_LAST_NAME,
062: ISemanticContext.CONTEXT_NAMESPACE_CORE);
063: identity.addAttribute(ISemanticContext.CONTEXT_ATTR_WEBSITE,
064: ISemanticContext.CONTEXT_NAMESPACE_CORE);
065:
066: // date time timezone definition
067: IStructureType dateTime = type.addChild(
068: ISemanticContext.CONTEXT_NODE_DATE_TIME,
069: ISemanticContext.CONTEXT_NAMESPACE_CORE);
070: IAttributeType date = dateTime.addAttribute(
071: ISemanticContext.CONTEXT_ATTR_DATE,
072: ISemanticContext.CONTEXT_NAMESPACE_CORE);
073: IAttributeType timeZone = dateTime.addAttribute(
074: ISemanticContext.CONTEXT_ATTR_TIME_ZONE,
075: ISemanticContext.CONTEXT_NAMESPACE_CORE);
076: date.setBaseType(BASETYPE.DATE);
077:
078: // date range (start time, end time) definition
079: IStructureType dateRange = type.addChild(
080: ISemanticContext.CONTEXT_NODE_DATERANGE,
081: ISemanticContext.CONTEXT_NAMESPACE_CORE);
082: IAttributeType startStart = dateRange.addAttribute(
083: ISemanticContext.CONTEXT_ATTR_STARTDATE,
084: ISemanticContext.CONTEXT_NAMESPACE_CORE);
085: startStart.setBaseType(BASETYPE.DATE);
086: IAttributeType endDate = dateRange.addAttribute(
087: ISemanticContext.CONTEXT_ATTR_ENDDATE,
088: ISemanticContext.CONTEXT_NAMESPACE_CORE);
089: endDate.setBaseType(BASETYPE.DATE);
090:
091: // document definition
092: IStructureType document = type.addChild("document",
093: ISemanticContext.CONTEXT_NAMESPACE_CORE);
094: document.addAttribute("author",
095: ISemanticContext.CONTEXT_NAMESPACE_CORE);
096: document.addAttribute("title",
097: ISemanticContext.CONTEXT_NAMESPACE_CORE);
098: document.addAttribute("summary",
099: ISemanticContext.CONTEXT_NAMESPACE_CORE);
100: document.addAttribute("body",
101: ISemanticContext.CONTEXT_NAMESPACE_CORE);
102:
103: // locale definition
104: IStructureType locale = type.addChild("locale",
105: ISemanticContext.CONTEXT_NAMESPACE_CORE);
106: locale.addAttribute("language",
107: ISemanticContext.CONTEXT_NAMESPACE_CORE);
108: locale.addAttribute("country",
109: ISemanticContext.CONTEXT_NAMESPACE_CORE);
110: locale.addAttribute("variant",
111: ISemanticContext.CONTEXT_NAMESPACE_CORE);
112:
113: // list of attachments
114: IStructureType attachmentList = type.addChild("attachmentList",
115: ISemanticContext.CONTEXT_NAMESPACE_CORE);
116: IStructureType attachment = attachmentList.addChild(
117: "attachment", ISemanticContext.CONTEXT_NAMESPACE_CORE);
118: attachment.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
119: // single attachment
120: attachment.addAttribute("name",
121: ISemanticContext.CONTEXT_NAMESPACE_CORE);
122: IAttributeType contentType = attachment.addAttribute("content",
123: ISemanticContext.CONTEXT_NAMESPACE_CORE);
124: contentType.setBaseType(BASETYPE.BINARY);
125:
126: // message
127: IStructureType message = type.addChild(
128: ISemanticContext.CONTEXT_NODE_MESSAGE,
129: ISemanticContext.CONTEXT_NAMESPACE_CORE);
130: message.addAttribute(ISemanticContext.CONTEXT_ATTR_SUBJECT,
131: ISemanticContext.CONTEXT_NAMESPACE_CORE);
132: // single sender - re-use identity type
133: IStructureType sender = message.addChild(
134: ISemanticContext.CONTEXT_ATTR_SENDER,
135: ISemanticContext.CONTEXT_NAMESPACE_CORE);
136: sender.addChild(identity);
137: sender.setCardinality(MULTIPLICITY.ONE_TO_ONE);
138: // re-use identity type for recipient list
139: IStructureType recipients = message.addChild(
140: ISemanticContext.CONTEXT_NODE_MESSAGE_RECIPIENTS,
141: ISemanticContext.CONTEXT_NAMESPACE_CORE);
142: recipients.setCardinality(MULTIPLICITY.ZERO_TO_MANY);
143: recipients.addChild(identity);
144: // message body
145: message.addAttribute(ISemanticContext.CONTEXT_ATTR_BODY_TEXT,
146: ISemanticContext.CONTEXT_NAMESPACE_CORE);
147: message.addAttribute(
148: ISemanticContext.CONTEXT_ATTR_SELECTED_BODYTEXT,
149: ISemanticContext.CONTEXT_NAMESPACE_CORE);
150: IAttributeType arrivalDate = message.addAttribute(
151: ISemanticContext.CONTEXT_ATTR_DATE,
152: ISemanticContext.CONTEXT_NAMESPACE_CORE);
153: arrivalDate.setBaseType(BASETYPE.DATE);
154: // message contains list of attachments
155: message.addChild(attachmentList);
156:
157: IStructureType event = type.addChild(
158: ISemanticContext.CONTEXT_NODE_EVENT,
159: ISemanticContext.CONTEXT_NAMESPACE_CORE);
160: IStructureType dateRangeType = event.addChild(dateRange);
161: dateRangeType.setCardinality(MULTIPLICITY.ONE_TO_ONE);
162: event.addAttribute(ISemanticContext.CONTEXT_ATTR_SUMMARY,
163: ISemanticContext.CONTEXT_NAMESPACE_CORE);
164: event.addAttribute(ISemanticContext.CONTEXT_ATTR_DESCRIPTION,
165: ISemanticContext.CONTEXT_NAMESPACE_CORE);
166: event.addAttribute(ISemanticContext.CONTEXT_ATTR_LOCATION,
167: ISemanticContext.CONTEXT_NAMESPACE_CORE);
168: }
169:
170: public IStructureValue createValue() {
171: value = MainInterface.contextFactory.createValue("context",
172: ISemanticContext.CONTEXT_NAMESPACE_CORE, type);
173:
174: return value;
175: }
176:
177: public IStructureType getType() {
178: return this .type;
179: }
180:
181: public synchronized IStructureValue getValue() {
182: return this .value;
183: }
184:
185: public synchronized void setValue(IStructureValue value) {
186: this .value = value;
187:
188: // notify all listeners
189: fireContextChangedEvent(value);
190: }
191:
192: public void addContextListener(IContextListener l) {
193: listenerList.add(IContextListener.class, l);
194:
195: }
196:
197: public void removeContextListener(IContextListener l) {
198: listenerList.remove(IContextListener.class, l);
199: }
200:
201: protected void fireContextChangedEvent(IStructureValue value) {
202:
203: IContextEvent e = new ContextEvent(this , value);
204: // Guaranteed to return a non-null array
205: Object[] listeners = listenerList.getListenerList();
206:
207: // Process the listeners last to first, notifying
208: // those that are interested in this event
209: for (int i = listeners.length - 2; i >= 0; i -= 2) {
210: if (listeners[i] == IContextListener.class) {
211: ((IContextListener) listeners[i + 1]).contextChanged(e);
212: }
213: }
214: }
215:
216: }
|