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.component;
020
021import icy.gui.util.ComponentUtil;
022import icy.image.ImageUtil;
023import icy.resource.ResourceUtil;
024
025import java.awt.BorderLayout;
026import java.awt.Dimension;
027import java.awt.GridBagConstraints;
028import java.awt.GridBagLayout;
029import java.awt.Image;
030import java.awt.Insets;
031
032import javax.swing.JLabel;
033import javax.swing.JPanel;
034import javax.swing.JToggleButton;
035import javax.swing.SwingConstants;
036
037public class ThumbnailComponent extends JToggleButton
038{
039    /**
040     * 
041     */
042    private static final long serialVersionUID = 6742578649112198581L;
043
044    // GUI
045    private ImageComponent imageComp;
046    private JLabel titleLabel;
047    private JLabel infosLabel;
048    private JLabel infos2Label;
049    private boolean shortDisplay;
050
051    /**
052     * Create the thumbnail.
053     * 
054     * @param selectable
055     *        If true then the thumbnail component can be selected as a toggle button.
056     */
057    public ThumbnailComponent(boolean selectable)
058    {
059        super();
060
061        shortDisplay = false;
062
063        setMinimumSize(new Dimension(120, 12));
064        setPreferredSize(new Dimension(160, 160));
065
066        initialize();
067
068        setEnabled(selectable);
069    }
070
071    private void initialize()
072    {
073        setMargin(new Insets(2, 2, 2, 2));
074        setLayout(new BorderLayout());
075
076        imageComp = new ImageComponent();
077        add(imageComp, BorderLayout.CENTER);
078
079        final JPanel southPanel = new JPanel();
080        southPanel.setOpaque(false);
081        GridBagLayout gbl_southPanel = new GridBagLayout();
082        gbl_southPanel.columnWidths = new int[] {0, 0};
083        gbl_southPanel.rowHeights = new int[] {0, 0, 0, 0};
084        gbl_southPanel.columnWeights = new double[] {1.0, Double.MIN_VALUE};
085        gbl_southPanel.rowWeights = new double[] {0.0, 0.0, 0.0, Double.MIN_VALUE};
086        southPanel.setLayout(gbl_southPanel);
087
088        titleLabel = new JLabel();
089        GridBagConstraints gbc_titleLabel = new GridBagConstraints();
090        gbc_titleLabel.fill = GridBagConstraints.HORIZONTAL;
091        gbc_titleLabel.insets = new Insets(0, 0, 0, 0);
092        gbc_titleLabel.gridx = 0;
093        gbc_titleLabel.gridy = 0;
094        southPanel.add(titleLabel, gbc_titleLabel);
095        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
096        titleLabel.setText(" ");
097        titleLabel.setHorizontalTextPosition(SwingConstants.LEADING);
098        ComponentUtil.setFontBold(titleLabel);
099        infosLabel = new JLabel();
100        GridBagConstraints gbc_infosLabel = new GridBagConstraints();
101        gbc_infosLabel.fill = GridBagConstraints.HORIZONTAL;
102        gbc_infosLabel.insets = new Insets(0, 0, 0, 0);
103        gbc_infosLabel.gridx = 0;
104        gbc_infosLabel.gridy = 1;
105        southPanel.add(infosLabel, gbc_infosLabel);
106        infosLabel.setHorizontalAlignment(SwingConstants.CENTER);
107        infosLabel.setText(" ");
108        infosLabel.setHorizontalTextPosition(SwingConstants.LEADING);
109        ComponentUtil.setFontSize(infosLabel, 11);
110        infos2Label = new JLabel();
111        GridBagConstraints gbc_infos2Label = new GridBagConstraints();
112        gbc_infos2Label.fill = GridBagConstraints.HORIZONTAL;
113        gbc_infos2Label.gridx = 0;
114        gbc_infos2Label.gridy = 2;
115        southPanel.add(infos2Label, gbc_infos2Label);
116        infos2Label.setHorizontalAlignment(SwingConstants.CENTER);
117        infos2Label.setText(" ");
118        infos2Label.setHorizontalTextPosition(SwingConstants.LEADING);
119        ComponentUtil.setFontSize(infos2Label, 11);
120
121        add(southPanel, BorderLayout.SOUTH);
122    }
123
124    public void setImage(Image img)
125    {
126        if (img == null)
127        {
128            imageComp.setImage(null);
129            return;
130        }
131
132        Image image = img;
133
134        // be sure image data are ready
135        ImageUtil.waitImageReady(image);
136
137        float ix = image.getWidth(null);
138        float iy = image.getHeight(null);
139
140        if ((ix <= 0f) || (iy <= 0f))
141        {
142            image = ResourceUtil.ICON_DELETE;
143            ix = image.getWidth(null);
144            iy = image.getHeight(null);
145        }
146
147        if ((imageComp.getWidth() != 0) && (imageComp.getHeight() != 0))
148        {
149            final float sx = imageComp.getWidth() / ix;
150            final float sy = imageComp.getHeight() / iy;
151            final float s = Math.min(sx, sy);
152            final int w = (int) (ix * s);
153            final int h = (int) (iy * s);
154
155            if ((w > 0) && (h > 0))
156                image = ImageUtil.scaleQuality(img, w, h);
157        }
158
159        imageComp.setImage(image);
160    }
161
162    /**
163     * @return the shortDisplay property
164     * @see #setShortDisplay(boolean)
165     */
166    public boolean getShortDisplay()
167    {
168        return shortDisplay;
169    }
170
171    /**
172     * When set to true, only 'infos' is visible otherwise title, infos and infos2 are all visible
173     */
174    public void setShortDisplay(boolean value)
175    {
176        if (shortDisplay != value)
177        {
178            shortDisplay = value;
179
180            titleLabel.setVisible(!value);
181            infos2Label.setVisible(!value);
182        }
183    }
184
185    public String getTitle()
186    {
187        return titleLabel.getText();
188    }
189
190    public String getInfos()
191    {
192        return infosLabel.getText();
193    }
194
195    public String getInfos2()
196    {
197        return infos2Label.getText();
198    }
199
200    public void setTitle(String value)
201    {
202        titleLabel.setText(value);
203    }
204
205    public void setInfos(String value)
206    {
207        infosLabel.setText(value);
208    }
209
210    public void setInfos2(String value)
211    {
212        infos2Label.setText(value);
213    }
214}