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                public void error(SaleProcess p, int nErrorCode) {
050                    if(nErrorCode==FormSheetStrategy.ErrorHandler.NOT_ENOUGH_ELEMENTS_ERROR){
051                        p.getCurrentGate();
052                    }
053                    else p.error(nErrorCode);
054                }
055            });
056            final TwoTableFormSheet ttfs = TwoTableFormSheet.create(
057                    "Produktauswahl",
058                    offer,
059                    shoppingBasket,
060                    db,
061                    uig,
062                    null,
063                    null,
064                    false, false,
065                    getTED(),
066                    getTED(),
067                    cscss);
068    
069            ttfs.addContentCreator(new FormSheetContentCreator(){
070                protected void createFormSheetContent(FormSheet fs) {
071                    JPanel jpMain = new JPanel();
072                    JPanel jpUpper = new JPanel();
073                    JPanel jpTables = new JPanel();
074                    JPanel jpDescription = new JPanel();
075                    JScrollPane jsc = new JScrollPane();
076                    final JTADescriptionArea da = new JTADescriptionArea();
077                    GridBagConstraints c = new GridBagConstraints();
078                    GridBagLayout gridbag = new GridBagLayout();
079    
080                    ttfs.getLeftTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
081                        public void valueChanged(ListSelectionEvent e) {
082                            if (!e.getValueIsAdjusting()&&(ttfs.getLeftSelectedRecord()!=null)) da.setDescription(Conversions.recordToCIArticle(ttfs.getLeftSelectedRecord()));
083                        }
084                    });
085    
086                    jpTables = (JPanel)fs.getComponent();
087                    jpUpper.setLayout(gridbag);
088                          c.gridx = 0;
089                          c.weightx = 1;
090                          c.weighty = 1;
091                          c.gridy = 0;
092                          c.fill = GridBagConstraints.BOTH;
093                        gridbag.setConstraints(jpTables, c);
094                    jpUpper.add(jpTables);
095                    jpDescription.setLayout(new BoxLayout(jpDescription, BoxLayout.X_AXIS));
096                    jpDescription.add(jsc);
097                    jsc.setViewportView(da);
098    
099                    jpMain.setLayout(new BoxLayout(jpMain, BoxLayout.Y_AXIS));
100                    jpMain.add(jpUpper);
101                    jpMain.add(Box.createVerticalStrut(10));
102                    jpMain.add(jpDescription);
103                    fs.setComponent(jpMain);
104    
105                    fs.removeAllButtons();
106                    fs.addButton("Kaufen", ButtonIDs.BTN_BUY, null);
107                    fs.addButton("Zurück", ButtonIDs.BTN_BACK, null);
108                }
109            });
110            return ttfs;
111        }
112    
113        /**
114         * @return the TableEntryDescriptor of the table.
115         */
116        private static TableEntryDescriptor getTED(){
117            return new AbstractTableEntryDescriptor(){
118                public int getColumnCount() {
119                    return 4;
120                }
121    
122                public String getColumnName(int nIdx) {
123                    return (new String[]{ "Artikel", "Kategorie", "Preis", "Anzahl"}) [nIdx];
124                }
125    
126                public Class getColumnClass(int nIdx) {
127                    return (new Class[] {String.class, String.class, Number.class, Number.class}) [nIdx];
128                }
129    
130                public Object getValueAt(Object oRecord, int nIdx) {
131                    CIArticle article = Conversions.recordToCIArticle(oRecord);
132                    int count = ((CountingStockTableModel.Record)oRecord).getCount();
133                    switch (nIdx) {
134                        case 0: return article.getArticleName();
135                        case 1: return article.getCategory();
136                        case 2: return Conversions.valueToCurrency(CIArticle.getCatalogItemValue().getValue(article));
137                        case 3: return new Integer(count).toString();
138                    }
139                    return null;
140                 }
141    
142                 public boolean canSortByColumn(int nIndex) {
143                    return true;
144                 }
145    
146                 public Comparator getColumnOrder(int nIndex) {
147                    switch (nIndex) {
148                       case 1: return null ;
149                       case 2: return new CmpNumbers(CmpNumbers.BID);
150                       case 3: return new CmpNumbers(CmpNumbers.COUNT);
151                       default: return null;
152                    }
153                 }
154            };
155        }
156    }