001    /*
002    Copyright (c) 1996-2012, Damon Hart-Davis
003    All rights reserved.
004    
005    Redistribution and use in source and binary forms, with or without
006    modification, are permitted provided that the following conditions are
007    met:
008    
009      * Redistributions of source code must retain the above copyright
010        notice, this list of conditions and the following disclaimer.
011    
012      * Redistributions in binary form must reproduce the above copyright
013        notice, this list of conditions and the following disclaimer in the
014        documentation and/or other materials provided with the
015        distribution.
016    
017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
018    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
019    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
020    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
021    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028    */
029    
030    package org.hd.d.pg2k.clApp.uploader;
031    
032    import java.util.concurrent.atomic.AtomicInteger;
033    
034    import javax.swing.table.AbstractTableModel;
035    
036    /**
037     * Created by IntelliJ IDEA.
038     * User: DHD
039     * Date: 26-Sep-2006
040     * Time: 11:51:09
041     */
042    
043    /**Table model used for JTable displays of files selected for upload.
044     * Most access is package-only for access by the GUI classes.
045     */
046    class UploadFileTableModel extends AbstractTableModel
047        {
048        private static final long serialVersionUID = -2592921016866914042L;
049    
050        /**Construct an instance of our table model. */
051        UploadFileTableModel(final UploaderLogic l,
052                   final SelectedFilesDB db,
053                   final AtomicInteger highlightRow)
054            {
055            if((l == null) || (db == null))
056                { throw new IllegalArgumentException(); }
057            logic = l;
058            this.db = db;
059            this.highlightRow = highlightRow;
060            }
061    
062        /**The "business logic". */
063        final UploaderLogic logic;
064    
065        /**The backing database. */
066        final SelectedFilesDB db;
067    
068        /**The holder for the row to highlight, or null. */
069        final AtomicInteger highlightRow;
070    
071        /**Return the number of columns; fixed for this model. */
072        public int getColumnCount() { return(SelectedFileDetails.getColumnCount()); }
073    
074        /**Return a column name. */
075        @Override
076        public String getColumnName(final int col) { return(SelectedFileDetails.getColumnName(col)); }
077    
078        public int getRowCount() { return(db.size()); }
079    
080        /**Get specified value; null if none.
081         * Uses a lock on the selectedFiles object
082         * to ensure that no race can cause an exception.
083         */
084        public Object getValueAt(final int row, final int col)
085            {
086            // Is this the the row to highlight?
087    //            final boolean isHighlightRow = (row == uploadTableEditRow.get());
088            final boolean isHighlightRow =
089                (highlightRow != null) && (row == highlightRow.get());
090    
091            // Hold lock just long enough for race-free access to row.
092            final Object valueAt;
093            synchronized(db)
094                {
095                if(row >= db.size()) { return(null); }
096                valueAt = db.get(row).getValueAt(logic, col);
097                }
098    
099            // Format String data for display...
100            if(valueAt instanceof String)
101                {
102                if(isHighlightRow &&
103                   ((col == SelectedFileDetails.COLNUM_EXHIBIT_NAME) ||
104                    (col == SelectedFileDetails.COLNUM_DESCRIPTION)))
105                    {
106                    // Highlight (simple String) items in the edit row...
107                    return("<html><strong>" + valueAt + "</strong></html>");
108                    }
109                else if(col == SelectedFileDetails.COLNUM_DESCRIPTION)
110                    {
111                    // Always show description as HTML...
112                    return("<html>" + valueAt + "</html>");
113                    }
114                else if(col == SelectedFileDetails.COLNUM_STATUS)
115                    {
116                    // Show any error in red...
117                    return("<html><font color=red>" + valueAt + "</font></html>");
118                    }
119                }
120            return(valueAt); // Return value as-is...
121            }
122        }