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;
21  
22  import com.cosylab.gui.components.util.IconHelper;
23  
24  import java.util.Vector;
25  
26  import javax.swing.Icon;
27  
28  
29  /**
30   * This class extends <code>javax.swing.JLabel</code> so that it allows display
31   * of multiple icons. It maintains internal list of icons that associates
32   * integer values with <code>Icon</code> objects. This index is guaranteed to
33   * be valid even if the desired icon could not be found, is not valid or is
34   * null. In such cases the corresponding icon will not be displayed.
35   * 
36   * <p>
37   * Icons can be provided by either the calling class via the
38   * <code>javax.swing.Icon</code> object or they can be loaded from the
39   * projects resource automatically. Two methods are provided to add the icons:
40   * <code>addIcon, addIcons</code> The later is preffered when adding multiple
41   * icons at the same time. <br>
42   * Use the <code>setIconByIndex</code> method to set which icon should be displayed.<br>
43   * The disabled state icon of the JLabel is not overriden and can be still
44   * specified.
45   * </p>
46   * 
47   * <p></p>
48   *
49   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
50   * @version $id$
51   */
52  public class MultiIconLabel extends javax.swing.JLabel
53  {
54  
55  	private static final long serialVersionUID = 1L;
56  	/** List storing all icons */
57  	private final Vector<Icon> iconList = new Vector<Icon>();
58  
59  	/**
60  	 * Default constructor for MultiIconLabel.
61  	 */
62  	public MultiIconLabel()
63  	{
64  		super();
65  	}
66  
67  	/**
68  	 * Constructs a MultiIconLabel and loads the icons from the specified
69  	 * resource names.
70  	 * 
71  	 * <p></p>
72  	 *
73  	 * @param resourceNames String[]
74  	 */
75  	public MultiIconLabel(String[] resourceNames)
76  	{
77  		this();
78  		addIcons(resourceNames);
79  	}
80  
81  	/**
82  	 * Constructs a MultiIconLabel and loads the icons from the specified
83  	 * resource names. Also sets the horizontal alignement of the text.
84  	 * 
85  	 * <p></p>
86  	 *
87  	 * @param resourceNames String[]
88  	 * @param horizontalAlignement int.
89  	 */
90  	public MultiIconLabel(String[] resourceNames, int horizontalAlignement)
91  	{
92  		this();
93  		addIcons(resourceNames);
94  		setHorizontalAlignment(horizontalAlignement);
95  	}
96  
97  	/**
98  	 * Constructs a MultiIconLabel and adds the icons to the icon list.
99  	 *
100 	 * @param icons Icon[]
101 	 */
102 	public MultiIconLabel(Icon[] icons)
103 	{
104 		this();
105 		addIcons(icons);
106 	}
107 
108 	/**
109 	 * Constructs a MultiIconLabel and adds the icons to the icon list. Also
110 	 * sets the horizontal alignement of the text.
111 	 * 
112 	 * <p></p>
113 	 *
114 	 * @param icons javax.swing.Icon[]
115 	 * @param horizontalAlignment int
116 	 */
117 	public MultiIconLabel(javax.swing.Icon[] icons, int horizontalAlignment)
118 	{
119 		this(icons);
120 		setHorizontalAlignment(horizontalAlignment);
121 	}
122 
123 	/**
124 	 * Appends the icon specified by resourceName to the end of internal list
125 	 * of icons. This method calls addIcon(Icon) and loadFromResource(String).
126 	 * If resourceName does not exist in the projects resources, method adds
127 	 * empty icon.
128 	 * 
129 	 * <p></p>
130 	 *
131 	 * @param resourceName java.lang.String
132 	 */
133 	public void addIcon(String resourceName)
134 	{
135 		addIcon(loadFromResource(resourceName));
136 	}
137 
138 	/**
139 	 * Appends the icon to the list of icons.
140 	 * 
141 	 * <p></p>
142 	 *
143 	 * @param icon javax.swing.Icon
144 	 */
145 	public void addIcon(Icon icon)
146 	{
147 		iconList.add(icon);
148 	}
149 
150 	/**
151 	 * Appends all icons specified by resourceNames to the end of internal list
152 	 * of icons. For adding multiple icons, this method outperforms individual
153 	 * calls to <code>addIcon</code> method.
154 	 * 
155 	 * <p>
156 	 * After calling this method, the list of icons will grow by the size of
157 	 * the array, regardless of its contentrs. If any of the icons is null or
158 	 * of invalid type, the associated icon will not be displayed.
159 	 * </p>
160 	 * 
161 	 * <p></p>
162 	 *
163 	 * @param resourceNames java.lang.String[]
164 	 */
165 	public void addIcons(String[] resourceNames)
166 	{
167 		if (resourceNames != null) {
168 			for (int i = 0; i < resourceNames.length; i++) {
169 				addIcon(resourceNames[i]);
170 			}
171 		}
172 	}
173 
174 	/**
175 	 * Appends all icons specified by icons parameter to the internal list of
176 	 * icons. For adding multiple icons, this method outperforms individual
177 	 * calls to <code>addIcon</code> method.
178 	 * 
179 	 * <p>
180 	 * After calling this method, the list of icons will grow by the size of
181 	 * the array, regardless of its contentrs. If any of the icons is null or
182 	 * of invalid type, the associated icon will not be displayed. <br>
183 	 * </p>
184 	 *
185 	 * @param icons javax.swing.Icon[]
186 	 */
187 	public void addIcons(Icon[] icons)
188 	{
189 		if (icons != null) {
190 			for (int i = 0; i < icons.length; i++) {
191 				addIcon(icons[i]);
192 			}
193 		}
194 	}
195 
196 	/**
197 	 * Returns the Icon resource assigned to the specified index. If the icon
198 	 * has not been defined, returns null.
199 	 * 
200 	 * <p></p>
201 	 *
202 	 * @param index int
203 	 *
204 	 * @return javax.swing.Icon
205 	 */
206 	public Icon getIconByIndex(int index)
207 	{
208 		if ((index >= 0) && (index < iconList.size())) {
209 			return iconList.get(index);
210 		}
211 
212 		return null;
213 	}
214 
215 	/**
216 	 * Loads the Icon from the projects resource. Returns null if the resource
217 	 * does not exist.
218 	 * 
219 	 * <p></p>
220 	 *
221 	 * @param resourceName java.lang.String
222 	 *
223 	 * @return javax.swing.Icon
224 	 */
225 	protected Icon loadFromResource(String resourceName)
226 	{
227 		return IconHelper.createIcon(resourceName);
228 	}
229 
230 	/**
231 	 * Displays the icon specified by index. If the icon with this index has
232 	 * not been defined, no icon will be displayed.
233 	 * 
234 	 * <p></p>
235 	 *
236 	 * @param index int
237 	 */
238 	public void setIconByIndex(int index)
239 	{
240 		setIcon(getIconByIndex(index));
241 	}
242 
243 	/**
244 	 * Returns number of available icons.
245 	 *
246 	 * @return Number of available icons.
247 	 */
248 	public int getIconCount()
249 	{
250 		return iconList.size();
251 	}
252 }
253 
254 /* __oOo__ */