View Javadoc

1   /***
2    * CyclingColorModel.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.swing.table.colormodel;
27  
28  import java.awt.Color;
29  
30  import org.jcreme.swing.table.TableColorModel;
31  
32  /***
33   * This class enables to give cycling colors to a Table. The cycle can have any
34   * length desired and can contain any number of colors. An obvious use of this
35   * ColorModel is to have a different color for every two or three lines to ease
36   * the reading of the Table. This ColorModel implementation acts on lines rather
37   * than on cells.
38   * 
39   * @author $Author: dbregeon $
40   * @version $Revision: 1.1 $
41   */
42  public class CyclingColorModel extends BaseCyclingColorModel implements
43          TableColorModel {
44      /***
45       * Creates new CyclingColorModel
46       * 
47       * @param bgColors
48       *            the backgroud colors of the cycle.
49       * @param fgColors
50       *            the foreground colors of the cycle.
51       * @param bgSelectedColor
52       *            the background color for selected rows.
53       * @param fgSelectedColor
54       *            the foreground color for selected rows.
55       * @param cycleLength
56       *            the number of reps of the same color in the cycle.
57       */
58      public CyclingColorModel(Color[] bgColors, Color[] fgColors,
59              Color bgSelectedColor, Color fgSelectedColor, int cycleLength) {
60          super(bgColors, fgColors, bgSelectedColor, fgSelectedColor, cycleLength);
61      }
62  
63      /***
64       * Gives access to the background color of a cell.
65       * 
66       * @param row
67       *            the cell's row.
68       * @param column
69       *            the cell's column.
70       * @param selected
71       *            whether the cell is currently selected or not.
72       * @param renderer
73       *            the renderer used to display the cell.
74       * @return the background color for the cell.
75       */
76      public Color getBackgroundColor(int row, int column, boolean selected,
77              java.awt.Component renderer) {
78          Color result = null;
79          Color[] backgroundColors = getBackgroundColors();
80          if (backgroundColors.length > 0) {
81              result = backgroundColors[(row / getCycleLength())
82                      % (backgroundColors.length)];
83          }
84          if (renderer != null) {
85              if (result == null) {
86                  result = renderer.getBackground();
87              } else {
88                  result = mixColors(result, renderer.getBackground());
89              }
90          }
91          if (selected) {
92              result = mixColors(getBackgroundSelectedColor(), result);
93          }
94          return result;
95      }
96  
97      /***
98       * Gives access to the foreground color of a cell.
99       * 
100      * @param row
101      *            the cell's row.
102      * @param column
103      *            the cell's column.
104      * @param selected
105      *            whether the cell is currently selected or not.
106      * @param renderer
107      *            the renderer used to display the cell.
108      * @return the foreground color for the cell.
109      */
110     public Color getForegroundColor(int row, int column, boolean selected,
111             java.awt.Component renderer) {
112         Color result = null;
113         Color presentBGColor = renderer.getBackground();
114         Color presentFGColor = renderer.getForeground();
115         Color[] foregroundColors = getForegroundColors();
116         if (presentFGColor != null) {
117             result = presentFGColor;
118         } else if (foregroundColors.length > 0) {
119             result = foregroundColors[(row / getCycleLength())
120                     % (foregroundColors.length)];
121         }
122         if (selected) {
123             result = mixColors(getForegroundSelectedColor(), result);
124         }
125         return ensureMinimumDistance(result, presentBGColor);
126     }
127 }