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.swimmingPool;
020
021import icy.gui.frame.IcyFrame;
022import icy.gui.util.ComponentUtil;
023import icy.main.Icy;
024import icy.system.thread.ThreadUtil;
025
026import java.awt.BorderLayout;
027import java.awt.Dimension;
028import java.awt.event.ActionEvent;
029import java.awt.event.ActionListener;
030
031import javax.swing.Box;
032import javax.swing.JPanel;
033
034public class SwimmingPoolViewer implements SwimmingPoolListener, ActionListener
035{
036
037    IcyFrame mainFrame = new IcyFrame("Swimming Pool Viewer", true, true, true, true);
038
039    SwimmingPoolViewerPanel spvp = new SwimmingPoolViewerPanel();
040
041    public SwimmingPoolViewer()
042    {
043
044        mainFrame.getContentPane().setLayout(new BorderLayout());
045        mainFrame.getContentPane().add(spvp, BorderLayout.CENTER);
046        mainFrame.setVisible(true);
047        mainFrame.setPreferredSize(new Dimension(400, 400));
048        mainFrame.addToDesktopPane();
049        mainFrame.center();
050        mainFrame.pack();
051
052        Icy.getMainInterface().getSwimmingPool().addListener(this);
053        spvp.getDeleteAllButton().addActionListener(this);
054
055        refreshGUI();
056
057        mainFrame.requestFocus();
058
059    }
060
061    private void refreshGUI()
062    {
063        spvp.getScrollPanel().removeAll();
064
065        for (SwimmingObject result : Icy.getMainInterface().getSwimmingPool().getObjects())
066        {
067            JPanel panel = new SwimmingPoolObjectPanel(result);
068            ComponentUtil.setFixedHeight(panel, 40);
069            spvp.getScrollPanel().add(panel);
070        }
071
072        spvp.getScrollPanel().add(Box.createVerticalGlue());
073
074        String text = "No object in swimming pool.";
075
076        int numberOfSwimmingObject = Icy.getMainInterface().getSwimmingPool().getObjects().size();
077        if (numberOfSwimmingObject > 0)
078        {
079            text = "" + numberOfSwimmingObject + " objects in swimming pool.";
080        }
081
082        spvp.getNumberOfSwimmingObjectLabel().setText(text);
083
084        spvp.getScrollPane().invalidate();
085        spvp.getScrollPane().repaint();
086
087    }
088
089    @Override
090    public void swimmingPoolChangeEvent(final SwimmingPoolEvent swimmingPoolEvent)
091    {
092
093        if (swimmingPoolEvent.getType() == SwimmingPoolEventType.ELEMENT_ADDED)
094        {
095            ThreadUtil.invokeLater(new Runnable()
096            {
097
098                @Override
099                public void run()
100                {
101
102                    refreshGUI();
103                }
104            });
105        }
106
107        if (swimmingPoolEvent.getType() == SwimmingPoolEventType.ELEMENT_REMOVED)
108        {
109            ThreadUtil.invokeLater(new Runnable()
110            {
111
112                @Override
113                public void run()
114                {
115
116                    refreshGUI();
117
118                }
119            });
120
121        }
122
123    }
124
125    @Override
126    public void actionPerformed(ActionEvent e)
127    {
128
129        if (e.getSource() == spvp.getDeleteAllButton())
130        {
131            Icy.getMainInterface().getSwimmingPool().removeAll();
132        }
133
134    }
135
136}