001    /*
002     * This program is free software; you can redistribute it and/or modify
003     * it under the terms of the GNU General Public License as published by
004     * the Free Software Foundation; version 2 of the License.
005     *
006     * This program is distributed in the hope that it will be useful,
007     * but WITHOUT ANY WARRANTY; without even the implied warranty of
008     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
009     * GNU General Public License for more details.
010     *
011     * You should have received a copy of the GNU General Public License along
012     * with this program; if not, write to the Free Software Foundation, Inc.,
013     * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
014     */
015    package org.tastybug.bugwerk.bugtrail.widget.queue;
016    
017    import javax.swing.table.AbstractTableModel;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.tastybug.bugwerk.blueprint.model.Ticket;
022    import org.tastybug.bugwerk.blueprint.model.TicketQueue;
023    
024    
025    
026    /**
027     * This is the table model of <code>TicketQueueTable</code>.
028     * <br>
029     * It displays 4 columns: the ticket Id, ticket type, incident count and ticket state.
030     * It is based on a given ticket queue and is not editable.
031     * <br><br>
032     * 
033     * <hr>
034     * Copyright 2006 Philipp Bartsch.<br>
035     * <a href="http://www.tastybug.com">www.tastybug.com</a><br>
036     * <hr>
037     * Created on Feb 8, 2006<br>
038     * @author Philipp Bartsch, philipp.bartsch{at}tastybug{dot}com</a>
039     */
040    public class TicketQueueTableModel extends AbstractTableModel {
041        
042    
043        /**The <code>serialVersionUID</code>*/
044        private static final long serialVersionUID = -8938086540732426880L;
045    
046        /**The commons logger.*/
047        private static final Log logger = LogFactory.getLog(TicketQueueTableModel.class);  
048    
049        protected static    final   String[]        columns = new String[] {"Ticket ID", "Type", "Incidents", "State"};
050        private             final   TicketQueue     ticketQueue;
051        private                     Ticket[]        tickets;
052        
053        /**
054         * Creates the model.
055         *
056         * @param _ticketQueue the connected queue
057         */
058        protected TicketQueueTableModel (final TicketQueue _ticketQueue) {
059            ticketQueue = _ticketQueue;
060            if (_ticketQueue!= null) {
061                tickets = ticketQueue.getTickets();
062            } else {
063                tickets = new Ticket[0];
064            }
065        }
066        
067        /**
068         * Returns the column name at position <code>col</code>.
069         *
070         * @see javax.swing.table.AbstractTableModel#getColumnName(int)
071         */
072        public String getColumnName(int col) {
073            return columns[col];
074        }
075        
076        /**
077         * (Re-)reads all tickets from the queue.
078         */
079        public void update () {
080            tickets = ticketQueue != null ? ticketQueue.getTickets() : new Ticket[0];
081        }
082    
083        /**
084         * Returns 4.
085         *
086         * @see javax.swing.table.TableModel#getColumnCount()
087         */
088        public int getColumnCount() {
089            return 4;
090        }
091        
092        /**
093         * Returns <code>false</code>.
094         * 
095         * @return <code>false</code>
096         */
097        public boolean isEditable () {
098            return false;
099        }
100        
101        /**
102         * Returns the number of rows, which is equal to the number of tickets in the connected queue.
103         *
104         * @see javax.swing.table.TableModel#getRowCount()
105         */
106        public int getRowCount() {
107            return tickets != null ? tickets.length : 0;
108        }
109    
110        /**
111         * Returns the ticket at row <code>rowNumber</code>.
112         * 
113         * @param   rowNumber   the rowNumber
114         * @return              the ticket or <code>null</code>, if rowNumber exceeds the number of tickets
115         */
116        public Ticket getTicketAt (int rowNumber) {
117            return tickets != null ? (rowNumber <= tickets.length ? tickets[rowNumber] : null) : null;
118        }
119        
120        /**
121         * Returns a cell value.
122         *
123         * @see javax.swing.table.TableModel#getValueAt(int, int)
124         */
125        public Object getValueAt(int row, int col) {
126            
127            if (tickets == null) {
128                return "";
129            } else if (col == 0) {
130                return tickets[row].getSignature();
131            } else if (col == 1) {
132                return tickets[row].getType();
133            } else if (col == 2) {
134                return tickets[row].getIncidentCount() + "";
135            } else if (col == 3) {
136                return tickets[row].getState();
137            } else {
138                logger.error("Requested column '" + col + "' exceeds range!");
139                return null;
140            }
141        }
142    
143        /**
144         * Returns <code>String.class</code>.
145         *
146         * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
147         */
148        public Class getColumnClass(int c) {
149            //return getValueAt(0, c).getClass();
150            return String.class;
151        }
152    }