Skip to content

Package: ComposedImageDescriptor

ComposedImageDescriptor

nameinstructionbranchcomplexitylinemethod
ComposedImageDescriptor(ComposedImage)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
drawCompositeImage(int, int)
M: 38 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
getSize()
M: 68 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (c) 2002-2010 IBM Corporation and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * IBM - Initial API and implementation
13: */
14: // REUSED CLASS
15: package org.eclipse.emf.edit.ui.provider;
16:
17: import java.io.BufferedInputStream;
18: import java.io.ByteArrayInputStream;
19: import java.io.ByteArrayOutputStream;
20: import java.io.DataInputStream;
21: import java.io.DataOutputStream;
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.lang.reflect.Method;
25: import java.net.URL;
26: import java.util.ArrayList;
27: import java.util.HashMap;
28: import java.util.Iterator;
29: import java.util.List;
30:
31: import org.eclipse.emf.common.CommonPlugin;
32: import org.eclipse.emf.common.util.URI;
33: import org.eclipse.emf.edit.EMFEditPlugin;
34: import org.eclipse.emf.edit.provider.ComposedImage;
35: import org.eclipse.jface.resource.CompositeImageDescriptor;
36: import org.eclipse.jface.resource.ImageDescriptor;
37: import org.eclipse.swt.graphics.Image;
38: import org.eclipse.swt.graphics.ImageData;
39: import org.eclipse.swt.graphics.Point;
40: import org.eclipse.swt.widgets.Display;
41:
42: /**
43: *
44: */
45: public class ExtendedImageRegistry {
46:         public static final ExtendedImageRegistry INSTANCE = new ExtendedImageRegistry() {
47:                 @Override
48:                 public Image getImage(Object object) {
49:                         return getInstance().getImage(object);
50:                 }
51:
52:                 @Override
53:                 public ImageDescriptor getImageDescriptor(Object object) {
54:                         return getInstance().getImageDescriptor(object);
55:                 }
56:         };
57:
58:         private static final Method INSTANCE_METHOD;
59:
60:         static {
61:                 Method instanceMethod = null;
62:                 try {
63:                         final Class<?> singletonUtilityClass = CommonPlugin.loadClass("org.eclipse.rap.rwt",
64:                                 "org.eclipse.rap.rwt.SingletonUtil");
65:                         instanceMethod = singletonUtilityClass.getMethod("getSessionInstance", Class.class);
66:                 } catch (final Exception exception) {
67:                         try {
68:                                 final Class<?> singletonUtilityClass = CommonPlugin.loadClass("org.eclipse.rap.rwt",
69:                                         "org.eclipse.rap.rwt.SessionSingletonBase");
70:                                 instanceMethod = singletonUtilityClass.getMethod("getInstance", Class.class);
71:                         } catch (final Exception exception2) {
72:                                 Activator.log(exception2);
73:                         }
74:                 }
75:                 INSTANCE_METHOD = instanceMethod;
76:         }
77:
78:         @SuppressWarnings("unchecked")
79:         static <T> T getInstance(Class<T> _class) {
80:                 try {
81:                         return (T) INSTANCE_METHOD.invoke(null, _class);
82:                 } catch (final Exception exception) {
83:                         throw new RuntimeException(exception);
84:                 }
85:         }
86:
87:         public static ExtendedImageRegistry getInstance() {
88:                 return getInstance(ExtendedImageRegistry.class);
89:         }
90:
91:         protected HashMap<Object, Image> table = new HashMap<Object, Image>(10);
92:
93:         public ExtendedImageRegistry() {
94:                 final Display display = Display.getCurrent();
95:                 hookDisplayDispose(display);
96:         }
97:
98:         public ExtendedImageRegistry(Display display) {
99:                 hookDisplayDispose(display);
100:         }
101:
102:         protected static Object resourceURL = EMFEditPlugin.INSTANCE.getImage("full/obj16/Resource");
103:
104:         protected static String resourceURLPrefix = resourceURL.toString() + "#";
105:
106:         protected static String itemURLPrefix = EMFEditPlugin.INSTANCE.getImage("full/obj16/Item").toString() + "#";
107:
108:         protected static String createChildURLPrefix = EMFEditPlugin.INSTANCE.getImage("full/ctool16/CreateChild")
109:                 .toString() + "#";
110:
111:         public Image getImage(Object object) {
112:                 if (object instanceof Image) {
113:                         return (Image) object;
114:                 } else {
115:                         Image result = table.get(object);
116:                         if (result == null) {
117:                                 if (object instanceof ImageDescriptor) {
118:                                         final ImageDescriptor imageDescriptor = (ImageDescriptor) object;
119:                                         result = imageDescriptor.createImage();
120:                                 } else if (object instanceof URL || object instanceof URI) {
121:                                         final String urlString = object.toString();
122:                                         ImageDescriptor imageDescriptor = null;
123:                                         if (urlString.startsWith(resourceURLPrefix)) {
124:                                                 result = getImage(resourceURL);
125:                                         } else if (urlString.startsWith(itemURLPrefix)) {
126:                                                 try {
127:                                                         final URL url = new URL(urlString.substring(0, itemURLPrefix.length()));
128:                                                         final String key1 = urlString.substring(itemURLPrefix.length());
129:                                                         imageDescriptor = new URLImageDescriptor(url, key1, null);
130:                                                 } catch (final IOException exception) {
131:                                                         // Ignore
132:                                                 }
133:                                         } else if (urlString.startsWith(createChildURLPrefix)) {
134:                                                 try {
135:                                                         final URL url = new URL(urlString.substring(0, createChildURLPrefix.length()));
136:                                                         String key1 = urlString.substring(createChildURLPrefix.length() + 1);
137:                                                         String key2 = null;
138:                                                         final int index = key1.indexOf("/");
139:                                                         if (index != -1) {
140:                                                                 key2 = key1.substring(index + 1);
141:                                                                 key1 = key1.substring(0, index);
142:                                                         }
143:                                                         imageDescriptor = new URLImageDescriptor(url, key1, key2);
144:                                                 } catch (final IOException exception) {
145:                                                         // Ignore
146:                                                 }
147:                                         } else {
148:                                                 try {
149:                                                         imageDescriptor = ImageDescriptor.createFromURL(new URL(urlString));
150:                                                 } catch (final IOException exception) {
151:                                                         // Ignore
152:                                                 }
153:                                         }
154:                                         if (imageDescriptor != null) {
155:                                                 result = imageDescriptor.createImage();
156:                                         }
157:                                 } else if (object instanceof ComposedImage) {
158:                                         final ImageDescriptor composedImageDescriptor = new ComposedImageDescriptor((ComposedImage) object);
159:                                         result = composedImageDescriptor.createImage();
160:                                 }
161:
162:                                 if (result != null) {
163:                                         table.put(object, result);
164:                                 }
165:
166:                         }
167:                         return result;
168:                 }
169:         }
170:
171:         public ImageDescriptor getImageDescriptor(Object object) {
172:                 if (object instanceof ImageDescriptor) {
173:                         return (ImageDescriptor) object;
174:                 } else {
175:                         final Image image = getImage(object);
176:                         if (image != null) {
177:                                 return new ImageWrapperImageDescriptor(image);
178:                         } else {
179:                                 return null;
180:                         }
181:                 }
182:         }
183:
184:         protected void handleDisplayDispose() {
185:                 // TODO https://bugs.eclipse.org/bugs/show_bug.cgi?id=314239
186:                 // for (Image image : table.values())
187:                 // {
188:                 // image.dispose();
189:                 // }
190:                 table = null;
191:         }
192:
193:         protected void hookDisplayDispose(Display display) {
194:                 display.disposeExec(new Runnable() {
195:                         @Override
196:                         public void run() {
197:                                 handleDisplayDispose();
198:                         }
199:                 });
200:         }
201: }
202:
203: class ImageWrapperImageDescriptor extends ImageDescriptor {
204:         /**
205:         *
206:         */
207:         private static final long serialVersionUID = 1L;
208:         protected Image image;
209:
210:         public ImageWrapperImageDescriptor(Image image) {
211:                 this.image = image;
212:         }
213:
214:         @Override
215:         public boolean equals(Object that) {
216:                 return that instanceof ImageWrapperImageDescriptor &&
217:                         ((ImageWrapperImageDescriptor) that).image.equals(image);
218:         }
219:
220:         @Override
221:         public int hashCode() {
222:                 return image.hashCode();
223:         }
224:
225:         @Override
226:         public ImageData getImageData() {
227:                 return image.getImageData();
228:         }
229: }
230:
231: class ComposedImageDescriptor extends CompositeImageDescriptor {
232:         /**
233:         *
234:         */
235:         private static final long serialVersionUID = 1L;
236:         protected ComposedImage composedImage;
237:         protected List<ImageData> imageDatas;
238:
239:         public ComposedImageDescriptor(ComposedImage composedImage) {
240:                 this.composedImage = composedImage;
241:         }
242:
243:         @Override
244:         public void drawCompositeImage(int width, int height) {
245:                 final ComposedImage.Size size = new ComposedImage.Size();
246:                 size.width = width;
247:                 size.height = height;
248:                 final Iterator<ImageData> images = imageDatas.iterator();
249:                 for (final Iterator<ComposedImage.Point> points = composedImage.getDrawPoints(size).iterator(); points
250:•                        .hasNext();) {
251:                         final ComposedImage.Point point = points.next();
252:                         drawImage(images.next(), point.x, point.y);
253:                 }
254:         }
255:
256:         @Override
257:         public Point getSize() {
258:                 final List<Object> images = composedImage.getImages();
259:                 imageDatas = new ArrayList<ImageData>(images.size());
260:                 final List<ComposedImage.Size> sizes = new ArrayList<ComposedImage.Size>(images.size());
261:•                for (final Object object : images) {
262:                         final Image image = ExtendedImageRegistry.getInstance().getImage(object);
263:                         final ImageData imageData = image.getImageData();
264:                         imageDatas.add(imageData);
265:
266:                         final ComposedImage.Size size = new ComposedImage.Size();
267:                         size.width = imageData.width;
268:                         size.height = imageData.height;
269:                         sizes.add(size);
270:                 }
271:
272:                 final ComposedImage.Size result = composedImage.getSize(sizes);
273:                 return new Point(result.width, result.height);
274:         }
275: }
276:
277: class URLImageDescriptor extends ImageDescriptor {
278:         /**
279:         *
280:         */
281:         private static final long serialVersionUID = 1L;
282:         protected URL url;
283:         protected String key1;
284:         protected String key2;
285:
286:         URLImageDescriptor(URL url, String key1, String key2) {
287:                 this.url = url;
288:                 this.key1 = key1;
289:                 this.key2 = key2;
290:         }
291:
292:         @Override
293:         public ImageData getImageData() {
294:                 InputStream in = null;
295:                 try {
296:                         if (key1 != null) {
297:                                 final ImageDataSynthesizer imageDataSynthesizer = new ImageDataSynthesizer(url);
298:                                 in = imageDataSynthesizer.generateGIF(key1, key2);
299:                                 return new ImageData(in);
300:                         } else {
301:                                 in = url.openStream();
302:                         }
303:
304:                         return new ImageData(in);
305:                 } catch (final IOException e) {
306:                         return null;
307:                 } finally {
308:                         if (in != null) {
309:                                 try {
310:                                         in.close();
311:                                 } catch (final IOException e) {
312:                                         return null;
313:                                 }
314:                         }
315:                 }
316:         }
317:
318:         protected InputStream getStream() throws IOException {
319:                 return url.openStream();
320:         }
321:
322:         @Override
323:         public int hashCode() {
324:                 return url.hashCode() | (key1 == null ? 0 : key1.hashCode()) | (key2 == null ? 0 : key2.hashCode());
325:         }
326:
327:         @Override
328:         public boolean equals(Object that) {
329:                 if (that instanceof URLImageDescriptor) {
330:                         final URLImageDescriptor otherURLImageDescriptor = (URLImageDescriptor) that;
331:                         return url.equals(otherURLImageDescriptor.url) &&
332:                                 (key1 == null ? otherURLImageDescriptor.key1 == null : key1.equals(otherURLImageDescriptor.key1)) &&
333:                                 (key2 == null ? otherURLImageDescriptor.key2 == null : key2.equals(otherURLImageDescriptor.key2));
334:                 } else {
335:                         return false;
336:                 }
337:         }
338:
339:         @Override
340:         public String toString() {
341:                 return getClass().getName() + "(" + url + "#" + key1 + "/" + key2 + ")";
342:         }
343: }
344:
345: class ImageDataSynthesizer {
346:         protected URL url;
347:
348:         protected static final int tableOffset1 = 49;
349:         protected static final int tableOffset2 = 25;
350:
351:         public ImageDataSynthesizer(URL url) {
352:                 this.url = url;
353:         }
354:
355:         protected int code(String code) {
356:                 int result = 0;
357:                 for (int i = 0; i < code.length(); ++i) {
358:                         result += code.charAt(i) - 32;
359:                 }
360:                 return result;
361:         }
362:
363:         public InputStream generateGIF(String key1, String key2) {
364:                 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
365:                 try {
366:                         final byte[] content = new byte[5000];
367:                         final int result = getContents(content, url);
368:
369:                         // generateColor();
370:                         final ColorInformation info1 = ColorInformation.getColor(code(key1));
371:                         final ColorInformation info2 = key2 == null ? null : ColorInformation.getColor(code(key2));
372:
373:                         for (int j = 0; j < result; ++j) {
374:                                 if (j == tableOffset1 || j == tableOffset1 + 3 || j == tableOffset1 + 6 || j == tableOffset1 + 9) {
375:                                         final int index = (j - tableOffset1) / 3;
376:                                         if (!info1.rainbow || info1.which == index - 1) {
377:                                                 content[j] = info1.scale(info1.red, info1.factor[index]);
378:                                         }
379:                                 } else if (j == tableOffset1 + 1 || j == tableOffset1 + 4 || j == tableOffset1 + 7
380:                                         || j == tableOffset1 + 10) {
381:                                         final int index = (j - tableOffset1 - 1) / 3;
382:                                         if (!info1.rainbow || info1.which == index - 1) {
383:                                                 content[j] = info1.scale(info1.green, info1.factor[index]);
384:                                         }
385:                                 } else if (j == tableOffset1 + 2 || j == tableOffset1 + 5 || j == tableOffset1 + 8
386:                                         || j == tableOffset1 + 11) {
387:                                         final int index = (j - tableOffset1 - 2) / 3;
388:                                         if (!info1.rainbow || info1.which == index - 1) {
389:                                                 content[j] = info1.scale(info1.blue, info1.factor[index]);
390:                                         }
391:                                 }
392:
393:                                 if (info2 != null) {
394:                                         if (j == tableOffset2 || j == tableOffset2 + 3 || j == tableOffset2 + 6 || j == tableOffset2 + 9) {
395:                                                 final int index = (j - tableOffset2) / 3;
396:                                                 if (!info2.rainbow || info2.which == index - 1) {
397:                                                         content[j] = info2.scale(info2.red, info2.factor[index]);
398:                                                 }
399:                                         } else if (j == tableOffset2 + 1 || j == tableOffset2 + 4 || j == tableOffset2 + 7
400:                                                 || j == tableOffset2 + 10) {
401:                                                 final int index = (j - tableOffset2 - 1) / 3;
402:                                                 if (!info2.rainbow || info2.which == index - 1) {
403:                                                         content[j] = info2.scale(info2.green, info2.factor[index]);
404:                                                 }
405:                                         } else if (j == tableOffset2 + 2 || j == tableOffset2 + 5 || j == tableOffset2 + 8
406:                                                 || j == tableOffset2 + 11) {
407:                                                 final int index = (j - tableOffset2 - 2) / 3;
408:                                                 if (!info2.rainbow || info2.which == index - 1) {
409:                                                         content[j] = info2.scale(info2.blue, info2.factor[index]);
410:                                                 }
411:                                         }
412:                                 }
413:                         }
414:
415:                         final DataOutputStream writer = new DataOutputStream(outputStream);
416:                         writer.write(content, 0, result);
417:                         writer.close();
418:                 } catch (final Exception exception) {
419:                         exception.printStackTrace();
420:                 }
421:
422:                 return new ByteArrayInputStream(outputStream.toByteArray());
423:         }
424:
425:         protected int getContents(byte[] content, URL gifURL) throws IOException {
426:                 final BufferedInputStream bufferedInputStream = new BufferedInputStream(gifURL.openStream());
427:                 final DataInputStream reader = new DataInputStream(bufferedInputStream);
428:                 final int result = reader.read(content, 0, content.length);
429:                 reader.close();
430:                 return result;
431:         }
432:
433:         protected static class ColorInformation {
434:                 public static ColorInformation getColor(int index) {
435:                         index = Math.abs(index) % 61;
436:                         while (entries.size() <= index) {
437:                                 instance.generateColor();
438:
439:                                 final ColorInformation entry = new ColorInformation();
440:                                 entry.red = instance.red;
441:                                 entry.green = instance.green;
442:                                 entry.blue = instance.blue;
443:                                 entry.which = instance.which;
444:                                 entry.factor = new double[] { instance.factor[0], instance.factor[1], instance.factor[2],
445:                                         instance.factor[3] };
446:                                 entry.rainbow = instance.rainbow;
447:                                 entries.add(entry);
448:                                 instance.fixFactor();
449:                         }
450:                         return entries.get(index);
451:                 }
452:
453:                 protected static ColorInformation instance = new ColorInformation();
454:
455:                 protected static List<ColorInformation> entries = new ArrayList<ColorInformation>(1000);
456:
457:                 public int red = 192;
458:                 public int green = 64;
459:                 public int blue = 64;
460:
461:                 public int which = 2;
462:                 public int change = 64;
463:
464:                 public double[] factor = { 0.35, 0.1, -0.1, -0.3 };
465:                 public boolean rainbow;
466:
467:                 public byte scale(int value, double factor) {
468:                         if (factor > 0.0) {
469:                                 return (byte) (value + (255 - value) * factor);
470:                         } else {
471:                                 return (byte) (value + value * factor);
472:                         }
473:                 }
474:
475:                 protected void generateColor() {
476:                         switch (which) {
477:                         case 0: {
478:                                 red += change;
479:                                 if (red <= 64) {
480:                                         which = 1;
481:                                         change = -change;
482:                                 } else if (red >= 192) {
483:                                         which = 1;
484:                                         change = -change;
485:                                 }
486:                                 break;
487:                         }
488:                         case 1: {
489:                                 green += change;
490:                                 if (green >= 192) {
491:                                         which = 2;
492:                                         change = -change;
493:                                 } else if (green <= 64) {
494:                                         which = 2;
495:                                         change = -change;
496:                                 }
497:                                 break;
498:                         }
499:                         case 2: {
500:                                 blue += change;
501:                                 if (blue >= 192) {
502:                                         which = 0;
503:                                         change = -change;
504:                                 } else if (blue <= 64) {
505:                                         which = 0;
506:                                         change = -change;
507:                                 }
508:                                 break;
509:                         }
510:                         }
511:                 }
512:
513:                 protected void fixFactor() {
514:                         if (red == 192 && green == 64 && blue == 64) {
515:                                 for (int j = 0; j < factor.length; ++j) {
516:                                         factor[j] += 0.3;
517:                                 }
518:                                 if (factor[0] >= 1.0) {
519:                                         rainbow = true;
520:                                         for (int j = 0; j < factor.length; ++j) {
521:                                                 factor[j] -= 0.8;
522:                                         }
523:                                 }
524:                         }
525:                 }
526:         }
527: }