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.util;
020
021import java.awt.CheckboxMenuItem;
022import java.awt.Menu;
023import java.awt.MenuBar;
024import java.awt.MenuItem;
025import java.awt.event.ActionEvent;
026import java.awt.event.ActionListener;
027import java.awt.event.ItemEvent;
028import java.awt.event.ItemListener;
029
030import javax.swing.JCheckBoxMenuItem;
031import javax.swing.JMenu;
032import javax.swing.JMenuBar;
033import javax.swing.JMenuItem;
034
035/**
036 * Swing utilities class.
037 * 
038 * @author Stephane
039 */
040public class SwingUtil
041{
042    /**
043     * Class used to wrap a AWT CheckboxMenuItem in a Swing JCheckBoxMenuItem.
044     * 
045     * @author Stephane
046     */
047    private static class JCheckBoxMenuItemWrapper extends JCheckBoxMenuItem implements ActionListener, ItemListener
048    {
049        /**
050         * 
051         */
052        private static final long serialVersionUID = -3283063959812167447L;
053
054        final CheckboxMenuItem checkboxMenuItem;
055
056        public JCheckBoxMenuItemWrapper(CheckboxMenuItem checkboxMenuItem)
057        {
058            super(checkboxMenuItem.getLabel(), checkboxMenuItem.getState());
059
060            // keep reference on source MenuItem
061            this.checkboxMenuItem = checkboxMenuItem;
062
063            setActionCommand(checkboxMenuItem.getActionCommand());
064
065            checkboxMenuItem.addItemListener(this);
066
067            addActionListener(this);
068            addItemListener(this);
069        }
070
071        @Override
072        public void actionPerformed(ActionEvent e)
073        {
074            // change source with original one
075            e.setSource(checkboxMenuItem);
076
077            // dispatch to original listeners
078            for (ActionListener al : checkboxMenuItem.getActionListeners())
079                al.actionPerformed(e);
080        }
081
082        @Override
083        public void itemStateChanged(ItemEvent e)
084        {
085            if (e.getSource() == checkboxMenuItem)
086                setSelected(checkboxMenuItem.getState());
087            else
088            {
089                final boolean state = isSelected();
090
091                if (checkboxMenuItem.getState() != state)
092                {
093                    checkboxMenuItem.setState(isSelected());
094
095                    // build event
096                    final ItemEvent iv = new ItemEvent(checkboxMenuItem, ItemEvent.ITEM_STATE_CHANGED, getText(),
097                            state ? ItemEvent.SELECTED : ItemEvent.DESELECTED);
098
099                    // dispatch to original listeners
100                    for (ItemListener il : checkboxMenuItem.getItemListeners())
101                        il.itemStateChanged(iv);
102                }
103            }
104        }
105    }
106
107    /**
108     * Class used to wrap a AWT MenuItem in a Swing JMenuItem.
109     * 
110     * @author Stephane
111     */
112    private static class JMenuItemWrapper extends JMenuItem implements ActionListener
113    {
114        /**
115         * 
116         */
117        private static final long serialVersionUID = -3283063959812167447L;
118
119        final MenuItem menuItem;
120
121        public JMenuItemWrapper(MenuItem menuItem)
122        {
123            super(menuItem.getLabel());
124
125            // keep reference on source MenuItem
126            this.menuItem = menuItem;
127
128            setActionCommand(menuItem.getActionCommand());
129
130            addActionListener(this);
131        }
132
133        @Override
134        public void actionPerformed(ActionEvent e)
135        {
136            // change source with original one
137            e.setSource(menuItem);
138
139            // dispatch to original listeners
140            for (ActionListener al : menuItem.getActionListeners())
141                al.actionPerformed(e);
142        }
143    }
144
145    /**
146     * Convert a AWT MenuBar to a Swing JMenuBar.
147     */
148    public static JMenuBar getJMenuBar(MenuBar menuBar, boolean heavy)
149    {
150        final JMenuBar result = new JMenuBar();
151
152        if (menuBar != null)
153        {
154            for (int i = 0; i < menuBar.getMenuCount(); i++)
155                result.add(getJMenu(menuBar.getMenu(i), heavy));
156        }
157
158        return result;
159    }
160
161    /**
162     * Convert a AWT Menu to a Swing JMenu.
163     */
164    public static JMenu getJMenu(Menu menu, boolean heavy)
165    {
166        final JMenu result = new JMenu();
167
168        if (menu != null)
169        {
170            result.setText(menu.getLabel());
171            if (heavy)
172                result.getPopupMenu().setLightWeightPopupEnabled(false);
173
174            for (int i = 0; i < menu.getItemCount(); i++)
175                result.add(getJMenuItem(menu.getItem(i), heavy));
176        }
177
178        return result;
179    }
180
181    /**
182     * Convert a AWT MenuItem to a Swing JMenuItem.
183     */
184    public static JMenuItem getJMenuItem(MenuItem menuItem, boolean heavy)
185    {
186        if (menuItem != null)
187        {
188            if (menuItem instanceof Menu)
189                return getJMenu((Menu) menuItem, heavy);
190            if (menuItem instanceof CheckboxMenuItem)
191                return new JCheckBoxMenuItemWrapper((CheckboxMenuItem) menuItem);
192
193            return new JMenuItemWrapper(menuItem);
194        }
195
196        return new JMenuItem();
197    }
198}