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.common.UpdateEventHandler;
023import icy.common.listener.ChangeListener;
024import icy.main.Icy;
025import icy.painter.PainterEvent.PainterEventType;
026import icy.sequence.Sequence;
027
028import java.util.ArrayList;
029import java.util.List;
030
031import javax.swing.event.EventListenerList;
032
033/**
034 * AbstractPainter class.<br>
035 * 
036 * @deprecated Uses the {@link Overlay} class instead.
037 * @author Stephane
038 */
039@Deprecated
040public abstract class AbstractPainter extends PainterAdapter implements ChangeListener
041{
042    /**
043     * listeners
044     */
045    protected final EventListenerList listeners;
046    /**
047     * internal updater
048     */
049    protected final UpdateEventHandler updater;
050
051    /**
052     * Create an AbstractPainter and attach it to the specified sequence.
053     * 
054     * @deprecated Uses the {@link Overlay} class instead.
055     */
056    @Deprecated
057    public AbstractPainter()
058    {
059        super();
060
061        listeners = new EventListenerList();
062        updater = new UpdateEventHandler(this, false);
063    }
064
065    /**
066     * Create an AbstractPainter and attach it to the specified sequence.
067     * 
068     * @deprecated Uses the {@link Overlay} class instead.
069     */
070    @Deprecated
071    public AbstractPainter(Sequence sequence)
072    {
073        this();
074
075        if (sequence != null)
076            sequence.addPainter(this);
077    }
078
079    /**
080     * Returns <code>true</code> if the overlay is attached to the specified {@link Sequence}.
081     */
082    public boolean isAttached(Sequence sequence)
083    {
084        if (sequence != null)
085            return sequence.contains(this);
086
087        return false;
088    }
089
090    /**
091     * @deprecated Use {@link Sequence#addPainter(Painter)} instead.
092     */
093    @Deprecated
094    public void attachTo(Sequence sequence)
095    {
096        if (sequence != null)
097            sequence.addPainter(this);
098    }
099
100    /**
101     * @deprecated Use {@link Sequence#removePainter(Painter)} instead.
102     */
103    @Deprecated
104    public void detachFrom(Sequence sequence)
105    {
106        if (sequence != null)
107            sequence.removePainter(this);
108    }
109
110    /**
111     * @deprecated Use {@link #remove()} instead.
112     */
113    @Deprecated
114    public void detachFromAll()
115    {
116        remove();
117    }
118
119    /**
120     * @deprecated Use {@link #remove()} instead.
121     */
122    @Deprecated
123    public void delete()
124    {
125        remove();
126    }
127
128    /**
129     * Remove the Painter from all sequences where it is currently attached.
130     */
131    public void remove()
132    {
133        final ArrayList<Sequence> sequences = Icy.getMainInterface().getSequencesContaining(this);
134
135        for (Sequence sequence : sequences)
136            sequence.removePainter(this);
137    }
138
139    public void changed()
140    {
141        updater.changed(new PainterEvent(this, PainterEventType.PAINTER_CHANGED));
142    }
143
144    /**
145     * Returns all sequences where the painter/overlay is currently attached.
146     */
147    public ArrayList<Sequence> getSequences()
148    {
149        return Icy.getMainInterface().getSequencesContaining(this);
150    }
151
152    /**
153     * @deprecated Use {@link Overlay} class instead.
154     */
155    @Deprecated
156    protected void fireChangedEvent(PainterEvent event)
157    {
158        for (PainterListener listener : listeners.getListeners(PainterListener.class))
159            listener.painterChanged(event);
160    }
161
162    /**
163     * @deprecated Use {@link Overlay#addOverlayListener(OverlayListener)} instead.
164     */
165    @Deprecated
166    public void addPainterListener(PainterListener listener)
167    {
168        listeners.add(PainterListener.class, listener);
169    }
170
171    /**
172     * @deprecated Use {@link Overlay#removeOverlayListener(OverlayListener)} instead.
173     */
174    @Deprecated
175    public void removePainterListener(PainterListener listener)
176    {
177        listeners.remove(PainterListener.class, listener);
178    }
179
180    public void beginUpdate()
181    {
182        updater.beginUpdate();
183    }
184
185    public void endUpdate()
186    {
187        updater.endUpdate();
188    }
189
190    public boolean isUpdating()
191    {
192        return updater.isUpdating();
193    }
194
195    @Override
196    public void onChanged(CollapsibleEvent object)
197    {
198        final PainterEvent event = (PainterEvent) object;
199        final List<Sequence> sequences = Icy.getMainInterface().getSequencesContaining(this);
200
201        // notify listeners
202        fireChangedEvent(event);
203
204        // notify sequence as they can't listen painter (interface)
205        for (Sequence sequence : sequences)
206            sequence.painterChanged(this);
207    }
208}