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.sequence.tools;
020
021import icy.gui.dialog.ActionDialog;
022import icy.gui.frame.progress.ProgressFrame;
023import icy.gui.util.ComponentUtil;
024import icy.sequence.AbstractSequenceModel;
025import icy.sequence.DimensionId;
026import icy.sequence.Sequence;
027import icy.system.thread.ThreadUtil;
028
029import java.awt.BorderLayout;
030import java.awt.event.ActionEvent;
031import java.awt.event.ActionListener;
032import java.awt.image.BufferedImage;
033
034import javax.swing.BorderFactory;
035
036/**
037 * @author Stephane
038 */
039public class SequenceDimensionAdjustFrame extends ActionDialog
040{
041    /**
042     * 
043     */
044    private static final long serialVersionUID = -383226926743211242L;
045
046    private class SequenceDimensionAdjustFrameModel extends AbstractSequenceModel
047    {
048        public SequenceDimensionAdjustFrameModel()
049        {
050            super();
051        }
052
053        @Override
054        public int getSizeX()
055        {
056            if (sequence != null)
057                return sequence.getSizeX();
058
059            return 0;
060        }
061
062        @Override
063        public int getSizeY()
064        {
065            if (sequence != null)
066                return sequence.getSizeY();
067
068            return 0;
069        }
070
071        @Override
072        public int getSizeZ()
073        {
074            if (sequence != null)
075                return sequence.getSizeZ();
076
077            return 0;
078        }
079
080        @Override
081        public int getSizeT()
082        {
083            if (sequence != null)
084                return sequence.getSizeT();
085
086            return 0;
087        }
088
089        @Override
090        public int getSizeC()
091        {
092            if (sequence != null)
093                return sequence.getSizeC();
094
095            return 0;
096        }
097
098        @Override
099        public BufferedImage getImage(int t, int z)
100        {
101            if (sequence != null)
102                return sequence.getImage(t, z);
103
104            return null;
105        }
106
107        @Override
108        public BufferedImage getImage(int t, int z, int c)
109        {
110            if (sequence != null)
111                return sequence.getImage(t, z, c);
112
113            return null;
114        }
115    }
116
117    final Sequence sequence;
118    final SequenceDimensionAdjustPanel rangePanel;
119    final DimensionId dim;
120
121    public SequenceDimensionAdjustFrame(Sequence sequence, DimensionId dim)
122    {
123        super("Adjust " + dim.toString() + " dimension");
124
125        this.sequence = sequence;
126        this.dim = dim;
127
128        rangePanel = new SequenceDimensionAdjustPanel(dim);
129        rangePanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 0, 4));
130
131        mainPanel.add(rangePanel, BorderLayout.CENTER);
132        validate();
133
134        rangePanel.setModel(new SequenceDimensionAdjustFrameModel());
135
136        setOkAction(new ActionListener()
137        {
138            @Override
139            public void actionPerformed(ActionEvent e)
140            {
141                ThreadUtil.bgRun(new Runnable()
142                {
143                    @Override
144                    public void run()
145                    {
146                        final DimensionId dim = SequenceDimensionAdjustFrame.this.dim;
147                        final ProgressFrame pf;
148
149                        if (dim == DimensionId.Z)
150                            pf = new ProgressFrame("Removing slices...");
151                        else
152                            pf = new ProgressFrame("Removing frames...");
153
154                        final Sequence seq = SequenceDimensionAdjustFrame.this.sequence;
155                        final Sequence tmp = new Sequence();
156
157                        int sizeT = seq.getSizeT();
158                        int sizeZ = seq.getSizeZ();
159
160                        tmp.beginUpdate();
161                        seq.beginUpdate();
162                        try
163                        {
164                            int i = 0;
165
166                            // create a temporary sequence containing resulting images
167                            for (int t = 0; t < sizeT; t++)
168                            {
169                                if (dim == DimensionId.Z)
170                                    i = 0;
171
172                                for (int z = 0; z < sizeZ; z++)
173                                {
174                                    if (dim == DimensionId.Z)
175                                    {
176                                        if (rangePanel.isIndexSelected(z))
177                                            tmp.setImage(t, i++, seq.getImage(t, z));
178                                    }
179                                    else
180                                    {
181                                        if (rangePanel.isIndexSelected(t))
182                                            tmp.setImage(i, z, seq.getImage(t, z));
183                                    }
184                                }
185
186                                if ((dim == DimensionId.T) && (rangePanel.isIndexSelected(t)))
187                                    i++;
188                            }
189
190                            sizeT = tmp.getSizeT();
191                            sizeZ = tmp.getSizeZ();
192
193                            // then copy back tmp images in seq
194                            seq.removeAllImages();
195                            for (int t = 0; t < sizeT; t++)
196                                for (int z = 0; z < sizeZ; z++)
197                                    seq.setImage(t, z, tmp.getImage(t, z));
198                        }
199                        finally
200                        {
201                            seq.endUpdate();
202                            tmp.endUpdate();
203                            pf.close();
204                        }
205                    }
206                });
207            }
208        });
209
210        setSize(320, 360);
211        ComponentUtil.center(this);
212
213        setVisible(true);
214    }
215
216    /**
217     * @wbp.parser.constructor
218     */
219    SequenceDimensionAdjustFrame()
220    {
221        this(new Sequence(), DimensionId.Z);
222    }
223}