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