001    /*
002     *  jDTAUS Core API
003     *  Copyright (c) 2005 Christian Schulte
004     *
005     *  Christian Schulte, Haldener Strasse 72, 58095 Hagen, Germany
006     *  <schulte2005@users.sourceforge.net> (+49 2331 3543887)
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
021     *
022     */
023    package org.jdtaus.core.monitor;
024    
025    import java.util.EventObject;
026    
027    /**
028     * Event produced by a {@code Task}.
029     *
030     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
031     * @version $Id: TaskEvent.java 8044 2009-07-02 01:29:05Z schulte2005 $
032     *
033     * @see TaskEventSource
034     */
035    public class TaskEvent extends EventObject
036    {
037        //--Constants---------------------------------------------------------------
038    
039        /** Event constant indicating the start of a {@code Task}. */
040        public static final int STARTED = 1001;
041    
042        /** Event constant indicating that state of a {@code Task} changed. */
043        public static final int CHANGED_STATE = 1002;
044    
045        /** Event constant indicating the end of a {@code Task}. */
046        public static final int ENDED = 1003;
047    
048        /** Serial version UID for backwards compatibility with 1.0.x classes. */
049        private static final long serialVersionUID = 4764885368541939098L;
050    
051        //---------------------------------------------------------------Constants--
052        //--Constructors------------------------------------------------------------
053    
054        /**
055         * Creates a new instance of {@code TaskEvent}.
056         *
057         * @param source the task producing the new event.
058         * @param type constant indicating the type of the event.
059         *
060         * @throws IllegalArgumentException if {@code type} is not equal to one of
061         * the constants {@code STARTED}, {@code CHANGED_STATE} or {@code ENDED}.
062         */
063        public TaskEvent( final Task source, final int type )
064        {
065            super( source );
066    
067            if ( type != STARTED && type != CHANGED_STATE && type != ENDED )
068            {
069                throw new IllegalArgumentException( Integer.toString( type ) );
070            }
071    
072            this.type = type;
073        }
074    
075        //------------------------------------------------------------Constructors--
076        //--TaskEvent---------------------------------------------------------------
077    
078        /**
079         * Event type.
080         * @serial
081         */
082        private final int type;
083    
084        /**
085         * Getter for property {@code type}.
086         *
087         * @return the type of the event.
088         */
089        public final int getType()
090        {
091            return this.type;
092        }
093    
094        /**
095         * Gets the {@code Task} producing the event.
096         *
097         * @return the source of the event.
098         */
099        public final Task getTask()
100        {
101            return (Task) this.getSource();
102        }
103    
104        /**
105         * Creates a string representing the properties of the instance.
106         *
107         * @return a string representing the properties of the instance.
108         */
109        private String internalString()
110        {
111            return new StringBuffer( 500 ).append( '{' ).
112                append( "source=" ).append( this.source ).
113                append( ", type=" ).append( this.type ).
114                append( '}' ).toString();
115    
116        }
117    
118        //---------------------------------------------------------------TaskEvent--
119        //--Object------------------------------------------------------------------
120    
121        /**
122         * Returns a string representation of the object.
123         *
124         * @return a string representation of the object.
125         */
126        public String toString()
127        {
128            return super.toString() + this.internalString();
129        }
130    
131        //------------------------------------------------------------------Object--
132    }