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                protected void createFormSheetContent(final FormSheet fs) {
043                    //define components
044                    JPanel jpMain = new JPanel();
045                    JPanel jpTable = new JPanel();
046                    JPanel jpInfo = new JPanel();
047                    JScrollPane jsp = (JScrollPane)fs.getComponent();
048                    GridBagConstraints c = new GridBagConstraints();
049                    GridBagLayout gridbag = new GridBagLayout();
050                    //add components
051                    jpTable.setLayout(gridbag);
052                          c.gridx = 0;
053                          c.weightx = 1;
054                          c.weighty = 1;
055                          c.gridy = 0;
056                          c.fill = GridBagConstraints.BOTH;
057                        gridbag.setConstraints(jsp, c);
058                    jpTable.add(jsp);
059                    jpInfo.setLayout(new BoxLayout(jpInfo, BoxLayout.Y_AXIS));
060                    jpInfo.add(new JLabel("Gesamtpreis:   " +
061                            Conversions.doubleToCurrency(opo.getOrdersValue().doubleValue(), " Euro")));
062                    jpInfo.add(new JLabel("Lieferung kommt in " + opo.getDaysTillArrival() + " Tagen an."));
063    
064                    jpMain.setLayout(new BoxLayout(jpMain, BoxLayout.Y_AXIS));
065                    jpMain.add(jpTable);
066                    jpMain.add(Box.createVerticalStrut(10));
067                    jpMain.add(jpInfo);
068                    fs.setComponent(jpMain);
069                    fs.removeAllButtons();
070                    fs.addButton("Zurück", ButtonIDs.BTN_BACK, null);
071                }
072            });
073            return stfs;
074        }
075    }
076    
077    /**
078     * The {@link util.swing.TableEntryDescriptor} used by {@link FSManagerOpenPurchaseOrderDetail}.
079     */
080    class TEDManagerOpenPurchaseOrderDetail extends AbstractTableEntryDescriptor {
081    
082        private Comparator sortOfferNoSum = new CmpNumbers(CmpNumbers.OFFER, CmpNumbers.SINGLE);
083        private Comparator sortOfferSum = new CmpNumbers(CmpNumbers.OFFER, CmpNumbers.SUMMED);
084        private Comparator sortCount = new CmpNumbers(CmpNumbers.COUNT);
085    
086        /**
087         * @return the number of the table's columns.
088         */
089        public int getColumnCount() {
090            return 5;
091        }
092    
093        /**
094         * @param nIndex the affected column.
095         * @return columns' names.
096         */
097        public String getColumnName(int nIndex) {
098            return (new String[]{ "Artikel", "Kategorie", "Einzeleinkaufspreis", "Anzahl", "Summe"}) [nIndex];
099        }
100    
101        /**
102         * @param nIndex the affected column.
103         * @return columns' classes. They indicate how column's values should be aligned.
104         */
105        public Class getColumnClass  (int nIndex) {
106            return (new Class[] {String.class, String.class, Number.class, Number.class, Number.class}) [nIndex];
107        }
108    
109        /**
110         * @param oRecord the affected table record.
111         * @param nIndex the affected column.
112         * @return columns' values
113         */
114        public Object getValueAt(Object oRecord, int nIndex) {
115            CIArticle article = Conversions.recordToCIArticle(oRecord);
116            int count = ((CountingStockTableModel.Record)oRecord).getCount();
117            switch (nIndex) {
118                case 0: return article.getArticleName();
119                case 1: return article.getCategory();
120                case 2: return Conversions.doubleToCurrency(article.getOffer());
121                case 3: return new Integer(count).toString();
122                case 4: return Conversions.doubleToCurrency(count * article.getOffer());
123            }
124            return null;
125        }
126    
127        /**
128         * Determines if columns can be sorted by the user.
129         *
130         * @param nIndex the affected column.
131         * @return <ul><li>true: columns can be sorted</li>
132         *              <li>false: columns cannot be sorted</li></ul>
133         */
134        public boolean canSortByColumn(int nIndex) {
135            return true;
136        }
137    
138        /**
139         * @param nIndex the affected column.
140         * @return the {@link Comparator} to be used when sorting the column.
141         */
142        public Comparator getColumnOrder(int nIndex) {
143            switch(nIndex) {
144                case 2: return sortOfferNoSum;
145                case 3: return sortCount;
146                case 4: return sortOfferSum;
147            }
148            return null;
149        }
150    }