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             * ID for serialization.
026             */
027            private static final long serialVersionUID = -5821214280590865410L;
028    
029            /**
030         * Creates a {@link UserTableFormSheet}. The look of the table is
031         * defined by the {@link TEDManagerCustomerStatsMain}.
032         */
033        public FSManagerCustomerStatsMain() {
034            super("Kundenstatistik", new UserManagerFilter(UMUserBase.getGlobalBase().getCustomers()),
035                    null, null, null, new TEDManagerCustomerStatsMain());
036            addContentCreator(new FormSheetContentCreator() {
037                            private static final long serialVersionUID = 6702310982310088632L;
038                            public void createFormSheetContent(final FormSheet fs) {
039                    fs.removeAllButtons();
040                    fs.addButton("Details", ButtonIDs.BTN_DETAIL, null);
041                }
042            });
043            //update view when time advances
044            SMarket.addEventListener(new MarketEventAdapter() {
045                            private static final long serialVersionUID = 2105879181881321827L;
046                            public void timeAdvanced() {
047                    ((util.swing.TableSorter)getTable().getModel()).fireTableDataChanged();
048                }
049            });
050        }
051    }
052    
053    /**
054     * The {@link util.swing.TableEntryDescriptor} used by {@link FSManagerCustomerStatsMain}.
055     */
056    class TEDManagerCustomerStatsMain extends AbstractTableEntryDescriptor {
057    
058        /**
059             * ID for serialization.
060             */
061            private static final long serialVersionUID = 5268132884960843114L;
062            private Comparator<Object> salesVolume = new CmpCustomersNumbers(CmpCustomersNumbers.SALES_VOLUME);
063        private Comparator<Object> discount = new CmpCustomersNumbers(CmpCustomersNumbers.DISCOUNT);
064    
065        /**
066         * @return the number of the table's columns.
067         */
068        public int getColumnCount() {
069            return 4;
070        }
071    
072        /**
073         * @param nIndex the affected column.
074         * @return columns' names.
075         */
076        public String getColumnName(int nIndex) {
077            return (new String[]{"Name", "Mitglied seit", "Anrechenbarer Umsatz", "Rabatt"}) [nIndex];
078        }
079    
080        /**
081         * @param nIndex the affected column.
082         * @return columns' classes. They indicate how column's values should be aligned.
083         */
084        public Class<?> getColumnClass  (int nIndex) {
085            return new Class[] {String.class, String.class, Integer.class, Integer.class}[nIndex];
086        }
087    
088        /**
089         * @param oRecord the affected table record.
090         * @param nIndex the affected column.
091         * @return columns' values
092         */
093        public Object getValueAt(Object oRecord, int nIndex) {
094            UCustomer usr = (UCustomer)oRecord;
095            switch (nIndex) {
096                case 0:
097                    return usr.getSurname() + ", " + usr.getFirstName();
098                case 1:
099                    return usr.getDayOfRegistration();
100                case 2:
101                    return Conversions.doubleToCurrency(Statistics.getAllowableCustomerRevenue(usr), " Euro");
102                case 3:
103                    return Conversions.fixedDecimal(100 * usr.getDiscount(), 3) + " %";
104            }
105            return null;
106        }
107    
108        /**
109         * Determines if columns can be sorted by the user.
110         *
111         * @param nIndex the affected column.
112         * @return <ul><li>true: columns can be sorted</li>
113         *              <li>false: columns cannot be sorted</li></ul>
114         */
115        public boolean canSortByColumn(int nIndex) {
116            return true;
117        }
118    
119        /**
120         * @param nIndex the affected column.
121         * @return the {@link Comparator} to be used when sorting the column.
122         */
123        public Comparator<Object> getColumnOrder(int nIndex) {
124            switch (nIndex) {
125                case 2:
126                    return salesVolume;
127                case 3:
128                    return discount;
129            }
130            return null;
131        }
132    }