001/*
002 * Copyright 2010, 2011 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 plugins.tutorial.gui;
020
021import java.awt.BorderLayout;
022import java.awt.Dimension;
023import java.awt.event.ActionEvent;
024import java.awt.event.ActionListener;
025
026import javax.swing.BorderFactory;
027import javax.swing.Box;
028import javax.swing.JLabel;
029import javax.swing.JPanel;
030import javax.swing.SwingConstants;
031
032import icy.gui.dialog.MessageDialog;
033import icy.gui.frame.ActionFrame;
034import icy.gui.frame.IcyFrameAdapter;
035import icy.gui.frame.IcyFrameEvent;
036import icy.gui.main.ActiveSequenceListener;
037import icy.gui.util.GuiUtil;
038import icy.main.Icy;
039import icy.plugin.abstract_.PluginActionable;
040import icy.sequence.Sequence;
041import icy.sequence.SequenceEvent;
042
043/**
044 * Plugin Example: A simple interface with some features.
045 * 
046 * @author Fabrice
047 * @author Stephane
048 */
049public class GuiBuildExample01 extends PluginActionable implements ActionListener
050{
051    /**
052     * gui
053     */
054    private ActionFrame frame;
055
056    @Override
057    public void run()
058    {
059        // build frame and others controls
060        frame = new ActionFrame("Plugin with panel", true, true);
061        final JPanel mainPanel = frame.getMainPanel();
062        final JLabel sequenceLabel = new JLabel("", SwingConstants.CENTER);
063        final JLabel sequenceNumberLabel = new JLabel();
064
065        // build an active sequence listener
066        final ActiveSequenceListener activeSequenceListener = new ActiveSequenceListener()
067        {
068            @Override
069            public void sequenceActivated(Sequence sequence)
070            {
071                if (sequence != null)
072                    sequenceLabel.setText(sequence.getName());
073                else
074                    sequenceLabel.setText("no sequence");
075
076                // update the number of opened sequence
077                sequenceNumberLabel.setText("" + getSequences().size());
078            }
079
080            @Override
081            public void sequenceDeactivated(Sequence sequence)
082            {
083                //
084            }
085
086            @Override
087            public void activeSequenceChanged(SequenceEvent event)
088            {
089                //
090            }
091        };
092
093        // get selected sequence
094        final Sequence sequenceFocused = getActiveSequence();
095
096        // if we have one, show his name in the label
097        if (sequenceFocused != null)
098            sequenceLabel.setText(sequenceFocused.getName());
099        else
100            sequenceLabel.setText("no sequence");
101
102        // create center panel as a big PAGE panel made from severals LINE panel
103        final JPanel centerPanel = GuiUtil.createPageBoxPanel(Box.createVerticalStrut(4),
104                GuiUtil.createLineBoxPanel(new JLabel("CurrentSequenceFocused : "), sequenceLabel),
105                Box.createVerticalStrut(4),
106                GuiUtil.createLineBoxPanel(new JLabel("Number of current sequence opened : "), sequenceNumberLabel),
107                Box.createVerticalGlue(), Box.createVerticalStrut(4));
108        // set a border to center panel
109        centerPanel.setBorder(BorderFactory.createEtchedBorder());
110
111        // panel in CENTER alignment can be resized both in width and height
112        mainPanel.add(centerPanel, BorderLayout.CENTER);
113
114        frame.setOkAction(this);
115        // don't want frame closed after action done
116        frame.setCloseAfterAction(false);
117
118        // add active sequence listener
119        // WARNING : don't forget to remove it when plugin exit or instance will never die
120        Icy.getMainInterface().addActiveSequenceListener(activeSequenceListener);
121
122        // add a listener to frame events
123        frame.addFrameListener(new IcyFrameAdapter()
124        {
125            // called when frame is closed
126            @Override
127            public void icyFrameClosed(IcyFrameEvent e)
128            {
129                // remove the listener so there is no more reference on plugin instance
130                Icy.getMainInterface().removeActiveSequenceListener(activeSequenceListener);
131            }
132        });
133
134        // set size
135        frame.setSize(new Dimension(480, 340));
136        // add frame to application desktop
137        addIcyFrame(frame);
138        // center
139        frame.center();
140        // and finally make it visible
141        frame.setVisible(true);
142        // get focus
143        frame.requestFocus();
144    }
145
146    @Override
147    public void actionPerformed(ActionEvent e)
148    {
149        MessageDialog.showDialog("Perform an action", "Number of sequence : " + getSequences().size());
150    }
151}