KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > ExtIcon


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.project.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.awt.image.ColorModel JavaDoc;
26 import java.awt.image.ImageConsumer JavaDoc;
27 import java.awt.image.ImageObserver JavaDoc;
28 import java.awt.image.MemoryImageSource JavaDoc;
29 import java.awt.image.PixelGrabber JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31
32 import java.io.Externalizable JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.ObjectInput JavaDoc;
35 import java.io.ObjectOutput JavaDoc;
36 import java.util.Hashtable JavaDoc;
37
38 import javax.swing.Icon JavaDoc;
39 import javax.swing.ImageIcon JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import org.openide.util.Utilities;
42
43 /**
44  * Class for persisting icons
45  * @author Milan Kubec
46  */

47 public class ExtIcon {
48     
49     private Image JavaDoc image;
50     
51     public ExtIcon() {
52     }
53     
54     public ExtIcon(byte[] content) {
55         Toolkit JavaDoc toolkit = Toolkit.getDefaultToolkit();
56         ColorModel JavaDoc cm = ColorModel.getRGBdefault();
57         image = toolkit.createImage(new MemoryImageSource JavaDoc(16, 16, cm, content, 0, 16 * 4));
58     }
59         
60     public void setIcon(Icon JavaDoc icn) {
61         image = Utilities.icon2Image(icn);
62     }
63     
64     public Icon JavaDoc getIcon() {
65         return new ImageIcon JavaDoc(image);
66     }
67     
68     
69     public byte[] getBytes() throws IOException JavaDoc {
70         PixelGrabber JavaDoc pg = new PixelGrabber JavaDoc(image, 0, 0, 16, 16, false);
71         try {
72             pg.grabPixels();
73             if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
74                 throw new IOException JavaDoc("Cannot load image data");
75             }
76         } catch (InterruptedException JavaDoc e) {
77             throw new IOException JavaDoc("Loading image interrupted");
78         }
79         Object JavaDoc obj = pg.getPixels();
80         if (obj instanceof byte[]) {
81             return (byte[])obj;
82         } else {
83             return intToByteArray((int[])obj);
84         }
85     }
86     
87     public static byte[] intToByteArray(int[] value) {
88         byte[] b = new byte[value.length * 4];
89         for (int j = 0; j < b.length; j = j + 4) {
90             int val = value[j / 4];
91             b[j] = (byte)(val >>> 24);
92             b[j + 1] = (byte)(val >> 16 & 0xff);
93             b[j + 2] = (byte)(val >> 8 & 0xff);
94             b[j + 3] = (byte)(val & 0xff);
95         }
96         return b;
97     }
98 }
99
Popular Tags