View Javadoc

1   /***
2    * LayeredColorModel.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  import java.util.Vector;
30  
31  import org.jcreme.swing.table.TableColorModel;
32  import org.jcreme.swing.table.TableColorModelEvent;
33  import org.jcreme.swing.table.TableColorModelListener;
34  
35  /***
36   * This class enables to layer color models. The first ColorModel is the top
37   * layer. Each layer hides the layers underneath it.
38   * 
39   * @author $Author: dbregeon $
40   * @version $Revision: 1.1 $
41   */
42  public class LayeredColorModel extends DefaultTableColorModel implements
43          TableColorModel {
44      /***
45       * Hols the layers of TableColorModels.
46       */
47      private final Vector layers = new Vector();
48  
49      private final TableColorModelListener layersListener = new TableColorModelListener() {
50          public void tableColorModelChanged(TableColorModelEvent e) {
51              TableColorModelEvent forwardEvent = new TableColorModelEvent(
52                      LayeredColorModel.this, e.getMinRow(), e.getMaxRow(), e
53                              .getColumn());
54              fireTableColorModelChanged(forwardEvent);
55          }
56  
57      };
58  
59      /***
60       * Enables to add a layer to the model.
61       * 
62       * @param layer
63       *            the layer to add.
64       */
65      public void addLayer(TableColorModel layer) {
66          if (layer != null) {
67              synchronized (this.layers) {
68                  this.layers.add(layer);
69              }
70              layer.addTableColorModelListener(this.layersListener);
71          }
72      }
73  
74      /***
75       * Enables to add a layer to the model at a given depth.
76       * 
77       * @param layer
78       *            the layer to add.
79       * @param position
80       *            the depth of the layer in the model.
81       */
82      public void setLayerAt(TableColorModel layer, int position) {
83          if (layer != null) {
84              synchronized (this.layers) {
85                  if ((position >= 0) && (position < this.layers.size())) {
86                      this.layers.insertElementAt(layer, position);
87                  } else if (position >= this.layers.size()) {
88                      addLayer(layer);
89                  }
90              }
91              layer.addTableColorModelListener(this.layersListener);
92  
93          }
94      }
95  
96      /***
97       * Gives access to the layers that compose the model.
98       * 
99       * @return an array containing the layers by increasing depth.
100      */
101     public TableColorModel[] getLayers() {
102         return (TableColorModel[]) this.layers.toArray(new TableColorModel[0]);
103     }
104 
105     /***
106      * Enables to remove a layer from the model.
107      * 
108      * @param layer
109      *            the layer to remove.
110      */
111     public void removeLayer(TableColorModel layer) {
112         if (layer != null) {
113             layer.removeTableColorModelListener(this.layersListener);
114             synchronized (this.layers) {
115                 this.layers.removeElement(layer);
116             }
117         }
118     }
119 
120     /***
121      * Enables to remove the layer at a given depth from the model.
122      * 
123      * @param position
124      *            the depth of the layer to remove.
125      */
126     public void removeLayerAt(int position) {
127         if ((position >= 0) && (position < this.layers.size())) {
128             TableColorModel layer = (TableColorModel) this.layers
129                     .elementAt(position);
130             layer.removeTableColorModelListener(this.layersListener);
131             synchronized (this.layers) {
132                 this.layers.removeElementAt(position);
133             }
134         }
135     }
136 
137     /***
138      * Gives access to the background color of a cell.
139      * 
140      * @param row
141      *            the cell's row.
142      * @param column
143      *            the cell's column.
144      * @param selected
145      *            whether the cell is currently selected or not.
146      * @param renderer
147      *            the renderer used to display the cell.
148      * @return the background color for the cell.
149      */
150     public Color getBackgroundColor(int row, int column, boolean selected,
151             java.awt.Component renderer) {
152         Color result = null;
153         Color newColor = null;
154         synchronized (this.layers) {
155             // loop in reverse order so that the topmost color has
156             // a stronger effect on the result.
157             int index = this.layers.size();
158             while ((result == null) && (index > 0)) {
159                 index--;
160                 result = ((TableColorModel) this.layers.elementAt(index))
161                         .getBackgroundColor(row, column, selected, renderer);
162             }
163             while (index > 0) {
164                 index--;
165                 newColor = ((TableColorModel) this.layers.elementAt(index))
166                         .getBackgroundColor(row, column, selected, renderer);
167                 if (newColor != null) {
168                     result = mixColors(newColor, result);
169                 }
170             }
171         }
172         return result;
173     }
174 
175     /***
176      * Gives access to the foreground color of a cell.
177      * 
178      * @param row
179      *            the cell's row.
180      * @param column
181      *            the cell's column.
182      * @param selected
183      *            whether the cell is currently selected or not.
184      * @param renderer
185      *            the renderer used to display the cell.
186      * @return the foreground color for the cell.
187      */
188     public Color getForegroundColor(int row, int column, boolean selected,
189             java.awt.Component renderer) {
190         Color newColor = null;
191         Color result = null;
192         synchronized (this.layers) {
193             // loop in reverse order so that the topmost color has
194             // a stronger effect on the result.
195             int index = this.layers.size();
196             while ((result == null) && (index > 0)) {
197                 index--;
198                 result = ((TableColorModel) this.layers.elementAt(index))
199                         .getForegroundColor(row, column, selected, renderer);
200             }
201             while (index > 0) {
202                 index--;
203                 newColor = ((TableColorModel) this.layers.elementAt(index))
204                         .getForegroundColor(row, column, selected, renderer);
205                 if (newColor != null) {
206                     result = mixColors(newColor, result);
207                 }
208             }
209         }
210         return result;
211     }
212 }