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.JLabel;
010    import javax.swing.JPanel;
011    import javax.swing.JScrollPane;
012    
013    import market.CIArticle;
014    import market.CIOpenPurchaseOrders;
015    import market.Conversions;
016    import market.swing.CmpNumbers;
017    import sale.FormSheet;
018    import sale.FormSheetContentCreator;
019    import util.swing.AbstractTableEntryDescriptor;
020    import data.stdforms.SingleTableFormSheet;
021    import data.swing.CountingStockTableModel;
022    
023    /**
024     * This FormSheet displays details of a market's order that has not yet arrived.
025     */
026    public class FSManagerOpenPurchaseOrderDetail {
027    
028        /**
029         * Creates a {@link SingleTableFormSheet} with some extra components. The look of the table is
030         * defined by the {@link TEDManagerOpenPurchaseOrderDetail}.
031         *
032         * @return the created SingleTableFormSheet.
033         */
034        public static SingleTableFormSheet create(final CIOpenPurchaseOrders opo) {
035            final SingleTableFormSheet stfs = SingleTableFormSheet.create(
036                    "Bestellung " + opo.getOrderNumber() + " vom " + opo.getDate(),
037                    opo.getOrders(),
038                    null,                                     //DataBasket
039                    new TEDManagerOpenPurchaseOrderDetail()   //TED
040            );
041            stfs.addContentCreator(new FormSheetContentCreator() {
042                            private static final long serialVersionUID = -5344729075434548585L;
043                            protected void createFormSheetContent(final FormSheet fs) {
044                    //define components
045                    JPanel jpMain = new JPanel();
046                    JPanel jpTable = new JPanel();
047                    JPanel jpInfo = new JPanel();
048                    JScrollPane jsp = (JScrollPane)fs.getComponent();
049                    GridBagConstraints c = new GridBagConstraints();
050                    GridBagLayout gridbag = new GridBagLayout();
051                    //add components
052                    jpTable.setLayout(gridbag);
053                          c.gridx = 0;
054                          c.weightx = 1;
055                          c.weighty = 1;
056                          c.gridy = 0;
057                          c.fill = GridBagConstraints.BOTH;
058                        gridbag.setConstraints(jsp, c);
059                    jpTable.add(jsp);
060                    jpInfo.setLayout(new BoxLayout(jpInfo, BoxLayout.Y_AXIS));
061                    jpInfo.add(new JLabel("Gesamtpreis:   " +
062                            Conversions.doubleToCurrency(opo.getOrdersValue().doubleValue(), " Euro")));
063                    jpInfo.add(new JLabel("Lieferung kommt in " + opo.getDaysTillArrival() + " Tagen an."));
064    
065                    jpMain.setLayout(new BoxLayout(jpMain, BoxLayout.Y_AXIS));
066                    jpMain.add(jpTable);
067                    jpMain.add(Box.createVerticalStrut(10));
068                    jpMain.add(jpInfo);
069                    fs.setComponent(jpMain);
070                    fs.removeAllButtons();
071                    fs.addButton("Zurück", ButtonIDs.BTN_BACK, null);
072                }
073            });
074            return stfs;
075        }
076    }
077    
078    /**
079     * The {@link util.swing.TableEntryDescriptor} used by {@link FSManagerOpenPurchaseOrderDetail}.
080     */
081    class TEDManagerOpenPurchaseOrderDetail extends AbstractTableEntryDescriptor {
082    
083        /**
084             * ID for serialization.
085             */
086            private static final long serialVersionUID = -5641235077828822700L;
087            
088            private Comparator<Object> sortOfferNoSum = new CmpNumbers(CmpNumbers.OFFER, CmpNumbers.SINGLE);
089        private Comparator<Object> sortOfferSum = new CmpNumbers(CmpNumbers.OFFER, CmpNumbers.SUMMED);
090        private Comparator<Object> sortCount = new CmpNumbers(CmpNumbers.COUNT);
091    
092        /**
093         * @return the number of the table's columns.
094         */
095        public int getColumnCount() {
096            return 5;
097        }
098    
099        /**
100         * @param nIndex the affected column.
101         * @return columns' names.
102         */
103        public String getColumnName(int nIndex) {
104            return (new String[]{ "Artikel", "Kategorie", "Einzeleinkaufspreis", "Anzahl", "Summe"}) [nIndex];
105        }
106    
107        /**
108         * @param nIndex the affected column.
109         * @return columns' classes. They indicate how column's values should be aligned.
110         */
111        public Class<?> getColumnClass  (int nIndex) {
112            return (new Class[] {String.class, String.class, Number.class, Number.class, Number.class}) [nIndex];
113        }
114    
115        /**
116         * @param oRecord the affected table record.
117         * @param nIndex the affected column.
118         * @return columns' values
119         */
120        public Object getValueAt(Object oRecord, int nIndex) {
121            CIArticle article = Conversions.recordToCIArticle(oRecord);
122            int count = ((CountingStockTableModel.Record)oRecord).getCount();
123            switch (nIndex) {
124                case 0: return article.getArticleName();
125                case 1: return article.getCategory();
126                case 2: return Conversions.doubleToCurrency(article.getOffer());
127                case 3: return new Integer(count).toString();
128                case 4: return Conversions.doubleToCurrency(count * article.getOffer());
129            }
130            return null;
131        }
132    
133        /**
134         * Determines if columns can be sorted by the user.
135         *
136         * @param nIndex the affected column.
137         * @return <ul><li>true: columns can be sorted</li>
138         *              <li>false: columns cannot be sorted</li></ul>
139         */
140        public boolean canSortByColumn(int nIndex) {
141            return true;
142        }
143    
144        /**
145         * @param nIndex the affected column.
146         * @return the {@link Comparator} to be used when sorting the column.
147         */
148        public Comparator<Object> getColumnOrder(int nIndex) {
149            switch(nIndex) {
150                case 2: return sortOfferNoSum;
151                case 3: return sortCount;
152                case 4: return sortOfferSum;
153            }
154            return null;
155        }
156    }