001    package market;
002    
003    import sale.Action;
004    import sale.FormSheet;
005    import sale.FormSheetContentCreator;
006    import sale.SaleProcess;
007    import sale.SalesPoint;
008    import sale.Transition;
009    import sale.UIGate;
010    
011    /**
012     * Helper class that supports the division of {@link FormSheet} and {@link SaleProcess}.<br>
013     * <br>
014     * Button actions which are attached to the FormSheets in the process are meant to be
015     * reloaded after the shop's state has been saved. Therefore it is necessary to put the assignment of the
016     * button actions to Buttons into a {@link FormSheetContentCreator}.<br>
017     * As this assignment code is always the same, it has been put into this extra class. That makes the original
018     * code more concise.
019     */
020    public abstract class SProcessMarket extends SaleProcess {
021    
022        /**
023         * @param name the name of the process.
024         */
025        public SProcessMarket(String name) {
026            super(name);
027        }
028    
029        /**
030         * Assigns an {@link Action} to a FormSheet's button using a {@link FormSheetContentCreator}.
031         *
032         * @param formSheet the FormSheet to which the action should be assigned.
033         * @param ac the action to be assigned.
034         * @param btn_id the button ID to which the action should be assigned. A button with this
035         * ID must be defined in the FormSheet itself or a {@link NullPointerException} will occur.
036         */
037        protected void setAction(FormSheet formSheet, final Action ac, final int btn_id){
038            formSheet.addContentCreator(new FormSheetContentCreator(){
039                protected void createFormSheetContent(FormSheet fs) {
040                    fs.getButton(btn_id).setAction(ac);
041                }
042            });
043        }
044    
045        /**
046         * Assigns an {@link Action}, which consists only of a {@link Transition} to a FormSheet's
047         * button using a {@link FormSheetContentCreator}.<br>
048         * Caution has to be taken when a circular reference occurs.<br>
049         * Example: Method getGateA() has a setTransition-method which changes to gateB using method
050         * getGateB(). This method getGateB() contains a setTransition-method which changes to gateA using
051         * the getGateA()-method. This causes an infinite loop.<br>
052         * In contrast the {@link #setAction(FormSheet, Action, int) setAction()} method works well,
053         * because the transition in it is not run on initialization, but only when the button has been pressed.
054         *
055         * @param formSheet the FormSheet from which the Transition starts.
056         * @param trans the Transition to be performed.
057         * @param btn_id the button ID that causes the transition to start. A button with this
058         * ID must be defined in the FormSheet itself or a {@link NullPointerException} will occur.
059         */
060        protected void setTransition(FormSheet formSheet, final Transition trans, int btn_id){
061            setAction(formSheet,
062                    new sale.Action(){
063                        public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
064                            if(p.getCurrentGate()instanceof UIGate)
065                                ((UIGate)p.getCurrentGate()).setNextTransition(trans);
066                        }
067                    },
068                    btn_id);
069        }
070    }