View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.util;
21  
22  import java.awt.Color;
23  import java.util.ArrayList;
24  
25  /**
26   * ColorManager provides a sequence of colors which can be used for example in charts.
27   * 
28   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
29   * @version $Id: ColorManager.java,v 1.8 2008-08-14 11:01:56 jbobnar Exp $
30   *
31   */
32  public class ColorManager {
33  	
34  	public static final Color[] DEFAULT_COLORS= new Color[]{Color.BLACK,Color.RED,Color.BLUE,Color.GREEN.darker(),Color.ORANGE,Color.DARK_GRAY,Color.RED.darker(),Color.BLUE.darker(),Color.GREEN.darker().darker(),Color.ORANGE.darker()};
35  	private Color[] availableColors = DEFAULT_COLORS;
36  	private ArrayList<Color> unusedColors = new ArrayList<Color>(3);
37  	private int count = 0;
38  	
39  	/**
40  	 * Loads Default colors as the colors used by this manager.
41  	 * 
42  	 */
43  	public void loadDefaultColors() {
44  		loadColors(DEFAULT_COLORS);
45  	}
46  	
47  	/**
48  	 * Loads the colors among which manager will pick a color when requested.
49  	 * 
50  	 * @param colors color bank (null or zero-length value not permitted)
51  	 */
52  	public void loadColors(Color[] colors) {
53  		if (colors == null) throw new IllegalArgumentException("Colors cannot be null.");
54  		else if (colors.length == 0) throw new IllegalArgumentException("There should be at least one Color available.");
55  		synchronized (unusedColors) {
56  			availableColors = new Color[colors.length];
57  			System.arraycopy(colors, 0, availableColors, 0, colors.length);
58  			if (count >= availableColors.length) {
59  				count %= availableColors.length;
60  			}
61  		}
62  	}
63  	
64  	/**
65  	 * Picks the next color in the sequence.
66  	 * 
67  	 * @return the next color
68  	 */
69  	public Color pickColor() {
70  		synchronized (unusedColors) {
71  			if (unusedColors.size() > 0) {
72  				Color c = unusedColors.remove(0);
73  				if (c != null) {
74  					count++;
75  					return c;
76  				}
77  			}
78  			return availableColors[count++%availableColors.length];
79  		}
80  	}
81  		
82  	/**
83  	 * Drops the color into a container with unused colors. When a new color will
84  	 * be picked, the manager will first choose among the dropped color and when
85  	 * there will be no more, it will continue the normal sequence as stated in the
86  	 * loaded colors.
87  	 * 
88  	 * @param color color to be dropped into the bank
89  	 */
90  	public void dropColor(Color color) {
91  		if (color == null) throw new IllegalArgumentException("Dropped color cannot be null.");
92  		synchronized (unusedColors) {
93  			unusedColors.add(color);
94  			count--;
95  			if (count <=0 ) count = 0;
96  		}
97  	}
98  	
99  	/**
100 	 * Returns the current index at which the color is picked from the array.
101 	 * @return color index
102 	 */
103 	public int getColorIndex() {
104 		return count;
105 	}
106 	
107 	/**
108 	 * Sets the index at which the next color will be picked from array.
109 	 * @param index new color index
110 	 */
111 	public void setColorIndex(int index) {
112 		this.count = index % availableColors.length;
113 	}
114 	
115 	/**
116 	 * Sets the color which will be the next to be picked out from the array.
117 	 * It the provided color does not exist nothing will change.
118 	 * 
119 	 * @param color the next color
120 	 */
121 	public void setIndexByColor(Color color) {
122 		for (int i = 0; i < availableColors.length; i++) {
123 			if (availableColors[i].equals(color)) {
124 				setColorIndex(i);
125 				return;
126 			}
127 		}
128 	}
129 	
130 	/**
131 	 * Empties the container with all dropped colors.
132 	 *
133 	 */
134 	public void reset() {
135 		synchronized (unusedColors) {
136 			unusedColors.clear();
137 			count = 0;
138 		}
139 	}
140 	
141 	/**
142 	 * Previews the next color that will be picked.
143 	 * 
144 	 * @return the next color
145 	 */
146 	public Color previewNextColor() {
147 		synchronized (unusedColors) {
148 			if (unusedColors.size() > 0) {
149 				return unusedColors.get(0);
150 			}
151 			return availableColors[count%availableColors.length];
152 		}
153 	}
154 	
155 }