Source Code Cross Referenced for IconsFactory.java in  » Swing-Library » jide-common » com » jidesoft » icons » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Swing Library » jide common » com.jidesoft.icons 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)IconsFactory.java
003:         *
004:         * Copyright 2002 JIDE Software Inc. All rights reserved.
005:         */
006:        package com.jidesoft.icons;
007:
008:        import javax.swing.*;
009:        import java.awt.*;
010:        import java.awt.image.BufferedImage;
011:        import java.io.*;
012:        import java.lang.reflect.Field;
013:        import java.util.HashMap;
014:        import java.util.Map;
015:        import java.util.StringTokenizer;
016:
017:        /**
018:         * <code>IconsFactory</code> provides a consistent way to access
019:         * icon resource in any application.
020:         * <p/>
021:         * Any application usually need to access image files. One way to do it is to put
022:         * those image files in the installation and access them use direct file access. However
023:         * this is not a good way because you have to know the full path to the image file. So
024:         * a better way that most Java applications take is to bundle the image files with in the jar
025:         * and use class loader to load them.
026:         * <p/>
027:         * For example, if a class Foo needs to access image files foo.gif and bar.png, we put the image files right below
028:         * the source code under icons subfolder. See an example directory structure below.
029:         * <pre>
030:         * /src/com/jidesoft/Foo.java
031:         *                  /icons/foo.gif
032:         *                  /icons/bar.png
033:         * </pre>
034:         * When you compile the java class, you copy those images to class output directory like this.
035:         * <pre>
036:         * /classes/com/jidesoft/Foo.class
037:         *                      /icons/foo.gif
038:         *                      /icons/bar.png
039:         * </pre>
040:         * Notes:<i>
041:         * In IntelliJ IDEA's "Compile" tab of "Project Property" dialog, there is a way to set "Resource Pattern". Here is
042:         * the setting on my computer - "?*.properties;?*.xml;?*.html;?*.tree;?*.gif;?*.png;?*.jpeg;?*.jpg;?*.vm;?*.xsd;?*.ilayout;?*.gz;?*.txt"
043:         * for your reference. If so, all your images will get copies automatically to class output folder. Although I haven't tried,
044:         * I believe most Java IDEs have the same or similar feature. This feature will make the usage of IconsFactory much easier.
045:         * </i>
046:         * <p/>
047:         * If you setup directory structure as above, you can now use IconsFactory to access the images like this.
048:         * <pre><code>
049:         * ImageIcon icon = IconsFactory.get(Foo.class, "icons/foo.gif");
050:         * </code></pre>
051:         * IconsFactory will cache the icon for you. So next time if you get the same icon,
052:         * it will get from cache instead of reading from disk again.
053:         * <p/>
054:         * There are a few methods on IconsFactory to create difference variation from the original icon.
055:         * For example, {@link #getDisabledImageIcon(Class,String)} will get the imaage icon with disabled effect.
056:         * <p/>
057:         * We also suggest you to use the template below to create a number of IconsFactory classes in your application.
058:         * The idea is that you should have one for each functional area so that all your image files can be grouped
059:         * into each functional area. All images used in that functional area should be put under the folder
060:         * where this IconsFactory is. Here is an template.
061:         * <pre><code>
062:         * class TemplateIconsFactory {
063:         *    public static class Group1 {
064:         *        public final static String IMAGE1 = "icons/image11.png";
065:         *        public final static String IMAGE2 = "icons/image12.png";
066:         *        public final static String IMAGE3 = "icons/image13.png";
067:         *    }
068:         * <p/>
069:         *    public static class Group2 {
070:         *        public final static String IMAGE1 = "icons/image21.png";
071:         *        public final static String IMAGE2 = "icons/image22.png";
072:         *        public final static String IMAGE3 = "icons/image23.png";
073:         *    }
074:         * <p/>
075:         *    public static ImageIcon getImageIcon(String name) {
076:         *        if (name != null)
077:         *            return IconsFactory.getImageIcon(TemplateIconsFactory.class, name);
078:         *        else
079:         *            return null;
080:         *    }
081:         * <p/>
082:         *    public static void main(String[] argv) {
083:         *        IconsFactory.generateHTML(TemplateIconsFactory.class);
084:         *    }
085:         * }
086:         * </code></pre>
087:         * In your own IconsFactory, you can further divide images into different groups.
088:         * The example above has two groups. There is also a convenient method getImageIcon()
089:         * which takes just the icon name.
090:         * <p/>
091:         * In the template, we defined the image names as constants. When you have a lot of images,
092:         * it's hard to remember all of them when writing code. If using the IconsFactory above, you can
093:         * use
094:         * <pre><code>
095:         * ImageIcon icon = TemplateIconsFactory.getImageIcon(TemplateIconsFactory.Group1.IMAGE1);
096:         * </code></pre>
097:         * without saying the actual image file name. With the help of intelli-sense (or code completion) feature in most Java IDE,
098:         * you will find it is much easier to find the icons you want. You can refer to JIDE Components Developer Guide
099:         * to see a screenshot of what it looks like in IntelliJ IDEA.
100:         * <p/>
101:         * You probably also notice this is a main() method in this template. You can run it. When you run, you will
102:         * see a message printed out like this.
103:         * <pre><code>
104:         * "File is generated at "... some directory ...\com.jidesoft.icons.TemplateIconsFactory.html". Please copy it to the same directory as TemplateIconsFactory.java"
105:         * </code></pre>
106:         * if you follow the instrcution and copy the html file to the same location as the source code and open the html, you will
107:         * see the all image files defined in this IconsFactory are listed nicely in the page.
108:         */
109:        public class IconsFactory {
110:
111:            static Map<String, ImageIcon> icons = new HashMap<String, ImageIcon>();
112:            static Map<String, ImageIcon> disableIcons = new HashMap<String, ImageIcon>();
113:            static Map<String, ImageIcon> enhancedIcons = new HashMap<String, ImageIcon>();
114:
115:            public static ImageIcon EMPTY_ICON = new ImageIcon() {
116:                @Override
117:                public int getIconHeight() {
118:                    return 16;
119:                }
120:
121:                @Override
122:                public int getIconWidth() {
123:                    return 16;
124:                }
125:
126:                @Override
127:                public synchronized void paintIcon(Component c, Graphics g,
128:                        int x, int y) {
129:                }
130:            };
131:
132:            /**
133:             * Gets ImageIcon by passing class and a relative image file path.
134:             * <p/>
135:             * Please note, getImageIcon will print out error message to stderr if image is not found.
136:             * The reason we did so is because we want you to make sure all image files are there in
137:             * your application. If you ever see the error message, you should correct it before shipping the product.
138:             * But if you just want to test if the image file is there, you don't want any error message print out.
139:             * If so, you can use {@link #findImageIcon(Class,String)} method. It will throw IOException
140:             * when image is not found.
141:             *
142:             * @param clazz    the Class<?>
143:             * @param fileName relative file name
144:             * @return the ImageIcon
145:             */
146:            public static ImageIcon getImageIcon(Class<?> clazz, String fileName) {
147:                String id = clazz.getName() + ":" + fileName;
148:                Icon saved = icons.get(id);
149:                if (saved != null)
150:                    return (ImageIcon) saved;
151:                else {
152:                    ImageIcon icon = createImageIcon(clazz, fileName);
153:                    icons.put(id, icon);
154:                    return icon;
155:                }
156:            }
157:
158:            /**
159:             * Gets ImageIcon by passing class and a relative image file path.
160:             *
161:             * @param clazz    the Class<?>
162:             * @param fileName relative file name
163:             * @return the ImageIcon
164:             * @throws IOException when image file is not found.
165:             */
166:            public static ImageIcon findImageIcon(Class<?> clazz,
167:                    String fileName) throws IOException {
168:                String id = clazz.getName() + ":" + fileName;
169:                ImageIcon saved = icons.get(id);
170:                if (saved != null)
171:                    return saved;
172:                else {
173:                    ImageIcon icon = createImageIconWithException(clazz,
174:                            fileName);
175:                    icons.put(id, icon);
176:                    return icon;
177:                }
178:            }
179:
180:            /**
181:             * Gets a disabled version of ImageIcon by passing class and a relative image file path.
182:             *
183:             * @param clazz
184:             * @param fileName
185:             * @return the ImageIcon
186:             */
187:            public static ImageIcon getDisabledImageIcon(Class<?> clazz,
188:                    String fileName) {
189:                String id = clazz.getName() + ":" + fileName;
190:                ImageIcon saved = disableIcons.get(id);
191:                if (saved != null)
192:                    return saved;
193:                else {
194:                    ImageIcon icon = createGrayImage(getImageIcon(clazz,
195:                            fileName));
196:                    disableIcons.put(id, icon);
197:                    return icon;
198:                }
199:            }
200:
201:            /**
202:             * Gets a brighter ImageIcon by passing class and a relative image file path.
203:             *
204:             * @param clazz
205:             * @param fileName
206:             * @return the ImageIcon
207:             */
208:            public static ImageIcon getBrighterImageIcon(Class<?> clazz,
209:                    String fileName) {
210:                String id = clazz.getName() + ":" + fileName;
211:                ImageIcon saved = enhancedIcons.get(id);
212:                if (saved != null)
213:                    return saved;
214:                else {
215:                    ImageIcon icon = createBrighterImage(getImageIcon(clazz,
216:                            fileName));
217:                    enhancedIcons.put(id, icon);
218:                    return icon;
219:                }
220:            }
221:
222:            /**
223:             * Creates a gray version from an input image. Usually gray icon indicates disabled.
224:             * If input image is null, a blank ImageIcon will be returned.
225:             *
226:             * @param image image
227:             * @return gray version of the image
228:             */
229:            public static ImageIcon createGrayImage(Image image) {
230:                if (image == null)
231:                    return EMPTY_ICON;
232:                return new ImageIcon(GrayFilter.createDisabledImage(image));
233:            }
234:
235:            /**
236:             * Creates a gray version from an input ImageIcon. Usually gray icon indicates disabled.
237:             * If input icon is null, a blank ImageIcon will be returned.
238:             *
239:             * @param icon image
240:             * @return gray version of the image
241:             */
242:            private static ImageIcon createGrayImage(ImageIcon icon) {
243:                if (icon == null)
244:                    return EMPTY_ICON;
245:                return new ImageIcon(GrayFilter.createDisabledImage(icon
246:                        .getImage()));
247:            }
248:
249:            /**
250:             * Creates a gray version from an input image. Usually gray icon indicates disabled.
251:             * If input icon is null, a blank ImageIcon will be returned.
252:             *
253:             * @param c    The component to get properties useful for painting, e.g. the foreground or background color.
254:             * @param icon icon
255:             * @return gray version of the image
256:             */
257:            public static ImageIcon createGrayImage(Component c, Icon icon) {
258:                if (icon == null)
259:                    return EMPTY_ICON;
260:
261:                int w = icon.getIconWidth(), h = icon.getIconHeight();
262:                if ((w == 0) || (h == 0))
263:                    return EMPTY_ICON;
264:
265:                BufferedImage image = new BufferedImage(icon.getIconWidth(),
266:                        icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
267:                icon.paintIcon(c, image.getGraphics(), 0, 0);
268:                return new ImageIcon(GrayFilter.createDisabledImage(image));
269:            }
270:
271:            /**
272:             * Creates a brighter image from an input image.
273:             * If input image is null, a blank ImageIcon will be returned.
274:             *
275:             * @param image image
276:             * @return dimmed version of the image
277:             */
278:            public static ImageIcon createBrighterImage(Image image) {
279:                if (image == null)
280:                    return EMPTY_ICON;
281:                return new ImageIcon(ColorFilter.createBrighterImage(image));
282:            }
283:
284:            /**
285:             * Creates a gray version from an input image. Usually gray icon indicates disabled.
286:             * If input icon is null, a blank ImageIcon will be returned.
287:             *
288:             * @param c    The component to get properties useful for painting, e.g. the foreground or background color.
289:             * @param icon icon
290:             * @return gray version of the image
291:             */
292:            public static ImageIcon createBrighterImage(Component c, Icon icon) {
293:                if (icon == null)
294:                    return EMPTY_ICON;
295:                BufferedImage image = new BufferedImage(icon.getIconWidth(),
296:                        icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
297:                icon.paintIcon(c, image.getGraphics(), 0, 0);
298:                return new ImageIcon(ColorFilter.createBrighterImage(image));
299:            }
300:
301:            /**
302:             * Creates a brighten version from an input ImageIcon.
303:             * If input icon is null, a blank ImageIcon will be returned.
304:             *
305:             * @param icon image
306:             * @return dimmed version of the image
307:             */
308:            private static ImageIcon createBrighterImage(ImageIcon icon) {
309:                if (icon == null)
310:                    return EMPTY_ICON;
311:                return new ImageIcon(ColorFilter.createBrighterImage(icon
312:                        .getImage()));
313:            }
314:
315:            /**
316:             * Creates a gray version from an input image. Usually gray icon indicates disabled.
317:             * If input image is null, a blank ImageIcon will be returned.
318:             *
319:             * @param image image
320:             * @return gray version of the image
321:             */
322:            public static ImageIcon createNegativeImage(Image image) {
323:                if (image == null)
324:                    return EMPTY_ICON;
325:                return new ImageIcon(MaskFilter.createNegativeImage(image));
326:            }
327:
328:            /**
329:             * Creates a gray version from an input ImageIcon. Usually gray icon indicates disabled.
330:             *
331:             * @param icon image
332:             * @return gray version of the image
333:             */
334:            private static ImageIcon createNegativeImage(ImageIcon icon) {
335:                return new ImageIcon(MaskFilter.createNegativeImage(icon
336:                        .getImage()));
337:            }
338:
339:            /**
340:             * Creates a version from an input image which replaces one color with another color.
341:             *
342:             * @param c        The component to get properties useful for painting, e.g. the foreground or background color.
343:             * @param icon     icon
344:             * @param oldColor the old color to be replaced.
345:             * @param newColor the new color that will replace the old color.
346:             * @return the image after replacing the color.
347:             */
348:            public static ImageIcon createMaskImage(Component c, Icon icon,
349:                    Color oldColor, Color newColor) {
350:                BufferedImage image = new BufferedImage(icon.getIconWidth(),
351:                        icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
352:                icon.paintIcon(c, image.getGraphics(), 0, 0);
353:                return new ImageIcon(MaskFilter.createImage(image, oldColor,
354:                        newColor));
355:            }
356:
357:            /**
358:             * Creates a negative version from an input black image which basically replaces black pixel with white pixel.
359:             *
360:             * @param c    The component to get properties useful for painting, e.g. the foreground or background color.
361:             * @param icon icon
362:             * @return the negative version of the image
363:             */
364:            public static ImageIcon createNegativeImage(Component c, Icon icon) {
365:                BufferedImage image = new BufferedImage(icon.getIconWidth(),
366:                        icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
367:                icon.paintIcon(c, image.getGraphics(), 0, 0);
368:                return new ImageIcon(MaskFilter.createNegativeImage(image));
369:            }
370:
371:            private static void doPrivileged(final Runnable doRun) {
372:                java.security.AccessController
373:                        .doPrivileged(new java.security.PrivilegedAction() {
374:                            public Object run() {
375:                                doRun.run();
376:                                return null;
377:                            }
378:                        });
379:            }
380:
381:            private static Object makeImageIcon(final Class<?> baseClass,
382:                    final String gifFile) {
383:                return new UIDefaults.LazyValue() {
384:                    public Object createValue(UIDefaults table) {
385:                        /* Copy resource into a byte array.  This is
386:                         * necessary because several browsers consider
387:                         * Class<?>.getResource a security risk because it
388:                         * can be used to load additional classes.
389:                         * Class<?>.getResourceAsStream just returns raw
390:                         * bytes, which we can convert to an image.
391:                         */
392:                        final byte[][] buffer = new byte[1][];
393:                        doPrivileged(new Runnable() {
394:                            public void run() {
395:                                try {
396:                                    InputStream resource = baseClass
397:                                            .getResourceAsStream(gifFile);
398:                                    if (resource == null) {
399:                                        return;
400:                                    }
401:                                    BufferedInputStream in = new BufferedInputStream(
402:                                            resource);
403:                                    ByteArrayOutputStream out = new ByteArrayOutputStream(
404:                                            1024);
405:                                    buffer[0] = new byte[1024];
406:                                    int n;
407:                                    while ((n = in.read(buffer[0])) > 0) {
408:                                        out.write(buffer[0], 0, n);
409:                                    }
410:                                    in.close();
411:                                    out.flush();
412:                                    buffer[0] = out.toByteArray();
413:                                } catch (IOException ioe) {
414:                                    System.err.println(ioe.toString());
415:                                }
416:                            }
417:                        });
418:
419:                        if (buffer[0] == null) {
420:                            System.err.println(baseClass.getName() + "/"
421:                                    + gifFile + " not found.");
422:                            return null;
423:                        }
424:                        if (buffer[0].length == 0) {
425:                            System.err.println("Warning: " + gifFile
426:                                    + " is zero-length");
427:                            return null;
428:                        }
429:
430:                        return new ImageIcon(Toolkit.getDefaultToolkit()
431:                                .createImage(buffer[0]));
432:                    }
433:                };
434:            }
435:
436:            private static ImageIcon createImageIcon(final Class<?> baseClass,
437:                    final String file) {
438:                try {
439:                    return createImageIconWithException(baseClass, file);
440:                } catch (IOException e) {
441:                    System.err.println(e.getLocalizedMessage());
442:                    return null;
443:                }
444:            }
445:
446:            private static ImageIcon createImageIconWithException(
447:                    final Class<?> baseClass, final String file)
448:                    throws IOException {
449:                InputStream resource = baseClass.getResourceAsStream(file);
450:
451:                final byte[][] buffer = new byte[1][];
452:                try {
453:                    if (resource == null) {
454:                        throw new IOException("File " + file + " not found");
455:                    }
456:                    BufferedInputStream in = new BufferedInputStream(resource);
457:                    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
458:
459:                    buffer[0] = new byte[1024];
460:                    int n;
461:                    while ((n = in.read(buffer[0])) > 0) {
462:
463:                        out.write(buffer[0], 0, n);
464:                    }
465:                    in.close();
466:                    out.flush();
467:                    buffer[0] = out.toByteArray();
468:                } catch (IOException ioe) {
469:                    throw ioe;
470:                }
471:
472:                if (buffer[0] == null) {
473:                    throw new IOException(baseClass.getName() + "/" + file
474:                            + " not found.");
475:                }
476:                if (buffer[0].length == 0) {
477:                    throw new IOException("Warning: " + file
478:                            + " is zero-length");
479:                }
480:
481:                return new ImageIcon(Toolkit.getDefaultToolkit().createImage(
482:                        buffer[0]));
483:            }
484:
485:            // Using ImageIO approach results in exception like this.
486:            //    Exception in thread "main" java.lang.NullPointerException
487:            //            at com.ctreber.aclib.image.ico.ICOReader.getICOEntry(ICOReader.java:120)
488:            //            at com.ctreber.aclib.image.ico.ICOReader.read(ICOReader.java:89)
489:            //            at javax.imageio.ImageIO.read(ImageIO.java:1400)
490:            //            at javax.imageio.ImageIO.read(ImageIO.java:1322)
491:            //            at com.jidesoft.icons.IconsFactory.b(Unknown Source)
492:            //            at com.jidesoft.icons.IconsFactory.a(Unknown Source)
493:            //            at com.jidesoft.icons.IconsFactory.getImageIcon(Unknown Source)
494:            //            at com.jidesoft.plaf.vsnet.VsnetMetalUtils.initComponentDefaults(Unknown Source)
495:
496:            //    private static ImageIcon createImageIconWithException(final Class<?> baseClass, final String file) throws IOException {
497:            //        try {
498:            //            InputStream resource =
499:            //                    baseClass.getResourceAsStream(file);
500:            //            if (resource == null) {
501:            //                throw new IOException("File " + file + " not found");
502:            //            }
503:            //            BufferedInputStream in =
504:            //                    new BufferedInputStream(resource);
505:            //            return new ImageIcon(ImageIO.read(in));
506:            //        }
507:            //        catch (IOException ioe) {
508:            //            throw ioe;
509:            //        }
510:            //    }
511:
512:            /**
513:             * Generates HTML that lists all icons in IconsFactory.
514:             *
515:             * @param clazz the IconsFactory class
516:             */
517:            public static void generateHTML(Class<?> clazz) {
518:                String fullClassName = clazz.getName();
519:                String className = getClassName(fullClassName);
520:                File file = new File(fullClassName + ".html");
521:
522:                try {
523:                    FileWriter writer = new FileWriter(file);
524:                    writer
525:                            .write("<html>\n<body>\n<p><b><font size=\"5\" face=\"Verdana\">Icons in "
526:                                    + fullClassName + "</font></b></p>");
527:                    writer
528:                            .write("<p><b><font size=\"3\" face=\"Verdana\">Generated by JIDE Icons</font></b></p>");
529:                    writer
530:                            .write("<p><b><font size=\"3\" color=\"#AAAAAA\" face=\"Verdana\">1. If you cannot view the images in this page, "
531:                                    + "make sure the file is at the same directory as "
532:                                    + className + ".java</font></b></p>");
533:                    writer
534:                            .write("<p><b><font size=\"3\" color=\"#AAAAAA\" face=\"Verdana\">2. To get a particular icon in your code, call "
535:                                    + className
536:                                    + ".getImageIcon(FULL_CONSTANT_NAME). Replace FULL_CONSTANT_NAME with the actual "
537:                                    + "full constant name as in the table below"
538:                                    + "</font></b></p>");
539:                    generate(clazz, writer, className);
540:                    writer.write("\n</body>\n</html>");
541:                    writer.close();
542:                    System.out.println("File is generated at \""
543:                            + file.getAbsolutePath()
544:                            + "\". Please copy it to the same directory as "
545:                            + className + ".java");
546:                } catch (IOException e) {
547:                    System.err.println(e);
548:                }
549:            }
550:
551:            private static String getClassName(String fullName) {
552:                int last = fullName.lastIndexOf(".");
553:                if (last != -1) {
554:                    fullName = fullName.substring(last + 1);
555:                }
556:                StringTokenizer tokenizer = new StringTokenizer(fullName, "$");
557:                StringBuffer buffer = new StringBuffer();
558:                while (tokenizer.hasMoreTokens()) {
559:                    buffer.append(tokenizer.nextToken());
560:                    buffer.append(".");
561:                }
562:                return buffer.substring(0, buffer.length() - 1);
563:            }
564:
565:            private static void generate(Class<?> aClass, FileWriter writer,
566:                    String prefix) throws IOException {
567:                Class<?>[] classes = aClass.getDeclaredClasses();
568:                // don't know why but the order is exactly the reverse of the order of definitions.
569:                for (int i = classes.length - 1; i >= 0; i--) {
570:                    Class<?> clazz = classes[i];
571:                    generate(clazz, writer, getClassName(clazz.getName()));
572:                }
573:
574:                Field[] fields = aClass.getFields();
575:                writer.write("<p><font face=\"Verdana\"><b>" + prefix
576:                        + "</b></font></p>");
577:                writer
578:                        .write("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#CCCCCC\" width=\"66%\">");
579:                writer.write("<tr>\n");
580:                writer
581:                        .write("<td width=\"24%\" align=\"center\"><b><font face=\"Verdana\" color=\"#003399\">Name</font></b></td>\n");
582:                writer
583:                        .write("<td width=\"13%\" align=\"center\"><b><font face=\"Verdana\" color=\"#003399\">Image</font></b></td>\n");
584:                writer
585:                        .write("<td width=\"32%\" align=\"center\"><b><font face=\"Verdana\" color=\"#003399\">File Name</font></b></td>\n");
586:                writer
587:                        .write("<td width=\"31%\" align=\"center\"><b><font face=\"Verdana\" color=\"#003399\">Full Constant Name</font></b></td>\n");
588:                writer.write("</tr>\n");
589:                for (Field field : fields) {
590:                    try {
591:                        Object name = field.getName();
592:                        Object value = field.get(aClass);
593:                        writer.write("<tr>\n");
594:                        writer
595:                                .write("<td align=\"left\"><font face=\"Verdana\">"
596:                                        + name + "</font></td>\n");
597:                        writer
598:                                .write("<td align=\"center\"><font face=\"Verdana\"><img border=\"0\" src=\""
599:                                        + value + "\"></font></td>\n");
600:                        writer
601:                                .write("<td align=\"left\"><font face=\"Verdana\">"
602:                                        + value + "</font></td>\n");
603:                        //                writer.write("<td align=\"left\"><font face=\"Verdana\"><a href=\"" + prefix + ".getImageIcon("+ prefix + "." + name + "\">" + prefix + "." + name  + "<a></font></td>\n");
604:                        writer
605:                                .write("<td align=\"left\"><font face=\"Verdana\">"
606:                                        + prefix
607:                                        + "."
608:                                        + name
609:                                        + "</font></td>\n");
610:                        writer.write("</tr>\n");
611:                    } catch (IllegalArgumentException e) {
612:                        e.printStackTrace();
613:                    } catch (IllegalAccessException e) {
614:                        e.printStackTrace();
615:                    }
616:                }
617:                writer.write("</table><br><p>\n");
618:            }
619:
620:            /**
621:             * Gets part of the image from input image icon. It bascially takes a snapshot of the input image
622:             * at {x, y} location and the size is width x height.
623:             *
624:             * @param c      the component where the returned icon will be used. The component is used as the ImageObserver. It could be null.
625:             * @param icon   the original icon. This is the larger icon where a sub-image will be created using this method.
626:             * @param x      the x location of the sub-image, relative to the original icon.
627:             * @param x      the y location of the sub-image, relative to the original icon.
628:             * @param width  the width of the sub-image. It should be less than the width of the original icon.
629:             * @param height the height of the sub-image. It should be less than the height of the original icon.
630:             * @return an new image icon that was part of the input image icon.
631:             */
632:            public static ImageIcon getIcon(Component c, ImageIcon icon, int x,
633:                    int y, int width, int height) {
634:                return getIcon(c, icon, x, y, width, height, width, height);
635:            }
636:
637:            /**
638:             * Gets part of the image from input image icon. It bascially takes a snapshot of the input image
639:             * at {x, y} location and the size is width x height, then resize it to a size of destWidth x destHeight.
640:             *
641:             * @param c          the component where the returned icon will be used. The component is used as the ImageObserver. It could be null.
642:             * @param icon       the original icon. This is the larger icon where a sub-image will be created using this method.
643:             * @param x          the x location of the sub-image, relative to the original icon.
644:             * @param x          the y location of the sub-image, relative to the original icon.
645:             * @param width      the width of the sub-image. It should be less than the width of the original icon.
646:             * @param height     the height of the sub-image. It should be less than the height of the original icon.
647:             * @param destWidth  the width of the returned icon. The sub-image will be resize if the destWidth is not the same as the width.
648:             * @param destHeight the height of the returned icon. The sub-image will be resize if the destHeight is not the same as the height.
649:             * @return an new image icon that was part of the input image icon.
650:             */
651:            public static ImageIcon getIcon(Component c, ImageIcon icon, int x,
652:                    int y, int width, int height, int destWidth, int destHeight) {
653:                return getIcon(c, icon, x, y, width, height,
654:                        BufferedImage.TYPE_INT_ARGB, destWidth, destHeight);
655:            }
656:
657:            /**
658:             * Gets part of the image from input image icon. It bascially takes a snapshot of the input image
659:             * at {x, y} location and the size is width x height.
660:             *
661:             * @param c         the component where the returned icon will be used. The component is used as the ImageObserver. It could be null.
662:             * @param icon      the original icon. This is the larger icon where a small icon will be created using this method.
663:             * @param x         the x location of the smaller icon, relative to the original icon.
664:             * @param x         the y location of the smaller icon, relative to the original icon.
665:             * @param width     the width of the smaller icon. It should be less than the width of the original icon.
666:             * @param height    the height of the smaller icon. It should be less than the height of the original icon.
667:             * @param imageType image type is defined in {@link BufferedImage}, such as {@link BufferedImage#TYPE_INT_ARGB}, {@link BufferedImage#TYPE_INT_RGB} etc.
668:             * @return an new image icon that was part of the input image icon.
669:             */
670:            public static ImageIcon getIcon(Component c, ImageIcon icon, int x,
671:                    int y, int width, int height, int imageType) {
672:                return getIcon(c, icon, x, y, width, height, imageType, width,
673:                        height);
674:            }
675:
676:            /**
677:             * Gets part of the image from input image icon. It bascially takes a snapshot of the input image
678:             * at {x, y} location and the size is width x height, then resize it to a size of destWidth x destHeight.
679:             * if the original icon is null or the specified location is outside the original icon, EMPTY_ICON will be returned.
680:             *
681:             * @param c          the component where the returned icon will be used. The component is used as the ImageObserver. It could be null.
682:             * @param icon       the original icon. This is the larger icon where a sub-image will be created using this method.
683:             * @param x          the x location of the sub-image, relative to the original icon.
684:             * @param x          the y location of the sub-image, relative to the original icon.
685:             * @param width      the width of the sub-image. It should be less than the width of the original icon.
686:             * @param height     the height of the sub-image. It should be less than the height of the original icon.
687:             * @param imageType  image type is defined in {@link BufferedImage}, such as {@link BufferedImage#TYPE_INT_ARGB}, {@link BufferedImage#TYPE_INT_RGB} etc.
688:             * @param destWidth  the width of the returned icon. The sub-image will be resize if the destWidth is not the same as the width.
689:             * @param destHeight the height of the returned icon. The sub-image will be resize if the destHeight is not the same as the height.
690:             * @return an new image icon that was part of the input image icon.
691:             */
692:            public static ImageIcon getIcon(Component c, ImageIcon icon, int x,
693:                    int y, int width, int height, int imageType, int destWidth,
694:                    int destHeight) {
695:                if (icon == null || x < 0 || x + width > icon.getIconWidth()
696:                        || y < 0 || y + height > icon.getIconHeight()) { // outside the original icon.
697:                    return EMPTY_ICON;
698:                }
699:                BufferedImage image = new BufferedImage(destWidth, destHeight,
700:                        imageType);
701:                image.getGraphics().drawImage(icon.getImage(), 0, 0, destWidth,
702:                        destHeight, x, y, x + width, y + height, c);
703:                return new ImageIcon(image);
704:            }
705:
706:            /**
707:             * Gets a new icon with the overlayIcon paints over the orginal icon.
708:             *
709:             * @param c           the component where the returned icon will be used. The component is used as the ImageObserver. It could be null.
710:             * @param icon        the original icon
711:             * @param overlayIcon the overlay icon.
712:             * @param location    the location as defined in SwingConstants - CENTER, NORTH, SOUTH, WEST, EAST, NORTH_EAST, NORTH_WEST, SOUTH_WEST and SOUTH_EAST.
713:             * @return the new icon.
714:             */
715:            public static ImageIcon getOverlayIcon(Component c, ImageIcon icon,
716:                    ImageIcon overlayIcon, int location) {
717:                return getOverlayIcon(c, icon, overlayIcon, location,
718:                        new Insets(0, 0, 0, 0));
719:            }
720:
721:            /**
722:             * Gets a new icon with the overlayIcon paints over the orginal icon.
723:             *
724:             * @param c           the component where the returned icon will be used. The component is used as the ImageObserver. It could be null.
725:             * @param icon        the original icon
726:             * @param overlayIcon the overlay icon.
727:             * @param location    the location as defined in SwingConstants - CENTER, NORTH, SOUTH, WEST, EAST, NORTH_EAST, NORTH_WEST, SOUTH_WEST and SOUTH_EAST.
728:             * @param insets      the insets to the border. This parameter has no effect if the location is CENTER. For example, if the location is WEST, insets.left will be the gap of the left side of the
729:             *                    original icon and the left side of the overlay icon.
730:             * @return the new icon.
731:             */
732:            public static ImageIcon getOverlayIcon(Component c, ImageIcon icon,
733:                    ImageIcon overlayIcon, int location, Insets insets) {
734:                int x = -1, y = -1;
735:                int w = icon.getIconWidth();
736:                int h = icon.getIconHeight();
737:                int sw = overlayIcon.getIconWidth();
738:                int sh = overlayIcon.getIconHeight();
739:                switch (location) {
740:                case SwingConstants.CENTER:
741:                    x = (w - sw) / 2;
742:                    y = (h - sh) / 2;
743:                    break;
744:                case SwingConstants.NORTH:
745:                    x = (w - sw) / 2;
746:                    y = insets.top;
747:                    break;
748:                case SwingConstants.SOUTH:
749:                    x = (w - sw) / 2;
750:                    y = h - insets.bottom - sh;
751:                    break;
752:                case SwingConstants.WEST:
753:                    x = insets.left;
754:                    y = (h - sh) / 2;
755:                    break;
756:                case SwingConstants.EAST:
757:                    x = w - insets.right - sw;
758:                    y = (h - sh) / 2;
759:                    break;
760:                case SwingConstants.NORTH_EAST:
761:                    x = w - insets.top - sw;
762:                    y = insets.right;
763:                    break;
764:                case SwingConstants.NORTH_WEST:
765:                    x = insets.top;
766:                    y = insets.left;
767:                    break;
768:                case SwingConstants.SOUTH_WEST:
769:                    x = insets.left;
770:                    y = h - insets.bottom - sh;
771:                    break;
772:                case SwingConstants.SOUTH_EAST:
773:                    x = w - insets.right - sw;
774:                    y = h - insets.bottom - sh;
775:                    break;
776:                }
777:                return getOverlayIcon(c, icon, overlayIcon, x, y);
778:            }
779:
780:            /**
781:             * Gets a new icon with the overlayIcon paints over the orginal icon.
782:             *
783:             * @param c           the component where the returned icon will be used. The component is used as the ImageObserver. It could be null.
784:             * @param icon        the original icon
785:             * @param overlayIcon the overlay icon.
786:             * @param x           the x location relative to the original icon where the overlayIcon will be pained.
787:             * @param y           the y location relative to the original icon where the overlayIcon will be pained.
788:             * @return
789:             */
790:            public static ImageIcon getOverlayIcon(Component c, ImageIcon icon,
791:                    ImageIcon overlayIcon, int x, int y) {
792:                int w = icon == null ? overlayIcon.getIconWidth() : icon
793:                        .getIconWidth();
794:                int h = icon == null ? overlayIcon.getIconHeight() : icon
795:                        .getIconHeight();
796:                int sw = overlayIcon.getIconWidth();
797:                int sh = overlayIcon.getIconHeight();
798:                if (x != -1 && y != -1) {
799:                    BufferedImage image = new BufferedImage(w, h,
800:                            BufferedImage.TYPE_INT_ARGB);
801:                    if (icon != null) {
802:                        image.getGraphics().drawImage(icon.getImage(), 0, 0, w,
803:                                h, c);
804:                    }
805:                    image.getGraphics().drawImage(overlayIcon.getImage(), x, y,
806:                            sw, sh, c);
807:                    return new ImageIcon(image);
808:                } else {
809:                    return icon;
810:                }
811:            }
812:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.