/*
 * TextMenu.java
 */
 
package samples;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/** 
 * TextMenu: Shows menubar with text menus.
 */
public class TextMenu extends JFrame implements ActionListener {

    /** 
     * Resource bundle with default locale 
     */
    private ResourceBundle resources = null;

    //
    // Command strings for various actions (e.g., buttons or menu items).
    // These strings are never presented to the user and should
    // not be internationalized.
    //
    private String CMD_EXIT = "cmd.exit"/*NOI18N*/;
    private String CMD_FIND = "cmd.find"/*NOI18N*/;
    private String CMD_FIND_AGAIN = "cmd.find.again"/*NOI18N*/;
    private String CMD_FONT_BOLD = "cmd.font.bold"/*NOI18N*/;
    private String CMD_FONT_ITALIC = "cmd.font.italic"/*NOI18N*/;
    private String CMD_FONT_DEFAULT = "cmd.font.default"/*NOI18N*/;
    private String CMD_FONT_SELECT = "cmd.font.select"/*NOI18N*/;
    private String CMD_FONT_UNDERLINE = "cmd.underline"/*NOI18N*/;
    private String CMD_SELECT_ALL = "cmd.select.all"/*NOI18N*/;
    private String CMD_HELP_ABOUT = "cmd.help.about"/*NOI18N*/;
    private String CMD_HELP_CONTENTS = "cmd.help.contents"/*NOI18N*/;
    private String CMD_HELP_INDEX = "cmd.help.index"/*NOI18N*/;
    private String CMD_HELP_SEARCH = "cmd.help.search"/*NOI18N*/;
    private String CMD_HELP_TUTORIAL = "cmd.help.tutorial"/*NOI18N*/;
    private String CMD_FILE_CLOSE = "cmd.file.close"/*NOI18N*/;
    private String CMD_FILE_NEW = "cmd.file.new"/*NOI18N*/;
    private String CMD_FILE_OPEN = "cmd.file.open"/*NOI18N*/;
    private String CMD_FILE_SAVE = "cmd.file.save"/*NOI18N*/;
    private String CMD_FILE_SAVE_AS = "cmd.file.saveAs"/*NOI18N*/;
    private String CMD_PAGE_SETUP = "cmd.page.setup"/*NOI18N*/;
    private String CMD_TEXT_ALIGN_LEFT = "cmd.text.align.left"/*NOI18N*/;
    private String CMD_TEXT_ALIGN_CENTER = "cmd.text.align.center"/*NOI18N*/;
    private String CMD_TEXT_ALIGN_RIGHT = "cmd.text.align.right"/*NOI18N*/;
    private String CMD_UNDO = "cmd.undo"/*NOI18N*/;
    private String CMD_REDO = "cmd.redo"/*NOI18N*/;
    private String CMD_CUT = "cmd.cut"/*NOI18N*/;
    private String CMD_COPY = "cmd.copy"/*NOI18N*/;
    private String CMD_PASTE = "cmd.paste"/*NOI18N*/;
    private String CMD_PRINT = "cmd.print"/*NOI18N*/;

    /** 
     * Creates new frame, demonstrating text menus 
     */
    public TextMenu() {
        initResources();
        initComponents();
        pack();
    }

    /**
     * Loads locale-specific resources: strings, images, et cetera
     */
    private void initResources() {
	Locale locale = Locale.getDefault();
        resources = ResourceBundle.getBundle(
	    "samples.resources.bundles.TextMenuResources", locale);
    }

    /**
     * 

* Called from the constructor to initialize this window *

*

* We use dynamic layout managers, so that layout is dynamic and will * adapt properly to user-customized fonts and localized text. The * GridBagLayout makes it easy to line up components of varying * sizes along invisible vertical and horizontal grid lines. It * is important to sketch the layout of the interface and decide * on the grid before writing the layout code. *

*

* Here we actually use * our own subclass of GridBagLayout called StringGridBagLayout, * which allows us to use strings to specify constraints, rather * than having to create GridBagConstraints objects manually. *

*

* We use the JLabel.setLabelFor() method to connect * labels to what they are labeling. This allows mnemonics to work * and assistive to technologies used by persons with disabilities * to provide much more useful information to the user. *

*/ private void initComponents() { // set properties on the window setTitle(resources.getString("window.title")); addWindowListener (new WindowAdapter () { public void windowClosing(WindowEvent event) { windowAction(CMD_EXIT); } }); // accessibility - all frames, dialogs, and applets should have // a description getAccessibleContext().setAccessibleName( resources.getString("window.description")); JMenuBar menuBar = buildMenuBar(); setJMenuBar (menuBar); } // initComponents() private JMenuBar buildMenuBar() { JMenuBar menuBar = new JMenuBar(); menuBar.add(buildFileMenu()); menuBar.add(buildEditMenu()); menuBar.add(buildFormatMenu()); menuBar.add(buildHelpMenu()); return menuBar; } // buildMenuBar(); private JMenu buildFileMenu() { JMenu menu = null; JMenuItem item = null; menu = new JMenu(); menu.setText(resources.getString("fileMenu.label")); menu.setMnemonic( resources.getString("fileMenu.mnemonic").charAt(0)); // file->new item item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.file.new.accelerator"))); item.setText( resources.getString("menuitem.file.new.label")); item.setMnemonic( resources.getString("menuitem.file.new.mnemonic").charAt(0)); item.setActionCommand(CMD_FILE_NEW); item.addActionListener(this); menu.add(item); // open item item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.file.open.accelerator"))); item.setText( resources.getString("menuitem.file.open.label")); item.setMnemonic( resources.getString("menuitem.file.open.mnemonic").charAt(0)); item.setActionCommand(CMD_FILE_OPEN); item.addActionListener(this); menu.add(item); // close item item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.file.close.accelerator"))); item.setText( resources.getString("menuitem.file.close.label")); item.setMnemonic( resources.getString("menuitem.file.close.mnemonic").charAt(0)); item.setActionCommand(CMD_FILE_CLOSE); item.addActionListener(this); menu.add(item); // separator menu.addSeparator(); // save item item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.file.save.accelerator"))); item.setText( resources.getString("menuitem.file.save.label")); item.setMnemonic( resources.getString("menuitem.file.save.mnemonic").charAt(0)); item.setActionCommand(CMD_FILE_SAVE); item.addActionListener(this); menu.add(item); // save as item item = new JMenuItem(); item.setText( resources.getString("menuitem.file.saveAs.label")); item.setMnemonic( resources.getString("menuitem.file.saveAs.mnemonic").charAt(0)); item.setActionCommand(CMD_FILE_SAVE_AS); item.addActionListener(this); menu.add(item); // separator menu.addSeparator(); // page setup item item = new JMenuItem(); item.setText( resources.getString("menuitem.pageSetup.label")); // mnemonic item.setMnemonic( resources.getString("menuitem.pageSetup.mnemonic").charAt(0)); item.setActionCommand(CMD_PAGE_SETUP); item.addActionListener(this); menu.add(item); // print item item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.print.accelerator"))); item.setText( resources.getString("menuitem.print.label")); item.setMnemonic( resources.getString("menuitem.print.mnemonic").charAt(0)); item.setActionCommand(CMD_PRINT); item.addActionListener(this); menu.add(item); menu.addSeparator(); item = new JMenuItem(); item.setText( resources.getString("menuitem.exit.label")); item.setMnemonic( resources.getString("menuitem.exit.mnemonic").charAt(0)); item.setActionCommand(CMD_EXIT); item.addActionListener(this); menu.add(item); return menu; } // buildFileMenu() private JMenu buildEditMenu() { JMenu menu = null; JMenuItem item = null; menu = new JMenu(); menu.setText(resources.getString("editMenu.label")); menu.setMnemonic( resources.getString("editMenu.mnemonic.").charAt(0)); // undo item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.undo.accelerator"))); item.setText( resources.getString("menuitem.undo.label")); item.setMnemonic( resources.getString("menuitem.undo.mnemonic").charAt(0)); item.setActionCommand(CMD_UNDO); item.addActionListener(this); menu.add(item); // redo item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.redo.accelerator"))); item.setText( resources.getString("menuitem.redo.label")); item.setMnemonic( resources.getString("menuitem.redo.mnemonic").charAt(0)); item.setActionCommand(CMD_REDO); item.addActionListener(this); menu.add(item); menu.addSeparator(); // cut item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.cut.accelerator"))); item.setText( resources.getString("menuitem.cut.label")); item.setMnemonic( resources.getString("menuitem.cut.mnemonic").charAt(0)); item.setActionCommand(CMD_CUT); item.addActionListener(this); menu.add(item); // copy item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.copy.accelerator"))); item.setText( resources.getString("menuitem.copy.label")); item.setMnemonic( resources.getString("menuitem.copy.mnemonic").charAt(0)); item.setActionCommand(CMD_COPY); item.addActionListener(this); menu.add(item); // paste item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.paste.accelerator"))); item.setText( resources.getString("menuitem.paste.label")); item.setMnemonic( resources.getString("menuitem.paste.mnemonic").charAt(0)); item.setActionCommand(CMD_PASTE); item.addActionListener(this); menu.add(item); menu.addSeparator(); // find item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.find.accelerator"))); item.setText( resources.getString("menuitem.find.label")); item.setMnemonic( resources.getString("menuitem.find.mnemonic").charAt(0)); item.setActionCommand(CMD_FIND); item.addActionListener(this); menu.add(item); // find again item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.findAgain.accelerator"))); item.setText( resources.getString("menuitem.findAgain.label")); item.setMnemonic( resources.getString("menuitem.findAgain.mnemonic").charAt(0)); item.setActionCommand(CMD_FIND_AGAIN); item.addActionListener(this); item.setEnabled(false); menu.add(item); // select all item = new JMenuItem(); item.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.selectAll.accelerator"))); item.setText( resources.getString("menuitem.selectAll.label")); item.setMnemonic( resources.getString("menuitem.selectAll.mnemonic").charAt(0)); item.setActionCommand(CMD_SELECT_ALL); item.addActionListener(this); menu.add(item); return menu; } // buildEditMenu() private JMenu buildFormatMenu() { JMenu menu = null; JMenuItem item = null; JMenu subMenu = null; // submenu JCheckBoxMenuItem checkItem = null; JRadioButtonMenuItem radioItem = null; menu = new JMenu(); menu.setText( resources.getString("formatMenu.label")); menu.setMnemonic( resources.getString("formatMenu.mnemonic").charAt(0)); /***** ***** Font submenu (empty) *****/ subMenu = new JMenu(); subMenu.setText( resources.getString("fontMenu.label")); subMenu.setMnemonic( resources.getString("fontMenu.mnemonic").charAt(0)); menu.add(subMenu); /***** ***** style submenu *****/ subMenu = new JMenu(); subMenu.setText( resources.getString("fontStyleMenu.label")); subMenu.setMnemonic( resources.getString("fontStyleMenu.mnemonic").charAt(0)); // bold checkbox item checkItem = new JCheckBoxMenuItem(); checkItem.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.font.bold.accelerator"))); checkItem.setSelected(true); checkItem.setText( resources.getString("menuitem.font.bold.label")); checkItem.setMnemonic( resources.getString("menuitem.font.bold.mnemonic").charAt(0)); checkItem.setActionCommand(CMD_FONT_BOLD); checkItem.addActionListener(this); subMenu.add(checkItem); // italic checkbox item checkItem = new JCheckBoxMenuItem(); checkItem.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.font.italic.accelerator"))); checkItem.setSelected(false); checkItem.setText( resources.getString("menuitem.font.italic.label")); checkItem.setMnemonic( resources.getString("menuitem.font.italic.mnemonic").charAt(0)); checkItem.setActionCommand(CMD_FONT_ITALIC); checkItem.addActionListener(this); subMenu.add(checkItem); // underline checkbox item checkItem = new JCheckBoxMenuItem(); checkItem.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.font.underline.accelerator"))); checkItem.setSelected(false); checkItem.setText( resources.getString("menuitem.font.underline.label")); checkItem.setMnemonic( resources.getString("menuitem.font.underline.mnemonic"). charAt(0)); checkItem.setActionCommand(CMD_FONT_UNDERLINE); checkItem.addActionListener(this); subMenu.add(checkItem); menu.add(subMenu); /***** ***** size submenu (empty) *****/ subMenu = new JMenu(); subMenu.setText( resources.getString("fontSizeMenu.label")); subMenu.setMnemonic( resources.getString("fontSizeMenu.mnemonic").charAt(0)); menu.add(subMenu); menu.addSeparator(); ButtonGroup alignRadioGroup = new ButtonGroup(); // align left radio item radioItem = new JRadioButtonMenuItem(); radioItem.setSelected(true); radioItem.setText( resources.getString("menuitem.text.alignLeft.label")); radioItem.setMnemonic( resources.getString("menuitem.text.alignLeft.mnemonic").charAt(0)); radioItem.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.text.alignLeft.accelerator"))); radioItem.setActionCommand(CMD_TEXT_ALIGN_LEFT); radioItem.addActionListener(this); alignRadioGroup.add(radioItem); menu.add(radioItem); // align center radio item radioItem = new JRadioButtonMenuItem(); radioItem.setText( resources.getString("menuitem.text.alignCenter.label")); radioItem.setMnemonic( resources.getString("menuitem.text.alignCenter.mnemonic").charAt(0)); radioItem.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.text.alignCenter.accelerator"))); radioItem.setActionCommand(CMD_TEXT_ALIGN_CENTER); radioItem.addActionListener(this); alignRadioGroup.add(radioItem); menu.add(radioItem); // align right radio item radioItem = new JRadioButtonMenuItem(); radioItem.setText( resources.getString("menuitem.text.alignRight.label")); radioItem.setMnemonic( resources.getString("menuitem.text.alignRight.mnemonic").charAt(0)); radioItem.setAccelerator(KeyStroke.getKeyStroke( resources.getString("menuitem.text.alignRight.accelerator"))); radioItem.setActionCommand(CMD_TEXT_ALIGN_RIGHT); radioItem.addActionListener(this); alignRadioGroup.add(radioItem); menu.add(radioItem); return menu; } // buildTextMenu(); private JMenu buildHelpMenu() { JMenu menu = null; JMenuItem item = null; menu = new JMenu(); menu.setText( resources.getString("helpMenu.label")); menu.setMnemonic( resources.getString("helpMenu.mnemonic").charAt(0)); // contents item = new JMenuItem(); item.setText( resources.getString("menuitem.help.contents.label")); item.setMnemonic( resources.getString("menuitem.help.contents.mnemonic").charAt(0)); item.setActionCommand(CMD_HELP_CONTENTS); item.addActionListener(this); menu.add(item); // tutorial item = new JMenuItem(); item.setText( resources.getString("menuitem.help.tutorial.label")); item.setMnemonic( resources.getString("menuitem.help.tutorial.mnemonic").charAt(0)); item.setActionCommand(CMD_HELP_TUTORIAL); item.addActionListener(this); menu.add(item); // index item = new JMenuItem(); item.setText( resources.getString("menuitem.help.index.label")); item.setMnemonic( resources.getString("menuitem.help.index.mnemonic").charAt(0)); item.setActionCommand(CMD_HELP_INDEX); item.addActionListener(this); menu.add(item); // search item = new JMenuItem(); item.setText( resources.getString("menuitem.help.search.label")); item.setMnemonic( resources.getString("menuitem.help.search.mnemonic").charAt(0)); item.setActionCommand(CMD_HELP_SEARCH); item.addActionListener(this); menu.add(item); menu.addSeparator(); // About... item = new JMenuItem(); item.setText( resources.getString("menuitem.help.about.label")); item.setMnemonic( resources.getString("menuitem.help.about.mnemonic").charAt(0)); item.setActionCommand(CMD_HELP_ABOUT); item.addActionListener(this); menu.add(item); return menu; } // buildHelpMenu() public Dimension getPreferredSize() { Dimension pref = super.getPreferredSize(); pref.width = Math.max(pref.width, 300); pref.height = Math.max(pref.height, 200); return pref; } /** * The user has selected an option. Here we close and dispose the dialog. * If actionCommand is an ActionEvent, getCommandString() is called, * otherwise toString() is used to get the action command. * * @param actionCommand may be null */ private void windowAction(Object actionCommand) { String cmd = null; if (actionCommand != null) { if (actionCommand instanceof ActionEvent) { cmd = ((ActionEvent)actionCommand).getActionCommand(); } else { cmd = actionCommand.toString(); } } if (cmd == null) { // do nothing } else if (cmd.equals(CMD_EXIT)) { System.out.println("your 'exit' code here..."); setVisible(false); dispose(); System.exit(0); } else if (cmd.equals(CMD_FILE_OPEN)) { System.out.println("your 'open file' code here..."); } else if (cmd.equals(CMD_HELP_ABOUT)) { System.out.println("your 'help about' code here..."); } else if (cmd.equals(CMD_HELP_CONTENTS)) { System.out.println("your 'help contents' code here..."); } else if (cmd.equals(CMD_HELP_INDEX)) { System.out.println("your 'help index' code here..."); } else if (cmd.equals(CMD_HELP_SEARCH)) { System.out.println("your 'help search' code here..."); } else if (cmd.equals(CMD_HELP_TUTORIAL)) { System.out.println("your 'help tutorial' code here..."); } else { // et cetera - add more cases here System.out.println("Command invoked - " + cmd); } } // windowAction() /** * Menus invoke this method */ public void actionPerformed(ActionEvent event) { windowAction(event); } /** * This main() is provided for debugging purposes, to display a * sample dialog. */ public static void main(String args[]) { TextMenu frame = new TextMenu(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { System.exit(0); } public void windowClosed(WindowEvent event) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } // main() } // class TextMenu
-----