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.incident;
016    
017    import javax.swing.table.AbstractTableModel;
018    
019    import org.tastybug.bugwerk.blueprint.model.Attachment;
020    import org.tastybug.bugwerk.blueprint.model.Incident;
021    
022    
023    /**
024     * This is the table model of <code>IncidentTable</code>.
025     * <br>
026     * 
027     * <hr>
028     * Copyright 2006 Philipp Bartsch.<br>
029     * <a href="http://www.tastybug.com">www.tastybug.com</a><br>
030     * <hr>
031     * Created on Feb 8, 2006<br>
032     * @author Philipp Bartsch, philipp.bartsch{at}tastybug{dot}com</a>
033     */
034    public class IncidentTableModel extends AbstractTableModel {
035          
036        /**The <code>serialVersionUID</code>*/
037        private static final long serialVersionUID = -5309523611100834818L;
038        
039        private Incident  incident;
040        private String[]  keys;
041        private Attachment[] attachments;
042        
043        /**
044         * Initialises the model, using the content of <code>_incident</code>.
045         *
046         * @param _incident the incident
047         */
048        protected IncidentTableModel (final Incident _incident) {
049            update(_incident);
050        }
051    
052        /**
053         * Re-reads the incident, as it has changed.
054         * 
055         * @param changedIncident the changed incident
056         */
057        public void update (final Incident changedIncident) {
058            incident = changedIncident;
059            if (incident == null) {
060                keys = new String[0];
061                attachments = new Attachment[0];
062            } else {
063                keys = (String[]) incident.getIncidentAsMap().keySet().toArray(new String[0]);
064                attachments = incident.getAttachments();
065            }
066        }
067        
068        /**
069         * Returns an empty string, no column names available yet.
070         *
071         * @see javax.swing.table.AbstractTableModel#getColumnName(int)
072         */
073        public String getColumnName(int col) {
074            return "";
075        }
076        
077        /**
078         * Returns 2, as 2 columns are to be displayed (property key/value
079         * and respectively attachment/path).
080         *
081         * @see javax.swing.table.TableModel#getColumnCount()
082         */
083        public int getColumnCount() {
084            return 2;
085        }
086        
087        /**
088         * Returns <code>false</code>.
089         * 
090         * @return <code>false</code>
091         */
092        public boolean isEditable () {
093            return false;
094        }
095        
096        /**
097         * Returns the number of properties + number of attachments.
098         *
099         * @see javax.swing.table.TableModel#getRowCount()
100         */
101        public int getRowCount() {
102            return keys.length + incident.getAttachmentCount();
103        }
104        
105        /**
106         * Returns a cell value.
107         *
108         * @see javax.swing.table.TableModel#getValueAt(int, int)
109         */
110        public Object getValueAt(int row, int col) {
111            if (col == 0) {
112                if (row >= keys.length)
113                    return "Attachment";
114                else
115                    return keys[row];
116            } else  if (keys.length == 0 || row >= getRowCount()) {
117                return "";
118            } else {
119                if (row >= keys.length)
120                    return attachments[row - keys.length].getPath(); 
121                else
122                    return incident.getAsMap().get(keys[row]);
123            }
124        }
125    
126        /**
127         * Returns always <code>String.class</code>.
128         *
129         * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
130         */
131        public Class getColumnClass(int c) {
132            return String.class;
133        }
134    }