001    /*
002     * $Id: TableCellContext.java 3424 2009-07-30 10:53:39Z kleopatra $
003     *
004     * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     *
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    package org.jdesktop.swingx.renderer;
022    
023    import java.awt.Color;
024    
025    import javax.swing.JTable;
026    
027    /**
028     * Table specific <code>CellContext</code>.
029     */
030    public class TableCellContext extends CellContext {
031    
032        /**
033         * Sets state of the cell's context. Note that the component might be null
034         * to indicate a cell without a concrete context. All accessors must cope
035         * with.
036         * 
037         * @param component the component the cell resides on, might be null
038         * @param value the content value of the cell
039         * @param row the cell's row index in view coordinates
040         * @param column the cell's column index in view coordinates
041         * @param selected the cell's selected state
042         * @param focused the cell's focused state
043         * @param expanded the cell's expanded state
044         * @param leaf the cell's leaf state
045         */
046        public void installContext(JTable component, Object value, int row, int column,
047                boolean selected, boolean focused, boolean expanded, boolean leaf) {
048            this.component = component;
049            installState(value, row, column, selected, focused, expanded, leaf);
050            this.dropOn = checkDropOnState();
051        }
052        
053        
054        /**
055         * 
056         */
057        private boolean checkDropOnState() {
058            if ((getComponent() == null) || !isValidRow() || !isValidColumn()) {
059                return false;
060            }
061            JTable.DropLocation dropLocation = getComponent().getDropLocation();
062            if (dropLocation != null
063                    && !dropLocation.isInsertRow()
064                    && !dropLocation.isInsertColumn()
065                    && dropLocation.getRow() == row
066                    && dropLocation.getColumn() == column) {
067                return true;
068            }
069            return false;
070        }
071    
072    
073        @Override
074        public JTable getComponent() {
075            return (JTable) super.getComponent();
076        }
077    
078        /**
079         * Returns the cell's editable property as returned by table.isCellEditable
080         * or false if the table is null.
081         * 
082         * @return the cell's editable property. 
083         */
084        @Override
085        public boolean isEditable() {
086            if ((getComponent() == null) || !isValidRow() || !isValidColumn()) {
087                return false;
088            }
089            return getComponent().isCellEditable(getRow(), getColumn());
090        }
091    
092        
093    
094        /**
095         * {@inheritDoc}
096         */
097        @Override
098        protected Color getSelectionBackground() {
099            Color selection = null;
100            if (isDropOn()) {
101                selection = getDropCellBackground();
102                if (selection != null) return selection;
103            }
104            return getComponent() != null ? getComponent()
105                    .getSelectionBackground() : null;
106        }
107    
108        /**
109         * {@inheritDoc}
110         */
111        @Override
112        protected Color getSelectionForeground() {
113            Color selection = null;
114            if (isDropOn()) {
115                selection = getDropCellForeground();
116                if (selection != null) return selection;
117            }
118            return getComponent() != null ? getComponent()
119                    .getSelectionForeground() : null;
120        }
121    
122        /**
123         * {@inheritDoc}
124         */
125        @Override
126        protected String getUIPrefix() {
127            return "Table.";
128        }
129    
130        /**
131         * PRE getComponent != null
132         * 
133         * @return whether the column coordinate is valid in this context
134         */
135        protected boolean isValidColumn() {
136            return getColumn() >= 0 && getColumn() < getComponent().getColumnCount() ;
137        }
138    
139        /**
140         * PRE getComponent != null
141         * 
142         * @return whether the row coordinate is valid in this context
143         */
144        protected boolean isValidRow() {
145            return getRow() >= 0 && getRow() < getComponent().getRowCount() ;
146        }
147    
148    
149    }