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  import java.awt.Color;
22  import java.util.HashMap;
23  
24  import javax.swing.Icon;
25  
26  /**
27   * Creates LED icon for given color.
28   *  
29   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
30   * @version $id$
31   */
32  public class LedIconFactory {
33  
34  	private static HashMap<Color,Icon> cache;
35  	
36  	
37  	/**
38  	 * Returns led icon from cache. New instance will be created if it does not
39  	 * exist already.
40  	 * 
41  	 * @param color Color of led.
42  	 * @return Instance of led icon.
43  	 */
44  	private static final Icon getCached(Color color) {
45  		if (cache == null) {
46  			cache = new HashMap<Color, Icon>();
47  		}
48  		Icon ic = cache.get(color);
49  		if (ic == null) {
50  			ic = new CustomLedIcon(color);
51  			cache.put(color, ic);
52  		}
53  		return ic;
54  	}
55  
56  	/**
57  	 * Creates the icon from <code>java.awt.Color</code>. See the class overview
58  	 * for details.
59  	 * 
60  	 * @param color color of the icon.
61  	 * @return Icon always an instance of an icon 
62  	 */
63  	public static Icon createIcon(Color color) {
64  		return getCached(color);
65  	}
66  
67  }