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.sequence.Sequence;
026import icy.sequence.SequenceUtil;
027import icy.system.thread.ThreadUtil;
028
029import java.awt.BorderLayout;
030import java.awt.event.ActionEvent;
031import java.awt.event.ActionListener;
032
033import javax.swing.BorderFactory;
034
035/**
036 * Advanced conversion of Z and T dimension.
037 * 
038 * @author Stephane
039 */
040public class SequenceDimensionConvertFrame extends ActionDialog
041{
042    /**
043     * 
044     */
045    private static final long serialVersionUID = -6541431462734831647L;
046
047    final SequenceDimensionConvertPanel convertPanel;
048
049    public SequenceDimensionConvertFrame(Sequence sequence)
050    {
051        super("Z / T dimension conversion");
052
053        convertPanel = new SequenceDimensionConvertPanel(sequence);
054        convertPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 0, 4));
055
056        mainPanel.add(convertPanel, BorderLayout.CENTER);
057        validate();
058
059        setOkAction(new ActionListener()
060        {
061            @Override
062            public void actionPerformed(ActionEvent e)
063            {
064                ThreadUtil.bgRun(new Runnable()
065                {
066                    @Override
067                    public void run()
068                    {
069                        final ProgressFrame pf = new ProgressFrame("Converting Z / T dimension...");
070                        final Sequence sequence = convertPanel.getSequence();
071
072                        // create undo point
073                        final boolean canUndo = sequence.createUndoDataPoint("Sequence dimension change");
074
075                        // cannot backup
076                        if (!canUndo)
077                        {
078                            // ask confirmation to continue
079                            if (!IdConfirmDialog.confirm(
080                                    "Not enough memory to undo the operation, do you want to continue ?",
081                                    "ZTDimensionChangeNoUndoConfirm"))
082                                return;
083                        }
084
085                        SequenceUtil.adjustZT(convertPanel.getSequence(), convertPanel.getNewSizeZ(),
086                                convertPanel.getNewSizeT(), convertPanel.isOrderReversed());
087
088                        // no undo, clear undo manager after modification
089                        if (!canUndo)
090                            sequence.clearUndoManager();
091
092                        pf.close();
093                    }
094                });
095            }
096        });
097
098        setSize(340, 400);
099        ComponentUtil.center(this);
100
101        setVisible(true);
102    }
103}