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.roi;
020
021import icy.common.CollapsibleEvent;
022import icy.util.StringUtil;
023
024/**
025 * @author stephane
026 */
027public class ROIEvent implements CollapsibleEvent
028{
029    @Deprecated
030    public enum ROIPointEventType
031    {
032        NULL, POINT_ADDED, POINT_REMOVED, POINT_CHANGED;
033    }
034
035    public enum ROIEventType
036    {
037        FOCUS_CHANGED, SELECTION_CHANGED, /**
038                                           * ROI position or/and content change event.<br>
039                                           * property = {@link ROI#ROI_CHANGED_POSITION} when only
040                                           * position has changed
041                                           */
042        ROI_CHANGED, /**
043                      * ROI property change event.<br>
044                      * check property field to know which property has actually changed
045                      */
046        PROPERTY_CHANGED, @Deprecated PAINTER_CHANGED, @Deprecated NAME_CHANGED;
047    }
048
049    private final ROI source;
050    private final ROIEventType type;
051    private String propertyName;
052
053    @Deprecated
054    private Object point;
055    @Deprecated
056    private ROIPointEventType pointEventType;
057
058    /**
059     * @deprecated Use {@link #ROIEvent(ROI, ROIEventType)} constructor instead
060     */
061    @Deprecated
062    public ROIEvent(ROI source, ROIEventType type, ROIPointEventType pointEventType, Object point)
063    {
064        super();
065
066        this.source = source;
067        this.type = type;
068        propertyName = null;
069
070        this.point = point;
071        this.pointEventType = pointEventType;
072    }
073
074    public ROIEvent(ROI source, ROIEventType type, String propertyName)
075    {
076        super();
077
078        this.source = source;
079        this.type = type;
080        this.propertyName = propertyName;
081    }
082
083    public ROIEvent(ROI source, String propertyName)
084    {
085        this(source, ROIEventType.PROPERTY_CHANGED, propertyName);
086    }
087
088    public ROIEvent(ROI source, ROIEventType type)
089    {
090        this(source, type, null);
091    }
092
093    /**
094     * @return the source
095     */
096    public ROI getSource()
097    {
098        return source;
099    }
100
101    /**
102     * @return the type
103     */
104    public ROIEventType getType()
105    {
106        return type;
107    }
108
109    /**
110     * @return the propertyName
111     */
112    public String getPropertyName()
113    {
114        return propertyName;
115    }
116
117    @Deprecated
118    public Object getPoint()
119    {
120        return point;
121    }
122
123    @Deprecated
124    public ROIPointEventType getPointEventType()
125    {
126        return pointEventType;
127    }
128
129    @Override
130    public boolean collapse(CollapsibleEvent event)
131    {
132        if (equals(event))
133        {
134            // nothing to do here
135            return true;
136        }
137
138        return false;
139    }
140
141    @Override
142    public int hashCode()
143    {
144        int res = source.hashCode() ^ type.hashCode();
145
146        if (type == ROIEventType.PROPERTY_CHANGED)
147            res ^= propertyName.hashCode();
148
149        return res;
150    }
151
152    @Override
153    public boolean equals(Object obj)
154    {
155        if (obj instanceof ROIEvent)
156        {
157            final ROIEvent e = (ROIEvent) obj;
158
159            if ((e.getSource() == source) && (e.getType() == type))
160            {
161                switch (type)
162                {
163                    case ROI_CHANGED:
164                        return StringUtil.equals(propertyName, e.getPropertyName());
165                        
166                    case FOCUS_CHANGED:
167                    case SELECTION_CHANGED:
168                    case NAME_CHANGED:
169                    case PAINTER_CHANGED:
170                        return true;
171
172                    case PROPERTY_CHANGED:
173                        return StringUtil.equals(propertyName, e.getPropertyName());
174                }
175            }
176        }
177
178        return super.equals(obj);
179    }
180}