001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.painter;
020
021import icy.common.CollapsibleEvent;
022import icy.util.StringUtil;
023
024/**
025 * @author Stephane
026 */
027public class OverlayEvent implements CollapsibleEvent
028{
029    public enum OverlayEventType
030    {
031        PAINTER_CHANGED, PROPERTY_CHANGED;
032    }
033
034    private final Overlay source;
035    private final OverlayEventType type;
036    private String propertyName;
037
038    public OverlayEvent(Overlay source, OverlayEventType type, String propertyName)
039    {
040        this.source = source;
041        this.type = type;
042        this.propertyName = propertyName;
043    }
044
045    public OverlayEvent(Overlay source, OverlayEventType type)
046    {
047        this(source, type, null);
048    }
049
050    /**
051     * @return the source
052     */
053    public Overlay getSource()
054    {
055        return source;
056    }
057
058    /**
059     * @return the type
060     */
061    public OverlayEventType getType()
062    {
063        return type;
064    }
065
066    /**
067     * @return the propertyName
068     */
069    public String getPropertyName()
070    {
071        return propertyName;
072    }
073
074    @Override
075    public boolean collapse(CollapsibleEvent event)
076    {
077        if (equals(event))
078        {
079            // nothing to change here
080            return true;
081        }
082
083        return false;
084    }
085
086    @Override
087    public int hashCode()
088    {
089        int res = source.hashCode() ^ type.hashCode();
090
091        if ((type == OverlayEventType.PROPERTY_CHANGED) && (propertyName != null))
092            res ^= propertyName.hashCode();
093
094        return res;
095    }
096
097    @Override
098    public boolean equals(Object obj)
099    {
100        if (obj instanceof OverlayEvent)
101        {
102            final OverlayEvent e = (OverlayEvent) obj;
103
104            // same source type and same type
105            // if property change event then we need to compare property name
106            return (e.getSource() == source) && (e.getType() == type) && ((type != OverlayEventType.PROPERTY_CHANGED)
107                    || StringUtil.equals(e.getPropertyName(), propertyName));
108        }
109
110        return super.equals(obj);
111    }
112}