001    package sale;
002    
003    import java.awt.event.ActionListener;
004    import java.awt.event.ActionEvent;
005    
006    /**
007     * A special ActionListener that allows to associate Actions with buttons that are not
008     * in a FormSheet's button bar or a MenuSheet.
009     *
010     * <p>You can use subclasses of this class as ActionListeners on any button in a FormSheet
011     * that is not in the button bar. You can then think of it as of an {@link Action}
012     * associated with that button. The {@link #doAction} method will be called with the same
013     * parameters as for an Action associated with a button in the FormSheet's button bar.</p>
014     *
015     * <p>If you do not override {@link #doAction} in subclasses, it will defer event handling
016     * to the Action object handed in on creation. Thus, you can create chains of responsibility
017     * which allow for, e.g., a {@link users.Capability capability} to be associated with any
018     * {@link java.awt.event.ActionEvent} in the FormSheet.</p>
019     *
020     * @see FormSheet
021     *
022     * @author Steffen Zschaler
023     * @version 2.0 07/06/1999
024     * @since v2.0
025     */
026    public class ActionActionListener extends Object implements Action, ActionListener {
027    
028        /**
029         * The FormSheet that contains this Action's button.
030         *
031         * @serial
032         */
033        protected FormSheet m_fsOwner;
034    
035        /**
036         * The action to be performed, when the listener is triggered.
037         *
038         * @serial
039         */
040        protected Action m_aAction;
041    
042        /**
043         * Create a new ActionActionListener. You must override {@link #doAction} when using this constructor.
044         *
045         * @param fsOwner the FormSheet that contains this Action's button.
046         */
047        public ActionActionListener(FormSheet fsOwner) {
048            this(fsOwner, null);
049        }
050    
051        /**
052         * Create a new ActionActionListener. You should not override {@link #doAction} when using this constructor.
053         *
054         * @param fsOwner the FormSheet that contains this Action's button.
055         * @param aAction the Action to perform when the listener is triggered.
056         */
057        public ActionActionListener(FormSheet fsOwner, Action aAction) {
058            super();
059    
060            m_fsOwner = fsOwner;
061            m_aAction = aAction;
062        }
063    
064        /**
065         * ActionListener interface method. Will redirect the event to the {@link #doAction}
066         * method.
067         *
068         * @override Never
069         */
070        public final void actionPerformed(ActionEvent e) {
071            new Thread("ActionActionListener event handler") {
072                public void run() {
073                    try {
074                        doAction(m_fsOwner.getProcess(), m_fsOwner.getSalesPoint());
075                    }
076                    catch (ThreadDeath td) {
077                        throw td;
078                    }
079                    catch (Throwable t) {
080                        System.err.println("Exception occured during event handling:");
081                        t.printStackTrace();
082                    }
083                }
084            }
085    
086            .start();
087        }
088    
089        /**
090         * Action interface method. Unless you override it, it will redirect the event to the
091         * {@link Action#doAction doAction()} method of the Action that is associated with this
092         * listener.
093         *
094         * @override Sometimes Override this method when you used
095         * {@link #ActionActionListener(sale.FormSheet)} as a constructor and want the listener to be the
096         *{@link Action Action object} at the same time.
097         *
098         * @exception Throwable on any error that shall be reported and lead to cancellation of
099         * the action.
100         *
101         * @see #ActionActionListener(sale.FormSheet, sale.Action)
102         */
103        public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
104            m_aAction.doAction(p, sp);
105        }
106    }