KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > provider > ExtendedImageRegistry


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: ExtendedImageRegistry.java,v 1.3 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.provider;
18
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.DataInputStream JavaDoc;
24 import java.io.DataOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.eclipse.jface.resource.CompositeImageDescriptor;
34 import org.eclipse.jface.resource.ImageDescriptor;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.graphics.ImageData;
37 import org.eclipse.swt.graphics.Point;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.ui.PlatformUI;
40
41 import org.eclipse.emf.common.util.URI;
42
43 import org.eclipse.emf.edit.EMFEditPlugin;
44 import org.eclipse.emf.edit.provider.ComposedImage;
45
46
47 /**
48  *
49  */

50 public class ExtendedImageRegistry
51 {
52   public static final ExtendedImageRegistry INSTANCE = new ExtendedImageRegistry();
53
54   public static ExtendedImageRegistry getInstance()
55   {
56     return INSTANCE;
57   }
58
59   protected HashMap JavaDoc table = new HashMap JavaDoc(10);
60
61   public ExtendedImageRegistry()
62   {
63     Display display = Display.getCurrent();
64     hookDisplayDispose(display);
65   }
66
67   public ExtendedImageRegistry(Display display)
68   {
69     hookDisplayDispose(display);
70   }
71
72   protected static String JavaDoc resourceURLPrefix =
73     EMFEditPlugin.INSTANCE.getImage("full/obj16/Resource").toString() + "#";
74
75   protected static String JavaDoc itemURLPrefix =
76     EMFEditPlugin.INSTANCE.getImage("full/obj16/Item").toString() + "#";
77
78   protected static String JavaDoc createChildURLPrefix =
79     EMFEditPlugin.INSTANCE.getImage("full/ctool16/CreateChild").toString() + "#";
80
81   public Image getImage(Object JavaDoc object)
82   {
83     if (object instanceof Image)
84     {
85       return (Image)object;
86     }
87     else
88     {
89       Image result = (Image)table.get(object);
90       if (result == null)
91       {
92         if (object instanceof Image)
93         {
94           result = (Image)object;
95         }
96         else if (object instanceof ImageDescriptor)
97         {
98           ImageDescriptor imageDescriptor= (ImageDescriptor)object;
99           result = imageDescriptor.createImage();
100         }
101         else if (object instanceof URL JavaDoc || object instanceof URI)
102         {
103           String JavaDoc urlString = object.toString();
104           ImageDescriptor imageDescriptor = null;
105           if (urlString.startsWith(resourceURLPrefix))
106           {
107             imageDescriptor =
108               PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor
109                 ("dummy." + urlString.substring(resourceURLPrefix.length()));
110           }
111           else if (urlString.startsWith(itemURLPrefix))
112           {
113             try
114             {
115               URL JavaDoc url = new URL JavaDoc(urlString.substring(0, itemURLPrefix.length()));
116               String JavaDoc key1 = urlString.substring(itemURLPrefix.length());
117               imageDescriptor = new URLImageDescriptor(url, key1, null);
118             }
119             catch (IOException JavaDoc exception)
120             {
121             }
122           }
123           else if (urlString.startsWith(createChildURLPrefix))
124           {
125             try
126             {
127               URL JavaDoc url = new URL JavaDoc(urlString.substring(0, createChildURLPrefix.length()));
128               String JavaDoc key1 = urlString.substring(createChildURLPrefix.length() + 1);
129               String JavaDoc key2 = null;
130               int index = key1.indexOf("/");
131               if (index != -1)
132               {
133                 key2 = key1.substring(index + 1);
134                 key1 = key1.substring(0, index);
135               }
136               imageDescriptor = new URLImageDescriptor(url, key1, key2);
137             }
138             catch (IOException JavaDoc exception)
139             {
140             }
141           }
142           else
143           {
144             try
145             {
146               imageDescriptor = ImageDescriptor.createFromURL(new URL JavaDoc(urlString));
147             }
148             catch (IOException JavaDoc exception)
149             {
150             }
151           }
152           result = imageDescriptor.createImage();
153         }
154         else if (object instanceof ComposedImage)
155         {
156           ImageDescriptor composedImageDescriptor = new ComposedImageDescriptor((ComposedImage)object);
157           result = composedImageDescriptor.createImage();
158         }
159
160         if (result != null)
161         {
162           table.put(object, result);
163         }
164
165       }
166       return result;
167     }
168   }
169
170
171   public ImageDescriptor getImageDescriptor(Object JavaDoc object)
172   {
173     if (object instanceof ImageDescriptor)
174     {
175       return (ImageDescriptor)object;
176     }
177     else
178     {
179       final Image image = getImage(object);
180       if (image != null)
181       {
182         return new ImageWrapperImageDescriptor(image);
183       }
184       else
185       {
186         return null;
187       }
188     }
189   }
190
191   protected void handleDisplayDispose()
192   {
193     for (Iterator JavaDoc images = table.values().iterator(); images.hasNext();)
194     {
195       Image image = (Image)images.next();
196       image.dispose();
197     }
198     table = null;
199   }
200
201   protected void hookDisplayDispose(Display display)
202   {
203     display.disposeExec
204       (new Runnable JavaDoc()
205        {
206          public void run()
207          {
208            handleDisplayDispose();
209          }
210        });
211   }
212 }
213
214
215 class ImageWrapperImageDescriptor extends ImageDescriptor
216 {
217   protected Image image;
218
219   public ImageWrapperImageDescriptor(Image image)
220   {
221     this.image = image;
222   }
223
224   public boolean equals(Object JavaDoc that)
225   {
226     return
227       that instanceof ImageWrapperImageDescriptor &&
228         ((ImageWrapperImageDescriptor)that).image.equals(image);
229   }
230
231   public int hashCode()
232   {
233     return image.hashCode();
234   }
235
236   public ImageData getImageData()
237   {
238     return image.getImageData();
239   }
240 }
241
242
243 class ComposedImageDescriptor extends CompositeImageDescriptor
244 {
245   protected ComposedImage composedImage;
246   protected List JavaDoc imageDatas;
247
248   public ComposedImageDescriptor(ComposedImage composedImage)
249   {
250     this.composedImage = composedImage;
251   }
252
253   public void drawCompositeImage(int width, int height)
254   {
255     ComposedImage.Size size = new ComposedImage.Size();
256     size.width = width;
257     size.height = height;
258     Iterator JavaDoc images = imageDatas.iterator();
259     for (Iterator JavaDoc points = composedImage.getDrawPoints(size).iterator(); points.hasNext(); )
260     {
261       ComposedImage.Point point = (ComposedImage.Point)points.next();
262       drawImage((ImageData)images.next(), point.x, point.y);
263     }
264   }
265
266   public Point getSize()
267   {
268     List JavaDoc images = composedImage.getImages();
269     imageDatas = new ArrayList JavaDoc(images.size());
270     List JavaDoc sizes = new ArrayList JavaDoc(images.size());
271     for (Iterator JavaDoc i = images.iterator(); i.hasNext(); )
272     {
273       Image image = ExtendedImageRegistry.getInstance().getImage(i.next());
274       ImageData imageData = image.getImageData();
275       imageDatas.add(imageData);
276
277       ComposedImage.Size size = new ComposedImage.Size();
278       size.width = imageData.width;
279       size.height = imageData.height;
280       sizes.add(size);
281     }
282
283     ComposedImage.Size result = composedImage.getSize(sizes);
284     return new Point(result.width, result.height);
285   }
286 }
287
288 class URLImageDescriptor extends ImageDescriptor
289 {
290   protected URL JavaDoc url;
291   protected String JavaDoc key1;
292   protected String JavaDoc key2;
293
294   URLImageDescriptor(URL JavaDoc url, String JavaDoc key1, String JavaDoc key2)
295   {
296     this.url = url;
297     this.key1 = key1;
298     this.key2 = key2;
299   }
300
301   public ImageData getImageData()
302   {
303     InputStream JavaDoc in = null;
304     try
305     {
306       if (key1 != null)
307       {
308         ImageDataSynthesizer imageDataSynthesizer = new ImageDataSynthesizer(url);
309         in = imageDataSynthesizer.generateGIF(key1, key2);
310         return new ImageData(in);
311       }
312       else
313       {
314         in = url.openStream();
315       }
316
317       return new ImageData(in);
318     }
319     catch (IOException JavaDoc e)
320     {
321       return null;
322     }
323     finally
324     {
325       if (in != null)
326       {
327         try
328         {
329           in.close();
330         }
331         catch (IOException JavaDoc e)
332         {
333           return null;
334         }
335       }
336     }
337   }
338
339   protected InputStream JavaDoc getStream() throws IOException JavaDoc
340   {
341       return url.openStream();
342   }
343
344   public int hashCode()
345   {
346     return url.hashCode() | (key1 == null ? 0 : key1.hashCode()) | (key2 == null ? 0 : key2.hashCode());
347   }
348
349   public boolean equals(Object JavaDoc that)
350   {
351     if (that instanceof URLImageDescriptor)
352     {
353       URLImageDescriptor otherURLImageDescriptor = (URLImageDescriptor)that;
354       return
355         url.equals(otherURLImageDescriptor.url) &&
356           (key1 == null ? otherURLImageDescriptor.key1 == null : key1.equals(otherURLImageDescriptor.key1)) &&
357           (key2 == null ? otherURLImageDescriptor.key2 == null : key2.equals(otherURLImageDescriptor.key2));
358     }
359     else
360     {
361       return false;
362     }
363   }
364
365   public String JavaDoc toString()
366   {
367     return getClass().getName() + "(" + url + "#" + key1 + "/" + key2 + ")";
368   }
369 }
370
371 class ImageDataSynthesizer
372 {
373   protected URL JavaDoc url;
374
375   protected static final int tableOffset1 = 49;
376   protected static final int tableOffset2 = 25;
377
378   public ImageDataSynthesizer(URL JavaDoc url)
379   {
380     this.url = url;
381   }
382
383   protected int code(String JavaDoc code)
384   {
385     int result = 0;
386     for (int i = 0; i < code.length(); ++ i)
387     {
388       result += code.charAt(i) - 32;
389     }
390     return result;
391   }
392
393   public InputStream JavaDoc generateGIF(String JavaDoc key1, String JavaDoc key2)
394   {
395     ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
396     try
397     {
398       byte [] content = new byte [5000];
399       int result = getContents(content, url);
400
401       // generateColor();
402
ColorInformation info1 = ColorInformation.getColor(code(key1));
403       ColorInformation info2 = key2 == null ? null : ColorInformation.getColor(code(key2));
404
405
406       for (int j = 0; j < result; ++j)
407       {
408         if (j == tableOffset1 || j == tableOffset1 + 3 || j == tableOffset1 + 6 || j == tableOffset1 + 9)
409         {
410           int index = (j - tableOffset1) / 3;
411           if (!info1.rainbow || info1.which == index - 1)
412           {
413             content[j] = info1.scale(info1.red, info1.factor[index]);
414           }
415         }
416         else if (j == tableOffset1 + 1 || j == tableOffset1 + 4 || j == tableOffset1 + 7 || j == tableOffset1 + 10)
417         {
418           int index = (j - tableOffset1 - 1) / 3;
419           if (!info1.rainbow || info1.which == index - 1)
420           {
421             content[j] = info1.scale(info1.green, info1.factor[index]);
422           }
423         }
424         else if (j == tableOffset1 + 2 || j == tableOffset1 + 5 || j == tableOffset1 + 8 || j == tableOffset1 + 11)
425         {
426           int index = (j - tableOffset1 - 2) / 3;
427           if (!info1.rainbow || info1.which == index - 1)
428           {
429             content[j] = info1.scale(info1.blue, info1.factor[index]);
430           }
431         }
432
433         if (info2 != null)
434         {
435           if (j == tableOffset2 || j == tableOffset2 + 3 || j == tableOffset2 + 6 || j == tableOffset2 + 9)
436           {
437             int index = (j - tableOffset2) / 3;
438             if (!info2.rainbow || info2.which == index - 1)
439             {
440               content[j] = info2.scale(info2.red, info2.factor[index]);
441             }
442           }
443           else if (j == tableOffset2 + 1 || j == tableOffset2 + 4 || j == tableOffset2 + 7 || j == tableOffset2 + 10)
444           {
445             int index = (j - tableOffset2 - 1) / 3;
446             if (!info2.rainbow || info2.which == index - 1)
447             {
448               content[j] = info2.scale(info2.green, info2.factor[index]);
449             }
450           }
451           else if (j == tableOffset2 + 2 || j == tableOffset2 + 5 || j == tableOffset2 + 8 || j == tableOffset2 + 11)
452           {
453             int index = (j - tableOffset2 - 2) / 3;
454             if (!info2.rainbow || info2.which == index - 1)
455             {
456               content[j] = info2.scale(info2.blue, info2.factor[index]);
457             }
458           }
459         }
460       }
461
462       DataOutputStream JavaDoc writer = new DataOutputStream JavaDoc(outputStream);
463       writer.write(content, 0, result);
464       writer.close();
465     }
466     catch (Exception JavaDoc exception)
467     {
468       exception.printStackTrace();
469     }
470
471     return new ByteArrayInputStream JavaDoc(outputStream.toByteArray());
472   }
473
474   protected int getContents(byte [] content, URL JavaDoc gifURL) throws IOException JavaDoc
475   {
476     BufferedInputStream JavaDoc bufferedInputStream = new BufferedInputStream JavaDoc(gifURL.openStream());
477     DataInputStream JavaDoc reader = new DataInputStream JavaDoc(bufferedInputStream);
478     int result = reader.read(content, 0, content.length);
479     reader.close();
480     return result;
481   }
482
483   protected static class ColorInformation
484   {
485     public static ColorInformation getColor(int index)
486     {
487       index = Math.abs(index) % 61;
488       while (entries.size() <= index)
489       {
490         instance.generateColor();
491
492         ColorInformation entry = new ColorInformation();
493         entry.red = instance.red;
494         entry.green = instance.green;
495         entry.blue = instance.blue;
496         entry.which = instance.which;
497         entry.factor = new double [] { instance.factor[0], instance.factor[1], instance.factor[2], instance.factor[3] };
498         entry.rainbow = instance.rainbow;
499         entries.add(entry);
500         instance.fixFactor();
501       }
502       return (ColorInformation)entries.get(index);
503     }
504
505     protected static ColorInformation instance = new ColorInformation();
506
507     protected static List JavaDoc entries = new ArrayList JavaDoc(1000);
508
509     public int red = 192;
510     public int green = 64;
511     public int blue = 64;
512
513     public int which = 2;
514     public int change = 64;
515
516     public double [] factor = { 0.35, 0.1, -0.1, -0.3 };
517     public boolean rainbow;
518
519     public byte scale(int value, double factor)
520     {
521       if (factor > 0.0)
522       {
523         return (byte)(value + (255 - value) * factor);
524       }
525       else
526       {
527         return (byte)(value + value * factor);
528       }
529     }
530
531     protected void generateColor()
532     {
533       switch (which)
534       {
535         case 0:
536         {
537           red += change;
538           if (red <= 64)
539           {
540             which = 1;
541             change = -change;
542           }
543           else if (red >= 192)
544           {
545             which = 1;
546             change = -change;
547           }
548           break;
549         }
550         case 1:
551         {
552           green += change;
553           if (green >= 192)
554           {
555             which = 2;
556             change = -change;
557           }
558           else if (green <= 64)
559           {
560             which = 2;
561             change = -change;
562           }
563           break;
564         }
565         case 2:
566         {
567           blue += change;
568           if (blue >= 192)
569           {
570             which = 0;
571             change = -change;
572           }
573           else if (blue <=64)
574           {
575             which = 0;
576             change = -change;
577           }
578           break;
579         }
580       }
581     }
582
583     protected void fixFactor()
584     {
585       if (red == 192 && green == 64 && blue == 64)
586       {
587         for (int j = 0; j < factor.length; ++j)
588         {
589           factor[j] += 0.3;
590         }
591         if (factor[0] >= 1.0)
592         {
593           rainbow = true;
594           for (int j = 0; j < factor.length; ++j)
595           {
596             factor[j] -= 0.8;
597           }
598         }
599       }
600     }
601   }
602 }
603
Popular Tags