001    /*
002     * This file is part of McIDAS-V
003     *
004     * Copyright 2007-2013
005     * Space Science and Engineering Center (SSEC)
006     * University of Wisconsin - Madison
007     * 1225 W. Dayton Street, Madison, WI 53706, USA
008     * https://www.ssec.wisc.edu/mcidas
009     * 
010     * All Rights Reserved
011     * 
012     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013     * some McIDAS-V source code is based on IDV and VisAD source code.  
014     * 
015     * McIDAS-V is free software; you can redistribute it and/or modify
016     * it under the terms of the GNU Lesser Public License as published by
017     * the Free Software Foundation; either version 3 of the License, or
018     * (at your option) any later version.
019     * 
020     * McIDAS-V is distributed in the hope that it will be useful,
021     * but WITHOUT ANY WARRANTY; without even the implied warranty of
022     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023     * GNU Lesser Public License for more details.
024     * 
025     * You should have received a copy of the GNU Lesser Public License
026     * along with this program.  If not, see http://www.gnu.org/licenses.
027     */
028    
029    package edu.wisc.ssec.mcidasv.probes;
030    
031    import java.util.EventObject;
032    
033    import edu.wisc.ssec.mcidasv.util.Contract;
034    
035    /**
036     * This class captures a change to a probe and stores both the previous and
037     * current (as of the event's creation) changed values.
038     */
039    @SuppressWarnings("serial")
040    public class ProbeEvent<T> extends EventObject {
041    
042        /**
043         * Previous value of the probe.
044         */
045        private final T oldValue;
046    
047        /**
048         * Current value of the probe.
049         */
050        private final T newValue;
051    
052        /**
053         * Generated when a {@link ReadoutProbe} changes either its position, 
054         * color, or visibility. Currently stores either position, color, or 
055         * visibility both before and after the change.
056         * 
057         * @param source Probe that generated this event.
058         * @param oldValue Old value of the probe.
059         * @param newValue New value of the probe.
060         * 
061         * @throws NullPointerException if any parameters are {@code null}.
062         */
063        public ProbeEvent(final ReadoutProbe source, final T oldValue, final T newValue) {
064            super(source);
065    
066            Contract.notNull(source, "Events cannot originate from a null source object");
067            Contract.notNull(oldValue, "Old value cannot be null");
068            Contract.notNull(newValue, "New value cannot be null");
069    
070            this.oldValue = oldValue;
071            this.newValue = newValue;
072        }
073    
074        public ReadoutProbe getProbe() {
075            return (ReadoutProbe)getSource();
076        }
077    
078        /**
079         * Returns the value of the probe before this event was generated.
080         * 
081         * @return Previous probe value.
082         */
083        public T getOldValue() {
084            return oldValue;
085        }
086    
087        /**
088         * Returns the current (as of this event) value of the probe.
089         * 
090         * @return Current probe value.
091         */
092        public T getNewValue() {
093            return newValue;
094        }
095    
096        /**
097         * Returns a brief summary of this event. Please note that this format is
098         * subject to change.
099         * 
100         * @return String that looks like {@code [ProbeEvent@HASHCODE: source=..., 
101         * oldValue=..., newValue=...]}.
102         */
103        @Override public String toString() {
104            return String.format("[ProbeEvent@%x: source=%s, oldValue=%s, newValue=%s]", hashCode(), source, oldValue, newValue);
105        }
106    }