Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DefaultListCellRenderer.java
38829 views
/*1* Copyright (c) 1998, 2013, 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;373839/**40* Renders an item in a list.41* <p>42* <strong><a name="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 JavaBeans™66* 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*/72public class DefaultListCellRenderer extends JLabel73implements ListCellRenderer<Object>, Serializable74{7576/**77* An empty <code>Border</code>. This field might not be used. To change the78* <code>Border</code> used by this renderer override the79* <code>getListCellRendererComponent</code> method and set the border80* of the returned component directly.81*/82private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);83private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);84protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;8586/**87* Constructs a default renderer object for an item88* in a list.89*/90public DefaultListCellRenderer() {91super();92setOpaque(true);93setBorder(getNoFocusBorder());94setName("List.cellRenderer");95}9697private Border getNoFocusBorder() {98Border border = DefaultLookup.getBorder(this, ui, "List.cellNoFocusBorder");99if (System.getSecurityManager() != null) {100if (border != null) return border;101return SAFE_NO_FOCUS_BORDER;102} else {103if (border != null &&104(noFocusBorder == null ||105noFocusBorder == DEFAULT_NO_FOCUS_BORDER)) {106return border;107}108return noFocusBorder;109}110}111112public Component getListCellRendererComponent(113JList<?> list,114Object value,115int index,116boolean isSelected,117boolean cellHasFocus)118{119setComponentOrientation(list.getComponentOrientation());120121Color bg = null;122Color fg = null;123124JList.DropLocation dropLocation = list.getDropLocation();125if (dropLocation != null126&& !dropLocation.isInsert()127&& dropLocation.getIndex() == index) {128129bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground");130fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground");131132isSelected = true;133}134135if (isSelected) {136setBackground(bg == null ? list.getSelectionBackground() : bg);137setForeground(fg == null ? list.getSelectionForeground() : fg);138}139else {140setBackground(list.getBackground());141setForeground(list.getForeground());142}143144if (value instanceof Icon) {145setIcon((Icon)value);146setText("");147}148else {149setIcon(null);150setText((value == null) ? "" : value.toString());151}152153setEnabled(list.isEnabled());154setFont(list.getFont());155156Border border = null;157if (cellHasFocus) {158if (isSelected) {159border = DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");160}161if (border == null) {162border = DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");163}164} else {165border = getNoFocusBorder();166}167setBorder(border);168169return this;170}171172/**173* Overridden for performance reasons.174* See the <a href="#override">Implementation Note</a>175* for more information.176*177* @since 1.5178* @return <code>true</code> if the background is completely opaque179* and differs from the JList's background;180* <code>false</code> otherwise181*/182@Override183public boolean isOpaque() {184Color back = getBackground();185Component p = getParent();186if (p != null) {187p = p.getParent();188}189// p should now be the JList.190boolean colorMatch = (back != null) && (p != null) &&191back.equals(p.getBackground()) &&192p.isOpaque();193return !colorMatch && super.isOpaque();194}195196/**197* Overridden for performance reasons.198* See the <a href="#override">Implementation Note</a>199* for more information.200*/201@Override202public void validate() {}203204/**205* Overridden for performance reasons.206* See the <a href="#override">Implementation Note</a>207* for more information.208*209* @since 1.5210*/211@Override212public void invalidate() {}213214/**215* Overridden for performance reasons.216* See the <a href="#override">Implementation Note</a>217* for more information.218*219* @since 1.5220*/221@Override222public void repaint() {}223224/**225* Overridden for performance reasons.226* See the <a href="#override">Implementation Note</a>227* for more information.228*/229@Override230public void revalidate() {}231/**232* Overridden for performance reasons.233* See the <a href="#override">Implementation Note</a>234* for more information.235*/236@Override237public void repaint(long tm, int x, int y, int width, int height) {}238239/**240* Overridden for performance reasons.241* See the <a href="#override">Implementation Note</a>242* for more information.243*/244@Override245public void repaint(Rectangle r) {}246247/**248* Overridden for performance reasons.249* See the <a href="#override">Implementation Note</a>250* for more information.251*/252@Override253protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {254// Strings get interned...255if (propertyName == "text"256|| ((propertyName == "font" || propertyName == "foreground")257&& oldValue != newValue258&& getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {259260super.firePropertyChange(propertyName, oldValue, newValue);261}262}263264/**265* Overridden for performance reasons.266* See the <a href="#override">Implementation Note</a>267* for more information.268*/269@Override270public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {}271272/**273* Overridden for performance reasons.274* See the <a href="#override">Implementation Note</a>275* for more information.276*/277@Override278public void firePropertyChange(String propertyName, char oldValue, char newValue) {}279280/**281* Overridden for performance reasons.282* See the <a href="#override">Implementation Note</a>283* for more information.284*/285@Override286public void firePropertyChange(String propertyName, short oldValue, short newValue) {}287288/**289* Overridden for performance reasons.290* See the <a href="#override">Implementation Note</a>291* for more information.292*/293@Override294public void firePropertyChange(String propertyName, int oldValue, int newValue) {}295296/**297* Overridden for performance reasons.298* See the <a href="#override">Implementation Note</a>299* for more information.300*/301@Override302public void firePropertyChange(String propertyName, long oldValue, long newValue) {}303304/**305* Overridden for performance reasons.306* See the <a href="#override">Implementation Note</a>307* for more information.308*/309@Override310public void firePropertyChange(String propertyName, float oldValue, float newValue) {}311312/**313* Overridden for performance reasons.314* See the <a href="#override">Implementation Note</a>315* for more information.316*/317@Override318public void firePropertyChange(String propertyName, double oldValue, double newValue) {}319320/**321* Overridden for performance reasons.322* See the <a href="#override">Implementation Note</a>323* for more information.324*/325@Override326public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}327328/**329* A subclass of DefaultListCellRenderer that implements UIResource.330* DefaultListCellRenderer doesn't implement UIResource331* directly so that applications can safely override the332* cellRenderer property with DefaultListCellRenderer subclasses.333* <p>334* <strong>Warning:</strong>335* Serialized objects of this class will not be compatible with336* future Swing releases. The current serialization support is337* appropriate for short term storage or RMI between applications running338* the same version of Swing. As of 1.4, support for long term storage339* of all JavaBeans™340* has been added to the <code>java.beans</code> package.341* Please see {@link java.beans.XMLEncoder}.342*/343public static class UIResource extends DefaultListCellRenderer344implements javax.swing.plaf.UIResource345{346}347}348349350