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  @version $Revision: 1.2 $
032  @author  Michael Meyling
033  */
034 public final class MenuHelper  {
035 
036     /**
037      * Hidden constructor.
038      */
039     private MenuHelper() {
040         // nothing to do
041     }
042 
043     /**
044      * Checks and answers whether the quit action has been moved to an
045      * operating system specific menu, e.g. the OS X application menu.
046      *
047      @return true if the quit action is in an OS-specific menu
048      */
049     public static boolean isQuitInOSMenu() {
050         return false;
051     }
052 
053     public static JMenu createMenu(final String text, final char mnemonic) {
054         JMenu menu = new JMenu(text);
055         menu.setMnemonic(mnemonic);
056         return menu;
057     }
058 
059     public static JMenuItem createMenuItem(final String text) {
060         return new JMenuItem(text);
061     }
062 
063     public static JMenuItem createMenuItem(final String text, final char mnemonic) {
064         return new JMenuItem(text, mnemonic);
065     }
066 
067     public static JMenuItem createMenuItem(final String text, final char mnemonic,
068             final KeyStroke key) {
069         JMenuItem menuItem = new JMenuItem(text, mnemonic);
070         menuItem.setAccelerator(key);
071         return menuItem;
072     }
073 
074     public static JMenuItem createMenuItem(final String text, final Icon icon) {
075         return new JMenuItem(text, icon);
076     }
077 
078     public static JMenuItem createMenuItem(final String text, final Icon icon,
079             final char mnemonic) {
080         final JMenuItem menuItem = new JMenuItem(text, icon);
081         menuItem.setMnemonic(mnemonic);
082         return menuItem;
083     }
084 
085     public static JMenuItem createMenuItem(final String text, final Icon icon, final char mnemonic,
086             final KeyStroke key) {
087         final JMenuItem menuItem = createMenuItem(text, icon, mnemonic);
088         menuItem.setAccelerator(key);
089         return menuItem;
090     }
091 
092     public static JRadioButtonMenuItem createRadioButtonMenuItem(final String text,
093             final boolean selected) {
094         return new JRadioButtonMenuItem(text, selected);
095     }
096 
097     public static JCheckBoxMenuItem createCheckBoxMenuItem(final String text,
098             final boolean selected) {
099         return new JCheckBoxMenuItem(text, selected);
100     }
101 
102     /**
103      * Checks and answers whether the about action has been moved to an
104      * operating system specific menu, e.g. the OS X application menu.
105      *
106      @return true if the about action is in an OS-specific menu
107      */
108     public static boolean isAboutInOSMenu() {
109         return false;
110     }
111 
112     /**
113      * Creates and returns a JRadioButtonMenuItem
114      * with the given enablement and selection state.
115      *
116      @param   text        Text to show.
117      @param   enabled     Enablement state.
118      @param   selected    Selection state.
119      @return  Created element.
120      */
121     public static JRadioButtonMenuItem createRadioItem(final String text, final boolean enabled,
122             final boolean selected) {
123         JRadioButtonMenuItem item = createRadioButtonMenuItem(text, selected);
124         item.setEnabled(enabled);
125         item.addChangeListener(new ChangeListener() {
126             public void stateChanged(final ChangeEvent e) {
127                 JRadioButtonMenuItem source = (JRadioButtonMenuIteme.getSource();
128                 source.setText(text);
129             }
130         });
131         return item;
132     }
133 
134     /**
135      * Creates and returns a JCheckBoxMenuItem
136      * with the given enablement and selection state.
137      *
138      @param   text        Text to show.
139      @param   enabled     Enablement state.
140      @param   selected    Selection state.
141      @return  Created element.
142      */
143     public static JCheckBoxMenuItem createCheckItem(final String text, final boolean enabled,
144             final boolean selected) {
145         JCheckBoxMenuItem item = createCheckBoxMenuItem(text, selected);
146         item.setEnabled(enabled);
147         item.addChangeListener(new ChangeListener() {
148             public void stateChanged(final ChangeEvent e) {
149                 JCheckBoxMenuItem source = (JCheckBoxMenuIteme.getSource();
150                 source.setText(text);
151             }
152         });
153         return item;
154     }
155 
156 }