KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > util > IconManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.util;
21
22 import java.io.File JavaDoc;
23 import org.openide.ErrorManager;
24
25 import java.awt.*;
26 import java.awt.image.*;
27 import java.util.HashMap JavaDoc;
28 import javax.imageio.ImageIO JavaDoc;
29 import javax.swing.ImageIcon JavaDoc;
30
31
32 /**
33  * Class which wraps functionality for managing icons. This class
34  * does nothing on JDK1.3, but on JDK1.4 its subclass (IconManager14)
35  * can export icons.
36  * <p>
37  * @author Tor Norbye
38  */

39 public class IconManager {
40     private HashMap JavaDoc written = null;
41     private File JavaDoc directory = null;
42     private int nextId = 1;
43
44     /**
45      * Constructor
46      *
47      * @param base directory in which icon files are written.
48      * Paths returned by getIcon are relative to this directory.
49      */

50     public IconManager(File JavaDoc base) {
51         this.directory = base;
52     }
53     
54     /**
55      * Return the file name of the given icon, relative to
56      * the directory that this IconManager was initialized with.
57      * If the icon does not yet exist on disk, it is created first.
58      * <b>You MUST call setBase before the first call to this method.</b>
59      * @param icon The image we want a url for
60      * @return A relative filename pointing to the image file
61      */

62     public String JavaDoc getIcon(Image icon) {
63         if (written == null) {
64             written = new HashMap JavaDoc(50);
65         }
66         
67         String JavaDoc name = (String JavaDoc)written.get(icon);
68         if (name == null) {
69             // Invent filename for the icon. Can we use any tricks
70
// here, if we pass in the node etc.?
71
// For now, just use sequential naming
72
BufferedImage image = toBufferedImage(icon);
73             File JavaDoc output = null;
74             // Find unused name
75
while (true) {
76                 // Using PNG format - gif is probably more common, but
77
// a gif writer often is not available in ImageIO
78
name = "tasklist-html-" + (nextId++) + ".png"; // NOI18N
79
output = new File JavaDoc(directory, name);
80                 if (!output.exists()) {
81                     break;
82                 }
83             }
84             try {
85                 ImageIO.write(image, "png", output); // NOI18N
86
} catch (Exception JavaDoc e) {
87                 e.printStackTrace();
88                 return null;
89             }
90             written.put(icon, name);
91         }
92         return name;
93     }
94
95     /**
96      * This method is from the The Java Developers Almanac 1.4
97      * http://javaalmanac.com/egs/java.awt.image/Image2Buf.html
98      * and is copyright by that book's author(s)
99      */

100     private static BufferedImage toBufferedImage(Image image) {
101         // This method returns a buffered image with the contents of an image
102
if (image instanceof BufferedImage) {
103             return (BufferedImage)image;
104         }
105     
106         // This code ensures that all the pixels in the image are loaded
107
image = new ImageIcon JavaDoc(image).getImage();
108     
109         // Determine if the image has transparent pixels; for this method's
110
// implementation, see e665 Determining If an Image Has Transparent Pixels
111
//boolean hasAlpha = hasAlpha(image);
112
boolean hasAlpha = true;
113     
114         // Create a buffered image with a format that's compatible with the screen
115
BufferedImage bimage = null;
116         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
117         try {
118             // Determine the type of transparency of the new buffered image
119
int transparency = Transparency.OPAQUE;
120             if (hasAlpha) {
121                 transparency = Transparency.BITMASK;
122             }
123     
124             // Create the buffered image
125
GraphicsDevice gs = ge.getDefaultScreenDevice();
126             GraphicsConfiguration gc = gs.getDefaultConfiguration();
127             bimage = gc.createCompatibleImage(
128                 image.getWidth(null), image.getHeight(null), transparency);
129         } catch (HeadlessException e) {
130             // The system does not have a screen
131
}
132     
133         if (bimage == null) {
134             // Create a buffered image using the default color model
135
int type = BufferedImage.TYPE_INT_RGB;
136             if (hasAlpha) {
137                 type = BufferedImage.TYPE_INT_ARGB;
138             }
139             bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
140         }
141     
142         // Copy image to buffered image
143
Graphics g = bimage.createGraphics();
144     
145         // Paint the image onto the buffered image
146
g.drawImage(image, 0, 0, null);
147         g.dispose();
148     
149         return bimage;
150     }
151 }
152
Popular Tags