Debian Codegraph @

src/com/jidesoft/icons/IconSetManager.java

raw
1 package com.jidesoft.icons;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 /**
9 * <code>IconSetManager</code> contains all the JIDE icon sets in one class so that you can access all of them from one
10 * single place and switch to different icon set just by calling {@link #setActiveIconSetName(String)}.
11 * <p/>
12 * In order to use IconSetManager, you need to define and register the JIDE icon set first. You can do something like
13 * this.
14 * <code><pre>
15 * public static IconSetManager ICON_SET_MANAGER = new IconSetManager();
16 * <p/>
17 * static {
18 * ICON_SET_MANAGER.add("vista", new int[]{16, 24, 32, 48, 64, 96, 128, 256}, "/com/jidesoft/icons/vista");
19 * ICON_SET_MANAGER.add("xp", new int[]{16, 24, 32, 48, 64}, "/com/jidesoft/icons/xp");
20 * // add more if you have. Right now JIDE only provides one icon set but we will add more
21 * }
22 * </pre></code>
23 * Later on if you want to use it, for example, to get a File-New icon, you just call
24 * <code><pre>
25 * ICON_SET_MANAGER.getImageIcon(IconSet.File.NEW, size);
26 * </pre></code>
27 * The size is the size you want such as 16, 24, 32, 48, or 64 depending on what sizes are available.
28 */
29 public class IconSetManager {
30 private Map<String, IconSet> _availableStyles;
31 private String _activeIconSetName = null;
32 private IconSet _activeIconSet = null;
33
34 /**
35 * finds the icon set if it is available.
36 *
37 * @param iconSetName the icon set name
38 *
39 * @return the icon set. Null if not found.
40 */
41 public IconSet findIconSet(String iconSetName) {
42 if (_availableStyles == null) {
43 return null;
44 }
45 return _availableStyles.get(iconSetName);
46 }
47
48 /**
49 * Gets the active icon set name. The active icon set will be used when you call {@link #getImageIcon(String)}
50 * method.
51 *
52 * @return the active icon set name.
53 */
54 public String getActiveIconSetName() {
55 return _activeIconSetName;
56 }
57
58 /**
59 * gets the active icon set.
60 *
61 * @return the active icon set.
62 */
63 public IconSet getActiveIconSet() {
64 return _activeIconSet;
65 }
66
67 public void add(String name, int[] sizes, String packageName) {
68 if (_availableStyles == null) {
69 _availableStyles = new HashMap<String, IconSet>();
70 }
71 _availableStyles.put(name, new IconSet(name, sizes, packageName));
72 if (_availableStyles.size() == 1) {
73 setActiveIconSetName(name);
74 }
75 }
76
77 public void remove(String name) {
78 if (_availableStyles != null) {
79 _availableStyles.remove(name);
80 }
81 }
82
83 /**
84 * Sets the active icon set name. If the icon set is not found, IllegalArgumentException will be thrown.
85 *
86 * @param activeIconSetName the new active icon set name.
87 *
88 * @throws IllegalArgumentException if the icon set is not found by that name.
89 */
90 public void setActiveIconSetName(String activeIconSetName) {
91 IconSet iconSet = findIconSet(activeIconSetName);
92 if (iconSet == null) {
93 throw new IllegalArgumentException("Icon set \"" + activeIconSetName + "\" not found");
94 }
95 _activeIconSet = iconSet;
96 _activeIconSetName = activeIconSetName;
97 }
98
99 /**
100 * Gets the ImageIcon.
101 *
102 * @param iconName the icon name as defined in IconSet.
103 *
104 * @return the ImageIcon.
105 */
106 public ImageIcon getImageIcon(String iconName) {
107 return getImageIcon(iconName, 16);
108 }
109
110 /**
111 * Gets the ImageIcon.
112 *
113 * @param iconName the icon name as defined in IconSet.
114 * @param size the icon size. If the size is not available, it will find the closest size that is larger than
115 * the requested size.
116 *
117 * @return the ImageIcon.
118 */
119 public ImageIcon getImageIcon(String iconName, int size) {
120 String packageName = _activeIconSet.getPackageName();
121 int actualSize = _activeIconSet.getNextAvailableSize(size);
122 String fullIconName = packageName + "/png/" + actualSize + "x" + actualSize + "/" + iconName;
123 ImageIcon icon = IconsFactory.getImageIcon(IconSetManager.class, fullIconName);
124 if (actualSize == size) {
125 return icon;
126 }
127 else {
128 return IconsFactory.getScaledImage(null, icon, size, size);
129 }
130 }
131
132 /**
133 * Gets the ImageIcon.
134 *
135 * @param iconName the icon name as defined in IconSet.
136 * @param size the icon size. If the size is not available, it will find the closest size that is larger
137 * than the requested size.
138 * @param overlayIconName the overlay icon name as defined in IconSet.
139 * @param location the location as defined in SwingConstants - CENTER, NORTH, SOUTH, WEST, EAST, NORTH_EAST,
140 * NORTH_WEST, SOUTH_WEST and SOUTH_EAST.
141 *
142 * @return the ImageIcon.
143 */
144 public ImageIcon getOverlayImageIcon(String iconName, int size, String overlayIconName, int location) {
145 return getOverlayImageIcon(iconName, size, overlayIconName, location, new Insets(0, 0, 0, 0));
146 }
147
148 /**
149 * Gets the ImageIcon with an overlay icon on it.
150 *
151 * @param iconName the icon name as defined in IconSet.
152 * @param size the icon size. If the size is not available, it will find the closest size that is larger
153 * than the requested size.
154 * @param overlayIconName the overlay icon name as defined in IconSet. The icons in the Overlay category are half
155 * the size of the regular icons. So this method will take the middle portion of the overlay
156 * icon specified in the overlayIconName and use it as the overlay icon.
157 * @param location the location as defined in SwingConstants - CENTER, NORTH, SOUTH, WEST, EAST, NORTH_EAST,
158 * NORTH_WEST, SOUTH_WEST and SOUTH_EAST.
159 * @param insets the margin of the overlay icon to the border of the icon.
160 *
161 * @return the ImageIcon.
162 */
163 public ImageIcon getOverlayImageIcon(String iconName, int size, String overlayIconName, int location, Insets insets) {
164 ImageIcon icon = getImageIcon(iconName, size);
165 if (icon == null) {
166 return null;
167 }
168 ImageIcon overlay = getImageIcon(overlayIconName, size);
169 if (overlay == null) {
170 return icon;
171 }
172
173 // grab the middle portion of the overlay icon only
174 overlay = IconsFactory.getIcon(null, overlay, size / 4, size / 4, size / 2, size / 2);
175
176 return IconsFactory.getOverlayIcon(null, icon, overlay, location, insets);
177 }
178
179 /**
180 * Gets the ImageIcon with an overlay icon on it.
181 *
182 * @param iconName the icon name as defined in IconSet.
183 * @param size the icon size. If the size is not available, it will find the closest size that is larger
184 * than the requested size.
185 * @param overlayIconName the overlay icon name as defined in IconSet. The overlay icon doesn't have to be in the
186 * overlay category. It can be any regular icon. Of course the size of the overlay icon
187 * should be smaller than the size of the regular icon.
188 * @param overlayIconSize the size of the overlay icon.
189 * @param location the location as defined in SwingConstants - CENTER, NORTH, SOUTH, WEST, EAST, NORTH_EAST,
190 * NORTH_WEST, SOUTH_WEST and SOUTH_EAST.
191 * @param insets the margin of the overlay icon to the border of the icon.
192 *
193 * @return the ImageIcon.
194 */
195 public ImageIcon getOverlayImageIcon(String iconName, int size, String overlayIconName, int overlayIconSize, int location, Insets insets) {
196 ImageIcon icon = getImageIcon(iconName, size);
197 if (icon == null) {
198 return null;
199 }
200 ImageIcon overlay = getImageIcon(overlayIconName, overlayIconSize);
201 if (overlay == null) {
202 return icon;
203 }
204
205 return IconsFactory.getOverlayIcon(null, icon, overlay, location, insets);
206 }
207
208 // public static void main(String[] args) {
209 // IconSetManager manager = new IconSetManager();
210 // manager.add("xp", new int[]{16, 24, 32, 48, 64}, "/com/jidesoft/icons/xp");
211 // JFrame frame = new JFrame();
212 // frame.add(new JLabel(manager.getImageIcon(IconSet.Direction.BOTTOM, 64)));
213 // frame.pack();
214 // frame.setVisible(true);
215 // }
216 }