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 the CVS Client Library.
027: * The Initial Developer of the Original Software is Robert Greig.
028: * Portions created by Robert Greig are Copyright (C) 2000.
029: * All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041:
042: * Contributor(s): Robert Greig.
043: *****************************************************************************/package org.netbeans.lib.cvsclient.event;
044:
045: /**
046: * An event sent from the server to indicate that a message should be
047: * displayed to the user
048: * @author Robert Greig
049: */
050: public class MessageEvent extends CVSEvent {
051: /**
052: * Holds value of property message.
053: */
054: private String message;
055:
056: /**
057: * Whether the message is an error message
058: */
059: private boolean error;
060:
061: /** Holds value of property tagged. */
062: private boolean tagged;
063:
064: private final byte[] raw;
065:
066: public MessageEvent(Object source, String message, byte[] raw,
067: boolean isError) {
068: super (source);
069: setMessage(message);
070: setError(isError);
071: setTagged(false);
072: this .raw = raw;
073: }
074:
075: /**
076: * Construct a MessageEvent
077: * @param source the source of the event
078: * @param message the message text
079: * @param isError true if the message is an error message (i.e. intended
080: * for stderr rather than stdout), false otherwise
081: */
082: public MessageEvent(Object source, String message, boolean isError) {
083: this (source, message, null, isError);
084: }
085:
086: /**
087: * Construct a MessageEvent with no message text
088: * @param source the source of the event
089: */
090: public MessageEvent(Object source) {
091: this (source, null, false);
092: }
093:
094: /**
095: * Getter for property message.
096: * @return Value of property message.
097: */
098: public String getMessage() {
099: return message;
100: }
101:
102: /** @return bytes from wire or null */
103: public byte[] getRawData() {
104: return raw;
105: }
106:
107: /**
108: * Setter for property message.
109: * @param message New value of property message.
110: */
111: public void setMessage(String message) {
112: this .message = message;
113: }
114:
115: /**
116: * Get whether the message should be displayed in stderr
117: * @return true if the message should be sent to stderr, false otherwise
118: */
119: public boolean isError() {
120: return error;
121: }
122:
123: /**
124: * Set whether the message should go to stderr
125: * @param error true if the message is an error message, false otherwise
126: */
127: public void setError(boolean error) {
128: this .error = error;
129: }
130:
131: /**
132: * Fire the event to the event listener. Subclasses should call the
133: * appropriate method on the listener to dispatch this event.
134: * @param listener the event listener
135: */
136: protected void fireEvent(CVSListener listener) {
137: listener.messageSent(this );
138: }
139:
140: /** Getter for property tagged.
141: * @return Value of property tagged.
142: */
143: public boolean isTagged() {
144: return tagged;
145: }
146:
147: /** Setter for property tagged.
148: * @param tagged New value of property tagged.
149: */
150: public void setTagged(boolean tagged) {
151: this .tagged = tagged;
152: }
153:
154: /**
155: * Parses the tagged message using the specified buffer.
156: * @returns != null, if the line is finished and could be processed
157: */
158: public static String parseTaggedMessage(
159: StringBuffer taggedLineBufferNotNull, String taggedMessage) {
160: String line = taggedMessage;
161:
162: if (line.charAt(0) == '+' || line.charAt(0) == '-') {
163: return null;
164: }
165:
166: String result = null;
167: if (line.equals("newline")) {//NOI18N
168: result = taggedLineBufferNotNull.toString();
169: taggedLineBufferNotNull.setLength(0);
170: }
171: int index = line.indexOf(' ');
172: if (index > 0) {
173: taggedLineBufferNotNull.append(line.substring(index + 1));
174: }
175: return result;
176: }
177: }
|