001 package market.stdform; 002 003 import java.util.Comparator; 004 005 import market.Conversions; 006 import market.SMarket; 007 import market.UCustomer; 008 import market.UMUserBase; 009 import market.event.MarketEventAdapter; 010 import market.statistics.Statistics; 011 import market.swing.CmpCustomersNumbers; 012 import sale.FormSheet; 013 import sale.FormSheetContentCreator; 014 import users.UserManagerFilter; 015 import users.stdforms.UserTableFormSheet; 016 import util.swing.AbstractTableEntryDescriptor; 017 018 /** 019 * This FormSheet displays all customers of the market in a table, along with some statistics. From here, 020 * a customer whose complete statistics wish to be viewed can be selected. 021 */ 022 public class FSManagerCustomerStatsMain extends UserTableFormSheet { 023 024 /** 025 * Creates a {@link UserTableFormSheet}. The look of the table is 026 * defined by the {@link TEDManagerCustomerStatsMain}. 027 */ 028 public FSManagerCustomerStatsMain() { 029 super("Kundenstatistik", new UserManagerFilter(UMUserBase.getGlobalBase().getCustomers()), 030 null, null, null, new TEDManagerCustomerStatsMain()); 031 addContentCreator(new FormSheetContentCreator() { 032 public void createFormSheetContent(final FormSheet fs) { 033 fs.removeAllButtons(); 034 fs.addButton("Details", ButtonIDs.BTN_DETAIL, null); 035 } 036 }); 037 //update view when time advances 038 SMarket.addEventListener(new MarketEventAdapter() { 039 public void timeAdvanced() { 040 ((util.swing.TableSorter)getTable().getModel()).fireTableDataChanged(); 041 } 042 }); 043 } 044 } 045 046 /** 047 * The {@link util.swing.TableEntryDescriptor} used by {@link FSManagerCustomerStatsMain}. 048 */ 049 class TEDManagerCustomerStatsMain extends AbstractTableEntryDescriptor { 050 051 private Comparator salesVolume = new CmpCustomersNumbers(CmpCustomersNumbers.SALES_VOLUME); 052 private Comparator discount = new CmpCustomersNumbers(CmpCustomersNumbers.DISCOUNT); 053 054 /** 055 * @return the number of the table's columns. 056 */ 057 public int getColumnCount() { 058 return 4; 059 } 060 061 /** 062 * @param nIndex the affected column. 063 * @return columns' names. 064 */ 065 public String getColumnName(int nIndex) { 066 return (new String[]{"Name", "Mitglied seit", "Anrechenbarer Umsatz", "Rabatt"}) [nIndex]; 067 } 068 069 /** 070 * @param nIndex the affected column. 071 * @return columns' classes. They indicate how column's values should be aligned. 072 */ 073 public Class getColumnClass (int nIndex) { 074 return new Class[] {String.class, String.class, Integer.class, Integer.class}[nIndex]; 075 } 076 077 /** 078 * @param oRecord the affected table record. 079 * @param nIndex the affected column. 080 * @return columns' values 081 */ 082 public Object getValueAt(Object oRecord, int nIndex) { 083 UCustomer usr = (UCustomer)oRecord; 084 switch (nIndex) { 085 case 0: 086 return usr.getSurname() + ", " + usr.getFirstName(); 087 case 1: 088 return usr.getDayOfRegistration(); 089 case 2: 090 return Conversions.doubleToCurrency(Statistics.getAllowableCustomerRevenue(usr), " Euro"); 091 case 3: 092 return Conversions.fixedDecimal(100 * usr.getDiscount(), 3) + " %"; 093 } 094 return null; 095 } 096 097 /** 098 * Determines if columns can be sorted by the user. 099 * 100 * @param nIndex the affected column. 101 * @return <ul><li>true: columns can be sorted</li> 102 * <li>false: columns cannot be sorted</li></ul> 103 */ 104 public boolean canSortByColumn(int nIndex) { 105 return true; 106 } 107 108 /** 109 * @param nIndex the affected column. 110 * @return the {@link Comparator} to be used when sorting the column. 111 */ 112 public Comparator getColumnOrder(int nIndex) { 113 switch (nIndex) { 114 case 2: 115 return salesVolume; 116 case 3: 117 return discount; 118 } 119 return null; 120 } 121 }