001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.core.support.umlmessagingcore;
043:
044: import java.util.Calendar;
045: import java.util.Date;
046:
047: import org.netbeans.modules.uml.core.support.umlutils.ETList;
048: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
049:
050: /**
051: *
052: */
053: public class MessageData implements IMessageData, Cloneable {
054: private String m_MessageString = null;
055: private int m_MessageType = 0;
056: private String m_Facility = null;
057: private ETList<IMessageData> m_SubMessages = new ETArrayList<IMessageData>();
058: private long m_osBinaryTime = new Date().getTime();
059:
060: /**
061: * Adds this message to the list of sub messages
062: *
063: * @param nMessageType[in]
064: * @param hInstance[in]
065: * @param nFacilityStringID[in]
066: * @param sMessageString[in]
067: * @return pCreatedMessage[out]
068: */
069: public IMessageData addSubMessage(
070: /* MESSAGE_TYPE */int nMessageType, int hInstance,
071: int nFacilityStringID, String sMessageString) {
072: String sFacilityString = "";
073: if (sFacilityString == null || sFacilityString.length() == 0) {
074: sFacilityString = "Unknown";
075: }
076: return addSubMessage(nMessageType, sFacilityString,
077: sMessageString);
078: }
079:
080: /**
081: * Adds this message to the list of sub messages
082: *
083: * @param nMessageType[in]
084: * @param sFacility[in]
085: * @param sMessageString[in]
086: * @return pCreateMessage[out]
087: */
088: public IMessageData addSubMessage(
089: /* MESSAGE_TYPE */int nMessageType, String sFacility,
090: String sMessageString) {
091: IMessageData subMsg = new MessageData();
092: if (subMsg != null) {
093: subMsg.setDetails(nMessageType, sFacility, sMessageString);
094: addSubMessage(subMsg);
095: }
096: return subMsg;
097: }
098:
099: /**
100: * Adds this message to the list of sub messages
101: *
102: * @param pSubMessage [in] The child message to this one.
103: */
104: public void addSubMessage(IMessageData pSubMessage) {
105: ETList<IMessageData> msgDatas = getSubMessages();
106: if (msgDatas != null) {
107: msgDatas.add(pSubMessage);
108: }
109: this .m_SubMessages = msgDatas;
110: }
111:
112: /**
113: * Sets various details of the message
114: */
115: public void setDetails( /* MESSAGE_TYPE */int nMessageType,
116: String sFacility, String value) {
117: setMessageType(nMessageType);
118: setFacility(sFacility);
119: setMessageString(value);
120: }
121:
122: /**
123: * Returns the message string for this particular message.
124: *
125: * @param pVal The message string
126: */
127: public String getMessageString() {
128: return this .m_MessageString;
129: }
130:
131: /**
132: * Sets the message string for this particular message.
133: *
134: * @param newVal The message string
135: */
136: public void setMessageString(String value) {
137: this .m_MessageString = value;
138: }
139:
140: /**
141: * @return
142: */
143: public int getMessageType() {
144: return this .m_MessageType;
145: }
146:
147: /**
148: * @param i
149: */
150: public void setMessageType(int i) {
151: this .m_MessageType = i;
152: }
153:
154: public IMessageData clone() {
155: MessageData clone = new MessageData();
156: clone.setFacility(getFacility());
157: clone.setMessageType(getMessageType());
158: clone.setMessageString(getMessageString());
159: clone.setSubMessages(getSubMessages());
160: clone.setTimeT(getTimeT());
161: return clone;
162: }
163:
164: /**
165: * @return
166: */
167: public String getFacility() {
168: return this .m_Facility;
169: }
170:
171: /**
172: * @param string
173: */
174: public void setFacility(String string) {
175: this .m_Facility = string;
176: }
177:
178: /**
179: * Gets the string formatted such that the time stamp, facility, message type, and
180: * message string are returned in one BSTR.
181: *
182: * @param bAddTimestamp Should the time.stamp be added to the message
183: * @param pFormattedString The returned formatted message string.
184: */
185: public String getFormattedMessageString(boolean bAddTimestamp) {
186: String messageType = getMessageTypeString();
187: if (messageType == null || messageType.length() == 0) {
188: messageType = "Unknown";
189: }
190: StringBuffer formatedBuff = new StringBuffer();
191: if (bAddTimestamp) {
192: String timestamp = getTimestamp();
193:
194: if (timestamp != null) {
195: formatedBuff.append(timestamp).append(" [").append(
196: this .m_Facility).append(" (").append(
197: messageType).append(")] ").append(
198: this .m_MessageString);
199: } else {
200: formatedBuff.append(" [").append(this .m_Facility)
201: .append(" (").append(messageType).append(")] ")
202: .append(this .m_MessageString);
203: }
204: } else {
205: formatedBuff.append(" [").append(this .m_Facility).append(
206: " (").append(this .m_Facility).append(")] ").append(
207: this .m_MessageString);
208: }
209: return formatedBuff.toString();
210: }
211:
212: public String getTimestamp() {
213: Date dat = new Date(this .m_osBinaryTime);
214: Calendar cal = Calendar.getInstance();
215: cal.setTime(dat);
216: StringBuffer buf = new StringBuffer();
217: buf.append(cal.get(Calendar.YEAR)).append("-").append(
218: cal.get(Calendar.MONTH)).append("-").append(
219: cal.get(Calendar.DAY_OF_WEEK)).append(" ").append(
220: cal.get(Calendar.HOUR)).append(":").append(
221: cal.get(Calendar.MINUTE)).append(":").append(
222: cal.get(Calendar.SECOND));
223: return buf.toString();
224: }
225:
226: /**
227: * Returns the message type for this message as a string.
228: *
229: * @param pVal The message type
230: */
231: public String getMessageTypeString() {
232: String messageType = new String();
233: if (this .m_MessageType == MsgCoreConstants.MT_ERROR) {
234: messageType = "Error";
235: } else if (m_MessageType == MsgCoreConstants.MT_WARNING) {
236: messageType = "Warning";
237: } else if (m_MessageType == MsgCoreConstants.MT_INFO) {
238: messageType = "Info";
239: } else if (m_MessageType == MsgCoreConstants.MT_DEBUG) {
240: messageType = "Debug";
241: } else if (m_MessageType == MsgCoreConstants.MT_CRITICAL) {
242: messageType = "Critical";
243: } else {
244: messageType = "Unknown";
245: }
246: return messageType;
247: }
248:
249: /**
250: * Returns a list of all the submessages for this message.
251: *
252: * @param pVal The sub messages
253: */
254: public ETList<IMessageData> getSubMessages() {
255: return this .m_SubMessages;
256: }
257:
258: public void setSubMessages(ETList<IMessageData> value) {
259: this .m_SubMessages = value;
260: }
261:
262: public long getTimeT() {
263: return this .m_osBinaryTime;
264: }
265:
266: public void setTimeT(long time) {
267: this.m_osBinaryTime = time;
268: }
269: }
|