001    package market.stdform;
002    
003    import java.awt.GridBagConstraints;
004    import java.awt.GridBagLayout;
005    import java.util.Comparator;
006    
007    import javax.swing.Box;
008    import javax.swing.BoxLayout;
009    import javax.swing.JPanel;
010    import javax.swing.JScrollPane;
011    import javax.swing.event.ListSelectionEvent;
012    import javax.swing.event.ListSelectionListener;
013    
014    import market.CIArticle;
015    import market.Conversions;
016    import market.swing.CmpNumbers;
017    import market.swing.JTADescriptionArea;
018    import sale.FormSheet;
019    import sale.FormSheetContentCreator;
020    import sale.SaleProcess;
021    import sale.UIGate;
022    import sale.stdforms.FormSheetStrategy;
023    import util.swing.AbstractTableEntryDescriptor;
024    import util.swing.TableEntryDescriptor;
025    import data.CountingStock;
026    import data.DataBasket;
027    import data.stdforms.TwoTableFormSheet;
028    import data.stdforms.twotableformsheet.CSCSStrategy;
029    import data.swing.CountingStockTableModel;
030    
031    /**
032     * This FormSheet displays a TwoTableFormSheet where customers can choose articles from the offer
033     * and put them in there shopping-baskets, furthermore there is a description-field at which
034     * a short description of the selected product is shown.
035     */
036    public class FSCustomerOfferTable {
037    
038        /**
039         * @return the offer to shopping-basket table.
040         *
041         * @param offer the markets offer.
042         * @param shoppingBasket the shopping-basket of the customer.
043         * @param db the DataBasket related to the transfers between offer and shopping-basket.
044         * @param uig the UIGate at which this FormSheet is shown.
045         */
046        public static TwoTableFormSheet getOfferFormSheet(CountingStock offer, CountingStock shoppingBasket, DataBasket db, UIGate uig){
047            CSCSStrategy cscss = new CSCSStrategy();
048            cscss.setErrorHandler(new FormSheetStrategy.ErrorHandler(){
049                            private static final long serialVersionUID = 9002356431043143409L;
050                            public void error(SaleProcess p, int nErrorCode) {
051                    if(nErrorCode==FormSheetStrategy.ErrorHandler.NOT_ENOUGH_ELEMENTS_ERROR){
052                        p.getCurrentGate();
053                    }
054                    else p.error(nErrorCode);
055                }
056            });
057            final TwoTableFormSheet ttfs = TwoTableFormSheet.create(
058                    "Produktauswahl",
059                    offer,
060                    shoppingBasket,
061                    db,
062                    uig,
063                    null,
064                    null,
065                    false, false,
066                    getTED(),
067                    getTED(),
068                    cscss);
069    
070            ttfs.addContentCreator(new FormSheetContentCreator(){
071                            private static final long serialVersionUID = 3127106951584690470L;
072                            protected void createFormSheetContent(FormSheet fs) {
073                    JPanel jpMain = new JPanel();
074                    JPanel jpUpper = new JPanel();
075                    JPanel jpTables = new JPanel();
076                    JPanel jpDescription = new JPanel();
077                    JScrollPane jsc = new JScrollPane();
078                    final JTADescriptionArea da = new JTADescriptionArea();
079                    GridBagConstraints c = new GridBagConstraints();
080                    GridBagLayout gridbag = new GridBagLayout();
081    
082                    ttfs.getLeftTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
083                        public void valueChanged(ListSelectionEvent e) {
084                            if (!e.getValueIsAdjusting()&&(ttfs.getLeftSelectedRecord()!=null)) da.setDescription(Conversions.recordToCIArticle(ttfs.getLeftSelectedRecord()));
085                        }
086                    });
087    
088                    jpTables = (JPanel)fs.getComponent();
089                    jpUpper.setLayout(gridbag);
090                          c.gridx = 0;
091                          c.weightx = 1;
092                          c.weighty = 1;
093                          c.gridy = 0;
094                          c.fill = GridBagConstraints.BOTH;
095                        gridbag.setConstraints(jpTables, c);
096                    jpUpper.add(jpTables);
097                    jpDescription.setLayout(new BoxLayout(jpDescription, BoxLayout.X_AXIS));
098                    jpDescription.add(jsc);
099                    jsc.setViewportView(da);
100    
101                    jpMain.setLayout(new BoxLayout(jpMain, BoxLayout.Y_AXIS));
102                    jpMain.add(jpUpper);
103                    jpMain.add(Box.createVerticalStrut(10));
104                    jpMain.add(jpDescription);
105                    fs.setComponent(jpMain);
106    
107                    fs.removeAllButtons();
108                    fs.addButton("Kaufen", ButtonIDs.BTN_BUY, null);
109                    fs.addButton("Zurück", ButtonIDs.BTN_BACK, null);
110                }
111            });
112            return ttfs;
113        }
114    
115        /**
116         * @return the TableEntryDescriptor of the table.
117         */
118        private static TableEntryDescriptor getTED(){
119            return new AbstractTableEntryDescriptor(){
120                            private static final long serialVersionUID = -3039507143399314727L;
121                            public int getColumnCount() {
122                    return 4;
123                }
124    
125                public String getColumnName(int nIdx) {
126                    return (new String[]{ "Artikel", "Kategorie", "Preis", "Anzahl"}) [nIdx];
127                }
128    
129                public Class<?> getColumnClass(int nIdx) {
130                    return (new Class<?>[] {String.class, String.class, Number.class, Number.class}) [nIdx];
131                }
132    
133                public Object getValueAt(Object oRecord, int nIdx) {
134                    CIArticle article = Conversions.recordToCIArticle(oRecord);
135                    int count = ((CountingStockTableModel.Record)oRecord).getCount();
136                    switch (nIdx) {
137                        case 0: return article.getArticleName();
138                        case 1: return article.getCategory();
139                        case 2: return Conversions.valueToCurrency(CIArticle.getCatalogItemValue().getValue(article));
140                        case 3: return new Integer(count).toString();
141                    }
142                    return null;
143                 }
144    
145                 public boolean canSortByColumn(int nIndex) {
146                    return true;
147                 }
148    
149                 public Comparator<Object> getColumnOrder(int nIndex) {
150                    switch (nIndex) {
151                       case 1: return null ;
152                       case 2: return new CmpNumbers(CmpNumbers.BID);
153                       case 3: return new CmpNumbers(CmpNumbers.COUNT);
154                       default: return null;
155                    }
156                 }
157            };
158        }
159    }