< prev index next >

src/java.desktop/share/classes/javax/swing/DefaultListCellRenderer.java

Print this page




  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 name="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&trade;
  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 


 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     */


 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&trade;
 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 }


  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 name="override">Implementation Note:</a></strong>
  44  * This class overrides
  45  * {@code invalidate},
  46  * {@code validate},
  47  * {@code revalidate},
  48  * {@code repaint},
  49  * {@code isOpaque},
  50  * and
  51  * {@code firePropertyChange}
  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&trade;
  67  * has been added to the {@code java.beans} 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}. This field might not be used. To change the
  81     * {@code Border} used by this renderer override the
  82     * {@code getListCellRendererComponent} 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 


 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} if the background is completely opaque
 185      *         and differs from the JList's background;
 186      *         {@code false} 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     */


 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&trade;
 346      * has been added to the {@code java.beans} 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 }
< prev index next >