| 1 | package com.hammurapi.render; |
| 2 | |
| 3 | import java.net.URL; |
| 4 | |
| 5 | import com.hammurapi.common.ClassResourceLoader; |
| 6 | import com.hammurapi.common.Context; |
| 7 | import com.hammurapi.convert.AtomicConverter; |
| 8 | import com.hammurapi.convert.Converter; |
| 9 | import com.hammurapi.convert.DuckConverterFactory; |
| 10 | |
| 11 | /** |
| 12 | * Converts objects to ImageProvider by looking up images in classloader |
| 13 | * traversing object's class hierarchy. |
| 14 | * @author Pavel Vlasov |
| 15 | * |
| 16 | */ |
| 17 | public class ImageProviderConverter implements AtomicConverter<Object, ImageProvider> { |
| 18 | |
| 19 | @Override |
| 20 | public ImageProvider convert(final Object source, Converter master, Context context, ClassLoader classLoader) { |
| 21 | return new ImageProvider() { |
| 22 | |
| 23 | private boolean resolved; |
| 24 | |
| 25 | private URL result; |
| 26 | |
| 27 | @Override |
| 28 | public Object getIcon() throws RenderingException { |
| 29 | if (!resolved) { |
| 30 | ClassResourceLoader crl = new ClassResourceLoader(source.getClass(), "META-INF/com.hammurapi.render/", DuckConverterFactory.getChildClassLoader(source.getClass().getClassLoader(), this.getClass().getClassLoader())); |
| 31 | result = crl.getResource(null, RenderingConstants.GIF); |
| 32 | resolved = true; |
| 33 | } |
| 34 | return result; |
| 35 | } |
| 36 | |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public Class<Object> getSourceType() { |
| 42 | return Object.class; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public Class<? extends ImageProvider> getTargetType() { |
| 47 | return ImageProvider.class; |
| 48 | } |
| 49 | |
| 50 | } |