Debian Codegraph @

plugins/org.eclipse.emf.edit.ui/src/org/eclipse/emf/edit/ui/provider/ExtendedImageRegistry.java

raw
1 /**
2 * Copyright (c) 2002-2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.edit.ui.provider;
12
13
14 import java.io.BufferedInputStream;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.eclipse.emf.common.EMFPlugin;
28 import org.eclipse.emf.common.util.CommonUtil;
29 import org.eclipse.emf.common.util.URI;
30 import org.eclipse.emf.edit.EMFEditPlugin;
31 import org.eclipse.emf.edit.provider.ComposedImage;
32 import org.eclipse.jface.resource.CompositeImageDescriptor;
33 import org.eclipse.jface.resource.ImageDescriptor;
34 import org.eclipse.swt.graphics.Device;
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
42 /**
43 *
44 */
45 public class ExtendedImageRegistry
46 {
47 public static final ExtendedImageRegistry INSTANCE = new ExtendedImageRegistry();
48
49 public static ExtendedImageRegistry getInstance()
50 {
51 return INSTANCE;
52 }
53
54 protected final Display display;
55
56 protected HashMap<Object, Image> table = new HashMap<Object, Image>(10);
57
58 protected HashMap<Object, ImageDescriptor> imageDescriptorTable = new HashMap<Object, ImageDescriptor>(10);
59
60 public ExtendedImageRegistry()
61 {
62 Display display = Display.getCurrent();
63 if (display == null)
64 {
65 final Display defaultDisplay= Display.getDefault();
66 defaultDisplay.asyncExec
67 (new Runnable()
68 {
69 public void run()
70 {
71 hookDisplayDispose(defaultDisplay);
72 }
73 });
74 this.display = defaultDisplay;
75 }
76 else
77 {
78 this.display = display;
79 }
80 }
81
82 public ExtendedImageRegistry(Display display)
83 {
84 this.display = display;
85 hookDisplayDispose(display);
86 }
87
88 protected static Object resourceURL =
89 EMFEditPlugin.INSTANCE.getImage("full/obj16/Resource");
90
91 protected static String resourceURLPrefix = resourceURL.toString() + "#";
92
93 protected static String itemURLPrefix =
94 EMFEditPlugin.INSTANCE.getImage("full/obj16/Item").toString() + "#";
95
96 protected static String createChildURLPrefix =
97 EMFEditPlugin.INSTANCE.getImage("full/ctool16/CreateChild").toString() + "#";
98
99 public Image getImage(Object object)
100 {
101 if (object instanceof Image)
102 {
103 return (Image)object;
104 }
105 else
106 {
107 Image result = table.get(object);
108 if (result == null)
109 {
110 if (object instanceof ImageDescriptor)
111 {
112 ImageDescriptor imageDescriptor = (ImageDescriptor)object;
113 result = imageDescriptor.createImage();
114 }
115 else if (object instanceof URL || object instanceof URI)
116 {
117 String urlString = object.toString();
118 ImageDescriptor imageDescriptor = null;
119 if (urlString.startsWith(resourceURLPrefix))
120 {
121 if (EMFPlugin.IS_RESOURCES_BUNDLE_AVAILABLE)
122 {
123 imageDescriptor =
124 PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor
125 ("dummy." + urlString.substring(resourceURLPrefix.length()));
126 }
127 else
128 {
129 result = getImage(resourceURL);
130 }
131 }
132 else if (urlString.startsWith(itemURLPrefix))
133 {
134 try
135 {
136 URL url = CommonUtil.newURL(urlString.substring(0, itemURLPrefix.length()));
137 String key1 = urlString.substring(itemURLPrefix.length());
138 imageDescriptor = new URLImageDescriptor(url, key1, null);
139 }
140 catch (IOException exception)
141 {
142 // Ignore
143 }
144 }
145 else if (urlString.startsWith(createChildURLPrefix))
146 {
147 try
148 {
149 URL url = CommonUtil.newURL(urlString.substring(0, createChildURLPrefix.length()));
150 String key1 = urlString.substring(createChildURLPrefix.length() + 1);
151 String key2 = null;
152 int index = key1.indexOf("/");
153 if (index != -1)
154 {
155 key2 = key1.substring(index + 1);
156 key1 = key1.substring(0, index);
157 }
158 imageDescriptor = new URLImageDescriptor(url, key1, key2);
159 }
160 catch (IOException exception)
161 {
162 // Ignore
163 }
164 }
165 else
166 {
167 try
168 {
169 imageDescriptor = ImageDescriptor.createFromURL(CommonUtil.newURL(urlString));
170 }
171 catch (IOException exception)
172 {
173 // Ignore
174 }
175 }
176 if (imageDescriptor != null)
177 {
178 result = imageDescriptor.createImage();
179 }
180 }
181 else if (object instanceof ComposedImage)
182 {
183 ImageDescriptor composedImageDescriptor = new ComposedImageDescriptor((ComposedImage)object);
184 result = composedImageDescriptor.createImage();
185 }
186
187 if (result != null)
188 {
189 table.put(object, result);
190 }
191 }
192 return result;
193 }
194 }
195
196 public ImageDescriptor getImageDescriptor(Object object)
197 {
198 if (object instanceof ImageDescriptor)
199 {
200 return (ImageDescriptor)object;
201 }
202 else if (display == Display.getCurrent())
203 {
204 final Image image = getImage(object);
205 if (image != null)
206 {
207 return new ImageWrapperImageDescriptor(image);
208 }
209 else
210 {
211 return null;
212 }
213 }
214 else
215 {
216 return getImageDescriptorOnBackgroundThread(object);
217 }
218 }
219
220 protected synchronized ImageDescriptor getImageDescriptorOnBackgroundThread(Object object)
221 {
222 if (object instanceof ImageDescriptor)
223 {
224 return (ImageDescriptor)object;
225 }
226 else
227 {
228 ImageDescriptor result = imageDescriptorTable.get(object);
229 if (result == null)
230 {
231 if (object instanceof Image)
232 {
233 result = new ImageWrapperImageDescriptor((Image)object);
234 }
235 else
236 {
237 Image image = table.get(object);
238 if (image != null)
239 {
240 result = new ImageWrapperImageDescriptor(image);
241 }
242 else if (object instanceof URL || object instanceof URI)
243 {
244 String urlString = object.toString();
245 if (urlString.startsWith(resourceURLPrefix))
246 {
247 if (EMFPlugin.IS_RESOURCES_BUNDLE_AVAILABLE)
248 {
249 result =
250 PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor
251 ("dummy." + urlString.substring(resourceURLPrefix.length()));
252 }
253 else
254 {
255 result = getImageDescriptor(resourceURL);
256 }
257 }
258 else if (urlString.startsWith(itemURLPrefix))
259 {
260 try
261 {
262 URL url = CommonUtil.newURL(urlString.substring(0, itemURLPrefix.length()));
263 String key1 = urlString.substring(itemURLPrefix.length());
264 result = new URLImageDescriptor(url, key1, null);
265 }
266 catch (IOException exception)
267 {
268 // Ignore
269 }
270 }
271 else if (urlString.startsWith(createChildURLPrefix))
272 {
273 try
274 {
275 URL url = CommonUtil.newURL(urlString.substring(0, createChildURLPrefix.length()));
276 String key1 = urlString.substring(createChildURLPrefix.length() + 1);
277 String key2 = null;
278 int index = key1.indexOf("/");
279 if (index != -1)
280 {
281 key2 = key1.substring(index + 1);
282 key1 = key1.substring(0, index);
283 }
284 result = new URLImageDescriptor(url, key1, key2);
285 }
286 catch (IOException exception)
287 {
288 // Ignore
289 }
290 }
291 else
292 {
293 try
294 {
295 result = ImageDescriptor.createFromURL(CommonUtil.newURL(urlString));
296 }
297 catch (IOException exception)
298 {
299 // Ignore
300 }
301 }
302 }
303 else if (object instanceof ComposedImage)
304 {
305 result = new ComposedImageDescriptor((ComposedImage)object);
306 }
307 }
308
309 if (result != null)
310 {
311 imageDescriptorTable.put(object, result);
312 }
313 }
314 return result;
315 }
316 }
317
318 protected void handleDisplayDispose()
319 {
320 for (Image image : table.values())
321 {
322 image.dispose();
323 }
324 table = null;
325 }
326
327 protected void hookDisplayDispose(Display display)
328 {
329 display.disposeExec
330 (new Runnable()
331 {
332 public void run()
333 {
334 handleDisplayDispose();
335 }
336 });
337 }
338 }
339
340
341 class ImageWrapperImageDescriptor extends ImageDescriptor
342 {
343 protected Image image;
344
345 public ImageWrapperImageDescriptor(Image image)
346 {
347 this.image = image;
348 }
349
350 @Override
351 public boolean equals(Object that)
352 {
353 return
354 that instanceof ImageWrapperImageDescriptor &&
355 ((ImageWrapperImageDescriptor)that).image.equals(image);
356 }
357
358 @Override
359 public int hashCode()
360 {
361 return image.hashCode();
362 }
363
364 @Override
365 public ImageData getImageData()
366 {
367 return image.getImageData();
368 }
369 }
370
371
372 class ComposedImageDescriptor extends CompositeImageDescriptor
373 {
374 protected ComposedImage composedImage;
375 protected List<ImageData> imageDatas;
376
377 public ComposedImageDescriptor(ComposedImage composedImage)
378 {
379 this.composedImage = composedImage;
380 }
381
382 @Override
383 @SuppressWarnings("deprecation")
384 public void drawCompositeImage(int width, int height)
385 {
386 ComposedImage.Size size = new ComposedImage.Size();
387 size.width = width;
388 size.height = height;
389 Iterator<ImageData> images = imageDatas.iterator();
390 for (Iterator<ComposedImage.Point> points = composedImage.getDrawPoints(size).iterator(); points.hasNext(); )
391 {
392 ComposedImage.Point point = points.next();
393 drawImage(images.next(), point.x, point.y);
394 }
395 }
396
397 @Override
398 public Point getSize()
399 {
400 List<Object> images = composedImage.getImages();
401 imageDatas = new ArrayList<ImageData>(images.size());
402 List<ComposedImage.Size> sizes = new ArrayList<ComposedImage.Size>(images.size());
403 for (Object object : images)
404 {
405 Image image = ExtendedImageRegistry.getInstance().getImage(object);
406 ImageData imageData = image.getImageData();
407 imageDatas.add(imageData);
408
409 ComposedImage.Size size = new ComposedImage.Size();
410 size.width = imageData.width;
411 size.height = imageData.height;
412 sizes.add(size);
413 }
414
415 ComposedImage.Size result = composedImage.getSize(sizes);
416 return new Point(result.width, result.height);
417 }
418 }
419
420 class URLImageDescriptor extends ImageDescriptor
421 {
422 protected URL url;
423 protected String key1;
424 protected String key2;
425
426 URLImageDescriptor(URL url, String key1, String key2)
427 {
428 this.url = url;
429 this.key1 = key1;
430 this.key2 = key2;
431 }
432
433 @Override
434 public ImageData getImageData()
435 {
436 InputStream in = null;
437 try
438 {
439 if (key1 != null)
440 {
441 ImageDataSynthesizer imageDataSynthesizer = new ImageDataSynthesizer(url);
442 in = imageDataSynthesizer.generateGIF(key1, key2);
443 return new ImageData(in);
444 }
445 else
446 {
447 in = url.openStream();
448 }
449
450 return new ImageData(in);
451 }
452 catch (IOException e)
453 {
454 return null;
455 }
456 finally
457 {
458 if (in != null)
459 {
460 try
461 {
462 in.close();
463 }
464 catch (IOException e)
465 {
466 return null;
467 }
468 }
469 }
470 }
471
472 protected InputStream getStream() throws IOException
473 {
474 return url.openStream();
475 }
476
477 @Override
478 public int hashCode()
479 {
480 return url.hashCode() | (key1 == null ? 0 : key1.hashCode()) | (key2 == null ? 0 : key2.hashCode());
481 }
482
483 @Override
484 public boolean equals(Object that)
485 {
486 if (that instanceof URLImageDescriptor)
487 {
488 URLImageDescriptor otherURLImageDescriptor = (URLImageDescriptor)that;
489 return
490 url.equals(otherURLImageDescriptor.url) &&
491 (key1 == null ? otherURLImageDescriptor.key1 == null : key1.equals(otherURLImageDescriptor.key1)) &&
492 (key2 == null ? otherURLImageDescriptor.key2 == null : key2.equals(otherURLImageDescriptor.key2));
493 }
494 else
495 {
496 return false;
497 }
498 }
499
500 @Override
501 public Image createImage(boolean returnMissingImageOnError, Device device)
502 {
503 // Don't call super because it loses the transparency information if we write this image to disk.
504 //
505 return new Image(device, getImageData());
506 }
507
508 @Override
509 public String toString()
510 {
511 return getClass().getName() + "(" + url + "#" + key1 + "/" + key2 + ")";
512 }
513 }
514
515 class ImageDataSynthesizer
516 {
517 protected URL url;
518
519 protected static final int tableOffset1 = 49;
520 protected static final int tableOffset2 = 25;
521
522 public ImageDataSynthesizer(URL url)
523 {
524 this.url = url;
525 }
526
527 protected int code(String code)
528 {
529 int result = 0;
530 for (int i = 0; i < code.length(); ++ i)
531 {
532 result += code.charAt(i) - 32;
533 }
534 return result;
535 }
536
537 public InputStream generateGIF(String key1, String key2)
538 {
539 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
540 try
541 {
542 byte [] content = new byte [5000];
543 int result = getContents(content, url);
544
545 // generateColor();
546 ColorInformation info1 = ColorInformation.getColor(code(key1));
547 ColorInformation info2 = key2 == null ? null : ColorInformation.getColor(code(key2));
548
549
550 for (int j = 0; j < result; ++j)
551 {
552 if (j == tableOffset1 || j == tableOffset1 + 3 || j == tableOffset1 + 6 || j == tableOffset1 + 9)
553 {
554 int index = (j - tableOffset1) / 3;
555 if (!info1.rainbow || info1.which == index - 1)
556 {
557 content[j] = info1.scale(info1.red, info1.factor[index]);
558 }
559 }
560 else if (j == tableOffset1 + 1 || j == tableOffset1 + 4 || j == tableOffset1 + 7 || j == tableOffset1 + 10)
561 {
562 int index = (j - tableOffset1 - 1) / 3;
563 if (!info1.rainbow || info1.which == index - 1)
564 {
565 content[j] = info1.scale(info1.green, info1.factor[index]);
566 }
567 }
568 else if (j == tableOffset1 + 2 || j == tableOffset1 + 5 || j == tableOffset1 + 8 || j == tableOffset1 + 11)
569 {
570 int index = (j - tableOffset1 - 2) / 3;
571 if (!info1.rainbow || info1.which == index - 1)
572 {
573 content[j] = info1.scale(info1.blue, info1.factor[index]);
574 }
575 }
576
577 if (info2 != null)
578 {
579 if (j == tableOffset2 || j == tableOffset2 + 3 || j == tableOffset2 + 6 || j == tableOffset2 + 9)
580 {
581 int index = (j - tableOffset2) / 3;
582 if (!info2.rainbow || info2.which == index - 1)
583 {
584 content[j] = info2.scale(info2.red, info2.factor[index]);
585 }
586 }
587 else if (j == tableOffset2 + 1 || j == tableOffset2 + 4 || j == tableOffset2 + 7 || j == tableOffset2 + 10)
588 {
589 int index = (j - tableOffset2 - 1) / 3;
590 if (!info2.rainbow || info2.which == index - 1)
591 {
592 content[j] = info2.scale(info2.green, info2.factor[index]);
593 }
594 }
595 else if (j == tableOffset2 + 2 || j == tableOffset2 + 5 || j == tableOffset2 + 8 || j == tableOffset2 + 11)
596 {
597 int index = (j - tableOffset2 - 2) / 3;
598 if (!info2.rainbow || info2.which == index - 1)
599 {
600 content[j] = info2.scale(info2.blue, info2.factor[index]);
601 }
602 }
603 }
604 }
605
606 DataOutputStream writer = new DataOutputStream(outputStream);
607 writer.write(content, 0, result);
608 writer.close();
609 }
610 catch (Exception exception)
611 {
612 exception.printStackTrace();
613 }
614
615 return new ByteArrayInputStream(outputStream.toByteArray());
616 }
617
618 protected int getContents(byte [] content, URL gifURL) throws IOException
619 {
620 BufferedInputStream bufferedInputStream = new BufferedInputStream(gifURL.openStream());
621 DataInputStream reader = new DataInputStream(bufferedInputStream);
622 int result = reader.read(content, 0, content.length);
623 reader.close();
624 return result;
625 }
626
627 protected static class ColorInformation
628 {
629 public static ColorInformation getColor(int index)
630 {
631 index = Math.abs(index) % 61;
632 while (entries.size() <= index)
633 {
634 instance.generateColor();
635
636 ColorInformation entry = new ColorInformation();
637 entry.red = instance.red;
638 entry.green = instance.green;
639 entry.blue = instance.blue;
640 entry.which = instance.which;
641 entry.factor = new double [] { instance.factor[0], instance.factor[1], instance.factor[2], instance.factor[3] };
642 entry.rainbow = instance.rainbow;
643 entries.add(entry);
644 instance.fixFactor();
645 }
646 return entries.get(index);
647 }
648
649 protected static ColorInformation instance = new ColorInformation();
650
651 protected static List<ColorInformation> entries = new ArrayList<ColorInformation>(1000);
652
653 public int red = 192;
654 public int green = 64;
655 public int blue = 64;
656
657 public int which = 2;
658 public int change = 64;
659
660 public double [] factor = { 0.35, 0.1, -0.1, -0.3 };
661 public boolean rainbow;
662
663 public byte scale(int value, double factor)
664 {
665 if (factor > 0.0)
666 {
667 return (byte)(value + (255 - value) * factor);
668 }
669 else
670 {
671 return (byte)(value + value * factor);
672 }
673 }
674
675 protected void generateColor()
676 {
677 switch (which)
678 {
679 case 0:
680 {
681 red += change;
682 if (red <= 64)
683 {
684 which = 1;
685 change = -change;
686 }
687 else if (red >= 192)
688 {
689 which = 1;
690 change = -change;
691 }
692 break;
693 }
694 case 1:
695 {
696 green += change;
697 if (green >= 192)
698 {
699 which = 2;
700 change = -change;
701 }
702 else if (green <= 64)
703 {
704 which = 2;
705 change = -change;
706 }
707 break;
708 }
709 case 2:
710 {
711 blue += change;
712 if (blue >= 192)
713 {
714 which = 0;
715 change = -change;
716 }
717 else if (blue <=64)
718 {
719 which = 0;
720 change = -change;
721 }
722 break;
723 }
724 }
725 }
726
727 protected void fixFactor()
728 {
729 if (red == 192 && green == 64 && blue == 64)
730 {
731 for (int j = 0; j < factor.length; ++j)
732 {
733 factor[j] += 0.3;
734 }
735 if (factor[0] >= 1.0)
736 {
737 rainbow = true;
738 for (int j = 0; j < factor.length; ++j)
739 {
740 factor[j] -= 0.8;
741 }
742 }
743 }
744 }
745 }
746 }