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.dialog.IdConfirmDialog;
023import icy.gui.frame.progress.ProgressFrame;
024import icy.gui.util.ComponentUtil;
025import icy.image.IcyBufferedImage;
026import icy.sequence.AbstractSequenceModel;
027import icy.sequence.DimensionId;
028import icy.sequence.Sequence;
029import icy.sequence.SequenceUtil;
030import icy.sequence.SequenceUtil.AddTHelper;
031import icy.sequence.SequenceUtil.AddZHelper;
032import icy.system.thread.ThreadUtil;
033
034import java.awt.BorderLayout;
035import java.awt.event.ActionEvent;
036import java.awt.event.ActionListener;
037import java.awt.image.BufferedImage;
038
039import javax.swing.BorderFactory;
040
041/**
042 * @author Stephane
043 */
044public class SequenceDimensionExtendFrame extends ActionDialog
045{
046    /**
047     * 
048     */
049    private static final long serialVersionUID = -1999644069532236365L;
050
051    private class SequenceDimensionExtendFrameModel extends AbstractSequenceModel
052    {
053        public SequenceDimensionExtendFrameModel()
054        {
055            super();
056        }
057
058        @Override
059        public int getSizeX()
060        {
061            return sequence.getSizeX();
062        }
063
064        @Override
065        public int getSizeY()
066        {
067            return sequence.getSizeX();
068        }
069
070        @Override
071        public int getSizeZ()
072        {
073            if (getDimensionId() == DimensionId.Z)
074                return extendPanel.getNewSize();
075
076            return sequence.getSizeZ();
077        }
078
079        @Override
080        public int getSizeT()
081        {
082            if (getDimensionId() == DimensionId.T)
083                return extendPanel.getNewSize();
084
085            return sequence.getSizeT();
086        }
087
088        @Override
089        public int getSizeC()
090        {
091            if (getDimensionId() == DimensionId.C)
092                return extendPanel.getNewSize();
093
094            return sequence.getSizeC();
095        }
096
097        @Override
098        public BufferedImage getImage(int t, int z)
099        {
100            switch (getDimensionId())
101            {
102                default:
103                case Z:
104                    return AddZHelper.getExtendedImage(sequence, t, z, extendPanel.getInsertPosition(),
105                            extendPanel.getNewSize() - sequence.getSizeZ(), extendPanel.getDuplicateNumber());
106
107                case T:
108                    return AddTHelper.getExtendedImage(sequence, t, z, extendPanel.getInsertPosition(),
109                            extendPanel.getNewSize() - sequence.getSizeT(), extendPanel.getDuplicateNumber());
110            }
111        }
112
113        @Override
114        public BufferedImage getImage(int t, int z, int c)
115        {
116            final IcyBufferedImage img = (IcyBufferedImage) getImage(t, z);
117
118            if (img != null)
119                return img.getImage(c);
120
121            return null;
122        }
123    }
124
125    final SequenceDimensionExtendPanel extendPanel;
126    final Sequence sequence;
127
128    public SequenceDimensionExtendFrame(Sequence sequence, DimensionId dim)
129    {
130        super(dim.toString() + " Dimension extend");
131
132        this.sequence = sequence;
133
134        extendPanel = new SequenceDimensionExtendPanel(dim);
135        extendPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 0, 4));
136
137        final int size = sequence.getSize(dim);
138
139        extendPanel.setNewSize(size);
140        extendPanel.setInsertPosition(size);
141        extendPanel.setMaxDuplicate(size);
142
143        mainPanel.add(extendPanel, BorderLayout.CENTER);
144        validate();
145
146        extendPanel.setModel(new SequenceDimensionExtendFrameModel());
147
148        setOkAction(new ActionListener()
149        {
150            @Override
151            public void actionPerformed(ActionEvent e)
152            {
153                ThreadUtil.bgRun(new Runnable()
154                {
155                    @Override
156                    public void run()
157                    {
158                        final Sequence sequence = SequenceDimensionExtendFrame.this.sequence;
159                        final ProgressFrame pf = new ProgressFrame("Extending sequence...");
160
161                        // create undo point
162                        final boolean canUndo = sequence.createUndoDataPoint("Dimension " + getDimensionId().toString()
163                                + " extended");
164
165                        // cannot backup
166                        if (!canUndo)
167                        {
168                            // ask confirmation to continue
169                            if (!IdConfirmDialog.confirm(
170                                    "Not enough memory to undo the operation, do you want to continue ?",
171                                    "AddZTNoUndoConfirm"))
172                                return;
173                        }
174
175                        switch (getDimensionId())
176                        {
177                            default:
178                            case Z:
179                                SequenceUtil.addZ(sequence, extendPanel.getInsertPosition(), extendPanel.getNewSize()
180                                        - sequence.getSizeZ(), extendPanel.getDuplicateNumber());
181                                break;
182
183                            case T:
184                                SequenceUtil.addT(sequence, extendPanel.getInsertPosition(), extendPanel.getNewSize()
185                                        - sequence.getSizeT(), extendPanel.getDuplicateNumber());
186                                break;
187                        }
188
189                        // no undo, clear undo manager after modification
190                        if (!canUndo)
191                            sequence.clearUndoManager();
192
193                        pf.close();
194                    }
195                });
196            }
197        });
198
199        setSize(340, 400);
200        ComponentUtil.center(this);
201
202        setVisible(true);
203    }
204
205    DimensionId getDimensionId()
206    {
207        return extendPanel.getDimensionId();
208    }
209
210}