001    package sale.stdforms;
002    
003    import sale.SaleProcess;
004    import sale.ProcessErrorCodes;
005    
006    import java.io.Serializable;
007    
008    /**
009     * <i>Abstract</i> super class of all strategies that are used with FormSheets.
010     *
011     * <p>Strategies are usually used to parameterize certain responses to user actions in standard FormSheets.
012     * Good examples are the parameterized responses to the buttons in a {@link data.stdforms.TwoTableFormSheet}.
013     * </p>
014     *
015     * <p>This <i>abstract</i> base class knows only about error handling. It uses
016     * {@link ErrorHandler error handling strategies} so that subclasses can easily be adapted to behave
017     * differently in case of an error.</p>
018     *
019     * @author Steffen Zschaler
020     * @version 2.0 18/08/1999
021     * @since v2.0
022     */
023    public abstract class FormSheetStrategy extends Object implements ProcessErrorCodes, Serializable {
024    
025        /**
026         * Interface that defines a error handling strategy for a {@link FormSheetStrategy}.
027         *
028         * @author Steffen Zschaler
029         * @version 2.0 18/08/1999
030         * @since v2.0
031         */
032        public static interface ErrorHandler extends Serializable, ProcessErrorCodes {
033            /**
034             * Handle an error.
035             *
036             * @param p the process in which the error occurred.
037             * @param nErrorCode an int value describing the error.
038             *
039             * @override Always
040             */
041            public void error(SaleProcess p, int nErrorCode);
042        }
043    
044        /**
045         * The default error handler. This will pass the error on to the process'
046         * {@link sale.SaleProcess#error(int)} method.
047         */
048        public static final ErrorHandler DEFAULT_ERROR_HANDLER = new ErrorHandler() {
049            public void error(SaleProcess p, int nErrorCode) {
050                p.error(nErrorCode);
051            }
052        };
053    
054        /**
055         * An alternative error handler that will pop up a modeless {@link MsgForm} in the process.
056         */
057        public static final ErrorHandler MSG_POPUP_ERROR_HANDLER = new ErrorHandler() {
058            public void error(SaleProcess p, int nErrorCode) {
059                MsgForm mfs = new MsgForm("Error", p.getErrorMsg(nErrorCode), false);
060    
061                try {
062                    p.getContext().popUpFormSheet(p, mfs);
063                }
064                catch (InterruptedException ie) {}
065            }
066        };
067    
068        /**
069         * The current error handler. Defaults to the {@link #DEFAULT_ERROR_HANDLER}.
070         *
071         * @serial
072         */
073        protected ErrorHandler m_ehErrHandler = DEFAULT_ERROR_HANDLER;
074    
075        /**
076         * Set the current error handler.
077         *
078         * @param ehErrHandler the new error handler.
079         *
080         * @override Never
081         */
082        public void setErrorHandler(ErrorHandler ehErrHandler) {
083            if (ehErrHandler != null) {
084                m_ehErrHandler = ehErrHandler;
085            } else {
086                m_ehErrHandler = DEFAULT_ERROR_HANDLER;
087            }
088        }
089    
090        /**
091         * Handle an error.
092         *
093         * <p>This is delegated to the error handler.</p>
094         *
095         * @param p the process in which the error occurred.
096         * @param nErrorCode an int describing the error.
097         *
098         * @override Never
099         */
100        public void error(SaleProcess p, int nErrorCode) {
101            m_ehErrHandler.error(p, nErrorCode);
102        }
103    }