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.JComponent;
010    import javax.swing.JPanel;
011    import javax.swing.JScrollPane;
012    import javax.swing.JTextArea;
013    import javax.swing.event.TableModelEvent;
014    import javax.swing.event.TableModelListener;
015    
016    import market.CIArticle;
017    import market.Conversions;
018    import market.SICustomer;
019    import market.swing.CmpNumbers;
020    import market.swing.ComponentFactory;
021    import sale.FormSheet;
022    import sale.FormSheetContentCreator;
023    import util.swing.AbstractTableEntryDescriptor;
024    import util.swing.TableEntryDescriptor;
025    import data.CountingStock;
026    import data.stdforms.SingleTableFormSheet;
027    import data.swing.CountingStockTableModel;
028    
029    /**
030     * This FormSheet shows the order of the customer that has to be deduct in a table,
031     * it also shows a message-field containing information about removed articles that aren`t avaible yet.
032     */
033    public class FSSellerOrderTable {
034    
035        /**
036         * @return the order-table.
037         *
038         * @param shoppingBasket the shopping-basket of the customer that has to be deduct.
039         * @param sic the SICustomer in the till-queue related to the customer.
040         */
041        public static SingleTableFormSheet getOrderTable(CountingStock shoppingBasket, final SICustomer sic){
042    
043            final SingleTableFormSheet stfs = SingleTableFormSheet.create(
044                    "Kundenauftrag",
045                    shoppingBasket,
046                    null,
047                    getTED());
048    
049            stfs.addContentCreator(new FormSheetContentCreator(){
050                protected void createFormSheetContent(FormSheet fs) {
051                    JPanel jp_main = new JPanel();
052                    JPanel jp_upper = new JPanel();
053                    JComponent jp_table = new JPanel();
054                    JPanel jp_message = new JPanel();
055                    JScrollPane jsc = new JScrollPane();
056                    final JTextArea jta = new JTextArea(10, 200);
057                    String missingA = sic.getMissingArticles();
058                    if(missingA.length()>1) jta.setText(getMessage(missingA));
059                    GridBagConstraints c = new GridBagConstraints();
060                    GridBagLayout gridbag = new GridBagLayout();
061    
062                    stfs.getTable().getModel().addTableModelListener(new TableModelListener(){
063                        public void tableChanged(TableModelEvent e) {
064                            String s = sic.getMissingArticles();
065                            if(s.length()>1) jta.setText(getMessage(s));
066                        }
067                    });
068    
069                    jp_table = fs.getComponent();
070                    jp_upper.setLayout(gridbag);
071                          c.gridx = 0;
072                          c.weightx = 1;
073                          c.weighty = 1;
074                          c.gridy = 0;
075                          c.fill = GridBagConstraints.BOTH;
076                        gridbag.setConstraints(jp_table, c);
077                    jp_upper.add(jp_table);
078                    jp_message.setBorder(ComponentFactory.createInsetBorder("Benachrichtigung"));
079                    jp_message.setLayout(new BoxLayout(jp_message, BoxLayout.X_AXIS));
080                    jp_message.add(jsc);
081                    jsc.setViewportView(jta);
082    
083                    jp_main.setLayout(new BoxLayout(jp_main, BoxLayout.Y_AXIS));
084                    jp_main.add(jp_upper);
085                    jp_main.add(Box.createVerticalStrut(10));
086                    jp_main.add(jp_message);
087                    fs.setComponent(jp_main);
088    
089                    fs.removeAllButtons();
090                    fs.addButton("Abrechnen",ButtonIDs.BTN_ACCEPT , null);
091                    fs.addButton("Zurück",ButtonIDs.BTN_BACK , null);
092                }
093            });
094            return stfs;
095        }
096    
097        /**
098         * @return the message to be shown if articles are missing.
099         *
100         * @param missingA String containing counts and articles that are missed
101         */
102        private static String getMessage(String missingA){
103            return new String("Aufgrund eines vorübergehenden Engpasses\n"+
104                        "mussten wir folgende Artikel aus ihrer Bestellung entfernen:"+missingA);
105        }
106    
107        /**
108         * @return the TableEntryDescriptor of the table.
109         */
110        private static TableEntryDescriptor getTED(){
111            AbstractTableEntryDescriptor ted = new AbstractTableEntryDescriptor(){
112                public int getColumnCount() {
113                    return 3;
114                }
115    
116                public String getColumnName(int nIdx) {
117                    return (new String[]{ "Artikel", "Preis", "Anzahl"}) [nIdx];
118                }
119    
120                public Class getColumnClass(int nIdx) {
121                    return (new Class[] {String.class, Number.class, Number.class}) [nIdx];
122                }
123    
124                public Object getValueAt(Object oRecord, int nIdx) {
125                    CIArticle article = Conversions.recordToCIArticle(oRecord);
126                    int count = ((CountingStockTableModel.Record)oRecord).getCount();
127                    switch (nIdx) {
128                        case 0:
129                            return article.getArticleName();
130                        case 1:
131                            return Conversions.valueToCurrency(
132                                    CIArticle.getCatalogItemValue().getValue(article));
133                        case 2:
134                            return new Integer(count).toString();
135                    }
136                    return null;
137                }
138    
139                public boolean canSortByColumn(int nIndex) {
140                    return true;
141                 }
142    
143                 public Comparator getColumnOrder(int nIndex) {
144                    switch (nIndex) {
145                       case 1: return new CmpNumbers(CmpNumbers.BID);
146                       case 2: return new CmpNumbers(CmpNumbers.COUNT);
147                       default: return null;
148                    }
149                 }
150            };
151            return ted;
152        }
153    }