1 | /*
|
---|
2 | * 09/05/2004
|
---|
3 | *
|
---|
4 | * IconGroup.java - Class encapsulating images used for RTextArea actions.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rtextarea;
|
---|
10 |
|
---|
11 | import java.awt.image.BufferedImage;
|
---|
12 | import java.io.File;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.net.URL;
|
---|
15 | import javax.imageio.ImageIO;
|
---|
16 | import javax.swing.Icon;
|
---|
17 | import javax.swing.ImageIcon;
|
---|
18 |
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * This class encapsulates the location, properties, etc. of an icon set used
|
---|
22 | * for an instance of <code>RTextArea</code>. If the location of the icon
|
---|
23 | * group is invalid in any way, any attempt to retrieve icons from an icon
|
---|
24 | * group will return <code>null</code>.
|
---|
25 | *
|
---|
26 | * @author Robert Futrell
|
---|
27 | * @version 0.5
|
---|
28 | */
|
---|
29 | public class IconGroup {
|
---|
30 |
|
---|
31 | private String path;
|
---|
32 | private boolean separateLargeIcons;
|
---|
33 | private String largeIconSubDir;
|
---|
34 | private String extension;
|
---|
35 | private String name;
|
---|
36 | private String jarFile;
|
---|
37 |
|
---|
38 | private static final String DEFAULT_EXTENSION = "gif";
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Creates an icon set without "large versions" of the icons.
|
---|
43 | *
|
---|
44 | * @param name The name of the icon group.
|
---|
45 | * @param path The directory containing the icon group.
|
---|
46 | */
|
---|
47 | public IconGroup(String name, String path) {
|
---|
48 | this(name, path, null);
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Constructor.
|
---|
54 | *
|
---|
55 | * @param name The name of the icon group.
|
---|
56 | * @param path The directory containing the icon group.
|
---|
57 | * @param largeIconSubDir The subdirectory containing "large versions" of
|
---|
58 | * the icons. If no subdirectory exists, pass in <code>null</code>.
|
---|
59 | */
|
---|
60 | public IconGroup(String name, String path, String largeIconSubDir) {
|
---|
61 | this(name, path, largeIconSubDir, DEFAULT_EXTENSION);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Constructor.
|
---|
67 | *
|
---|
68 | * @param name The name of the icon group.
|
---|
69 | * @param path The directory containing the icon group.
|
---|
70 | * @param largeIconSubDir The subdirectory containing "large versions" of
|
---|
71 | * the icons. If no subdirectory exists, pass in <code>null</code>.
|
---|
72 | * @param extension The extension of the icons (one of <code>gif</code>,
|
---|
73 | * <code>jpg</code>, or <code>png</code>).
|
---|
74 | */
|
---|
75 | public IconGroup(String name, String path, String largeIconSubDir,
|
---|
76 | String extension) {
|
---|
77 | this(name, path, largeIconSubDir, extension, null);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Constructor.
|
---|
83 | *
|
---|
84 | * @param name The name of the icon group.
|
---|
85 | * @param path The directory containing the icon group.
|
---|
86 | * @param largeIconSubDir The subdirectory containing "large versions" of
|
---|
87 | * the icons. If no subdirectory exists, pass in <code>null</code>.
|
---|
88 | * @param extension The extension of the icons (one of <code>gif</code>,
|
---|
89 | * <code>jpg</code>, or <code>png</code>).
|
---|
90 | * @param jar The Jar file containing the icons, or <code>null</code> if
|
---|
91 | * the icons are on the local file system. If a Jar is specified,
|
---|
92 | * the value of <code>path</code> must be a path in the Jar file.
|
---|
93 | * If this is not a valid Jar file, then no Jar file will be used,
|
---|
94 | * meaning all icons returned from this icon group will be
|
---|
95 | * <code>null</code>.
|
---|
96 | */
|
---|
97 | public IconGroup(String name, String path, String largeIconSubDir,
|
---|
98 | String extension, String jar) {
|
---|
99 | this.name = name;
|
---|
100 | this.path = path;
|
---|
101 | this.separateLargeIcons = (largeIconSubDir!=null);
|
---|
102 | this.largeIconSubDir = largeIconSubDir;
|
---|
103 | this.extension = extension!=null ? extension : DEFAULT_EXTENSION;
|
---|
104 | this.jarFile = jar;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns whether two icon groups are equal.
|
---|
110 | *
|
---|
111 | * @param o2 The object to check against.
|
---|
112 | * @return Whether <code>o2</code> represents the same icons as this icon
|
---|
113 | * group.
|
---|
114 | */
|
---|
115 | public boolean equals(Object o2) {
|
---|
116 | if (o2!=null && o2 instanceof IconGroup) {
|
---|
117 | IconGroup ig2 = (IconGroup)o2;
|
---|
118 | if (ig2.getName().equals(getName()) &&
|
---|
119 | separateLargeIcons==ig2.hasSeparateLargeIcons()) {
|
---|
120 | if (separateLargeIcons) {
|
---|
121 | if (!largeIconSubDir.equals(ig2.largeIconSubDir))
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 | return path.equals(ig2.path);
|
---|
125 | }
|
---|
126 | // If we got here, separateLargeIcons values weren't equal.
|
---|
127 | }
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Returns the icon from this icon group with the specified name.
|
---|
134 | *
|
---|
135 | * @param name The name of the icon. For example, if you want the icon
|
---|
136 | * specified in <code>new.gif</code>, this value should be
|
---|
137 | * <code>new</code>.
|
---|
138 | * @return The icon, or <code>null</code> if it could not be found or
|
---|
139 | * loaded.
|
---|
140 | * @see #getLargeIcon
|
---|
141 | */
|
---|
142 | public Icon getIcon(String name) {
|
---|
143 | Icon icon = getIconImpl(path + name + "." + extension);
|
---|
144 | // JDK 6.0 b74 returns icons with width/height==-1 in certain error
|
---|
145 | // cases (new ImageIcon(url) where url is not resolved?). We'll
|
---|
146 | // just return null in this case as Swing AbstractButtons throw
|
---|
147 | // exceptions when expected to paint an icon with width or height
|
---|
148 | // is less than 1.
|
---|
149 | if (icon!=null && (icon.getIconWidth()<1 || icon.getIconHeight()<1)) {
|
---|
150 | icon = null;
|
---|
151 | }
|
---|
152 | return icon;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Does the dirty work of loading an image.
|
---|
158 | *
|
---|
159 | * @param iconFullPath The full path to the icon, either on the local
|
---|
160 | * file system or in the Jar file, if this icon group represents
|
---|
161 | * icons in a Jar file.
|
---|
162 | * @return The icon.
|
---|
163 | */
|
---|
164 | private Icon getIconImpl(String iconFullPath) {
|
---|
165 | try {
|
---|
166 | if (jarFile==null) {
|
---|
167 | // First see if it's on our classpath (e.g. an icon in
|
---|
168 | // RText.jar, so we'd need to use the class loader).
|
---|
169 | URL url = getClass().getClassLoader().
|
---|
170 | getResource(iconFullPath);
|
---|
171 | if (url!=null)
|
---|
172 | return new ImageIcon(url);
|
---|
173 | // If not, see if it's a plain file on disk.
|
---|
174 | BufferedImage image = ImageIO.read(new File(iconFullPath));
|
---|
175 | return image!=null ? new ImageIcon(image) : null;
|
---|
176 | }
|
---|
177 | else { // If it's in a Jar, create a URL and grab it.
|
---|
178 | URL url = new URL("jar:file:///" +
|
---|
179 | jarFile + "!/" + iconFullPath);
|
---|
180 | //System.err.println("***** " + url.toString());
|
---|
181 | return new ImageIcon(url);
|
---|
182 | }
|
---|
183 | } catch (IOException ioe) {
|
---|
184 | return null;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Returns the large icon from this icon group with the specified name.
|
---|
191 | * If this icon group does not have large icons, <code>null</code> is
|
---|
192 | * returned.
|
---|
193 | *
|
---|
194 | * @param name The name of the icon. For example, if you want the icon
|
---|
195 | * specified in <code>new.gif</code>, this value should be
|
---|
196 | * <code>new</code>.
|
---|
197 | * @return The icon, or <code>null</code> if it could not be found or
|
---|
198 | * loaded.
|
---|
199 | * @see #getLargeIcon
|
---|
200 | */
|
---|
201 | public Icon getLargeIcon(String name) {
|
---|
202 | return getIconImpl(path + largeIconSubDir + "/" +
|
---|
203 | name + "." + extension);
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Returns the name of this icon group.
|
---|
209 | *
|
---|
210 | * @return This icon group's name.
|
---|
211 | */
|
---|
212 | public String getName() {
|
---|
213 | return name;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Returns whether a separate directory for the large icons exists.
|
---|
219 | *
|
---|
220 | * @return Whether a directory containing "large versions" ov the icons
|
---|
221 | * exists.
|
---|
222 | * @see #getLargeIcon(String)
|
---|
223 | */
|
---|
224 | public boolean hasSeparateLargeIcons() {
|
---|
225 | return separateLargeIcons;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Overridden since we also override {@link #equals(Object)}, to honor
|
---|
231 | * the invariant that equal objects must have equal hashcodes. This also
|
---|
232 | * keeps FindBugs happy.
|
---|
233 | */
|
---|
234 | public int hashCode() {
|
---|
235 | return getName().hashCode();
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | } |
---|