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.SMarket;
017    import market.swing.CCSStrategyMarket;
018    import market.swing.CmpNumbers;
019    import market.swing.JTADescriptionArea;
020    import sale.FormSheet;
021    import sale.FormSheetContentCreator;
022    import sale.UIGate;
023    import util.swing.AbstractTableEntryDescriptor;
024    import data.CountingStock;
025    import data.DataBasket;
026    import data.stdforms.TwoTableFormSheet;
027    import data.swing.CountingStockTableModel;
028    
029    
030    /**
031     * This FormSheet is used by the manager to purchase new items for the market's stock.
032     */
033    public class FSManagerPurchase {
034    
035        /**
036         * Creates a {@link TwoTableFormSheet}. The look of the table is defined by the
037         * {@link TEDManagerPurchase}.
038         *
039         * @param gate the gate on which the TwoTableFormSheet should be displayed. It must not be null,
040         * because a valid gate is needed to enable the shifting of items between the tables.
041         * @param cs the CountingStock in which the items that the manager selected for purchase are stored.
042         * @param db the transaction's DataBasket.
043         *
044         * @return the created TwoTableFormSheet.
045         */
046        public static TwoTableFormSheet create(UIGate gate, CountingStock cs, DataBasket db) {
047            final TwoTableFormSheet ttfs = TwoTableFormSheet.create(
048                "Einkauf",                                                    //Caption
049                SMarket.getArticleCatalog(),                                  //TableSources
050                cs,
051                db,                                                           //DB
052                gate,                                                         //UIGate
053                null, null,                                                   //Initial Comparators
054                false,                                                        //Display zeros
055                new TEDManagerPurchase(true), new TEDManagerPurchase(false),  //TableEntryDescriptors
056                new CCSStrategyMarket()                                       //MoveStrategy
057            );
058            ttfs.addContentCreator(new FormSheetContentCreator() {
059                protected void createFormSheetContent(final FormSheet fs) {
060                    //define components
061                    JPanel jpMain = new JPanel();
062                    JPanel jpUpper = new JPanel();
063                    JPanel jpTables = new JPanel();
064                    JPanel jpDescription = new JPanel();
065                    JScrollPane jsc = new JScrollPane();
066                    final JTADescriptionArea da = new JTADescriptionArea();
067                    GridBagConstraints c = new GridBagConstraints();
068                    GridBagLayout gridbag = new GridBagLayout();
069                    //add Listener to table that reacts on selection change
070                    ttfs.getLeftTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
071                        public void valueChanged(ListSelectionEvent e) {
072                            if (!e.getValueIsAdjusting()) {
073                                da.setDescription(Conversions.recordToCIArticle(
074                                    ttfs.getLeftSelectedRecord()));
075                            }
076                        }
077                    });
078                    //add components
079                    jpTables = (JPanel)fs.getComponent();
080                    jpDescription.setLayout(new BoxLayout(jpDescription, BoxLayout.X_AXIS));
081                    jpDescription.add(jsc);
082                        jsc.setViewportView(da);
083                    jpMain.setLayout(new BoxLayout(jpMain, BoxLayout.Y_AXIS));
084                    jpMain.add(jpTables);
085                    jpMain.add(Box.createVerticalStrut(10));
086                    jpMain.add(jpDescription);
087                    fs.setComponent(jpMain);
088                    fs.removeAllButtons();
089                    fs.addButton("Kaufen", ButtonIDs.BTN_BUY, null);
090                }
091            });
092            return ttfs;
093        }
094    }
095    
096    /**
097     * The {@link util.swing.TableEntryDescriptor} used by {@link FSManagerPurchase}.
098     */
099    class TEDManagerPurchase extends AbstractTableEntryDescriptor {
100    
101        /**
102         * Is queried to determine whether this TED is used for the left or for the right table.
103         */
104        private boolean left;
105        private Comparator sortOfferSum = new CmpNumbers(CmpNumbers.OFFER, CmpNumbers.SUMMED);
106        private Comparator sortOfferNoSum = new CmpNumbers(CmpNumbers.OFFER, CmpNumbers.SINGLE);
107        private Comparator sortCount = new CmpNumbers(CmpNumbers.COUNT);
108    
109        /**
110         * @param left <ul><li>true: Use this TableEntryDescriptor for the left table.</li>
111         *                  <li>false: Use this TableEntryDescriptor for the right table.</li</ul>
112         */
113        public TEDManagerPurchase(boolean left) {
114            this.left = left;
115        }
116    
117        /**
118         * @return the number of the table's columns.
119         */
120        public int getColumnCount() {
121            return 3;
122        }
123    
124        /**
125         * @param nIndex the affected column.
126         * @return columns' names.
127         */
128        public String getColumnName(int nIndex) {
129            return (new String[]{ "Artikel", left ? "Kategorie" : "Preis", left ? "Preis" : "Anzahl"}) [nIndex];
130        }
131    
132        /**
133         * @param nIndex the affected column.
134         * @return columns' classes. They indicate how column's values should be aligned.
135         */
136        public Class getColumnClass  (int nIndex) {
137            return (new Class[] {String.class, left ? String.class : Number.class, Number.class}) [nIndex];
138        }
139    
140        /**
141         * @param oRecord the affected table record.
142         * @param nIndex the affected column.
143         * @return columns' values
144         */
145        public Object getValueAt(Object oRecord, int nIndex) {
146            CountingStockTableModel.Record stockitem = null;
147            CIArticle article = Conversions.recordToCIArticle(oRecord);
148            int count = 0;
149            if (!left) {
150                count = ((CountingStockTableModel.Record)oRecord).getCount();
151            }
152            switch (nIndex) {
153                case 0: return article.getArticleName();
154                case 1: return left ? article.getCategory() :
155                                Conversions.doubleToCurrency(count * article.getOffer());
156                case 2: return left ? Conversions.doubleToCurrency(article.getOffer()) :
157                                new Integer(count).toString();
158            }
159            return null;
160        }
161    
162        /**
163         * Determines if columns can be sorted by the user.
164         *
165         * @param nIndex the affected column.
166         * @return <ul><li>true: columns can be sorted</li>
167         *              <li>false: columns cannot be sorted</li></ul>
168         */
169        public boolean canSortByColumn(int nIndex) {
170            return true;
171        }
172    
173        /**
174         * @param nIndex the affected column.
175         * @return the {@link Comparator} to be used when sorting the column.
176         */
177        public Comparator getColumnOrder(int nIndex) {
178            switch(nIndex) {
179                case 1: return left ? null : sortOfferSum;
180                case 2: return left ? sortOfferNoSum : sortCount;
181            }
182            return null;
183        }
184    }