1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.table;
  27 
  28 import javax.swing.*;
  29 import javax.swing.border.*;
  30 
  31 import java.awt.Component;
  32 import java.awt.Color;
  33 import java.awt.Rectangle;
  34 
  35 import java.io.Serializable;
  36 import sun.swing.DefaultLookup;
  37 
  38 
  39 /**
  40  * The standard class for rendering (displaying) individual cells
  41  * in a <code>JTable</code>.
  42  * <p>
  43  *
  44  * <strong><a name="override">Implementation Note:</a></strong>
  45  * This class inherits from <code>JLabel</code>, a standard component class.
  46  * However <code>JTable</code> employs a unique mechanism for rendering
  47  * its cells and therefore requires some slightly modified behavior
  48  * from its cell renderer.
  49  * The table class defines a single cell renderer and uses it as a
  50  * as a rubber-stamp for rendering all cells in the table;
  51  * it renders the first cell,
  52  * changes the contents of that cell renderer,
  53  * shifts the origin to the new location, re-draws it, and so on.
  54  * The standard <code>JLabel</code> component was not
  55  * designed to be used this way and we want to avoid
  56  * triggering a <code>revalidate</code> each time the
  57  * cell is drawn. This would greatly decrease performance because the
  58  * <code>revalidate</code> message would be
  59  * passed up the hierarchy of the container to determine whether any other
  60  * components would be affected.
  61  * As the renderer is only parented for the lifetime of a painting operation
  62  * we similarly want to avoid the overhead associated with walking the
  63  * hierarchy for painting operations.
  64  * So this class
  65  * overrides the <code>validate</code>, <code>invalidate</code>,
  66  * <code>revalidate</code>, <code>repaint</code>, and
  67  * <code>firePropertyChange</code> methods to be
  68  * no-ops and override the <code>isOpaque</code> method solely to improve
  69  * performance.  If you write your own renderer,
  70  * please keep this performance consideration in mind.
  71  * <p>
  72  *
  73  * <strong>Warning:</strong>
  74  * Serialized objects of this class will not be compatible with
  75  * future Swing releases. The current serialization support is
  76  * appropriate for short term storage or RMI between applications running
  77  * the same version of Swing.  As of 1.4, support for long term storage
  78  * of all JavaBeans&trade;
  79  * has been added to the <code>java.beans</code> package.
  80  * Please see {@link java.beans.XMLEncoder}.
  81  *
  82  * @author Philip Milne
  83  * @see JTable
  84  */
  85 @SuppressWarnings("serial") // Same-version serialization only
  86 public class DefaultTableCellRenderer extends JLabel
  87     implements TableCellRenderer, Serializable
  88 {
  89 
  90    /**
  91     * An empty <code>Border</code>. This field might not be used. To change the
  92     * <code>Border</code> used by this renderer override the
  93     * <code>getTableCellRendererComponent</code> method and set the border
  94     * of the returned component directly.
  95     */
  96     private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
  97     private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
  98     protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;
  99 
 100     // We need a place to store the color the JLabel should be returned
 101     // to after its foreground and background colors have been set
 102     // to the selection background color.
 103     // These ivars will be made protected when their names are finalized.
 104     private Color unselectedForeground;
 105     private Color unselectedBackground;
 106 
 107     /**
 108      * Creates a default table cell renderer.
 109      */
 110     public DefaultTableCellRenderer() {
 111         super();
 112         setOpaque(true);
 113         setBorder(getNoFocusBorder());
 114         setName("Table.cellRenderer");
 115     }
 116 
 117     private Border getNoFocusBorder() {
 118         Border border = DefaultLookup.getBorder(this, ui, "Table.cellNoFocusBorder");
 119         if (System.getSecurityManager() != null) {
 120             if (border != null) return border;
 121             return SAFE_NO_FOCUS_BORDER;
 122         } else if (border != null) {
 123             if (noFocusBorder == null || noFocusBorder == DEFAULT_NO_FOCUS_BORDER) {
 124                 return border;
 125             }
 126         }
 127         return noFocusBorder;
 128     }
 129 
 130     /**
 131      * Overrides <code>JComponent.setForeground</code> to assign
 132      * the unselected-foreground color to the specified color.
 133      *
 134      * @param c set the foreground color to this value
 135      */
 136     public void setForeground(Color c) {
 137         super.setForeground(c);
 138         unselectedForeground = c;
 139     }
 140 
 141     /**
 142      * Overrides <code>JComponent.setBackground</code> to assign
 143      * the unselected-background color to the specified color.
 144      *
 145      * @param c set the background color to this value
 146      */
 147     public void setBackground(Color c) {
 148         super.setBackground(c);
 149         unselectedBackground = c;
 150     }
 151 
 152     /**
 153      * Notification from the <code>UIManager</code> that the look and feel
 154      * [L&amp;F] has changed.
 155      * Replaces the current UI object with the latest version from the
 156      * <code>UIManager</code>.
 157      *
 158      * @see JComponent#updateUI
 159      */
 160     public void updateUI() {
 161         super.updateUI();
 162         setForeground(null);
 163         setBackground(null);
 164     }
 165 
 166     // implements javax.swing.table.TableCellRenderer
 167     /**
 168      *
 169      * Returns the default table cell renderer.
 170      * <p>
 171      * During a printing operation, this method will be called with
 172      * <code>isSelected</code> and <code>hasFocus</code> values of
 173      * <code>false</code> to prevent selection and focus from appearing
 174      * in the printed output. To do other customization based on whether
 175      * or not the table is being printed, check the return value from
 176      * {@link javax.swing.JComponent#isPaintingForPrint()}.
 177      *
 178      * @param table  the <code>JTable</code>
 179      * @param value  the value to assign to the cell at
 180      *                  <code>[row, column]</code>
 181      * @param isSelected true if cell is selected
 182      * @param hasFocus true if cell has focus
 183      * @param row  the row of the cell to render
 184      * @param column the column of the cell to render
 185      * @return the default table cell renderer
 186      * @see javax.swing.JComponent#isPaintingForPrint()
 187      */
 188     public Component getTableCellRendererComponent(JTable table, Object value,
 189                           boolean isSelected, boolean hasFocus, int row, int column) {
 190         if (table == null) {
 191             return this;
 192         }
 193 
 194         Color fg = null;
 195         Color bg = null;
 196 
 197         JTable.DropLocation dropLocation = table.getDropLocation();
 198         if (dropLocation != null
 199                 && !dropLocation.isInsertRow()
 200                 && !dropLocation.isInsertColumn()
 201                 && dropLocation.getRow() == row
 202                 && dropLocation.getColumn() == column) {
 203 
 204             fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
 205             bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");
 206 
 207             isSelected = true;
 208         }
 209 
 210         if (isSelected) {
 211             super.setForeground(fg == null ? table.getSelectionForeground()
 212                                            : fg);
 213             super.setBackground(bg == null ? table.getSelectionBackground()
 214                                            : bg);
 215         } else {
 216             Color background = unselectedBackground != null
 217                                     ? unselectedBackground
 218                                     : table.getBackground();
 219             if (background == null || background instanceof javax.swing.plaf.UIResource) {
 220                 Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
 221                 if (alternateColor != null && row % 2 != 0) {
 222                     background = alternateColor;
 223                 }
 224             }
 225             super.setForeground(unselectedForeground != null
 226                                     ? unselectedForeground
 227                                     : table.getForeground());
 228             super.setBackground(background);
 229         }
 230 
 231         setFont(table.getFont());
 232 
 233         if (hasFocus) {
 234             Border border = null;
 235             if (isSelected) {
 236                 border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
 237             }
 238             if (border == null) {
 239                 border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
 240             }
 241             setBorder(border);
 242 
 243             if (!isSelected && table.isCellEditable(row, column)) {
 244                 Color col;
 245                 col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
 246                 if (col != null) {
 247                     super.setForeground(col);
 248                 }
 249                 col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
 250                 if (col != null) {
 251                     super.setBackground(col);
 252                 }
 253             }
 254         } else {
 255             setBorder(getNoFocusBorder());
 256         }
 257 
 258         setValue(value);
 259 
 260         return this;
 261     }
 262 
 263     /*
 264      * The following methods are overridden as a performance measure to
 265      * to prune code-paths are often called in the case of renders
 266      * but which we know are unnecessary.  Great care should be taken
 267      * when writing your own renderer to weigh the benefits and
 268      * drawbacks of overriding methods like these.
 269      */
 270 
 271     /**
 272      * Overridden for performance reasons.
 273      * See the <a href="#override">Implementation Note</a>
 274      * for more information.
 275      */
 276     public boolean isOpaque() {
 277         Color back = getBackground();
 278         Component p = getParent();
 279         if (p != null) {
 280             p = p.getParent();
 281         }
 282 
 283         // p should now be the JTable.
 284         boolean colorMatch = (back != null) && (p != null) &&
 285             back.equals(p.getBackground()) &&
 286                         p.isOpaque();
 287         return !colorMatch && super.isOpaque();
 288     }
 289 
 290     /**
 291      * Overridden for performance reasons.
 292      * See the <a href="#override">Implementation Note</a>
 293      * for more information.
 294      *
 295      * @since 1.5
 296      */
 297     public void invalidate() {}
 298 
 299     /**
 300      * Overridden for performance reasons.
 301      * See the <a href="#override">Implementation Note</a>
 302      * for more information.
 303      */
 304     public void validate() {}
 305 
 306     /**
 307      * Overridden for performance reasons.
 308      * See the <a href="#override">Implementation Note</a>
 309      * for more information.
 310      */
 311     public void revalidate() {}
 312 
 313     /**
 314      * Overridden for performance reasons.
 315      * See the <a href="#override">Implementation Note</a>
 316      * for more information.
 317      */
 318     public void repaint(long tm, int x, int y, int width, int height) {}
 319 
 320     /**
 321      * Overridden for performance reasons.
 322      * See the <a href="#override">Implementation Note</a>
 323      * for more information.
 324      */
 325     public void repaint(Rectangle r) { }
 326 
 327     /**
 328      * Overridden for performance reasons.
 329      * See the <a href="#override">Implementation Note</a>
 330      * for more information.
 331      *
 332      * @since 1.5
 333      */
 334     public void repaint() {
 335     }
 336 
 337     /**
 338      * Overridden for performance reasons.
 339      * See the <a href="#override">Implementation Note</a>
 340      * for more information.
 341      */
 342     protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
 343         // Strings get interned...
 344         if (propertyName=="text"
 345                 || propertyName == "labelFor"
 346                 || propertyName == "displayedMnemonic"
 347                 || ((propertyName == "font" || propertyName == "foreground")
 348                     && oldValue != newValue
 349                     && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {
 350 
 351             super.firePropertyChange(propertyName, oldValue, newValue);
 352         }
 353     }
 354 
 355     /**
 356      * Overridden for performance reasons.
 357      * See the <a href="#override">Implementation Note</a>
 358      * for more information.
 359      */
 360     public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
 361 
 362 
 363     /**
 364      * Sets the <code>String</code> object for the cell being rendered to
 365      * <code>value</code>.
 366      *
 367      * @param value  the string value for this cell; if value is
 368      *          <code>null</code> it sets the text value to an empty string
 369      * @see JLabel#setText
 370      *
 371      */
 372     protected void setValue(Object value) {
 373         setText((value == null) ? "" : value.toString());
 374     }
 375 
 376 
 377     /**
 378      * A subclass of <code>DefaultTableCellRenderer</code> that
 379      * implements <code>UIResource</code>.
 380      * <code>DefaultTableCellRenderer</code> doesn't implement
 381      * <code>UIResource</code>
 382      * directly so that applications can safely override the
 383      * <code>cellRenderer</code> property with
 384      * <code>DefaultTableCellRenderer</code> subclasses.
 385      * <p>
 386      * <strong>Warning:</strong>
 387      * Serialized objects of this class will not be compatible with
 388      * future Swing releases. The current serialization support is
 389      * appropriate for short term storage or RMI between applications running
 390      * the same version of Swing.  As of 1.4, support for long term storage
 391      * of all JavaBeans&trade;
 392      * has been added to the <code>java.beans</code> package.
 393      * Please see {@link java.beans.XMLEncoder}.
 394      */
 395     @SuppressWarnings("serial") // Same-version serialization only
 396     public static class UIResource extends DefaultTableCellRenderer
 397         implements javax.swing.plaf.UIResource
 398     {
 399     }
 400 
 401 }