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™ 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 /** 99 * A border without focus. 100 */ 101 protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER; 102 103 // We need a place to store the color the JLabel should be returned 104 // to after its foreground and background colors have been set 105 // to the selection background color. 106 // These ivars will be made protected when their names are finalized. 107 private Color unselectedForeground; 108 private Color unselectedBackground; 109 110 /** 111 * Creates a default table cell renderer. 112 */ 113 public DefaultTableCellRenderer() { 114 super(); 115 setOpaque(true); 116 setBorder(getNoFocusBorder()); 117 setName("Table.cellRenderer"); 118 } 119 120 private Border getNoFocusBorder() { 121 Border border = DefaultLookup.getBorder(this, ui, "Table.cellNoFocusBorder"); 122 if (System.getSecurityManager() != null) { 123 if (border != null) return border; 124 return SAFE_NO_FOCUS_BORDER; 125 } else if (border != null) { 126 if (noFocusBorder == null || noFocusBorder == DEFAULT_NO_FOCUS_BORDER) { 127 return border; 128 } 129 } 130 return noFocusBorder; 131 } 132 133 /** 134 * Overrides <code>JComponent.setForeground</code> to assign 135 * the unselected-foreground color to the specified color. 136 * 137 * @param c set the foreground color to this value 138 */ 139 public void setForeground(Color c) { 140 super.setForeground(c); 141 unselectedForeground = c; 142 } 143 144 /** 145 * Overrides <code>JComponent.setBackground</code> to assign 146 * the unselected-background color to the specified color. 147 * 148 * @param c set the background color to this value 149 */ 150 public void setBackground(Color c) { 151 super.setBackground(c); 152 unselectedBackground = c; 153 } 154 155 /** 156 * Notification from the <code>UIManager</code> that the look and feel 157 * [L&F] has changed. 158 * Replaces the current UI object with the latest version from the 159 * <code>UIManager</code>. 160 * 161 * @see JComponent#updateUI 162 */ 163 public void updateUI() { 164 super.updateUI(); 165 setForeground(null); 166 setBackground(null); 167 } 168 169 // implements javax.swing.table.TableCellRenderer 170 /** 171 * 172 * Returns the default table cell renderer. 173 * <p> 174 * During a printing operation, this method will be called with 175 * <code>isSelected</code> and <code>hasFocus</code> values of 176 * <code>false</code> to prevent selection and focus from appearing 177 * in the printed output. To do other customization based on whether 178 * or not the table is being printed, check the return value from 179 * {@link javax.swing.JComponent#isPaintingForPrint()}. 180 * 181 * @param table the <code>JTable</code> 182 * @param value the value to assign to the cell at 183 * <code>[row, column]</code> 184 * @param isSelected true if cell is selected 185 * @param hasFocus true if cell has focus 186 * @param row the row of the cell to render 187 * @param column the column of the cell to render 188 * @return the default table cell renderer 189 * @see javax.swing.JComponent#isPaintingForPrint() 190 */ 191 public Component getTableCellRendererComponent(JTable table, Object value, 192 boolean isSelected, boolean hasFocus, int row, int column) { 193 if (table == null) { 194 return this; 195 } 196 197 Color fg = null; 198 Color bg = null; 199 200 JTable.DropLocation dropLocation = table.getDropLocation(); 201 if (dropLocation != null 202 && !dropLocation.isInsertRow() 203 && !dropLocation.isInsertColumn() 347 if (propertyName=="text" 348 || propertyName == "labelFor" 349 || propertyName == "displayedMnemonic" 350 || ((propertyName == "font" || propertyName == "foreground") 351 && oldValue != newValue 352 && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) { 353 354 super.firePropertyChange(propertyName, oldValue, newValue); 355 } 356 } 357 358 /** 359 * Overridden for performance reasons. 360 * See the <a href="#override">Implementation Note</a> 361 * for more information. 362 */ 363 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } 364 365 366 /** 367 * Sets the <code>String</code> object for the cell being rendered to 368 * <code>value</code>. 369 * 370 * @param value the string value for this cell; if value is 371 * <code>null</code> it sets the text value to an empty string 372 * @see JLabel#setText 373 * 374 */ 375 protected void setValue(Object value) { 376 setText((value == null) ? "" : value.toString()); 377 } 378 379 380 /** 381 * A subclass of <code>DefaultTableCellRenderer</code> that 382 * implements <code>UIResource</code>. 383 * <code>DefaultTableCellRenderer</code> doesn't implement 384 * <code>UIResource</code> 385 * directly so that applications can safely override the 386 * <code>cellRenderer</code> property with 387 * <code>DefaultTableCellRenderer</code> subclasses. 388 * <p> 389 * <strong>Warning:</strong> 390 * Serialized objects of this class will not be compatible with 391 * future Swing releases. The current serialization support is 392 * appropriate for short term storage or RMI between applications running 393 * the same version of Swing. As of 1.4, support for long term storage 394 * of all JavaBeans™ 395 * has been added to the <code>java.beans</code> package. 396 * Please see {@link java.beans.XMLEncoder}. 397 */ 398 @SuppressWarnings("serial") // Same-version serialization only 399 public static class UIResource extends DefaultTableCellRenderer 400 implements javax.swing.plaf.UIResource 401 { 402 } 403 404 } | 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}. 42 * <p> 43 * 44 * <strong><a name="override">Implementation Note:</a></strong> 45 * This class inherits from {@code JLabel}, a standard component class. 46 * However {@code JTable} 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} component was not 55 * designed to be used this way and we want to avoid 56 * triggering a {@code revalidate} each time the 57 * cell is drawn. This would greatly decrease performance because the 58 * {@code revalidate} 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 invalidate}, 66 * {@code revalidate}, {@code repaint}, and 67 * {@code firePropertyChange} methods to be 68 * no-ops and override the {@code isOpaque} 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™ 79 * has been added to the {@code java.beans} 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}. This field might not be used. To change the 92 * {@code Border} used by this renderer override the 93 * {@code getTableCellRendererComponent} 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 /** 99 * A border without focus. 100 */ 101 protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER; 102 103 // We need a place to store the color the JLabel should be returned 104 // to after its foreground and background colors have been set 105 // to the selection background color. 106 // These ivars will be made protected when their names are finalized. 107 private Color unselectedForeground; 108 private Color unselectedBackground; 109 110 /** 111 * Creates a default table cell renderer. 112 */ 113 public DefaultTableCellRenderer() { 114 super(); 115 setOpaque(true); 116 setBorder(getNoFocusBorder()); 117 setName("Table.cellRenderer"); 118 } 119 120 private Border getNoFocusBorder() { 121 Border border = DefaultLookup.getBorder(this, ui, "Table.cellNoFocusBorder"); 122 if (System.getSecurityManager() != null) { 123 if (border != null) return border; 124 return SAFE_NO_FOCUS_BORDER; 125 } else if (border != null) { 126 if (noFocusBorder == null || noFocusBorder == DEFAULT_NO_FOCUS_BORDER) { 127 return border; 128 } 129 } 130 return noFocusBorder; 131 } 132 133 /** 134 * Overrides {@code JComponent.setForeground} to assign 135 * the unselected-foreground color to the specified color. 136 * 137 * @param c set the foreground color to this value 138 */ 139 public void setForeground(Color c) { 140 super.setForeground(c); 141 unselectedForeground = c; 142 } 143 144 /** 145 * Overrides {@code JComponent.setBackground} to assign 146 * the unselected-background color to the specified color. 147 * 148 * @param c set the background color to this value 149 */ 150 public void setBackground(Color c) { 151 super.setBackground(c); 152 unselectedBackground = c; 153 } 154 155 /** 156 * Notification from the {@code UIManager} that the look and feel 157 * [L&F] has changed. 158 * Replaces the current UI object with the latest version from the 159 * {@code UIManager}. 160 * 161 * @see JComponent#updateUI 162 */ 163 public void updateUI() { 164 super.updateUI(); 165 setForeground(null); 166 setBackground(null); 167 } 168 169 // implements javax.swing.table.TableCellRenderer 170 /** 171 * 172 * Returns the default table cell renderer. 173 * <p> 174 * During a printing operation, this method will be called with 175 * {@code isSelected} and {@code hasFocus} values of 176 * {@code false} to prevent selection and focus from appearing 177 * in the printed output. To do other customization based on whether 178 * or not the table is being printed, check the return value from 179 * {@link javax.swing.JComponent#isPaintingForPrint()}. 180 * 181 * @param table the {@code JTable} 182 * @param value the value to assign to the cell at 183 * {@code [row, column]} 184 * @param isSelected true if cell is selected 185 * @param hasFocus true if cell has focus 186 * @param row the row of the cell to render 187 * @param column the column of the cell to render 188 * @return the default table cell renderer 189 * @see javax.swing.JComponent#isPaintingForPrint() 190 */ 191 public Component getTableCellRendererComponent(JTable table, Object value, 192 boolean isSelected, boolean hasFocus, int row, int column) { 193 if (table == null) { 194 return this; 195 } 196 197 Color fg = null; 198 Color bg = null; 199 200 JTable.DropLocation dropLocation = table.getDropLocation(); 201 if (dropLocation != null 202 && !dropLocation.isInsertRow() 203 && !dropLocation.isInsertColumn() 347 if (propertyName=="text" 348 || propertyName == "labelFor" 349 || propertyName == "displayedMnemonic" 350 || ((propertyName == "font" || propertyName == "foreground") 351 && oldValue != newValue 352 && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) { 353 354 super.firePropertyChange(propertyName, oldValue, newValue); 355 } 356 } 357 358 /** 359 * Overridden for performance reasons. 360 * See the <a href="#override">Implementation Note</a> 361 * for more information. 362 */ 363 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } 364 365 366 /** 367 * Sets the {@code String} object for the cell being rendered to 368 * {@code value}. 369 * 370 * @param value the string value for this cell; if value is 371 * {@code null} it sets the text value to an empty string 372 * @see JLabel#setText 373 * 374 */ 375 protected void setValue(Object value) { 376 setText((value == null) ? "" : value.toString()); 377 } 378 379 380 /** 381 * A subclass of {@code DefaultTableCellRenderer} that 382 * implements {@code UIResource}. 383 * {@code DefaultTableCellRenderer} doesn't implement 384 * {@code UIResource} 385 * directly so that applications can safely override the 386 * {@code cellRenderer} property with 387 * {@code DefaultTableCellRenderer} subclasses. 388 * <p> 389 * <strong>Warning:</strong> 390 * Serialized objects of this class will not be compatible with 391 * future Swing releases. The current serialization support is 392 * appropriate for short term storage or RMI between applications running 393 * the same version of Swing. As of 1.4, support for long term storage 394 * of all JavaBeans™ 395 * has been added to the {@code java.beans} package. 396 * Please see {@link java.beans.XMLEncoder}. 397 */ 398 @SuppressWarnings("serial") // Same-version serialization only 399 public static class UIResource extends DefaultTableCellRenderer 400 implements javax.swing.plaf.UIResource 401 { 402 } 403 404 } |