001    /*
002     * Copyright 2003-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.events.observable;
017    
018    import java.util.Collection;
019    import java.util.EventObject;
020    
021    /**
022     * Base event class extended by each class that encapsulates event information.
023     * <p>
024     * This class can be used as is, but generally it is subclassed.
025     *
026     * @since Commons Events 1.0
027     * @version $Revision: 155443 $ $Date: 2005-02-26 13:19:51 +0000 (Sat, 26 Feb 2005) $
028     * 
029     * @author Stephen Colebourne
030     */
031    public class ModificationEvent extends EventObject {
032    
033        /** The source collection */
034        protected final ObservableCollection collection;
035        /** The handler */
036        protected final ModificationHandler handler;
037        /** The event code */
038        protected final int type;
039    
040        // Constructor
041        //-----------------------------------------------------------------------
042        /**
043         * Constructor.
044         * 
045         * @param obsCollection  the event source
046         * @param handler  the handler
047         * @param type  the event type
048         */
049        public ModificationEvent(
050            final ObservableCollection obsCollection,
051            final ModificationHandler handler,
052            final int type) {
053    
054            super(obsCollection);
055            this.collection = obsCollection;
056            this.handler = handler;
057            this.type = type;
058        }
059    
060        // Basic info
061        //-----------------------------------------------------------------------
062        /**
063         * Gets the collection the event is reporting on.
064         * <p>
065         * Using this collection will bypass any decorators that have been added
066         * to the <code>ObservableCollection</code>. For example, if a synchronized
067         * decorator was added it will not be called by changes to this collection.
068         * <p>
069         * For the synchronization case, you are normally OK however. If you
070         * process the event in the same thread as the original change then your
071         * code will be protected by the original synchronized decorator and this
072         * collection may be used freely.
073         * 
074         * @return the collection
075         */
076        public ObservableCollection getObservedCollection() {
077            return collection;
078        }
079    
080        /**
081         * Gets the base collection underlying the observable collection.
082         * <p>
083         * Using this collection will bypass the event sending mechanism.
084         * It will also bypass any other decorators, such as synchronization.
085         * Use with care.
086         * 
087         * @return the collection
088         */
089        public Collection getBaseCollection() {
090            return handler.getBaseCollection();
091        }
092    
093        /**
094         * Gets the handler of the events.
095         * 
096         * @return the handler
097         */
098        public ModificationHandler getHandler() {
099            return handler;
100        }
101    
102        /**
103         * Gets the event type constant.
104         * <p>
105         * This is one of the <i>method</i> constants from {@link ModificationEventType}.
106         * 
107         * @return the method event type constant
108         */
109        public int getType() {
110            return type;
111        }
112    
113        // toString
114        //-----------------------------------------------------------------------
115        /**
116         * Gets a debugging string version of the event.
117         * 
118         * @return a debugging string
119         */
120        public String toString() {
121            StringBuffer buf = new StringBuffer(64);
122            buf.append("ModificationEvent[type=");
123            buf.append(ModificationEventType.toString(type));
124            buf.append(']');
125            return buf.toString();
126        }
127    
128    }