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.AbstractRoisPanel;
022import icy.main.Icy;
023import icy.preferences.GeneralPreferences;
024import icy.roi.ROI;
025import icy.roi.ROIEvent;
026import icy.sequence.Sequence;
027import icy.sequence.SequenceEvent;
028import icy.sequence.SequenceEvent.SequenceEventSourceType;
029
030import java.awt.BorderLayout;
031
032import javax.swing.event.ListSelectionEvent;
033
034/**
035 * ROI Panel component displayed in the Icy inspector.<br>
036 * Use the {@link AbstractRoisPanel} if you want to embed the ROI table in your own component.
037 * 
038 * @author Stephane
039 */
040public class RoisPanel extends AbstractRoisPanel
041{
042    protected static final String PREF_ID = "ROIPanel";
043
044    // GUI
045    protected RoiControlPanel roiControlPanel;
046
047    public RoisPanel()
048    {
049        super(GeneralPreferences.getPreferences().node(PREF_ID));
050    }
051
052    @Override
053    protected void initialize()
054    {
055        super.initialize();
056
057        // build control panel
058        roiControlPanel = new RoiControlPanel(this);
059
060        add(roiControlPanel, BorderLayout.SOUTH);
061
062        validate();
063    }
064
065    @Override
066    protected ROIResults createNewROIResults(ROI roi)
067    {
068        return new CustomROIResults(roi);
069    }
070
071    protected Sequence getSequence()
072    {
073        return Icy.getMainInterface().getActiveSequence();
074    }
075
076    @Override
077    protected void refreshTableDataStructureInternal()
078    {
079        super.refreshTableDataStructureInternal();
080
081        // notify the ROI control panel that selection changed
082        roiControlPanel.selectionChanged();
083    }
084
085    @Override
086    protected void refreshTableDataInternal()
087    {
088        super.refreshTableDataInternal();
089
090        // notify the ROI control panel that selection changed (force data refresh)
091        roiControlPanel.selectionChanged();
092    }
093
094    @Override
095    protected void refreshTableSelectionInternal()
096    {
097        super.refreshTableSelectionInternal();
098
099        // notify the ROI control panel that selection changed
100        roiControlPanel.selectionChanged();
101    }
102
103    // called when selection changed in the ROI roiTable
104    @Override
105    public void valueChanged(ListSelectionEvent e)
106    {
107        super.valueChanged(e);
108
109        // currently changing the selection ? --> exit
110        if (e.getValueIsAdjusting())
111            return;
112        // currently changing the selection ? --> exit
113        if (roiSelectionModel.getValueIsAdjusting())
114            return;
115        // currently changing the selection ? --> exit
116        if (modifySelection.availablePermits() <= 0)
117            return;
118
119        // notify the ROI control panel that selection changed
120        roiControlPanel.selectionChanged();
121    }
122
123    @Override
124    public void activeSequenceChanged(SequenceEvent event)
125    {
126        super.activeSequenceChanged(event);
127
128        // if data changed (more or less Z, T or C) we need to refresh action
129        // so we can change ROI position correctly
130        if (event.getSourceType() == SequenceEventSourceType.SEQUENCE_DATA)
131            roiControlPanel.refreshROIActions();
132    }
133
134    protected class CustomROIResults extends ROIResults
135    {
136        protected CustomROIResults(ROI roi)
137        {
138            super(roi);
139        }
140
141        @Override
142        public void roiChanged(ROIEvent event)
143        {
144            final ROI roi = event.getSource();
145
146            // ROI selected ? --> propagate event to control panel
147            if (roi.isSelected())
148                roiControlPanel.roiChanged(event);
149
150            super.roiChanged(event);
151        }
152    }
153
154   
155}