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.canvas;
020
021import icy.common.CollapsibleEvent;
022import icy.util.StringUtil;
023
024/**
025 * Event for canvas layer.
026 * 
027 * @author Stephane
028 */
029public class CanvasLayerEvent implements CollapsibleEvent
030{
031    public enum LayersEventType
032    {
033        ADDED, REMOVED, CHANGED;
034    }
035
036    private Layer source;
037    private final LayersEventType type;
038    // only meaningful when type == CHANGED
039    private String property;
040
041    public CanvasLayerEvent(Layer source, LayersEventType type, String property)
042    {
043        super();
044
045        this.source = source;
046        this.type = type;
047        this.property = property;
048    }
049
050    public CanvasLayerEvent(Layer source, LayersEventType type)
051    {
052        this(source, type, null);
053    }
054
055    /**
056     * @return the source
057     */
058    public Layer getSource()
059    {
060        return source;
061    }
062
063    /**
064     * @return the type
065     */
066    public LayersEventType getType()
067    {
068        return type;
069    }
070
071    /**
072     * Return the property name which has changed.<br>
073     * <br>
074     * It can be <code>null</code> or one of the following :<br>
075     * <code> 
076     * Layer.PROPERTY_NAME<br>
077     * Layer.PROPERTY_PRIORITY<br>
078     * Layer.PROPERTY_ALPHA<br>
079     * Layer.PROPERTY_VISIBLE<br>
080     * </code>
081     */
082    public String getProperty()
083    {
084        return property;
085    }
086
087    @Override
088    public boolean collapse(CollapsibleEvent event)
089    {
090        if (equals(event))
091        {
092            // nothing to change here
093            return true;
094        }
095
096        return false;
097    }
098
099    @Override
100    public int hashCode()
101    {
102        return source.hashCode() ^ type.hashCode() ^ ((property != null) ? property.hashCode() : 0);
103    }
104
105    @Override
106    public boolean equals(Object obj)
107    {
108        if (obj instanceof CanvasLayerEvent)
109        {
110            final CanvasLayerEvent e = (CanvasLayerEvent) obj;
111
112            return (e.getSource() == source) && (e.getType() == type) && StringUtil.equals(e.getProperty(), property);
113        }
114
115        return super.equals(obj);
116    }
117}