| Actions.java |
1 import org.swixml.SwingEngine;
2
3 import javax.swing.*;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.WindowAdapter;
7 import java.awt.event.WindowEvent;
8
9 /**
10 * The Actions class shows how to use the
11 * <code>Actions</code> and <code>ActionCommand</code> attributes
12 *
13 * @author <a href="mailto:[email protected]">Wolf Paulus</a>
14 * @version $Revision: 1.1 $
15 * @since swixml #065
16 */
17public class Actions extends WindowAdapter implements ActionListener {
18 private SwingEngine swix;
19
20 public JMenuItem mi_exit, mi_save;
21 public JPanel pnl_North;
22 //
23 // For every Actions, there needs to be a
24 // public AbstractAction member variables with an anonymous inner class instantiation
25 //
26
27 /** <code>Action</code> newAction handles the <em>new</em> action attribute */
28 public Action newAction = new AbstractAction() {
29
30 public void actionPerformed(ActionEvent e) {
31 System.out.println( "New" ); // show the access outer class member ..
32 this.setEnabled( false ); // disables ALL buttons that are tied to this action
33 }
34 };
35
36 /** <code>Action</code> modifyAction handles the <em>modify</em> action attribute */
37 public Action openAction = new AbstractAction() {
38 /** Invoked when an action occurs. */
39 public void actionPerformed(ActionEvent e) {
40 System.out.println( "Open" );
41 }
42 };
43
44 /** <code>Action</code> petAction handles the <em>combobox</em> */
45 public Action petAction = new AbstractAction() {
46 public void actionPerformed(ActionEvent e) {
47 System.out.println( ((JComboBox) e.getSource()).getSelectedItem().toString() );
48 }
49 };
50
51
52 /**
53 * Constructs a new Actions object, registering action handlers for center_panel components.
54 */
55 private Actions() {
56 try {
57 swix = new SwingEngine( this );
58 swix.render( "xml/actions.xml" );
59
60 // at this point all AbstractActions are linked with the button etc.
61 // ActionCommands however need to be linked manually, see below ...
62
63 // add this class as an action listener to all buttons inside the panel with the id = center_panel
64 swix.setActionListener( pnl_North, this );
65 // add this class as an action listener to MenuItem with the id = mi_exit.
66 mi_exit.addActionListener( this );
67 // add this class as an action listener to MenuItem with the id = mi_save
68 mi_save.addActionListener( this );
69 //
70 // Note, the mi_about MenuItem was not linked at all so far. Therefore, no action is performed when this
71 // menu item gets requested.
72 // The Toolbar button with the Actions="newAction" attribute is covered twice,
73 // during parsing the AbstactAction newAction is linked in and later, the setActionListener() adds
74 // this object's actionPerformed(). Therefore, when clicked, both actionPerformed() methods are getting called
75 //
76 swix.getRootComponent().setVisible(true);
77 } catch (Exception e) {
78 e.printStackTrace();
79 }
80 }
81
82 //
83 // Implement ActionListener
84 //
85
86 /**
87 * Invoked when an action occurs.
88 */
89 public void actionPerformed(ActionEvent e) {
90 String command = e.getActionCommand();
91 if ("AC_EXIT".equals( command )) {
92 this.windowClosing( null );
93 } else if ("AC_SAVE".equals( command )) {
94 System.out.println( "Save" );
95 } else {
96 System.out.println( "Click" );
97 }
98 }
99
00 //
01 // Overwrite Superclass implementation
02 //
03
04 /**
05 * Invoked when the user attempts to close the window
06 * from the window's system menu. If the program does not
07 * explicitly hide or dispose the window while processing
08 * this event, the window close operation will be cancelled.
09 */
10 public void windowClosing(WindowEvent e) {
11 System.out.println( "Good Bye!" );
12 super.windowClosing(e);
13 System.exit( 0 );
14 }
15
16 //
17 // Make the class bootable
18 //
19
20 public static void main(String[] args) {
21 SwingEngine.DEBUG_MODE=true;
22 new Actions();
23 }
24}
25| Actions.java |