001/**
002 * 
003 */
004package icy.gui.frame.sequence;
005
006import icy.gui.frame.ActionFrame;
007import icy.gui.main.ActiveSequenceListener;
008import icy.gui.util.ComponentUtil;
009import icy.main.Icy;
010import icy.sequence.Sequence;
011import icy.sequence.SequenceEvent;
012
013import java.awt.Dimension;
014import java.util.ArrayList;
015import java.util.List;
016
017import javax.swing.BorderFactory;
018import javax.swing.Box;
019import javax.swing.BoxLayout;
020import javax.swing.JLabel;
021import javax.swing.JPanel;
022
023/**
024 * Basic frame to do a simple action on the current active sequence.
025 * 
026 * @author Stephane
027 */
028public class ActiveSequenceActionFrame extends ActionFrame implements ActiveSequenceListener
029{
030    public interface SourceChangeListener
031    {
032        public void sourceSequenceChanged(Sequence seq);
033    }
034
035    /**
036     * listeners and event handler
037     */
038    private final List<SourceChangeListener> sourceChangeListeners;
039
040    /**
041     * gui
042     */
043    JPanel sourcePanel;
044    JLabel sequenceLabel;
045
046    /**
047     * @param title
048     * @param resizable
049     * @param iconifiable
050     */
051    public ActiveSequenceActionFrame(String title, boolean resizable, boolean iconifiable)
052    {
053        super(title, resizable, iconifiable);
054
055        sourceChangeListeners = new ArrayList<SourceChangeListener>();
056
057        buildGUI();
058
059        final Sequence sequence = getSequence();
060
061        if (sequence != null)
062            sequenceLabel.setText(sequence.getName());
063        else
064            sequenceLabel.setText("no sequence");
065
066        // add listener
067        Icy.getMainInterface().addActiveSequenceListener(this);
068    }
069
070    /**
071     * @param title
072     * @param resizable
073     */
074    public ActiveSequenceActionFrame(String title, boolean resizable)
075    {
076        this(title, resizable, false);
077    }
078
079    /**
080     * @param title
081     */
082    public ActiveSequenceActionFrame(String title)
083    {
084        this(title, false);
085    }
086
087    @Override
088    public void onClosed()
089    {
090        Icy.getMainInterface().removeActiveSequenceListener(this);
091
092        super.onClosed();
093    }
094
095    protected void buildGUI()
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
118    /**
119     * @return the sourcePanel
120     */
121    public JPanel getSourcePanel()
122    {
123        return sourcePanel;
124    }
125
126    /**
127     * @deprecated Use {@link #getSequence()} instead.
128     */
129    @Deprecated
130    public Sequence getSeqIn()
131    {
132        return getSequence();
133    }
134
135    /**
136     * @return the active sequence
137     */
138    public Sequence getSequence()
139    {
140        return Icy.getMainInterface().getActiveSequence();
141    }
142
143    public void addSourceChangeListener(SourceChangeListener listener)
144    {
145        if (!sourceChangeListeners.contains(listener))
146            sourceChangeListeners.add(listener);
147    }
148
149    public void removeSourceChangeListener(SourceChangeListener listener)
150    {
151        sourceChangeListeners.remove(listener);
152    }
153
154    private void fireSequenceChangeEvent(Sequence seq)
155    {
156        for (SourceChangeListener listener : sourceChangeListeners)
157            listener.sourceSequenceChanged(seq);
158    }
159
160    @Override
161    public void sequenceActivated(Sequence sequence)
162    {
163        if (sequence != null)
164            sequenceLabel.setText(sequence.getName());
165        else
166            sequenceLabel.setText("no sequence");
167
168        fireSequenceChangeEvent(sequence);
169    }
170
171    @Override
172    public void sequenceDeactivated(Sequence sequence)
173    {
174        // nothing here
175    }
176
177    @Override
178    public void activeSequenceChanged(SequenceEvent event)
179    {
180        // nothing here
181    }
182}