KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > PrintIcon


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  * Portions Copyrighted 2007 Sun Microsystems, Inc.
16  */

17
18 package org.netbeans.nbbuild;
19
20 import java.awt.image.BufferedImage JavaDoc;
21 import java.io.BufferedWriter JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.SortedSet JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import javax.imageio.ImageIO JavaDoc;
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.Project;
34 import org.apache.tools.ant.Task;
35 import org.apache.tools.ant.types.FileSet;
36
37 /**
38  *
39  * @author Jaroslav Tulach
40  */

41 public class PrintIcon extends Task {
42     private FileSet first;
43     private FileSet second;
44     
45     
46     
47     
48     /** Creates a new instance of PrintIcon */
49     public PrintIcon() {
50     }
51
52     private File JavaDoc duplicates;
53     public void setDuplicates(File JavaDoc f) {
54         duplicates = f;
55     }
56
57     private File JavaDoc difference;
58     public void setDifference(File JavaDoc f) {
59         difference = f;
60     }
61     
62     /**
63      *
64      * @return
65      */

66     public FileSet createFirstPool() {
67         if (first != null) {
68             throw new BuildException();
69         }
70         first = new FileSet();
71         return first;
72     }
73     
74     public FileSet createSecondPool() {
75         if (second != null) {
76             throw new BuildException();
77         }
78         second = new FileSet();
79         return second;
80     }
81     
82     @Override JavaDoc
83     public void execute() throws BuildException {
84         if (first == null) {
85             throw new BuildException("You need to specify firstpool element for this task!"); // NOI18N
86
}
87         
88         try {
89             
90             SortedSet JavaDoc<IconInfo> firstSet = new TreeSet JavaDoc<IconInfo>();
91             for (String JavaDoc f : first.getDirectoryScanner(getProject()).getIncludedFiles()) {
92                 File JavaDoc baseDir = first.getDir(getProject());
93                 File JavaDoc file = new File JavaDoc(baseDir, f);
94                 firstSet.add(new IconInfo(file.toURI().toURL(), getProject()));
95             }
96
97             SortedSet JavaDoc<IconInfo> sndSet = new TreeSet JavaDoc<IconInfo>();
98             if (second != null) {
99                 for (String JavaDoc f : second.getDirectoryScanner(getProject()).getIncludedFiles()) {
100                     File JavaDoc baseDir = second.getDir(getProject());
101                     File JavaDoc file = new File JavaDoc(baseDir, f);
102                     sndSet.add(new IconInfo(file.toURI().toURL(), getProject()));
103                 }
104             }
105             
106             if (duplicates != null) {
107                 Set JavaDoc<IconInfo> both = new TreeSet JavaDoc<IconInfo>(firstSet);
108                 both.addAll(sndSet);
109                 
110                 BufferedWriter JavaDoc os = new BufferedWriter JavaDoc(new FileWriter JavaDoc(duplicates));
111                 IconInfo prev = null;
112                 boolean prevPrinted = false;
113                 for (IconInfo info : both) {
114                     IconInfo p = prev;
115                     prev = info;
116                     if (p == null || p.hash != info.hash) {
117                         prevPrinted = false;
118                         continue;
119                     }
120                     
121                     if (!prevPrinted) {
122                         os.write(p.toString());
123                         os.newLine();
124                         prevPrinted = true;
125                     }
126                     
127                     os.write(info.toString());
128                     os.newLine();
129                 }
130                 os.close();
131             }
132             if (difference != null) {
133                 SortedSet JavaDoc<IconInfo> union = new TreeSet JavaDoc<IconInfo>(firstSet);
134                 union.addAll(sndSet);
135                 
136                 BufferedWriter JavaDoc os = new BufferedWriter JavaDoc(new FileWriter JavaDoc(difference));
137                 for (IconInfo info : union) {
138                     if (!contains(firstSet, info.hash)) {
139                         os.write('+');
140                         os.write(info.toString());
141                         os.newLine();
142                         continue;
143                     }
144                     if (!contains(sndSet, info.hash)) {
145                         os.write('-');
146                         os.write(info.toString());
147                         os.newLine();
148                         continue;
149                     }
150                 }
151                 os.close();
152             }
153             
154         } catch (IOException JavaDoc ex) {
155             throw new BuildException(ex);
156         }
157         
158     }
159     
160     private static boolean contains(SortedSet JavaDoc<IconInfo> set, int hashCode) {
161         IconInfo fake = new IconInfo("", "", hashCode);
162         Set JavaDoc<IconInfo> greaterOrEqual = set.tailSet(fake);
163         if (greaterOrEqual.isEmpty()) {
164             return false;
165         }
166         IconInfo first = greaterOrEqual.iterator().next();
167         return hashCode == first.hash;
168     }
169
170     static final int hash(Throwable JavaDoc t) {
171         String JavaDoc msg = t.getMessage();
172         if (msg != null) {
173             return 7 + msg.hashCode();
174         }
175         return 5 + t.getClass().hashCode();
176     }
177     
178     private static final class IconInfo implements Comparable JavaDoc<IconInfo> {
179         final String JavaDoc name;
180         final String JavaDoc path;
181         final int hash;
182         
183         public IconInfo(URL JavaDoc from, Project p) throws IOException JavaDoc {
184             this.path = from.toExternalForm();
185             int last = this.path.lastIndexOf('/');
186             assert last >= 0;
187             this.name = this.path.substring(last + 1);
188             
189             p.log("Parsing " + from, Project.MSG_VERBOSE);
190             BufferedImage JavaDoc image;
191             int hash;
192             try {
193                 InputStream JavaDoc is = from.openStream();
194                 image = ImageIO.read(is);
195                 is.close();
196                 int w = image.getWidth();
197                 int h = image.getHeight();
198                 hash = w * 3 + h * 7;
199                 
200                 for (int i = 0; i < w; i++) {
201                     for (int j = 0; j < h; j++) {
202                         int rgb = image.getRGB(i, j);
203                         hash += (rgb >> 2);
204                     }
205                 }
206             } catch (IOException JavaDoc e) {
207                 p.log("Broken icon at " + from, Project.MSG_WARN);
208                 hash = hash(e);
209             } catch (IndexOutOfBoundsException JavaDoc ex) {
210                 p.log("Broken icon at " + from, Project.MSG_WARN);
211                 hash = hash(ex);
212             }
213             
214             this.hash = hash;
215         }
216         
217         public IconInfo(String JavaDoc name, String JavaDoc path, int hash) {
218             this.name = name;
219             this.path = path;
220             this.hash = hash;
221         }
222         
223         @Override JavaDoc
224         public int hashCode() {
225             return hash;
226         }
227
228         @Override JavaDoc
229         public boolean equals(Object JavaDoc obj) {
230             if (obj == null)
231                 return false;
232             if (getClass() != obj.getClass())
233                 return false;
234             final IconInfo other = (IconInfo) obj;
235
236             if (this.path != other.path &&
237                 (this.path == null || !this.path.equals(other.path)))
238                 return false;
239             if (this.hash != other.hash)
240                 return false;
241             return true;
242         }
243     
244         public int compareTo(IconInfo another) {
245             if (hash != another.hash) {
246                 return hash - another.hash;
247             }
248             
249             return path.compareTo(another.path);
250         }
251         
252         public String JavaDoc toString() {
253             String JavaDoc h = Integer.toHexString(hash);
254             if (h.length() < 8) {
255                 h = "00000000".substring(h.length()) + h;
256             }
257             String JavaDoc n = name;
258             if (n.length() < 30) {
259                 n = n + " ".substring(n.length());
260             }
261             
262             return MessageFormat.format("{0} {1} {2}", h, n, path);
263         }
264     } // end of IconInfo
265
}
266
Popular Tags