1 /* 2 * Copyright (c) 1998, 2017, 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; 27 28 import javax.swing.*; 29 import javax.swing.event.*; 30 import javax.swing.border.*; 31 32 import java.awt.Component; 33 import java.awt.Color; 34 import java.awt.Rectangle; 35 36 import java.io.Serializable; 37 import sun.swing.DefaultLookup; 38 39 40 /** 41 * Renders an item in a list. 42 * <p> 43 * <strong><a id="override">Implementation Note:</a></strong> 44 * This class overrides 45 * <code>invalidate</code>, 46 * <code>validate</code>, 47 * <code>revalidate</code>, 48 * <code>repaint</code>, 49 * <code>isOpaque</code>, 50 * and 51 * <code>firePropertyChange</code> 52 * solely to improve performance. 53 * If not overridden, these frequently called methods would execute code paths 54 * that are unnecessary for the default list cell renderer. 55 * If you write your own renderer, 56 * take care to weigh the benefits and 57 * drawbacks of overriding these methods. 58 * 59 * <p> 60 * 61 * <strong>Warning:</strong> 62 * Serialized objects of this class will not be compatible with 63 * future Swing releases. The current serialization support is 64 * appropriate for short term storage or RMI between applications running 65 * the same version of Swing. As of 1.4, support for long term storage 66 * of all JavaBeans™ 67 * has been added to the <code>java.beans</code> package. 68 * Please see {@link java.beans.XMLEncoder}. 69 * 70 * @author Philip Milne 71 * @author Hans Muller 72 * @since 1.2 73 */ 74 @SuppressWarnings("serial") // Same-version serialization only 75 public class DefaultListCellRenderer extends JLabel 76 implements ListCellRenderer<Object>, Serializable 77 { 78 79 /** 80 * An empty <code>Border</code>. This field might not be used. To change the 81 * <code>Border</code> used by this renderer override the 82 * <code>getListCellRendererComponent</code> method and set the border 83 * of the returned component directly. 84 */ 85 private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1); 86 private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1); 87 /** 88 * No focus border 89 */ 90 protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER; 91 92 /** 93 * Constructs a default renderer object for an item 94 * in a list. 95 */ 96 public DefaultListCellRenderer() { 97 super(); 98 setOpaque(true); 99 setBorder(getNoFocusBorder()); 100 setName("List.cellRenderer"); 101 } 102 103 private Border getNoFocusBorder() { 104 Border border = DefaultLookup.getBorder(this, ui, "List.cellNoFocusBorder"); 105 if (System.getSecurityManager() != null) { 106 if (border != null) return border; 107 return SAFE_NO_FOCUS_BORDER; 108 } else { 109 if (border != null && 110 (noFocusBorder == null || 111 noFocusBorder == DEFAULT_NO_FOCUS_BORDER)) { 112 return border; 113 } 114 return noFocusBorder; 115 } 116 } 117 118 public Component getListCellRendererComponent( 119 JList<?> list, 120 Object value, 121 int index, 122 boolean isSelected, 123 boolean cellHasFocus) 124 { 125 setComponentOrientation(list.getComponentOrientation()); 126 127 Color bg = null; 128 Color fg = null; 129 130 JList.DropLocation dropLocation = list.getDropLocation(); 131 if (dropLocation != null 132 && !dropLocation.isInsert() 133 && dropLocation.getIndex() == index) { 134 135 bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground"); 136 fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground"); 137 138 isSelected = true; 139 } 140 141 if (isSelected) { 142 setBackground(bg == null ? list.getSelectionBackground() : bg); 143 setForeground(fg == null ? list.getSelectionForeground() : fg); 144 } 145 else { 146 setBackground(list.getBackground()); 147 setForeground(list.getForeground()); 148 } 149 150 if (value instanceof Icon) { 151 setIcon((Icon)value); 152 setText(""); 153 } 154 else { 155 setIcon(null); 156 setText((value == null) ? "" : value.toString()); 157 } 158 159 setEnabled(list.isEnabled()); 160 setFont(list.getFont()); 161 162 Border border = null; 163 if (cellHasFocus) { 164 if (isSelected) { 165 border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder"); 166 } 167 if (border == null) { 168 border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder"); 169 } 170 } else { 171 border = getNoFocusBorder(); 172 } 173 setBorder(border); 174 175 return this; 176 } 177 178 /** 179 * Overridden for performance reasons. 180 * See the <a href="#override">Implementation Note</a> 181 * for more information. 182 * 183 * @since 1.5 184 * @return <code>true</code> if the background is completely opaque 185 * and differs from the JList's background; 186 * <code>false</code> otherwise 187 */ 188 @Override 189 public boolean isOpaque() { 190 Color back = getBackground(); 191 Component p = getParent(); 192 if (p != null) { 193 p = p.getParent(); 194 } 195 // p should now be the JList. 196 boolean colorMatch = (back != null) && (p != null) && 197 back.equals(p.getBackground()) && 198 p.isOpaque(); 199 return !colorMatch && super.isOpaque(); 200 } 201 202 /** 203 * Overridden for performance reasons. 204 * See the <a href="#override">Implementation Note</a> 205 * for more information. 206 */ 207 @Override 208 public void validate() {} 209 210 /** 211 * Overridden for performance reasons. 212 * See the <a href="#override">Implementation Note</a> 213 * for more information. 214 * 215 * @since 1.5 216 */ 217 @Override 218 public void invalidate() {} 219 220 /** 221 * Overridden for performance reasons. 222 * See the <a href="#override">Implementation Note</a> 223 * for more information. 224 * 225 * @since 1.5 226 */ 227 @Override 228 public void repaint() {} 229 230 /** 231 * Overridden for performance reasons. 232 * See the <a href="#override">Implementation Note</a> 233 * for more information. 234 */ 235 @Override 236 public void revalidate() {} 237 /** 238 * Overridden for performance reasons. 239 * See the <a href="#override">Implementation Note</a> 240 * for more information. 241 */ 242 @Override 243 public void repaint(long tm, int x, int y, int width, int height) {} 244 245 /** 246 * Overridden for performance reasons. 247 * See the <a href="#override">Implementation Note</a> 248 * for more information. 249 */ 250 @Override 251 public void repaint(Rectangle r) {} 252 253 /** 254 * Overridden for performance reasons. 255 * See the <a href="#override">Implementation Note</a> 256 * for more information. 257 */ 258 @Override 259 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 260 // Strings get interned... 261 if (propertyName == "text" 262 || ((propertyName == "font" || propertyName == "foreground") 263 && oldValue != newValue 264 && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) { 265 266 super.firePropertyChange(propertyName, oldValue, newValue); 267 } 268 } 269 270 /** 271 * Overridden for performance reasons. 272 * See the <a href="#override">Implementation Note</a> 273 * for more information. 274 */ 275 @Override 276 public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {} 277 278 /** 279 * Overridden for performance reasons. 280 * See the <a href="#override">Implementation Note</a> 281 * for more information. 282 */ 283 @Override 284 public void firePropertyChange(String propertyName, char oldValue, char newValue) {} 285 286 /** 287 * Overridden for performance reasons. 288 * See the <a href="#override">Implementation Note</a> 289 * for more information. 290 */ 291 @Override 292 public void firePropertyChange(String propertyName, short oldValue, short newValue) {} 293 294 /** 295 * Overridden for performance reasons. 296 * See the <a href="#override">Implementation Note</a> 297 * for more information. 298 */ 299 @Override 300 public void firePropertyChange(String propertyName, int oldValue, int newValue) {} 301 302 /** 303 * Overridden for performance reasons. 304 * See the <a href="#override">Implementation Note</a> 305 * for more information. 306 */ 307 @Override 308 public void firePropertyChange(String propertyName, long oldValue, long newValue) {} 309 310 /** 311 * Overridden for performance reasons. 312 * See the <a href="#override">Implementation Note</a> 313 * for more information. 314 */ 315 @Override 316 public void firePropertyChange(String propertyName, float oldValue, float newValue) {} 317 318 /** 319 * Overridden for performance reasons. 320 * See the <a href="#override">Implementation Note</a> 321 * for more information. 322 */ 323 @Override 324 public void firePropertyChange(String propertyName, double oldValue, double newValue) {} 325 326 /** 327 * Overridden for performance reasons. 328 * See the <a href="#override">Implementation Note</a> 329 * for more information. 330 */ 331 @Override 332 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {} 333 334 /** 335 * A subclass of DefaultListCellRenderer that implements UIResource. 336 * DefaultListCellRenderer doesn't implement UIResource 337 * directly so that applications can safely override the 338 * cellRenderer property with DefaultListCellRenderer subclasses. 339 * <p> 340 * <strong>Warning:</strong> 341 * Serialized objects of this class will not be compatible with 342 * future Swing releases. The current serialization support is 343 * appropriate for short term storage or RMI between applications running 344 * the same version of Swing. As of 1.4, support for long term storage 345 * of all JavaBeans™ 346 * has been added to the <code>java.beans</code> package. 347 * Please see {@link java.beans.XMLEncoder}. 348 */ 349 @SuppressWarnings("serial") // Same-version serialization only 350 public static class UIResource extends DefaultListCellRenderer 351 implements javax.swing.plaf.UIResource 352 { 353 } 354 }