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                            private static final long serialVersionUID = 4901773519416313359L;
040    
041                            protected void createFormSheetContent(FormSheet fs) {
042                    fs.getButton(btn_id).setAction(ac);
043                }
044            });
045        }
046    
047        /**
048         * Assigns an {@link Action}, which consists only of a {@link Transition} to a FormSheet's
049         * button using a {@link FormSheetContentCreator}.<br>
050         * Caution has to be taken when a circular reference occurs.<br>
051         * Example: Method getGateA() has a setTransition-method which changes to gateB using method
052         * getGateB(). This method getGateB() contains a setTransition-method which changes to gateA using
053         * the getGateA()-method. This causes an infinite loop.<br>
054         * In contrast the {@link #setAction(FormSheet, Action, int) setAction()} method works well,
055         * because the transition in it is not run on initialization, but only when the button has been pressed.
056         *
057         * @param formSheet the FormSheet from which the Transition starts.
058         * @param trans the Transition to be performed.
059         * @param btn_id the button ID that causes the transition to start. A button with this
060         * ID must be defined in the FormSheet itself or a {@link NullPointerException} will occur.
061         */
062        protected void setTransition(FormSheet formSheet, final Transition trans, int btn_id){
063            setAction(formSheet,
064                    new sale.Action(){
065                                            private static final long serialVersionUID = -1435650383336503583L;
066    
067                                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
068                            if(p.getCurrentGate()instanceof UIGate)
069                                ((UIGate)p.getCurrentGate()).setNextTransition(trans);
070                        }
071                    },
072                    btn_id);
073        }
074    }