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.action;
020
021import java.awt.event.ActionEvent;
022import java.awt.event.InputEvent;
023import java.awt.event.KeyEvent;
024import java.lang.reflect.Field;
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.List;
028
029import icy.gui.main.MainFrame;
030import icy.main.Icy;
031import icy.preferences.GeneralPreferences;
032import icy.resource.ResourceUtil;
033import icy.resource.icon.IcyIcon;
034import icy.swimmingPool.SwimmingPoolViewer;
035import icy.util.ClassUtil;
036
037/**
038 * @author Stephane
039 */
040public class WindowActions
041{
042    public static IcyAbstractAction stayOnTopAction = new IcyAbstractAction("Stay on top",
043            new IcyIcon(ResourceUtil.ICON_PIN), "Keep window on top", "Icy window always stays above other windows.")
044    {
045        /**
046         * 
047         */
048        private static final long serialVersionUID = 389778521530821291L;
049
050        @Override
051        public boolean doAction(ActionEvent e)
052        {
053            final boolean value = !Icy.getMainInterface().isAlwaysOnTop();
054
055            // set "always on top" state
056            Icy.getMainInterface().setAlwaysOnTop(value);
057            // and save state
058            GeneralPreferences.setAlwaysOnTop(value);
059
060            return true;
061        }
062    };
063
064    public static IcyAbstractAction swimmingPoolAction = new IcyAbstractAction("Swimming Pool Viewer",
065            new IcyIcon(ResourceUtil.ICON_BOX), "Show the swimming pool", "Show and edit the swimming pool objects")
066    {
067        /**
068         * 
069         */
070        private static final long serialVersionUID = -2243906270795266643L;
071
072        @Override
073        public boolean doAction(ActionEvent e)
074        {
075            new SwimmingPoolViewer();
076
077            return true;
078        }
079    };
080
081    public static IcyAbstractAction gridTileAction = new IcyAbstractAction("Grid (Shift+G)", new IcyIcon("2x2_grid"),
082            "Grid tile arrangement", "Reorganise all opened windows in grid tile.", KeyEvent.VK_G,
083            InputEvent.SHIFT_MASK)
084    {
085        /**
086         * 
087         */
088        private static final long serialVersionUID = 5529845883985655784L;
089
090        @Override
091        public boolean doAction(ActionEvent e)
092        {
093            final MainFrame mainFrame = Icy.getMainInterface().getMainFrame();
094
095            if (mainFrame != null)
096            {
097                mainFrame.organizeTile(MainFrame.TILE_GRID);
098                return true;
099            }
100
101            return false;
102        }
103    };
104
105    public static IcyAbstractAction horizontalTileAction = new IcyAbstractAction("Horizontal (Shift+H)",
106            new IcyIcon("tile_horizontal"), "Horizontal tile arrangement",
107            "Reorganise all opened windows in horizontal tile.", KeyEvent.VK_H, InputEvent.SHIFT_MASK)
108    {
109        /**
110         * 
111         */
112        private static final long serialVersionUID = 5752682613042198566L;
113
114        @Override
115        public boolean doAction(ActionEvent e)
116        {
117            final MainFrame mainFrame = Icy.getMainInterface().getMainFrame();
118
119            if (mainFrame != null)
120            {
121                mainFrame.organizeTile(MainFrame.TILE_HORIZONTAL);
122                return true;
123            }
124
125            return false;
126        }
127    };
128
129    public static IcyAbstractAction verticalTileAction = new IcyAbstractAction("Vertical (Shift+V)",
130            new IcyIcon("tile_vertical"), "Vertical tile arrangement",
131            "Reorganise all opened windows in vertical tile.", KeyEvent.VK_V, InputEvent.SHIFT_MASK)
132    {
133        /**
134         * 
135         */
136        private static final long serialVersionUID = -3978957277869827951L;
137
138        @Override
139        public boolean doAction(ActionEvent e)
140        {
141            final MainFrame mainFrame = Icy.getMainInterface().getMainFrame();
142
143            if (mainFrame != null)
144            {
145                mainFrame.organizeTile(MainFrame.TILE_VERTICAL);
146                return true;
147            }
148
149            return false;
150        }
151    };
152
153    public static IcyAbstractAction cascadeAction = new IcyAbstractAction("Cascade", new IcyIcon("cascade"),
154            "Cascade arrangement", "Reorganise all opened windows in cascade.")
155    {
156        /**
157         * 
158         */
159        private static final long serialVersionUID = 5074922972421168033L;
160
161        @Override
162        public boolean doAction(ActionEvent e)
163        {
164            final MainFrame mainFrame = Icy.getMainInterface().getMainFrame();
165
166            if (mainFrame != null)
167            {
168                mainFrame.organizeCascade();
169                return true;
170            }
171
172            return false;
173        }
174    };
175
176    /**
177     * Return all actions of this class
178     */
179    public static List<IcyAbstractAction> getAllActions()
180    {
181        final List<IcyAbstractAction> result = new ArrayList<IcyAbstractAction>();
182
183        for (Field field : WindowActions.class.getFields())
184        {
185            final Class<?> type = field.getType();
186
187            try
188            {
189                if (ClassUtil.isSubClass(type, IcyAbstractAction[].class))
190                    result.addAll(Arrays.asList(((IcyAbstractAction[]) field.get(null))));
191                else if (ClassUtil.isSubClass(type, IcyAbstractAction.class))
192                    result.add((IcyAbstractAction) field.get(null));
193            }
194            catch (Exception e)
195            {
196                // ignore
197            }
198        }
199
200        return result;
201    }
202}