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.gui.frame.sequence;
020
021import icy.gui.frame.ActionFrame;
022import icy.gui.main.MainAdapter;
023import icy.gui.main.MainEvent;
024import icy.gui.util.ComponentUtil;
025import icy.main.Icy;
026import icy.sequence.Sequence;
027import icy.sequence.SequenceEvent;
028import icy.sequence.SequenceListener;
029
030import java.awt.Dimension;
031import java.util.ArrayList;
032
033import javax.swing.BorderFactory;
034import javax.swing.Box;
035import javax.swing.BoxLayout;
036import javax.swing.JLabel;
037import javax.swing.JPanel;
038
039/**
040 * @deprecated Use {@link ActiveSequenceActionFrame} instead.
041 */
042@Deprecated
043public abstract class FocusedSequenceActionFrame extends ActionFrame implements SequenceListener
044{
045    public interface SourceChangeListener
046    {
047        public void sourceSequenceChanged(Sequence seq);
048    }
049
050    /**
051     * input sequence
052     */
053    Sequence seqIn;
054
055    /**
056     * listeners and event handler
057     */
058    private final MainAdapter mainAdapter;
059    private final ArrayList<SourceChangeListener> sourceChangeListeners;
060
061    /**
062     * gui
063     */
064    final JPanel sourcePanel;
065    final JLabel sequenceLabel;
066
067    /**
068     * @param title
069     * @param resizable
070     * @param iconifiable
071     */
072    public FocusedSequenceActionFrame(String title, boolean resizable, boolean iconifiable)
073    {
074        super(title, resizable, iconifiable);
075
076        sourceChangeListeners = new ArrayList<SourceChangeListener>();
077
078        mainAdapter = new MainAdapter()
079        {
080            /*
081             * (non-Javadoc)
082             * 
083             * @see icy.gui.main.MainListener#sequenceFocused(icy.gui.main.MainEvent)
084             */
085            @Override
086            public void sequenceFocused(MainEvent event)
087            {
088                setSeqIn((Sequence) event.getSource());
089            }
090        };
091
092        Icy.getMainInterface().addListener(mainAdapter);
093
094        // init value
095        seqIn = null;
096
097        // GUI
098        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
099
100        sourcePanel = new JPanel();
101        sourcePanel.setBorder(BorderFactory.createTitledBorder("Selected sequence"));
102        sourcePanel.setLayout(new BoxLayout(sourcePanel, BoxLayout.LINE_AXIS));
103
104        // sequence label
105        sequenceLabel = new JLabel();
106        sequenceLabel.setMinimumSize(new Dimension(100, 24));
107
108        sourcePanel.add(Box.createHorizontalStrut(10));
109        sourcePanel.add(sequenceLabel);
110        sourcePanel.add(Box.createHorizontalGlue());
111
112        // fix the height of source panel
113        ComponentUtil.setFixedHeight(sourcePanel, 54);
114
115        mainPanel.add(sourcePanel);
116
117        // set input sequence once GUI is built
118        setSeqIn(Icy.getMainInterface().getActiveSequence());
119    }
120
121    /**
122     * @param title
123     * @param resizable
124     */
125    public FocusedSequenceActionFrame(String title, boolean resizable)
126    {
127        this(title, resizable, false);
128    }
129
130    /**
131     * @param title
132     */
133    public FocusedSequenceActionFrame(String title)
134    {
135        this(title, false);
136    }
137
138    /*
139     * (non-Javadoc)
140     * 
141     * @see icy.gui.frame.IcyFrame#onClosed()
142     */
143    @Override
144    public void onClosed()
145    {
146        Icy.getMainInterface().removeListener(mainAdapter);
147
148        super.onClosed();
149    }
150
151    /**
152     * @return the sourcePanel
153     */
154    public JPanel getSourcePanel()
155    {
156        return sourcePanel;
157    }
158
159    /**
160     * @return the sequence
161     */
162    public Sequence getSeqIn()
163    {
164        return seqIn;
165    }
166
167    /**
168     * @param value
169     *        the sequence to set
170     */
171    void setSeqIn(Sequence value)
172    {
173        if (seqIn != value)
174        {
175            if (seqIn != null)
176                seqIn.removeListener(this);
177
178            seqIn = value;
179
180            if (seqIn != null)
181            {
182                sequenceLabel.setText(seqIn.getName());
183                seqIn.addListener(this);
184            }
185            else
186                sequenceLabel.setText("no sequence");
187
188            fireSequenceChangeEvent(seqIn);
189        }
190    }
191
192    public void addSourceChangeListener(SourceChangeListener listener)
193    {
194        if (!sourceChangeListeners.contains(listener))
195            sourceChangeListeners.add(listener);
196    }
197
198    public void removeSourceChangeListener(SourceChangeListener listener)
199    {
200        sourceChangeListeners.remove(listener);
201    }
202
203    private void fireSequenceChangeEvent(Sequence seq)
204    {
205        for (SourceChangeListener listener : sourceChangeListeners)
206            listener.sourceSequenceChanged(seq);
207    }
208
209    @Override
210    public void sequenceChanged(SequenceEvent event)
211    {
212        // just for overload
213    }
214
215    @Override
216    public void sequenceClosed(Sequence sequence)
217    {
218        // just for overload
219    }
220
221}