View Javadoc

1   /***
2    * LineTitlesColorModel.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  import org.jcreme.swing.table.TableColorModelEvent;
32  
33  /***
34   * This class enables to give cycling colors to a Table. The cycle can have any
35   * length desired and can contain any number of colors. An obvious use of this
36   * ColorModel is to have a different color for every two or three lines to ease
37   * the reading of the Table. This ColorModel implementation acts on lines rather
38   * than on cells.
39   * 
40   * @author $Author: dbregeon $
41   * @version $Revision: 1.1 $
42   */
43  public class LineTitlesColorModel extends DefaultTableColorModel implements
44          TableColorModel {
45      /***
46       * The color used for the background of the columns.
47       */
48      private Color backgroundColor = null;
49  
50      /***
51       * The color used for the foreground of the columns.
52       */
53      private Color foregroundColor = null;
54  
55      /***
56       * The background color to be used when a line is selected.
57       */
58      private Color backgroundSelectedColor = null;
59  
60      /***
61       * The foreground color to be used when a line is selected.
62       */
63      private Color foregroundSelectedColor = null;
64  
65      /***
66       * This is the number of columns that are part of a line's title.
67       */
68      private int numberOfColumnsInTitle = 1;
69  
70      /***
71       * Creates new LineTitlesColorModel
72       * 
73       * @param bgColor
74       *            the backgroud colors of the cycle.
75       * @param fgColor
76       *            the foreground colors of the cycle.
77       * @param bgSelectedColor
78       *            the background color for selected rows.
79       * @param fgSelectedColor
80       *            the foreground color for selected rows.
81       * @param numberOfColumnsInTitle
82       *            the number of columns that are part of a line's title.
83       */
84      public LineTitlesColorModel(Color bgColor, Color fgColor,
85              Color bgSelectedColor, Color fgSelectedColor,
86              int numberOfColumnsInTitle) {
87          this.backgroundColor = bgColor;
88          this.foregroundColor = fgColor;
89          this.backgroundSelectedColor = bgSelectedColor;
90          this.foregroundSelectedColor = fgSelectedColor;
91          this.numberOfColumnsInTitle = numberOfColumnsInTitle;
92      }
93  
94      /***
95       * Gives access to the background color of a cell.
96       * 
97       * @param row
98       *            the cell's row.
99       * @param column
100      *            the cell's column.
101      * @param selected
102      *            whether the cell is currently selected or not.
103      * @param renderer
104      *            the renderer used to display the cell.
105      * @return the background color for the cell.
106      */
107     public Color getBackgroundColor(int row, int column, boolean selected,
108             java.awt.Component renderer) {
109         Color result = null;
110         if (column < this.numberOfColumnsInTitle) {
111             result = this.backgroundColor;
112             if (selected) {
113                 result = mixColors(this.backgroundSelectedColor, result);
114             }
115         }
116         return result;
117     }
118 
119     /***
120      * Gives access to the foreground color of a cell.
121      * 
122      * @param row
123      *            the cell's row.
124      * @param column
125      *            the cell's column.
126      * @param selected
127      *            whether the cell is currently selected or not.
128      * @param renderer
129      *            the renderer used to display the cell.
130      * @return the foreground color for the cell.
131      */
132     public Color getForegroundColor(int row, int column, boolean selected,
133             java.awt.Component renderer) {
134         Color result = null;
135         Color presentBGColor = renderer.getBackground();
136         Color presentFGColor = renderer.getForeground();
137         if (presentFGColor != null) {
138             result = presentFGColor;
139         } else if (column < this.numberOfColumnsInTitle) {
140             result = this.foregroundColor;
141             if (selected) {
142                 result = mixColors(this.foregroundSelectedColor, result);
143             }
144         }
145         return ensureMinimumDistance(result, presentBGColor);
146     }
147 
148     /***
149      * Gives access to the color to apply to the foreground to mark title
150      * columns.
151      * 
152      * @return the foreground color for the title columns.
153      */
154     public Color getForegroundColor() {
155         return this.foregroundColor;
156     }
157 
158     /***
159      * Gives access to the color to apply to the foreground to mark title
160      * columns when the row is selected.
161      * 
162      * @return the foreground color for the title columns.
163      */
164     public Color getForegroundSelectedColor() {
165         return this.foregroundSelectedColor;
166     }
167 
168     /***
169      * Gives access to the color to apply to the background to mark title
170      * columns.
171      * 
172      * @return the background color for the title columns.
173      */
174     public Color getBackgroundColor() {
175         return this.backgroundColor;
176     }
177 
178     /***
179      * Gives access to the color to apply to the background to mark title
180      * columns when the row is selected.
181      * 
182      * @return the background color for the title columns.
183      */
184     public Color getBackgroundSelectedColor() {
185         return this.foregroundColor;
186     }
187 
188     /***
189      * Gives access to the number of columns to include in the title.
190      * 
191      * @return the number of columns in the line title.
192      */
193     public int getNumberOfColumnsInTitle() {
194         return this.numberOfColumnsInTitle;
195     }
196 
197     /***
198      * Enables to change the number of columns to be included as title.
199      * 
200      * @param titleSize
201      *            the new number of columns to be included as titles.
202      */
203     public void setNumberOfColumnsInTitle(int titleSize) {
204         if (this.numberOfColumnsInTitle != titleSize) {
205             this.numberOfColumnsInTitle = titleSize;
206             fireTableColorModelChanged(new TableColorModelEvent(this));
207         }
208     }
209 
210     /***
211      * Enables to change the background color for title columns.
212      * 
213      * @param bgColor
214      *            the new background title color.
215      */
216     public void setBackgroundColor(Color bgColor) {
217         if (((bgColor == null) && (this.backgroundColor != null))
218                 || ((bgColor != null) && ((this.backgroundColor == null) || (!this.backgroundColor
219                         .equals(bgColor))))) {
220             this.backgroundColor = bgColor;
221             fireTableColorModelChanged(new TableColorModelEvent(this));
222         }
223     }
224 
225     /***
226      * Enables to change the foreground color for title columns.
227      * 
228      * @param fgColor
229      *            the new foreground title color.
230      */
231     public void setForegroundColor(Color fgColor) {
232         if (((fgColor == null) && (this.foregroundColor != null))
233                 || ((fgColor != null) && ((this.foregroundColor == null) || (!this.foregroundColor
234                         .equals(fgColor))))) {
235             this.foregroundColor = fgColor;
236             fireTableColorModelChanged(new TableColorModelEvent(this));
237         }
238     }
239 
240     /***
241      * This method enables to change the color used as background for selected
242      * cells.
243      * 
244      * @param bgSelectedColor
245      *            the new background for selected cells.
246      */
247     public void setBackgroundSelectedColor(Color bgSelectedColor) {
248         if (((bgSelectedColor == null) && (this.backgroundSelectedColor != null))
249                 || ((bgSelectedColor != null) && ((this.backgroundSelectedColor == null) || (!this.backgroundSelectedColor
250                         .equals(bgSelectedColor))))) {
251             this.backgroundSelectedColor = bgSelectedColor;
252             fireTableColorModelChanged(new TableColorModelEvent(this));
253         }
254     }
255 
256     /***
257      * This method enables to change the color used as foreground for selected
258      * cells.
259      * 
260      * @param fgSelectedColor
261      *            the new foreground for selected cells.
262      */
263     public void setForegroundSelectedColor(Color fgSelectedColor) {
264         if (((fgSelectedColor == null) && (this.foregroundSelectedColor != null))
265                 || ((fgSelectedColor != null) && ((this.foregroundSelectedColor == null) || (!this.foregroundSelectedColor
266                         .equals(fgSelectedColor))))) {
267             this.foregroundSelectedColor = fgSelectedColor;
268             fireTableColorModelChanged(new TableColorModelEvent(this));
269         }
270     }
271 }