/* * @(#)ActionDemo.java 1.6 00/04/18 * * Copyright 2000 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import java.awt.*; import java.awt.event.*; import java.util.Enumeration; import java.util.Vector; import javax.swing.*; /** * Demonstrates the JLF Actions classes. * * - The actions use the JLF icons, text and mnemonics. * - Actions are shared between the JToolBar and JMenuBar. * - if an Action is enabled/disabled then it will be disabled in both places. * - When a mouse is over a toolbar button or a menu item, then the long * description of that action will be displayed in the status bar. * - Abstracts the actionPerformed method from the Action class to a handler. * * This demo requires JDK 1.3 * * @version 1.6 04/18/00 * @author Mark Davidson */ public class ActionDemo extends JFrame implements ActionListener { // These are the actions defined for the application private AboutAction aboutAction; private CutAction cutAction; private CopyAction copyAction; private PasteAction pasteAction; // Vector for holding all the actions. private Vector actions; // Status bar private JLabel status; // Text area which acts as a clipboard. private JTextArea textArea; // This adapter handles Mouse over messages on toolbar buttons and // menu items. private MouseHandler mouseHandler; // Popup Menu with the actions. private JPopupMenu popup; /** * ctor */ public ActionDemo() { super("Actions Demo"); initActions(); status = createStatusBar(); mouseHandler = new MouseHandler(status); setJMenuBar(createMenu()); getContentPane().add(createToolBar(), BorderLayout.NORTH); getContentPane().add(createPanel(), BorderLayout.CENTER); getContentPane().add(status, BorderLayout.SOUTH); popup = createPopupMenu(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } // This method should be called before creating the UI // to create all the Actions private void initActions() { actions = new Vector(); aboutAction = new AboutAction(); registerAction(aboutAction); cutAction = new CutAction(); registerAction(cutAction); copyAction = new CopyAction(); registerAction(copyAction); pasteAction = new PasteAction(); registerAction(pasteAction); } private void registerAction(JLFAbstractAction action) { action.addActionListener(this); actions.addElement(action); } // Creates the application menu bar. private JMenuBar createMenu() { JMenuBar menuBar = new JMenuBar(); JMenuItem menuItem; // Build the File menu JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); menuItem = fileMenu.add(aboutAction); menuItem.addMouseListener(mouseHandler); // Build the edit menu JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic('E'); menuItem = editMenu.add(cutAction); menuItem.addMouseListener(mouseHandler); menuItem = editMenu.add(copyAction); menuItem.addMouseListener(mouseHandler); menuItem = editMenu.add(pasteAction); menuItem.addMouseListener(mouseHandler); menuBar.add(fileMenu); menuBar.add(editMenu); return menuBar; } private JToolBar createToolBar() { JToolBar toolbar = new JToolBar(); JButton button; button = toolbar.add(cutAction); button.addMouseListener(mouseHandler); button = toolbar.add(copyAction); button.addMouseListener(mouseHandler); button = toolbar.add(pasteAction); button.addMouseListener(mouseHandler); toolbar.addSeparator(); button = toolbar.add(aboutAction); button.addMouseListener(mouseHandler); return toolbar; } private JPopupMenu createPopupMenu() { JPopupMenu menu = new JPopupMenu(); JMenuItem menuItem; menuItem = menu.add(cutAction); menuItem.addMouseListener(mouseHandler); menuItem = menu.add(copyAction); menuItem.addMouseListener(mouseHandler); menuItem = menu.add(pasteAction); menuItem.addMouseListener(mouseHandler); menu.addSeparator(); menuItem = menu.add(aboutAction); menuItem.addMouseListener(mouseHandler); return menu; } // Panel which allows for the enabling and disabling of all the actions. private JPanel createPanel() { // This check box enables or disables all the actions. JCheckBox enable = new JCheckBox("Enable All Actions", true); enable.setAlignmentX(0.5f); enable.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent evt) { boolean enabled = false; if (evt.getStateChange() == ItemEvent.SELECTED) { // Enabled all actions enabled = true; } Enumeration e = actions.elements(); Action action; while (e.hasMoreElements()) { action = (Action)e.nextElement(); action.setEnabled(enabled); } } }); JLabel label = new JLabel("Right click in Text Area for popup menu"); label.setAlignmentX(0.5f); textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); textArea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if(e.isPopupTrigger()) { popup.show(textArea, e.getX(), e.getY()); } } public void mouseReleased(MouseEvent e) { if(e.isPopupTrigger()) { popup.show(textArea, e.getX(), e.getY()); } } }); JPanel p1 = new JPanel(); p1.add(enable); p1.add(label); JPanel panel = new JPanel(new BorderLayout()); panel.setPreferredSize(new Dimension(450, 200)); panel.add(p1, BorderLayout.NORTH); panel.add(scrollPane, BorderLayout.CENTER); return panel; } // Creates the status bar. private JLabel createStatusBar() { status = new JLabel("Ready..."); status.setBorder(BorderFactory.createEtchedBorder()); return status; } /** * This method acts as the Action handler delegate for all the actions. * The Cut, Copy and Paste Actions operate on the JTextArea. */ public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); // Compare the action command to the known actions. if (command.equals(aboutAction.getActionCommand())) { // The about action was invoked JOptionPane.showMessageDialog(this, aboutAction.getLongDescription(), aboutAction.getShortDescription(), JOptionPane.INFORMATION_MESSAGE); } else if (command.equals(cutAction.getActionCommand())) { textArea.cut(); } else if (command.equals(copyAction.getActionCommand())) { textArea.copy(); } else if (command.equals(pasteAction.getActionCommand())) { textArea.paste(); } } /** * This adapter is constructed to handle mouse over component events. */ private class MouseHandler extends MouseAdapter { private JLabel label; /** * ctor for the adapter. * @param label the JLabel which will recieve value of the * Action.LONG_DESCRIPTION key. */ public MouseHandler(JLabel label) { setLabel(label); } public void setLabel(JLabel label) { this.label = label; } public void mouseEntered(MouseEvent evt) { if (evt.getSource() instanceof AbstractButton) { AbstractButton button = (AbstractButton)evt.getSource(); Action action = button.getAction(); // getAction is new in JDK 1.3 if (action != null) { String message = (String)action.getValue(Action.LONG_DESCRIPTION); label.setText(message); } } } } /** * Main method */ public static void main(String[] args) { ActionDemo demo = new ActionDemo(); demo.pack(); demo.setVisible(true); } }