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.ledder;
21  
22  import java.awt.Color;
23  
24  import java.util.ArrayList;
25  
26  
27  /**
28   * Default implementation of <code>BitDescriptor</code>.
29   *
30   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
31   */
32  public class DefaultBitDescriptor implements BitDescriptor
33  {
34  	class BitDescription
35  	{
36  		/**
37  		 * Creates a new BitDescription object.
38  		 *
39  		 * @param on DOCUMENT ME!
40  		 * @param off DOCUMENT ME!
41  		 * @param desc DOCUMENT ME!
42  		 */
43  		public BitDescription(Color on, Color off, String desc)
44  		{
45  			this.on = on;
46  			this.off = off;
47  			this.desc = desc;
48  		}
49  
50  		/** ON bit color. */
51  		public Color on;
52  
53  		/** OFF bit color. */
54  		public Color off;
55  
56  		/** Bit description. */
57  		public String desc;
58  	}
59  
60  	private BitDescription defaultBitDesc = new BitDescription(Color.GREEN,
61  		    Color.GRAY, null);
62  	private ArrayList bits = new ArrayList(32);
63  
64  	/**
65  	 * Creates new BitDescriptor with default colors set to <code>GREEN</code>
66  	 * for on and <code>GRAY</code> for off.
67  	 */
68  	public DefaultBitDescriptor()
69  	{
70  		super();
71  	}
72  
73  	/**
74  	 * Returns the number of led descriptions.
75  	 *
76  	 * @return int
77  	 */
78  	public int size()
79  	{
80  		return bits.size();
81  	}
82  
83  	protected synchronized BitDescription getBitDescription(int i)
84  	{
85  		if (i > -1 && i < bits.size()) {
86  			return (BitDescription)bits.get(i);
87  		}
88  
89  		return null;
90  	}
91  
92  	/**
93  	 * Appends new bit descriptions to the end of  the descriptions lists.
94  	 *
95  	 * @param label String
96  	 * @param colorOn Color
97  	 * @param colorOff Color
98  	 */
99  	public synchronized void add(String label, Color colorOn, Color colorOff)
100 	{
101 		bits.add(new BitDescription(colorOn, colorOff, label));
102 	}
103 
104 	/**
105 	 * Removes bit description at the specified index from the descriptor. If
106 	 * index is invalid or the model is empty, this method does nothing. Order
107 	 * of the remaining leds will not change.
108 	 *
109 	 * @param index int
110 	 */
111 	public synchronized void remove(int index)
112 	{
113 		if ((index < 0) || (index >= size())) {
114 			return;
115 		}
116 
117 		bits.remove(index);
118 	}
119 
120 	/**
121 	 * Removes all bit descriptions from the descriptor.
122 	 */
123 	public synchronized void clear()
124 	{
125 		bits.clear();
126 	}
127 
128 	/**
129 	 * Returns the default description of the bit at the specified index or
130 	 * default string if no description is defined.
131 	 *
132 	 * @param index int
133 	 *
134 	 * @return String Description of led.
135 	 */
136 	public String getDescription(int index)
137 	{
138 		BitDescription bd = getBitDescription(index);
139 
140 		if (bd != null && bd.desc != null) {
141 			return bd.desc;
142 		}
143 
144 		StringBuffer sb = new StringBuffer();
145 		sb.append("<bit");
146 		sb.append(index);
147 		sb.append('>');
148 
149 		return sb.toString();
150 	}
151 
152 	/**
153 	 * Sets description for the bit at the specified index. If the index
154 	 * greater than size, method does nothing.
155 	 *
156 	 * @param index int
157 	 * @param description String
158 	 */
159 	public void setDescription(int index, String description)
160 	{
161 		BitDescription bd = getBitDescription(index);
162 
163 		if (bd != null) {
164 			bd.desc = description;
165 		}
166 	}
167 
168 	/**
169 	 * Returns the default color representing ON state for bit at specified
170 	 * index or default value if color is not defined.
171 	 *
172 	 * @param index int
173 	 *
174 	 * @return Color Color of bit or default value
175 	 */
176 	public Color getColorWhenOn(int index)
177 	{
178 		BitDescription bd = getBitDescription(index);
179 
180 		if (bd != null && bd.on != null) {
181 			return bd.on;
182 		}
183 
184 		return defaultBitDesc.on;
185 	}
186 
187 	/**
188 	 * Returns the default color representing ON state for bit used when no
189 	 * specific description is defined.
190 	 *
191 	 * @return Color default value
192 	 */
193 	public Color getDefaultColorWhenOn()
194 	{
195 		return defaultBitDesc.on;
196 	}
197 
198 	/**
199 	 * Sets new color indicating the ON state for the specified bit index. If
200 	 * the  index is invalid, method does nothing.
201 	 *
202 	 * @param index int
203 	 * @param color Color
204 	 */
205 	public void setColorWhenOn(int index, Color color)
206 	{
207 		BitDescription bd = getBitDescription(index);
208 
209 		if (bd != null) {
210 			bd.on = color;
211 		}
212 	}
213 
214 	/**
215 	 * Sets default color indicating the ON state.
216 	 *
217 	 * @param color Color
218 	 */
219 	public void setDefaultColorWhenOn(Color color)
220 	{
221 		defaultBitDesc.on = color;
222 	}
223 
224 	/**
225 	 * Returns the color representing OFF state for bit at specified index or
226 	 * default OFF value if there is no description.
227 	 *
228 	 * @param index int
229 	 *
230 	 * @return Color
231 	 */
232 	public Color getColorWhenOff(int index)
233 	{
234 		BitDescription bd = getBitDescription(index);
235 
236 		if (bd != null && bd.off!=null) {
237 			return bd.off;
238 		}
239 
240 		return defaultBitDesc.off;
241 	}
242 
243 	/**
244 	 * Returns the default color representing OFF state for bit used when no
245 	 * specific description is defined.
246 	 *
247 	 * @return Color
248 	 */
249 	public Color getDefaultColorWhenOff()
250 	{
251 		return defaultBitDesc.off;
252 	}
253 
254 	/**
255 	 * Sets new color indicating OFF state for the specified bit. If the index
256 	 * is larger or equal to size, method does nothing.
257 	 *
258 	 * @param index int
259 	 * @param color Color
260 	 */
261 	public void setColorWhenOff(int index, Color color)
262 	{
263 		BitDescription bd = getBitDescription(index);
264 
265 		if (bd != null) {
266 			bd.off = color;
267 		}
268 	}
269 
270 	/**
271 	 * Sets default color indicating OFF state.
272 	 *
273 	 * @param color Color
274 	 */
275 	public void setDefaultColorWhenOff(Color color)
276 	{
277 		defaultBitDesc.off = color;
278 	}
279 }
280 
281 /* __oOo__ */