MenuHelper.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.gui.se.util;
017 
018 import javax.swing.Icon;
019 import javax.swing.JCheckBoxMenuItem;
020 import javax.swing.JMenu;
021 import javax.swing.JMenuItem;
022 import javax.swing.JRadioButtonMenuItem;
023 import javax.swing.KeyStroke;
024 import javax.swing.event.ChangeEvent;
025 import javax.swing.event.ChangeListener;
026 
027 
028 /**
029  * Various menu utility methods.
030  *
031  @author  Michael Meyling
032  */
033 public final class MenuHelper {
034 
035     /**
036      * Hidden constructor.
037      */
038     private MenuHelper() {
039         // nothing to do
040     }
041 
042     /**
043      * Checks and answers whether the quit action has been moved to an
044      * operating system specific menu, e.g. the OS X application menu.
045      *
046      @return true if the quit action is in an OS-specific menu
047      */
048     public static boolean isQuitInOSMenu() {
049         return false;
050     }
051 
052     public static JMenu createMenu(final String text, final char mnemonic) {
053         JMenu menu = new JMenu(text);
054         menu.setMnemonic(mnemonic);
055         return menu;
056     }
057 
058     public static JMenuItem createMenuItem(final String text) {
059         return new JMenuItem(text);
060     }
061 
062     public static JMenuItem createMenuItem(final String text, final char mnemonic) {
063         return new JMenuItem(text, mnemonic);
064     }
065 
066     public static JMenuItem createMenuItem(final String text, final char mnemonic,
067             final KeyStroke key) {
068         JMenuItem menuItem = new JMenuItem(text, mnemonic);
069         menuItem.setAccelerator(key);
070         return menuItem;
071     }
072 
073     public static JMenuItem createMenuItem(final String text, final Icon icon) {
074         return new JMenuItem(text, icon);
075     }
076 
077     public static JMenuItem createMenuItem(final String text, final Icon icon,
078             final char mnemonic) {
079         final JMenuItem menuItem = new JMenuItem(text, icon);
080         menuItem.setMnemonic(mnemonic);
081         return menuItem;
082     }
083 
084     public static JMenuItem createMenuItem(final String text, final Icon icon, final char mnemonic,
085             final KeyStroke key) {
086         final JMenuItem menuItem = createMenuItem(text, icon, mnemonic);
087         menuItem.setAccelerator(key);
088         return menuItem;
089     }
090 
091     public static JRadioButtonMenuItem createRadioButtonMenuItem(final String text,
092             final boolean selected) {
093         return new JRadioButtonMenuItem(text, selected);
094     }
095 
096     public static JCheckBoxMenuItem createCheckBoxMenuItem(final String text,
097             final boolean selected) {
098         return new JCheckBoxMenuItem(text, selected);
099     }
100 
101     /**
102      * Checks and answers whether the about action has been moved to an
103      * operating system specific menu, e.g. the OS X application menu.
104      *
105      @return true if the about action is in an OS-specific menu
106      */
107     public static boolean isAboutInOSMenu() {
108         return false;
109     }
110 
111     /**
112      * Creates and returns a JRadioButtonMenuItem
113      * with the given enablement and selection state.
114      *
115      @param   text        Text to show.
116      @param   enabled     Enablement state.
117      @param   selected    Selection state.
118      @return  Created element.
119      */
120     public static JRadioButtonMenuItem createRadioItem(final String text, final boolean enabled,
121             final boolean selected) {
122         JRadioButtonMenuItem item = createRadioButtonMenuItem(text, selected);
123         item.setEnabled(enabled);
124         item.addChangeListener(new ChangeListener() {
125             public void stateChanged(final ChangeEvent e) {
126                 JRadioButtonMenuItem source = (JRadioButtonMenuIteme.getSource();
127                 source.setText(text);
128             }
129         });
130         return item;
131     }
132 
133     /**
134      * Creates and returns a JCheckBoxMenuItem
135      * with the given enablement and selection state.
136      *
137      @param   text        Text to show.
138      @param   enabled     Enablement state.
139      @param   selected    Selection state.
140      @return  Created element.
141      */
142     public static JCheckBoxMenuItem createCheckItem(final String text, final boolean enabled,
143             final boolean selected) {
144         JCheckBoxMenuItem item = createCheckBoxMenuItem(text, selected);
145         item.setEnabled(enabled);
146         item.addChangeListener(new ChangeListener() {
147             public void stateChanged(final ChangeEvent e) {
148                 JCheckBoxMenuItem source = (JCheckBoxMenuIteme.getSource();
149                 source.setText(text);
150             }
151         });
152         return item;
153     }
154 
155 }