001 package market.stdform; 002 003 import java.util.Comparator; 004 005 import javax.swing.Box; 006 import javax.swing.BoxLayout; 007 import javax.swing.JPanel; 008 import javax.swing.JScrollPane; 009 import javax.swing.event.ListSelectionEvent; 010 import javax.swing.event.ListSelectionListener; 011 012 import market.CIArticle; 013 import market.Conversions; 014 import market.SMarket; 015 import market.swing.CCSStrategyMarket; 016 import market.swing.CmpNumbers; 017 import market.swing.JTADescriptionArea; 018 import sale.FormSheet; 019 import sale.FormSheetContentCreator; 020 import sale.UIGate; 021 import util.swing.AbstractTableEntryDescriptor; 022 import data.CountingStock; 023 import data.DataBasket; 024 import data.stdforms.TwoTableFormSheet; 025 import data.swing.CountingStockTableModel; 026 027 028 /** 029 * This FormSheet is used by the manager to purchase new items for the market's stock. 030 */ 031 public class FSManagerPurchase { 032 033 /** 034 * Creates a {@link TwoTableFormSheet}. The look of the table is defined by the 035 * {@link TEDManagerPurchase}. 036 * 037 * @param gate the gate on which the TwoTableFormSheet should be displayed. It must not be null, 038 * because a valid gate is needed to enable the shifting of items between the tables. 039 * @param cs the CountingStock in which the items that the manager selected for purchase are stored. 040 * @param db the transaction's DataBasket. 041 * 042 * @return the created TwoTableFormSheet. 043 */ 044 public static TwoTableFormSheet create(UIGate gate, CountingStock cs, DataBasket db) { 045 final TwoTableFormSheet ttfs = TwoTableFormSheet.create( 046 "Einkauf", //Caption 047 SMarket.getArticleCatalog(), //TableSources 048 cs, 049 db, //DB 050 gate, //UIGate 051 null, null, //Initial Comparators 052 false, //Display zeros 053 new TEDManagerPurchase(true), new TEDManagerPurchase(false), //TableEntryDescriptors 054 new CCSStrategyMarket() //MoveStrategy 055 ); 056 ttfs.addContentCreator(new FormSheetContentCreator() { 057 private static final long serialVersionUID = -407676705651293497L; 058 protected void createFormSheetContent(final FormSheet fs) { 059 //define components 060 JPanel jpMain = new JPanel(); 061 JPanel jpTables = new JPanel(); 062 JPanel jpDescription = new JPanel(); 063 JScrollPane jsc = new JScrollPane(); 064 final JTADescriptionArea da = new JTADescriptionArea(); 065 //add Listener to table that reacts on selection change 066 ttfs.getLeftTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() { 067 public void valueChanged(ListSelectionEvent e) { 068 if (!e.getValueIsAdjusting()) { 069 da.setDescription(Conversions.recordToCIArticle( 070 ttfs.getLeftSelectedRecord())); 071 } 072 } 073 }); 074 //add components 075 jpTables = (JPanel)fs.getComponent(); 076 jpDescription.setLayout(new BoxLayout(jpDescription, BoxLayout.X_AXIS)); 077 jpDescription.add(jsc); 078 jsc.setViewportView(da); 079 jpMain.setLayout(new BoxLayout(jpMain, BoxLayout.Y_AXIS)); 080 jpMain.add(jpTables); 081 jpMain.add(Box.createVerticalStrut(10)); 082 jpMain.add(jpDescription); 083 fs.setComponent(jpMain); 084 fs.removeAllButtons(); 085 fs.addButton("Kaufen", ButtonIDs.BTN_BUY, null); 086 } 087 }); 088 return ttfs; 089 } 090 } 091 092 /** 093 * The {@link util.swing.TableEntryDescriptor} used by {@link FSManagerPurchase}. 094 */ 095 class TEDManagerPurchase extends AbstractTableEntryDescriptor { 096 097 /** 098 * ID for serialization. 099 */ 100 private static final long serialVersionUID = 4046390546093078053L; 101 102 /** 103 * Is queried to determine whether this TED is used for the left or for the right table. 104 */ 105 private boolean left; 106 private Comparator<Object> sortOfferSum = new CmpNumbers(CmpNumbers.OFFER, CmpNumbers.SUMMED); 107 private Comparator<Object> sortOfferNoSum = new CmpNumbers(CmpNumbers.OFFER, CmpNumbers.SINGLE); 108 private Comparator<Object> sortCount = new CmpNumbers(CmpNumbers.COUNT); 109 110 /** 111 * @param left <ul><li>true: Use this TableEntryDescriptor for the left table.</li> 112 * <li>false: Use this TableEntryDescriptor for the right table.</li</ul> 113 */ 114 public TEDManagerPurchase(boolean left) { 115 this.left = left; 116 } 117 118 /** 119 * @return the number of the table's columns. 120 */ 121 public int getColumnCount() { 122 return 3; 123 } 124 125 /** 126 * @param nIndex the affected column. 127 * @return columns' names. 128 */ 129 public String getColumnName(int nIndex) { 130 return (new String[]{ "Artikel", left ? "Kategorie" : "Preis", left ? "Preis" : "Anzahl"}) [nIndex]; 131 } 132 133 /** 134 * @param nIndex the affected column. 135 * @return columns' classes. They indicate how column's values should be aligned. 136 */ 137 public Class<?> getColumnClass (int nIndex) { 138 return (new Class[] {String.class, left ? String.class : Number.class, Number.class}) [nIndex]; 139 } 140 141 /** 142 * @param oRecord the affected table record. 143 * @param nIndex the affected column. 144 * @return columns' values 145 */ 146 public Object getValueAt(Object oRecord, int nIndex) { 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<Object> 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 }