AlternateRowHighlighter.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: AlternateRowHighlighter.java,v 1.3 2005/10/10 18:02:31 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.decorator;
  22. import java.awt.Color;
  23. import java.awt.Component;
  24. /**
  25.  * AlternateRowHighlighter prepares a cell renderer to use different background
  26.  * colors for alternating rows in a data view.
  27.  *
  28.  * @author Ramesh Gupta
  29.  */
  30. public class AlternateRowHighlighter extends Highlighter {
  31.     private final static Color  defaultOddRowColor = Color.white;
  32.     private final static Color  defaultEvenRowColor = new Color(0xF0, 0xF0, 0xE0);
  33.     public final static Highlighter beige =
  34.         new AlternateRowHighlighter(Color.white, new Color(245, 245, 220), null);
  35.     public final static Highlighter linePrinter =
  36.         new AlternateRowHighlighter(Color.white, new Color(0xCC, 0xCC, 0xFF), null);
  37.     public final static Highlighter classicLinePrinter =
  38.         new AlternateRowHighlighter(Color.white, new Color(0xCC, 0xFF, 0xCC), null);
  39.     public final static Highlighter floralWhite =
  40.         new AlternateRowHighlighter(Color.white, new Color(255, 250, 240), null);
  41.     public final static Highlighter quickSilver =
  42.         new AlternateRowHighlighter(Color.white, defaultEvenRowColor, null);
  43.     private Color oddRowBackground = defaultOddRowColor;
  44.     private Color evenRowBackground = defaultEvenRowColor;
  45.     /**
  46.      * Constructs a default <code>AlternateRowHighlighter</code> that prepares a
  47.      * cell renderer to paint a white background for odd rows and a silver
  48.      * <code>(0xF0, 0xF0, 0xE0)</code> background for even rows.
  49.      */
  50.     public AlternateRowHighlighter() {
  51.     }
  52.     /**
  53.      * Constructs an <code>AlternateRowHighlighter</code> that prepares a
  54.      * cell renderer to paint the specified background colors for odd and even
  55.      * and even rows. A foreground color may also be specified. If null is
  56.      * specified for the foreground color, the foreground color for the renderer
  57.      * is unchanged. Otherwise, it is set to the specified foreground color for
  58.      * both odd and even rows.
  59.      *
  60.      * @param oddRowBackground
  61.      * @param evenRowBackground
  62.      * @param foreground
  63.      */
  64.     public AlternateRowHighlighter(Color oddRowBackground,
  65.                                    Color evenRowBackground, Color foreground) {
  66.         super(oddRowBackground, foreground); // same background for odd and even
  67.         this.oddRowBackground = oddRowBackground;
  68.         this.evenRowBackground = evenRowBackground;
  69.     }
  70.     /**
  71.      * Returns the background color for odd rows, or null if the background color
  72.      * of the cell renderer should be left unchanged for odd rows.
  73.      *
  74.      * @return the background color for odd rows, or null if the background color
  75.      * of the cell renderer should be left unchanged for odd rows
  76.      */
  77.     public Color getOddRowBackground() {
  78.         return oddRowBackground;
  79.     }
  80.     /**
  81.      * Sets the background color for odd rows to the specified color. If null is
  82.      * specified, the background color for odd rows is left unchanged in the
  83.      * renderer
  84.      *
  85.      * @param color the background color for odd rows, or null if the background
  86.      * color of the cell renderer should be left unchanged for odd rows
  87.      */
  88.     public void setOddRowBackground(Color color) {
  89.         oddRowBackground = color;
  90.         fireStateChanged();
  91.     }
  92.     /**
  93.      * Returns the background color for even rows, or null if the background color
  94.      * of the cell renderer should be left unchanged for even rows.
  95.      *
  96.      * @return the background color for even rows, or null if the background color
  97.      * of the cell renderer should be left unchanged for even rows
  98.      */
  99.     public Color getEvenRowBackground() {
  100.         return evenRowBackground;
  101.     }
  102.     /**
  103.      * Sets the background color for even rows to the specified color. If null is
  104.      * specified, the background color for even rows is left unchanged in the
  105.      * renderer.
  106.      *
  107.      * @param color the background color for even rows, or null if the background
  108.      * color of the cell renderer should be left unchanged for even rows
  109.      */
  110.     public void setEvenRowBackground(Color color) {
  111.         evenRowBackground = color;
  112.         fireStateChanged();
  113.     }
  114.     /**
  115.      * Computes the background color for the current rendering context for the
  116.      * specified adapter. It first chooses the raw background color for the
  117.      * renderer depending on whether the row being rendered is odd or even. If
  118.      * the chosen background color is not null
  119.      * calls {@link Highlighter#computeSelectedBackground(java.awt.Color) computeSelectedBackground}
  120.      * passing in the chosen raw background color as the seed color, but only if
  121.      * the row being rendered is selected.
  122.      *
  123.      * @param renderer the cell renderer component
  124.      * @param adapter
  125.      * @return the computed background color
  126.      */
  127.     protected Color computeBackground(Component renderer,
  128.                                       ComponentAdapter adapter) {
  129.         // row is zero-based; so even is actually odd!
  130.         Color color = (adapter.row % 2) == 0 ?
  131.             oddRowBackground : evenRowBackground;
  132.         if ((color != null) && adapter.isSelected()) {
  133.             color = computeSelectedBackground(color);
  134.         }
  135.         return color;
  136.     }
  137. }