001    package market.swing;
002    
003    import java.util.List;
004    
005    import javax.swing.table.AbstractTableModel;
006    
007    import market.Conversions;
008    import market.statistics.HistoryEntry;
009    
010    /**
011     * TableModel for tables that display {@link HistoryEntry HistoryEntries}.
012     */
013    public class HistoryEntryModel extends AbstractTableModel {
014    
015        /**
016             * ID for serialization.
017             */
018            private static final long serialVersionUID = -9112475436033459400L;
019            
020            public final static int PRICE_HISTORY = 0;
021        public final static int ORDER_HISTORY = 1;
022    
023        private List data;
024        private int type;
025    
026        /**
027         * @param data the history to be displayed
028         * @param type the kind of history:
029         *      <ul><li>0: interpreted as {@link market.statistics.CISalesStats#priceHistory}</li>
030         *          <li>1: interpreted as {@link market.statistics.CISalesStats#orderHistory}</li></ul>
031         */
032        public HistoryEntryModel(List data, int type) {
033            super();
034            this.data = data;
035            this.type = type;
036        }
037    
038        /**
039         * @param i the affected column.
040         * @return columns' names.
041         */
042        public String getColumnName(int i) {
043            return new String[] {"Datum", type == 0 ? "Preis" : "Bestellt"}[i];
044        }
045    
046        /**
047         * @return the number of the table's rows.
048         */
049        public int getRowCount() {
050            return data.size();
051        }
052    
053        /**
054         * @return the number of the table's columns.
055         */
056        public int getColumnCount() {
057            return 2;
058        }
059    
060        /**
061         * @param column the affected column.
062         * @return columns' classes. They indicate how column's values should be aligned.
063         */
064        public Class<?> getColumnClass(int column) {
065            return String.class;
066        }
067    
068        /**
069         * @param row the affected row.
070         * @param column the affected column.
071         * @return the table cell's value.
072         */
073        public Object getValueAt(int row, int column) {
074            HistoryEntry oRecord = (HistoryEntry)data.get(row);
075            switch (column) {
076                case 0:
077                    return oRecord.getDate();
078                case 1:
079                    return type == 0 ? Conversions.doubleToCurrency(oRecord.getValue()) :
080                            Integer.toString(oRecord.getValue());
081                default:
082                    return "";
083            }
084        }
085    }