001    package videoautomat.contentcreator;
002    import javax.swing.Box;
003    import javax.swing.BoxLayout;
004    import javax.swing.JComponent;
005    import javax.swing.JLabel;
006    import javax.swing.JPanel;
007    
008    import sale.FormSheet;
009    import sale.FormSheetContentCreator;
010    import sale.FormSheet.FormButton;
011    import videoautomat.SaleProcessRent;
012    import videoautomat.contentcreator.stdactions.TransitWithAction;
013    import videoautomat.transition.RentPayConfirmTransition;
014    import videoautomat.transition.RentPayRollbackTransition;
015    import data.CatalogItemValue;
016    import data.DataBasket;
017    import data.IntegerValue;
018    import data.NumberValue;
019    import data.events.StockChangeAdapter;
020    import data.events.StockChangeEvent;
021    import data.ooimpl.MoneyBagImpl;
022    /**
023     * Changes the FormSheet layout and adds some labels and a listener
024     * to change the pay button state.
025     *
026     * @author Tobias Ruch
027     */
028    public class RentPayFSContentCreator extends FormSheetContentCreator {   
029       /**
030            * ID for Serialization.
031            */
032       private static final long serialVersionUID = 9181091654692530939L;
033       
034       /** String of the pay value */
035       private String payValue;
036       /** Process in which the FormSheet and so the content creator is used */
037       private SaleProcessRent processRent;
038       
039       /**
040        * Creates the content creator.
041        * @param process - <code>SaleProcessRent</code> in which the ContentCreator is use to have access to some
042        *                     process data 
043        */
044       public RentPayFSContentCreator(SaleProcessRent process){
045          this.processRent = process;
046          payValue = "";
047       }
048       
049       /**
050       * Changes the FormSheetContent of the gives <code>FormSheet</code>.
051       * Adds labels, button and there actions
052       * @param fs - a <code>FormSheet</code> which should be changed.
053       */
054       protected void createFormSheetContent(FormSheet fs) {      
055          JComponent jc = new JPanel();
056          jc.setLayout(new BoxLayout(jc, BoxLayout.Y_AXIS));
057          jc.add(Box.createVerticalStrut(10));
058          jc.add(new JLabel("You have to pay: " + payValue));
059          jc.add(Box.createVerticalStrut(10));
060          jc.add(fs.getComponent());
061          
062          fs.setComponent(jc);
063          fs.removeAllButtons();
064            
065          fs.addButton("Pay", 1, new TransitWithAction(new RentPayConfirmTransition()));
066    
067          fs.addButton("Cancel", 2, new TransitWithAction(new RentPayRollbackTransition()));
068          fs.getButton(1).setEnabled(false);
069          setButtonStockListener(fs.getButton(1));
070       }    
071    
072       
073       public void setPayValue(String payValue){
074          this.payValue = payValue;
075       }   
076       /**
077         * Adds an implementation of the <code>StockChangeListener</code> to the given button, so that this button gets
078         * enabled if there is enough money in the temporar-moneybag and otherwise it gets disabled
079         * 
080         * @param fb
081         *                  the <code>FormButton</code> the listener should en/disable
082         */
083       private void setButtonStockListener(final FormButton fb) {
084          final MoneyBagImpl bag = processRent.getTemporaryMoneyBag();
085          final DataBasket basket = processRent.getBasket();
086          final NumberValue nv_sum = processRent.getSumNumberValue();
087          
088          //new StockChangeApdater, which is extended by an anonymous class 
089          //which overrides the addedStockItems method
090          
091          StockChangeAdapter sca = new StockChangeAdapter() {
092                    private static final long serialVersionUID = 5693744331821735484L;
093                    public void addedStockItems(StockChangeEvent e) {
094                
095                
096                if (bag.sumStock(basket, new CatalogItemValue(), new IntegerValue(0))
097                      .compareTo(nv_sum)>= 0){
098                   fb.setEnabled(true);
099                }
100             }
101             public void removedStockItems(StockChangeEvent e) {
102                if (bag.sumStock(basket, new CatalogItemValue(), new IntegerValue(0))
103                      .compareTo(nv_sum) < 0){
104                   fb.setEnabled(false);
105                }
106             }
107          };
108          
109          bag.addStockChangeListener(sca);
110       }  
111    }