1 17 package org.eclipse.emf.mapping.presentation; 18 19 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 23 import org.eclipse.jface.resource.CompositeImageDescriptor; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.graphics.ImageData; 26 import org.eclipse.swt.graphics.Point; 27 28 import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; 29 import org.eclipse.emf.mapping.MappingPlugin; 30 import org.eclipse.emf.mapping.domain.AdapterFactoryMappingDomain; 31 import org.eclipse.emf.mapping.provider.MappedObjectItemProvider; 32 33 34 public class MappingDomainLabelProvider extends AdapterFactoryLabelProvider 35 { 36 static class Key 37 { 38 public Image mainImage; 39 public Image overlayImage; 40 public Key (Image mainImage, Image overlayImage) 41 { 42 this.mainImage = mainImage; 43 this.overlayImage = overlayImage; 44 } 45 46 public int hashCode() 47 { 48 return mainImage.hashCode() + overlayImage.hashCode(); 49 } 50 51 public boolean equals(Object object) 52 { 53 if (object instanceof Key) 54 { 55 Key key = (Key)object; 56 return key.mainImage == mainImage && key.overlayImage == overlayImage; 57 } 58 else 59 { 60 return false; 61 } 62 } 63 } 64 65 protected static HashMap images = new HashMap (); 66 67 protected AdapterFactoryMappingDomain mappingDomain; 68 69 public MappingDomainLabelProvider(AdapterFactoryMappingDomain mappingDomain) 70 { 71 super(mappingDomain.getAdapterFactory()); 72 this.mappingDomain = mappingDomain; 73 } 74 75 class MappingIndicatorCompositeImage extends CompositeImageDescriptor 76 { 77 protected ImageData mainImage; 78 protected ImageData overlayImage; 79 public MappingIndicatorCompositeImage(ImageData mainImage, ImageData overlayImage) 80 { 81 this.mainImage = mainImage; 82 this.overlayImage = overlayImage; 83 } 84 85 public void drawCompositeImage(int width, int height) 86 { 87 drawImage(mainImage, 0, 0); 88 drawImage(overlayImage, mainImage.width, 0); 89 } 90 91 public Point getSize() 92 { 93 return new Point(mainImage.width + overlayImage.width, Math.max(mainImage.height, overlayImage.height)); 94 } 95 96 public int hashCode() 97 { 98 return mainImage.hashCode() + overlayImage.hashCode(); 99 } 100 101 public boolean equals(Object object) 102 { 103 if (object instanceof MappingIndicatorCompositeImage) 104 { 105 MappingIndicatorCompositeImage that = (MappingIndicatorCompositeImage)object; 106 return that.mainImage == mainImage && that.overlayImage == overlayImage; 107 } 108 else 109 { 110 return false; 111 } 112 } 113 } 114 115 public Image getImage(Object object) 116 { 117 Image result = super.getImage(object); 118 119 if (result != null) 120 { 121 Object overlayImage; 122 123 Object mappedObject = 124 object instanceof MappedObjectItemProvider ? 125 ((MappedObjectItemProvider)object).getMappedObject() : 126 object; 127 128 Collection mappings = mappingDomain.getMappingRoot().getMappings(mappedObject); 129 130 if (mappingDomain.getMappingRoot().isTopObject(mappedObject)) 131 { 132 overlayImage = 133 MappingPlugin.getPlugin().getImage(mappings.isEmpty() ? "full/ovr16/OverlayBlank" : "full/ovr16/OverlayMappedTop"); 134 } 135 else if (mappingDomain.getMappingRoot().isBottomObject(mappedObject)) 136 { 137 overlayImage = 138 MappingPlugin.getPlugin().getImage(mappings.isEmpty() ? "full/ovr16/OverlayBlank" : "full/ovr16/OverlayMappedBottom"); 139 } 140 else 141 { 142 overlayImage= MappingPlugin.getPlugin().getImage("full/ovr16/OverlayBlank"); 143 } 144 145 Image image = getImageFromObject(overlayImage); 146 Key key = new Key(result, image); 147 Image cachedImage = (Image)images.get(key); 148 if (cachedImage == null) 149 { 150 result = new MappingIndicatorCompositeImage(result.getImageData(), image.getImageData()).createImage(); 151 images.put(key, result); 152 } 153 else 154 { 155 result = cachedImage; 156 } 157 } 158 159 return result; 160 } 161 } 162 | Popular Tags |