View Javadoc

1   /***
2    * ColumnCyclingColorModel.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) 2004 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  import java.awt.Component;
30  
31  import org.jcreme.swing.table.TableColorModel;
32  
33  /***
34   * This class enables to give cycling colors to the columns of a Table. The
35   * cycle can have any length desired and can contain any number of colors. This
36   * ColorModel implementation acts on columns rather than on cells.
37   * 
38   * @author $Author: dbregeon $
39   * @version $Revision: 1.1 $
40   */
41  public class ColumnCyclingColorModel extends BaseCyclingColorModel implements
42          TableColorModel {
43      /***
44       * Creates new CyclingColorModel
45       * 
46       * @param bgColors
47       *            the backgroud colors of the cycle.
48       * @param fgColors
49       *            the foreground colors of the cycle.
50       * @param bgSelectedColor
51       *            the background color for selected columns.
52       * @param fgSelectedColor
53       *            the foreground color for selected columns.
54       * @param cycleLength
55       *            the number of reps of the same color in the cycle.
56       */
57      public ColumnCyclingColorModel(Color[] bgColors, Color[] fgColors,
58              Color bgSelectedColor, Color fgSelectedColor, int cycleLength) {
59          super(bgColors, fgColors, bgSelectedColor, fgSelectedColor, cycleLength);
60      }
61  
62      /***
63       * Gives access to the background color of a cell.
64       * 
65       * @param row
66       *            the cell's row.
67       * @param column
68       *            the cell's column.
69       * @param selected
70       *            whether the cell is currently selected or not.
71       * @param renderer
72       *            the renderer used to display the cell.
73       * @return the background color for the cell.
74       */
75      public Color getBackgroundColor(int row, int column, boolean selected,
76              java.awt.Component renderer) {
77          Color result = null;
78          Color[] backgroundColors = getBackgroundColors();
79          if (backgroundColors.length > 0) {
80              result = backgroundColors[(column / getCycleLength())
81                      % (backgroundColors.length)];
82          }
83          if (renderer != null) {
84              if (result == null) {
85                  result = renderer.getBackground();
86              } else {
87                  result = mixColors(result, renderer.getBackground());
88              }
89          }
90          if (selected) {
91              result = mixColors(getBackgroundSelectedColor(), result);
92          }
93          return result;
94      }
95  
96      /***
97       * Gives access to the foreground color of a cell.
98       * 
99       * @param row
100      *            the cell's row.
101      * @param column
102      *            the cell's column.
103      * @param selected
104      *            whether the cell is currently selected or not.
105      * @param renderer
106      *            the renderer used to display the cell.
107      * @return the foreground color for the cell.
108      */
109     public Color getForegroundColor(int row, int column, boolean selected,
110             Component renderer) {
111         Color result = null;
112         Color presentBGColor = renderer.getBackground();
113         Color presentFGColor = renderer.getForeground();
114         Color[] foregroundColors = getForegroundColors();
115         if (presentFGColor != null) {
116             result = presentFGColor;
117         } else if (foregroundColors.length > 0) {
118             result = foregroundColors[(column / getCycleLength())
119                     % (foregroundColors.length)];
120         }
121         if (selected) {
122             result = mixColors(getForegroundSelectedColor(), result);
123         }
124         return ensureMinimumDistance(result, presentBGColor);
125     }
126 
127 }