001/**
002 * 
003 */
004package icy.action;
005
006import icy.canvas.IcyCanvas;
007import icy.gui.viewer.Viewer;
008import icy.image.lut.LUT;
009import icy.main.Icy;
010import icy.resource.ResourceUtil;
011import icy.resource.icon.IcyIcon;
012import icy.sequence.Sequence;
013import icy.system.thread.ThreadUtil;
014import icy.util.ClassUtil;
015
016import java.awt.event.ActionEvent;
017import java.awt.event.KeyEvent;
018import java.lang.reflect.Field;
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.List;
022
023import plugins.kernel.canvas.VtkCanvas;
024
025/**
026 * Viewer associated actions (Duplicate, externalize...)
027 * 
028 * @author Stephane
029 */
030public class ViewerActions
031{
032    public static IcyAbstractAction duplicateAction = new IcyAbstractAction("Duplicate view", new IcyIcon(
033            ResourceUtil.ICON_PICTURE_COPY), "Duplicate view (no data duplication)", KeyEvent.VK_F2)
034    {
035        /**
036         * 
037         */
038        private static final long serialVersionUID = -8660425976560135450L;
039
040        @Override
041        public boolean doAction(ActionEvent e)
042        {
043            ThreadUtil.invokeLater(new Runnable()
044            {
045                @Override
046                public void run()
047                {
048                    // so it won't change during process
049                    final Viewer viewer = Icy.getMainInterface().getActiveViewer();
050                    final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null;
051                    final Sequence sequence = (viewer != null) ? viewer.getSequence() : null;
052
053                    if ((sequence != null) && (canvas != null))
054                    {
055                        final Viewer v = new Viewer(sequence);
056                        final LUT oldLut = viewer.getLut();
057                        final LUT newLut = v.getLut();
058
059                        // copy LUT
060                        if (canvas instanceof VtkCanvas)
061                        {
062                            // don't copy alpha colormap
063                            newLut.setColorMaps(oldLut, false);
064                            newLut.setScalers(oldLut);
065                        }
066                        else
067                            newLut.copyFrom(oldLut);
068                    }
069                }
070            });
071
072            return true;
073        }
074
075        @Override
076        public boolean isEnabled()
077        {
078            return super.isEnabled() && (Icy.getMainInterface().getActiveViewer() != null);
079        }
080    };
081
082    /**
083     * Return all actions of this class
084     */
085    public static List<IcyAbstractAction> getAllActions()
086    {
087        final List<IcyAbstractAction> result = new ArrayList<IcyAbstractAction>();
088
089        for (Field field : ViewerActions.class.getFields())
090        {
091            final Class<?> type = field.getType();
092
093            try
094            {
095                if (ClassUtil.isSubClass(type, IcyAbstractAction[].class))
096                    result.addAll(Arrays.asList(((IcyAbstractAction[]) field.get(null))));
097                else if (ClassUtil.isSubClass(type, IcyAbstractAction.class))
098                    result.add((IcyAbstractAction) field.get(null));
099            }
100            catch (Exception e)
101            {
102                // ignore
103            }
104        }
105
106        return result;
107    }
108}