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 org.netbeans.modules.uml.core.coreapplication.CoreProductManager;
045: import org.netbeans.modules.uml.core.coreapplication.ICoreProduct;
046: import org.netbeans.modules.uml.core.coreapplication.ICoreProductManager;
047:
048: /**
049: * This is a messenger helper class. It automatically gets the correct message service so
050: * you don't have to remember how to do it. Users just call SendMessage to send messages out
051: * the interface.
052: *
053: */
054: public class UMLMessagingHelper {
055: private IMessageService m_pMessageService = null;
056: private String m_Facility = null;
057:
058: public UMLMessagingHelper() {
059: }
060:
061: public UMLMessagingHelper(String facility) {
062: m_Facility = facility;
063: }
064:
065: /**
066: * Sends a message.
067: *
068: * @param pMessageData The message to be sent.
069: */
070: public void sendMessage(IMessageData pMessageData) {
071: IMessageService pMessageService = getMessageService();
072: if (pMessageService != null) {
073: pMessageService.addMessage(pMessageData);
074: }
075: }
076:
077: /**
078: * Sends a message.
079: *
080: * @param MESSAGE_TYPE The type of the message
081: * @param message The message
082: */
083: public void sendMessage(int messageType, String message) {
084: IMessageService pMessageService = getMessageService();
085: if (pMessageService != null) {
086: pMessageService
087: .addMessage(messageType, m_Facility, message);
088: }
089: }
090:
091: /**
092: * Returns an IMessageService. If possible it returns a cached one, otherwise this routine goes to the
093: * product manager and then to the product to get the message service. Note that the message service is
094: * not a smart pointer 'cause I don't want this little helper class to be holding onto the service.
095: */
096: public IMessageService getMessageService() {
097: if (m_pMessageService == null) {
098: ICoreProductManager pProductManager = CoreProductManager
099: .instance();
100: ICoreProduct pCoreProduct = pProductManager
101: .getCoreProduct();
102: if (pCoreProduct != null) {
103: m_pMessageService = pCoreProduct.getMessageService();
104: }
105: }
106: return m_pMessageService;
107: }
108:
109: /**
110: * Sends a message that is marked as a critical error.
111: *
112: * @param message The message.
113: */
114: public void sendCriticalMessage(String message) {
115: sendMessage(MsgCoreConstants.MT_CRITICAL, message);
116: }
117:
118: /**
119: * Sends a message that is marked as an error.
120: *
121: * @param message The message.
122: */
123: public void sendErrorMessage(String message) {
124: sendMessage(MsgCoreConstants.MT_ERROR, message);
125: }
126:
127: public void sendWarningMessage(String message) {
128: sendMessage(MsgCoreConstants.MT_WARNING, message);
129: }
130:
131: /**
132: * Sends a message that is marked as an informational message.
133: *
134: * @param message The message.
135: */
136: public void sendInfoMessage(String message) {
137: sendMessage(MsgCoreConstants.MT_INFO, message);
138: }
139:
140: /**
141: * Sends a message that is marked as a debug message.
142: *
143: * @param message The message.
144: */
145: public void sendDebugMessage(String message) {
146: sendMessage(MsgCoreConstants.MT_DEBUG, message);
147: }
148:
149: /**
150: * Sends a message that is marked as an error message. The error message
151: * is retrieved from the throwable exception.
152: *
153: * @param message The message.
154: */
155: public void sendExceptionMessage(Throwable e) {
156: sendMessage(MsgCoreConstants.MT_ERROR, e.getLocalizedMessage());
157: }
158: }
|