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.component.sequence.SequenceChooser;
022import icy.gui.component.sequence.SequenceChooser.SequenceChooserListener;
023import icy.gui.frame.ActionFrame;
024import icy.gui.util.ComponentUtil;
025import icy.main.Icy;
026import icy.sequence.Sequence;
027
028import java.awt.Dimension;
029import java.util.ArrayList;
030
031import javax.swing.BorderFactory;
032import javax.swing.Box;
033import javax.swing.BoxLayout;
034import javax.swing.JLabel;
035import javax.swing.JPanel;
036
037/**
038 * @author Stephane
039 */
040public class SequenceActionFrame extends ActionFrame
041{
042    public interface SourceChangeListener
043    {
044        public void sequenceChanged(Sequence sequence);
045    }
046
047    /**
048     * GUI
049     */
050    private final SequenceChooser sequenceSelector;
051
052    /**
053     * internals
054     */
055    private final ArrayList<SourceChangeListener> sourceChangeListeners;
056
057    /**
058     * @param title
059     * @param resizable
060     * @param iconifiable
061     */
062    public SequenceActionFrame(String title, boolean resizable, boolean iconifiable)
063    {
064        super(title, resizable, iconifiable);
065
066        sourceChangeListeners = new ArrayList<SourceChangeListener>();
067
068        // GUI
069        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
070
071        final JPanel sourcePanel = new JPanel();
072        sourcePanel.setBorder(BorderFactory.createTitledBorder("Select the source"));
073        sourcePanel.setLayout(new BoxLayout(sourcePanel, BoxLayout.PAGE_AXIS));
074        // fix the height of source panel
075        ComponentUtil.setFixedHeight(sourcePanel, 58);
076
077        final JPanel sequenceSelectPanel = new JPanel();
078        sequenceSelectPanel.setLayout(new BoxLayout(sequenceSelectPanel, BoxLayout.LINE_AXIS));
079
080        // select sequence
081        final JLabel sequenceSelectLabel = new JLabel("Sequence  ");
082        sequenceSelector = new SequenceChooser(true, null, 48);
083        sequenceSelector.setSelectedSequence(Icy.getMainInterface().getActiveSequence());
084        sequenceSelector.setMinimumSize(new Dimension(100, 24));
085        sequenceSelector.addListener(new SequenceChooserListener()
086        {
087            @Override
088            public void sequenceChanged(Sequence sequence)
089            {
090                fireSequenceChangeEvent();
091            }
092        });
093
094        sequenceSelectPanel.add(Box.createHorizontalStrut(10));
095        sequenceSelectPanel.add(sequenceSelectLabel);
096        sequenceSelectPanel.add(sequenceSelector);
097        sequenceSelectPanel.add(Box.createHorizontalStrut(10));
098
099        sourcePanel.add(sequenceSelectPanel);
100
101        mainPanel.add(sourcePanel);
102    }
103
104    /**
105     * @param title
106     * @param resizable
107     */
108    public SequenceActionFrame(String title, boolean resizable)
109    {
110        this(title, resizable, false);
111    }
112
113    /**
114     * @param title
115     */
116    public SequenceActionFrame(String title)
117    {
118        this(title, false, false);
119    }
120
121    /**
122     * @return the sequence
123     */
124    public Sequence getSequence()
125    {
126        return sequenceSelector.getSelectedSequence();
127    }
128
129    public void addSourceChangeListener(SourceChangeListener listener)
130    {
131        if (!sourceChangeListeners.contains(listener))
132            sourceChangeListeners.add(listener);
133    }
134
135    public void removeSourceChangeListener(SourceChangeListener listener)
136    {
137        sourceChangeListeners.remove(listener);
138    }
139
140    void fireSequenceChangeEvent()
141    {
142        final Sequence sequence = getSequence();
143
144        for (SourceChangeListener listener : sourceChangeListeners)
145            listener.sequenceChanged(sequence);
146    }
147}