KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > presentation > MappingDomainLabelProvider


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: MappingDomainLabelProvider.java,v 1.2 2005/06/08 06:23:57 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.presentation;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
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 JavaDoc 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 JavaDoc images = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc object)
116   {
117     Image result = super.getImage(object);
118
119     if (result != null)
120     {
121       Object JavaDoc overlayImage;
122
123       Object JavaDoc mappedObject =
124         object instanceof MappedObjectItemProvider ?
125           ((MappedObjectItemProvider)object).getMappedObject() :
126           object;
127
128       Collection JavaDoc 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