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

Save This Page
Home » openjdk-7 » javax » swing » [javadoc | source]