Path: blob/master/src/java.desktop/share/classes/javax/swing/DefaultListCellRenderer.java
41153 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import javax.swing.*;28import javax.swing.event.*;29import javax.swing.border.*;3031import java.awt.Component;32import java.awt.Color;33import java.awt.Rectangle;3435import java.io.Serializable;36import sun.swing.DefaultLookup;37import sun.swing.SwingUtilities2;3839/**40* Renders an item in a list.41* <p>42* <strong><a id="override">Implementation Note:</a></strong>43* This class overrides44* <code>invalidate</code>,45* <code>validate</code>,46* <code>revalidate</code>,47* <code>repaint</code>,48* <code>isOpaque</code>,49* and50* <code>firePropertyChange</code>51* solely to improve performance.52* If not overridden, these frequently called methods would execute code paths53* that are unnecessary for the default list cell renderer.54* If you write your own renderer,55* take care to weigh the benefits and56* drawbacks of overriding these methods.57*58* <p>59*60* <strong>Warning:</strong>61* Serialized objects of this class will not be compatible with62* future Swing releases. The current serialization support is63* appropriate for short term storage or RMI between applications running64* the same version of Swing. As of 1.4, support for long term storage65* of all JavaBeans66* has been added to the <code>java.beans</code> package.67* Please see {@link java.beans.XMLEncoder}.68*69* @author Philip Milne70* @author Hans Muller71* @since 1.272*/73@SuppressWarnings("serial") // Same-version serialization only74public class DefaultListCellRenderer extends JLabel75implements ListCellRenderer<Object>, Serializable76{7778/**79* An empty <code>Border</code>. This field might not be used. To change the80* <code>Border</code> used by this renderer override the81* <code>getListCellRendererComponent</code> method and set the border82* of the returned component directly.83*/84private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);85private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);86/**87* No focus border88*/89protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;9091/**92* Constructs a default renderer object for an item93* in a list.94*/95public DefaultListCellRenderer() {96super();97setOpaque(true);98setBorder(getNoFocusBorder());99setName("List.cellRenderer");100}101102@SuppressWarnings("removal")103private Border getNoFocusBorder() {104Border border = DefaultLookup.getBorder(this, ui, "List.cellNoFocusBorder");105if (System.getSecurityManager() != null) {106if (border != null) return border;107return SAFE_NO_FOCUS_BORDER;108} else {109if (border != null &&110(noFocusBorder == null ||111noFocusBorder == DEFAULT_NO_FOCUS_BORDER)) {112return border;113}114return noFocusBorder;115}116}117118public Component getListCellRendererComponent(119JList<?> list,120Object value,121int index,122boolean isSelected,123boolean cellHasFocus)124{125setComponentOrientation(list.getComponentOrientation());126127Color bg = null;128Color fg = null;129130JList.DropLocation dropLocation = list.getDropLocation();131if (dropLocation != null132&& !dropLocation.isInsert()133&& dropLocation.getIndex() == index) {134135bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground");136fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground");137138isSelected = true;139}140141if (isSelected) {142setBackground(bg == null ? list.getSelectionBackground() : bg);143setForeground(fg == null ? list.getSelectionForeground() : fg);144}145else {146setBackground(list.getBackground());147setForeground(list.getForeground());148}149150if (value instanceof Icon) {151setIcon((Icon)value);152setText("");153}154else {155setIcon(null);156setText((value == null) ? "" : value.toString());157}158159setEnabled(list.isEnabled());160setFont(list.getFont());161162Border border = null;163if (cellHasFocus) {164if (isSelected) {165border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");166}167if (border == null) {168border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");169}170} else {171border = getNoFocusBorder();172}173setBorder(border);174175return this;176}177178/**179* Overridden for performance reasons.180* See the <a href="#override">Implementation Note</a>181* for more information.182*183* @since 1.5184* @return <code>true</code> if the background is completely opaque185* and differs from the JList's background;186* <code>false</code> otherwise187*/188@Override189public boolean isOpaque() {190Color back = getBackground();191Component p = getParent();192if (p != null) {193p = p.getParent();194}195// p should now be the JList.196boolean colorMatch = (back != null) && (p != null) &&197back.equals(p.getBackground()) &&198p.isOpaque();199return !colorMatch && super.isOpaque();200}201202/**203* Overridden for performance reasons.204* See the <a href="#override">Implementation Note</a>205* for more information.206*/207@Override208public void validate() {}209210/**211* Overridden for performance reasons.212* See the <a href="#override">Implementation Note</a>213* for more information.214*215* @since 1.5216*/217@Override218public void invalidate() {}219220/**221* Overridden for performance reasons.222* See the <a href="#override">Implementation Note</a>223* for more information.224*225* @since 1.5226*/227@Override228public void repaint() {}229230/**231* Overridden for performance reasons.232* See the <a href="#override">Implementation Note</a>233* for more information.234*/235@Override236public void revalidate() {}237/**238* Overridden for performance reasons.239* See the <a href="#override">Implementation Note</a>240* for more information.241*/242@Override243public void repaint(long tm, int x, int y, int width, int height) {}244245/**246* Overridden for performance reasons.247* See the <a href="#override">Implementation Note</a>248* for more information.249*/250@Override251public void repaint(Rectangle r) {}252253/**254* Overridden for performance reasons.255* See the <a href="#override">Implementation Note</a>256* for more information.257*/258@Override259protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {260// Strings get interned...261if (propertyName == "text"262|| ((SwingUtilities2.isScaleChanged(propertyName, oldValue, newValue)263|| propertyName == "font" || propertyName == "foreground")264&& oldValue != newValue265&& getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {266267super.firePropertyChange(propertyName, oldValue, newValue);268}269}270271/**272* Overridden for performance reasons.273* See the <a href="#override">Implementation Note</a>274* for more information.275*/276@Override277public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {}278279/**280* Overridden for performance reasons.281* See the <a href="#override">Implementation Note</a>282* for more information.283*/284@Override285public void firePropertyChange(String propertyName, char oldValue, char newValue) {}286287/**288* Overridden for performance reasons.289* See the <a href="#override">Implementation Note</a>290* for more information.291*/292@Override293public void firePropertyChange(String propertyName, short oldValue, short newValue) {}294295/**296* Overridden for performance reasons.297* See the <a href="#override">Implementation Note</a>298* for more information.299*/300@Override301public void firePropertyChange(String propertyName, int oldValue, int newValue) {}302303/**304* Overridden for performance reasons.305* See the <a href="#override">Implementation Note</a>306* for more information.307*/308@Override309public void firePropertyChange(String propertyName, long oldValue, long newValue) {}310311/**312* Overridden for performance reasons.313* See the <a href="#override">Implementation Note</a>314* for more information.315*/316@Override317public void firePropertyChange(String propertyName, float oldValue, float newValue) {}318319/**320* Overridden for performance reasons.321* See the <a href="#override">Implementation Note</a>322* for more information.323*/324@Override325public void firePropertyChange(String propertyName, double oldValue, double newValue) {}326327/**328* Overridden for performance reasons.329* See the <a href="#override">Implementation Note</a>330* for more information.331*/332@Override333public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}334335/**336* A subclass of DefaultListCellRenderer that implements UIResource.337* DefaultListCellRenderer doesn't implement UIResource338* directly so that applications can safely override the339* cellRenderer property with DefaultListCellRenderer subclasses.340* <p>341* <strong>Warning:</strong>342* Serialized objects of this class will not be compatible with343* future Swing releases. The current serialization support is344* appropriate for short term storage or RMI between applications running345* the same version of Swing. As of 1.4, support for long term storage346* of all JavaBeans347* has been added to the <code>java.beans</code> package.348* Please see {@link java.beans.XMLEncoder}.349*/350@SuppressWarnings("serial") // Same-version serialization only351public static class UIResource extends DefaultListCellRenderer352implements javax.swing.plaf.UIResource353{354/**355* Constructs a {@code UIResource}.356*/357public UIResource() {}358}359}360361362