View Javadoc

1   /***
2    * BaseCyclingColorModel.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  
30  import org.jcreme.swing.table.TableColorModel;
31  import org.jcreme.swing.table.TableColorModelEvent;
32  
33  /***
34   * The base class for Cycle based color models.
35   * 
36   * @author $Author: dbregeon $
37   * @version $Revision: 1.1 $
38   */
39  public abstract class BaseCyclingColorModel extends DefaultTableColorModel
40          implements TableColorModel {
41      /***
42       * The colors used for the background.
43       */
44      private Color[] backgroundColors = null;
45  
46      /***
47       * The colors used for the foreground.
48       */
49      private Color[] foregroundColors = null;
50  
51      /***
52       * The background color to be used when a cell is selected.
53       */
54      private Color backgroundSelectedColor = null;
55  
56      /***
57       * The foreground color to be used when a cell is selected.
58       */
59      private Color foregroundSelectedColor = null;
60  
61      /***
62       * The length of the cycle. This is the number of successive times that the
63       * same color is used.
64       */
65      private int cycleLength = 0;
66  
67      /***
68       * Creates new BaseCyclingColorModel
69       * 
70       * @param bgColors
71       *            the backgroud colors of the cycle.
72       * @param fgColors
73       *            the foreground colors of the cycle.
74       * @param bgSelectedColor
75       *            the background color for selected rows.
76       * @param fgSelectedColor
77       *            the foreground color for selected rows.
78       * @param cycleLength
79       *            the number of reps of the same color in the cycle.
80       */
81      public BaseCyclingColorModel(Color[] bgColors, Color[] fgColors,
82              Color bgSelectedColor, Color fgSelectedColor, int cycleLength) {
83          this.backgroundColors = bgColors;
84          this.foregroundColors = fgColors;
85          this.backgroundSelectedColor = bgSelectedColor;
86          this.foregroundSelectedColor = fgSelectedColor;
87          this.cycleLength = cycleLength;
88      }
89  
90      /***
91       * This is an information method that enables to query the background color
92       * cycle in use.
93       * 
94       * @return the background colors used in the cycle.
95       */
96      public Color[] getBackgroundColors() {
97          return this.backgroundColors;
98      }
99  
100     /***
101      * This is an information method that enables to query the foreground color
102      * cycle in use.
103      * 
104      * @return the foreground colors used in the cycle.
105      */
106     public Color[] getForegroundColors() {
107         return this.foregroundColors;
108     }
109 
110     /***
111      * This is an information method that enables to query the background color
112      * when a cell is selected.
113      * 
114      * @return the background color used for selected cells.
115      */
116     public Color getBackgroundSelectedColor() {
117         return this.backgroundSelectedColor;
118     }
119 
120     /***
121      * This is an information method that enables to query the foreground color
122      * when a cell is selected.
123      * 
124      * @return the foreground color used for selected cells.
125      */
126     public Color getForegroundSelectedColor() {
127         return this.foregroundSelectedColor;
128     }
129 
130     /***
131      * This is an information method that gives access to the cycle length.
132      * 
133      * @return the color cycle length.
134      */
135     public int getCycleLength() {
136         return this.cycleLength;
137     }
138 
139     /***
140      * This method enables to change the colors used in the background cycle.
141      * 
142      * @param bgColors
143      *            the new background cycle colors.
144      */
145     public void setBackgroundColors(Color[] bgColors) {
146         if (((bgColors == null) && (this.backgroundColors != null))
147                 || ((bgColors != null) && ((this.backgroundColors == null) || (!this.backgroundColors
148                         .equals(bgColors))))) {
149             this.backgroundColors = bgColors;
150             fireTableColorModelChanged(new TableColorModelEvent(this));
151         }
152     }
153 
154     /***
155      * This method enables to change the colors used in the foreground cycle.
156      * 
157      * @param fgColors
158      *            the new foreground cycle colors.
159      */
160     public void setForegroundColors(Color[] fgColors) {
161         if (((fgColors == null) && (this.foregroundColors != null))
162                 || ((fgColors != null) && ((this.foregroundColors == null) || (!this.foregroundColors
163                         .equals(fgColors))))) {
164             this.foregroundColors = fgColors;
165             fireTableColorModelChanged(new TableColorModelEvent(this));
166         }
167     }
168 
169     /***
170      * This method enables to change the color used as background for selected
171      * cells.
172      * 
173      * @param bgSelectedColor
174      *            the new background for selected cells.
175      */
176     public void setBackgroundSelectedColor(Color bgSelectedColor) {
177         if (((bgSelectedColor == null) && (this.backgroundSelectedColor != null))
178                 || ((bgSelectedColor != null) && ((this.backgroundSelectedColor == null) || (!this.backgroundSelectedColor
179                         .equals(bgSelectedColor))))) {
180             this.backgroundSelectedColor = bgSelectedColor;
181             fireTableColorModelChanged(new TableColorModelEvent(this));
182         }
183     }
184 
185     /***
186      * This method enables to change the color used as foreground for selected
187      * cells.
188      * 
189      * @param fgSelectedColor
190      *            the new foreground for selected cells.
191      */
192     public void setForegroundSelectedColor(Color fgSelectedColor) {
193         if (((fgSelectedColor == null) && (this.foregroundSelectedColor != null))
194                 || ((fgSelectedColor != null) && ((this.foregroundSelectedColor == null) || (!this.foregroundSelectedColor
195                         .equals(fgSelectedColor))))) {
196             this.foregroundSelectedColor = fgSelectedColor;
197             fireTableColorModelChanged(new TableColorModelEvent(this));
198         }
199     }
200 
201     /***
202      * This method enables to change the length of the color cycle.
203      * 
204      * @param length
205      *            the length of the cycle.
206      */
207     public void setCycleLength(int length) {
208         if (length != this.cycleLength) {
209             this.cycleLength = length;
210             fireTableColorModelChanged(new TableColorModelEvent(this));
211         }
212     }
213 }