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.inspector;
020
021import icy.gui.component.PopupPanel;
022import icy.gui.main.ActiveSequenceListener;
023import icy.gui.main.ActiveViewerListener;
024import icy.gui.sequence.SequenceInfosPanel;
025import icy.gui.viewer.Viewer;
026import icy.gui.viewer.ViewerEvent;
027import icy.sequence.Sequence;
028import icy.sequence.SequenceEvent;
029
030import java.awt.BorderLayout;
031import java.awt.GridBagConstraints;
032import java.awt.GridBagLayout;
033import java.awt.Insets;
034
035import javax.swing.JPanel;
036
037/**
038 * @author Stephane
039 */
040public class SequencePanel extends JPanel implements ActiveSequenceListener, ActiveViewerListener
041{
042    /**
043     * 
044     */
045    private static final long serialVersionUID = -5727785928741370159L;
046
047    private PopupPanel canvasPopupPanel;
048    private PopupPanel lutPopupPanel;
049    private PopupPanel infosPopupPanel;
050
051    private JPanel canvasPanel;
052    private JPanel lutPanel;
053    private JPanel infosPanel;
054
055    private SequenceInfosPanel sequenceInfosPanel;
056
057    /**
058     * 
059     */
060    public SequencePanel()
061    {
062        super();
063
064        initialize();
065    }
066
067    private void initialize()
068    {
069        GridBagLayout gridBagLayout = new GridBagLayout();
070        gridBagLayout.columnWidths = new int[] {0, 0};
071        gridBagLayout.rowHeights = new int[] {0, 0, 0, 0, 0};
072        gridBagLayout.columnWeights = new double[] {1.0, Double.MIN_VALUE};
073        gridBagLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
074        setLayout(gridBagLayout);
075
076        sequenceInfosPanel = new SequenceInfosPanel();
077        infosPopupPanel = new PopupPanel("Sequence Properties");
078        infosPanel = infosPopupPanel.getMainPanel();
079        infosPanel.setLayout(new BorderLayout());
080        infosPopupPanel.expand();
081
082        canvasPopupPanel = new PopupPanel("Canvas");
083        canvasPanel = canvasPopupPanel.getMainPanel();
084        canvasPanel.setLayout(new BorderLayout());
085        canvasPopupPanel.expand();
086        GridBagConstraints gbc_canvasPopupPanel = new GridBagConstraints();
087        gbc_canvasPopupPanel.fill = GridBagConstraints.HORIZONTAL;
088        gbc_canvasPopupPanel.insets = new Insets(0, 0, 0, 0);
089        gbc_canvasPopupPanel.gridx = 0;
090        gbc_canvasPopupPanel.gridy = 0;
091        add(canvasPopupPanel, gbc_canvasPopupPanel);
092
093        lutPopupPanel = new PopupPanel("Histogram and colormap");
094        lutPanel = lutPopupPanel.getMainPanel();
095        lutPanel.setLayout(new BorderLayout());
096        lutPopupPanel.expand();
097        GridBagConstraints gbc_lutPopupPanel = new GridBagConstraints();
098        gbc_lutPopupPanel.fill = GridBagConstraints.HORIZONTAL;
099        gbc_lutPopupPanel.insets = new Insets(0, 0, 0, 0);
100        gbc_lutPopupPanel.gridx = 0;
101        gbc_lutPopupPanel.gridy = 1;
102        add(lutPopupPanel, gbc_lutPopupPanel);
103        infosPanel.add(sequenceInfosPanel, BorderLayout.CENTER);
104        GridBagConstraints gbc_infosPopupPanel = new GridBagConstraints();
105        gbc_infosPopupPanel.insets = new Insets(0, 0, 0, 0);
106        gbc_infosPopupPanel.fill = GridBagConstraints.HORIZONTAL;
107        gbc_infosPopupPanel.gridx = 0;
108        gbc_infosPopupPanel.gridy = 2;
109        add(infosPopupPanel, gbc_infosPopupPanel);
110    }
111
112    public void setCanvasPanel(JPanel panel)
113    {
114        canvasPanel.removeAll();
115
116        if (panel != null)
117            canvasPanel.add(panel, BorderLayout.CENTER);
118
119        canvasPanel.revalidate();
120        // we need it for zoom value refresh in detached mode
121        // FIXME : normally revalidate should be enough
122        canvasPanel.repaint();
123    }
124
125    public void setLutPanel(JPanel panel)
126    {
127        lutPanel.removeAll();
128
129        if (panel != null)
130            lutPanel.add(panel, BorderLayout.CENTER);
131
132        lutPanel.revalidate();
133        // we need it for histogram refresh in detached mode
134        // FIXME : normally revalidate should be enough
135        lutPanel.repaint();
136    }
137
138    @Override
139    public void viewerActivated(Viewer viewer)
140    {
141        if (viewer != null)
142        {
143            setLutPanel(viewer.getLutViewer());
144            setCanvasPanel(viewer.getCanvasPanel());
145        }
146        else
147        {
148            setLutPanel(null);
149            setCanvasPanel(null);
150        }
151    }
152
153    @Override
154    public void viewerDeactivated(Viewer viewer)
155    {
156        // nothing here
157    }
158
159    @Override
160    public void activeViewerChanged(ViewerEvent event)
161    {
162        // we receive from current focused viewer only
163        switch (event.getType())
164        {
165            case CANVAS_CHANGED:
166                // refresh canvas panel
167                setCanvasPanel(event.getSource().getCanvasPanel());
168                break;
169
170            case LUT_CHANGED:
171                // refresh lut panel
172                setLutPanel(event.getSource().getLutViewer());
173                break;
174
175            case POSITION_CHANGED:
176                // nothing to do
177                break;
178        }
179    }
180
181    @Override
182    public void sequenceActivated(Sequence sequence)
183    {
184        sequenceInfosPanel.sequenceActivated(sequence);
185    }
186
187    @Override
188    public void sequenceDeactivated(Sequence sequence)
189    {
190        // nothing here
191    }
192
193    @Override
194    public void activeSequenceChanged(SequenceEvent event)
195    {
196        sequenceInfosPanel.activeSequenceChanged(event);
197    }
198
199}