View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 11 package com.sun.xml.registry.client.browser; 12 13 import javax.swing.*; 14 import javax.swing.table.TableCellRenderer; 15 import javax.swing.border.*; 16 17 import java.awt.Component; 18 import java.awt.Color; 19 import java.awt.Rectangle; 20 21 import java.io.Serializable; 22 23 24 /*** 25 * Based upon code from JDK class java.swing.table.DefaultTableCellRenderer. 26 * 27 * The standard class for rendering (displaying) individual cells 28 * in a <code>JTable</code>. 29 * <p> 30 * 31 * <strong><a name="override">Implementation Note:</a></strong> 32 * This class inherits from <code>JLabel</code>, a standard component class. 33 * However <code>JTable</code> employs a unique mechanism for rendering 34 * its cells and therefore requires some slightly modified behavior 35 * from its cell renderer. 36 * The table class defines a single cell renderer and uses it as a 37 * as a rubber-stamp for rendering all cells in the table; 38 * it renders the first cell, 39 * changes the contents of that cell renderer, 40 * shifts the origin to the new location, re-draws it, and so on. 41 * The standard <code>JLabel</code> component was not 42 * designed to be used this way and we want to avoid 43 * triggering a <code>revalidate</code> each time the 44 * cell is drawn. This would greatly decrease performance because the 45 * <code>revalidate</code> message would be 46 * passed up the hierarchy of the container to determine whether any other 47 * components would be affected. So this class 48 * overrides the <code>validate</code>, <code>revalidate</code>, 49 * <code>repaint</code>, and <code>firePropertyChange</code> methods to be 50 * no-ops. If you write your own renderer, 51 * please keep this performance consideration in mind. 52 * <p> 53 * 54 * <strong>Warning:</strong> 55 * Serialized objects of this class will not be compatible with 56 * future Swing releases. The current serialization support is 57 * appropriate for short term storage or RMI between applications running 58 * the same version of Swing. As of 1.4, support for long term storage 59 * of all JavaBeans<sup><font size="-2">TM</font></sup> 60 * has been added to the <code>java.beans</code> package. 61 * Please see {@link java.beans.XMLEncoder}. 62 * 63 * @version 1.31 12/03/01 64 * @author Philip Milne 65 * @see JTable 66 */ 67 public class JBDefaultTableCellRenderer extends HyperLinkLabel 68 implements TableCellRenderer, Serializable 69 { 70 71 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 72 73 // We need a place to store the color the JLabel should be returned 74 // to after its foreground and background colors have been set 75 // to the selection background color. 76 // These ivars will be made protected when their names are finalized. 77 private Color unselectedForeground; 78 private Color unselectedBackground; 79 80 /*** 81 * Creates a default table cell renderer. 82 */ 83 public JBDefaultTableCellRenderer() { 84 super(); 85 setOpaque(true); 86 setBorder(noFocusBorder); 87 setHorizontalAlignment(SwingConstants.LEFT); 88 } 89 90 /*** 91 * Overrides <code>JComponent.setForeground</code> to assign 92 * the unselected-foreground color to the specified color. 93 * 94 * @param c set the foreground color to this value 95 */ 96 public void setForeground(Color c) { 97 super.setForeground(c); 98 unselectedForeground = c; 99 } 100 101 /*** 102 * Overrides <code>JComponent.setBackground</code> to assign 103 * the unselected-background color to the specified color. 104 * 105 * @param c set the background color to this value 106 */ 107 public void setBackground(Color c) { 108 super.setBackground(c); 109 unselectedBackground = c; 110 } 111 112 /*** 113 * Notification from the <code>UIManager</code> that the look and feel 114 * [L&F] has changed. 115 * Replaces the current UI object with the latest version from the 116 * <code>UIManager</code>. 117 * 118 * @see JComponent#updateUI 119 */ 120 public void updateUI() { 121 super.updateUI(); 122 setForeground(null); 123 setBackground(null); 124 } 125 126 // implements javax.swing.table.TableCellRenderer 127 /*** 128 * 129 * Returns the default table cell renderer. 130 * 131 * @param table the <code>JTable</code> 132 * @param value the value to assign to the cell at 133 * <code>[row, column]</code> 134 * @param isSelected true if cell is selected 135 * @param isFocus true if cell has focus 136 * @param row the row of the cell to render 137 * @param column the column of the cell to render 138 * @return the default table cell renderer 139 */ 140 public Component getTableCellRendererComponent(JTable table, Object value, 141 boolean isSelected, boolean hasFocus, int row, int column) { 142 143 if (isSelected) { 144 super.setForeground(table.getSelectionForeground()); 145 super.setBackground(table.getSelectionBackground()); 146 } 147 else { 148 super.setForeground((unselectedForeground != null) ? unselectedForeground 149 : table.getForeground()); 150 super.setBackground((unselectedBackground != null) ? unselectedBackground 151 : table.getBackground()); 152 } 153 154 setFont(table.getFont()); 155 156 if (hasFocus) { 157 setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") ); 158 if (table.isCellEditable(row, column)) { 159 super.setForeground( UIManager.getColor("Table.focusCellForeground") ); 160 super.setBackground( UIManager.getColor("Table.focusCellBackground") ); 161 } 162 } else { 163 setBorder(noFocusBorder); 164 } 165 166 value = RegistryObjectsTable.convertValue(value); 167 168 setValue(value); 169 170 return this; 171 } 172 173 /* 174 * The following methods are overridden as a performance measure to 175 * to prune code-paths are often called in the case of renders 176 * but which we know are unnecessary. Great care should be taken 177 * when writing your own renderer to weigh the benefits and 178 * drawbacks of overriding methods like these. 179 */ 180 181 /*** 182 * Overridden for performance reasons. 183 * See the <a href="#override">Implementation Note</a> 184 * for more information. 185 */ 186 public boolean isOpaque() { 187 Color back = getBackground(); 188 Component p = getParent(); 189 if (p != null) { 190 p = p.getParent(); 191 } 192 // p should now be the JTable. 193 boolean colorMatch = (back != null) && (p != null) && 194 back.equals(p.getBackground()) && 195 p.isOpaque(); 196 return !colorMatch && super.isOpaque(); 197 } 198 199 /*** 200 * Overridden for performance reasons. 201 * See the <a href="#override">Implementation Note</a> 202 * for more information. 203 */ 204 public void validate() {} 205 206 /*** 207 * Overridden for performance reasons. 208 * See the <a href="#override">Implementation Note</a> 209 * for more information. 210 */ 211 public void revalidate() {} 212 213 /*** 214 * Overridden for performance reasons. 215 * See the <a href="#override">Implementation Note</a> 216 * for more information. 217 */ 218 public void repaint(long tm, int x, int y, int width, int height) {} 219 220 /*** 221 * Overridden for performance reasons. 222 * See the <a href="#override">Implementation Note</a> 223 * for more information. 224 */ 225 public void repaint(Rectangle r) { } 226 227 /*** 228 * Overridden for performance reasons. 229 * See the <a href="#override">Implementation Note</a> 230 * for more information. 231 */ 232 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 233 // Strings get interned... 234 if (propertyName=="text") { 235 super.firePropertyChange(propertyName, oldValue, newValue); 236 } 237 } 238 239 /*** 240 * Overridden for performance reasons. 241 * See the <a href="#override">Implementation Note</a> 242 * for more information. 243 */ 244 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } 245 246 247 /*** 248 * Sets the <code>String</code> object for the cell being rendered to 249 * <code>value</code>. 250 * 251 * @param value the string value for this cell; if value is 252 * <code>null</code> it sets the text value to an empty string 253 * @see JLabel#setText 254 * 255 */ 256 protected void setValue(Object value) { 257 try { 258 setURL(null); 259 } 260 catch (java.net.MalformedURLException e) { 261 //Do nothing as this will never be thrown here. 262 } 263 264 setText((value == null) ? "" : value.toString()); 265 } 266 267 268 /*** 269 * A subclass of <code>JBDefaultTableCellRenderer</code> that 270 * implements <code>UIResource</code>. 271 * <code>JBDefaultTableCellRenderer</code> doesn't implement 272 * <code>UIResource</code> 273 * directly so that applications can safely override the 274 * <code>cellRenderer</code> property with 275 * <code>JBDefaultTableCellRenderer</code> subclasses. 276 * <p> 277 * <strong>Warning:</strong> 278 * Serialized objects of this class will not be compatible with 279 * future Swing releases. The current serialization support is 280 * appropriate for short term storage or RMI between applications running 281 * the same version of Swing. As of 1.4, support for long term storage 282 * of all JavaBeans<sup><font size="-2">TM</font></sup> 283 * has been added to the <code>java.beans</code> package. 284 * Please see {@link java.beans.XMLEncoder}. 285 */ 286 public static class UIResource extends JBDefaultTableCellRenderer 287 implements javax.swing.plaf.UIResource 288 { 289 } 290 291 } 292 293

This page was automatically generated by Maven