KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > parts > SharedLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.ui.parts;
12
13 import java.net.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Hashtable JavaDoc;
17
18 import org.eclipse.jface.resource.*;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.swt.*;
21 import org.eclipse.swt.graphics.*;
22 import org.eclipse.update.internal.ui.*;
23
24 /**
25  * @version 1.0
26  * @author
27  */

28 public class SharedLabelProvider
29     extends LabelProvider
30     implements ITableLabelProvider {
31     public static final int F_ERROR = 1;
32     public static final int F_WARNING = 2;
33     public static final int F_CURRENT = 4;
34     public static final int F_INSTALLABLE = 8;
35     public static final int F_LINKED = 16;
36     public static final int F_MOD = 32;
37     public static final int F_UPDATED = 64;
38     public static final int F_UNCONFIGURED = 128;
39     public static final int F_ADD = 256;
40     public static final int F_DEL = 512;
41
42     Hashtable JavaDoc images = new Hashtable JavaDoc();
43     ArrayList JavaDoc consumers = new ArrayList JavaDoc();
44
45     public SharedLabelProvider() {
46     }
47
48     public void connect(Object JavaDoc consumer) {
49         if (!consumers.contains(consumer))
50             consumers.add(consumer);
51     }
52
53     public void disconnect(Object JavaDoc consumer) {
54         consumers.remove(consumer);
55         if (consumers.size() == 0) {
56             reset();
57         }
58     }
59     
60     public void dispose() {
61         reset();
62         super.dispose();
63     }
64
65     private void reset() {
66         for (Enumeration JavaDoc iterator = images.elements(); iterator.hasMoreElements();) {
67             Image image = (Image) iterator.nextElement();
68             image.dispose();
69         }
70         images.clear();
71     }
72
73     public Image get(ImageDescriptor desc) {
74         return get(desc, 0);
75     }
76
77     public Image get(ImageDescriptor desc, int flags) {
78         Object JavaDoc key = desc;
79
80         if (flags != 0) {
81             key = getKey(desc.hashCode(), flags);
82         }
83         Image image = (Image) images.get(key);
84         if (image == null) {
85             image = createImage(desc, flags);
86             images.put(key, image);
87         }
88         return image;
89     }
90     
91     public Image get(Image image, int flags) {
92         if (flags==0) return image;
93         String JavaDoc key = getKey(image.hashCode(), flags);
94         Image resultImage = (Image)images.get(key);
95         if (resultImage == null) {
96             resultImage = createImage(image, flags);
97             images.put(key, resultImage);
98         }
99         return resultImage;
100     }
101
102     private String JavaDoc getKey(long hashCode, int flags) {
103         return (""+hashCode) + ":"+flags; //$NON-NLS-1$ //$NON-NLS-2$
104
}
105
106     private Image createImage(ImageDescriptor baseDesc, int flags) {
107         if (flags == 0) {
108             return baseDesc.createImage();
109         }
110         ImageDescriptor[] lowerLeft = getLowerLeftOverlays(flags);
111         ImageDescriptor[] upperRight = getUpperRightOverlays(flags);
112         ImageDescriptor[] lowerRight = getLowerRightOverlays(flags);
113         ImageDescriptor[] upperLeft = getUpperLeftOverlays(flags);
114         OverlayIcon compDesc =
115             new OverlayIcon(
116                 baseDesc,
117                 new ImageDescriptor[][] { upperRight, lowerRight, lowerLeft, upperLeft });
118         return compDesc.createImage();
119     }
120     
121     private Image createImage(Image baseImage, int flags) {
122         if (flags == 0) {
123             return baseImage;
124         }
125         ImageDescriptor[] lowerLeft = getLowerLeftOverlays(flags);
126         ImageDescriptor[] upperRight = getUpperRightOverlays(flags);
127         ImageDescriptor[] lowerRight = getLowerRightOverlays(flags);
128         ImageDescriptor[] upperLeft = getUpperLeftOverlays(flags);
129         ImageOverlayIcon compDesc =
130             new ImageOverlayIcon(
131                 baseImage,
132                 new ImageDescriptor[][] { upperRight, lowerRight, lowerLeft, upperLeft });
133         return compDesc.createImage();
134     }
135
136     private ImageDescriptor[] getLowerLeftOverlays(int flags) {
137         if ((flags & F_ERROR) != 0)
138             return new ImageDescriptor[] { UpdateUIImages.DESC_ERROR_CO };
139         if ((flags & F_WARNING) != 0)
140             return new ImageDescriptor[] { UpdateUIImages.DESC_WARNING_CO };
141         return null;
142     }
143
144     private ImageDescriptor[] getUpperRightOverlays(int flags) {
145         if ((flags & F_ADD) != 0)
146             return new ImageDescriptor[] { UpdateUIImages.DESC_ADD_CO };
147         if ((flags & F_DEL) != 0)
148             return new ImageDescriptor[] { UpdateUIImages.DESC_DEL_CO };
149         if ((flags & F_UNCONFIGURED) != 0)
150             return new ImageDescriptor[] { UpdateUIImages.DESC_UNCONF_CO };
151         if ((flags & F_LINKED) != 0)
152             return new ImageDescriptor[] { UpdateUIImages.DESC_LINKED_CO };
153         return null;
154     }
155     
156     private ImageDescriptor[] getLowerRightOverlays(int flags) {
157         if ((flags & F_CURRENT) != 0)
158             return new ImageDescriptor[] { UpdateUIImages.DESC_CURRENT_CO };
159         if ((flags & F_MOD) != 0)
160             return new ImageDescriptor[] { UpdateUIImages.DESC_MOD_CO };
161         /*
162         if ((flags & F_ADD) != 0)
163             return new ImageDescriptor[] { UpdateUIImages.DESC_ADD_CO };
164         if ((flags & F_DEL) != 0)
165             return new ImageDescriptor[] { UpdateUIImages.DESC_DEL_CO };
166         if ((flags & F_UNCONFIGURED) != 0)
167             return new ImageDescriptor[] { UpdateUIImages.DESC_UNCONF_CO };
168         */

169         return null;
170     }
171     
172     private ImageDescriptor[] getUpperLeftOverlays(int flags) {
173         if ((flags & F_UPDATED) != 0)
174             return new ImageDescriptor[] { UpdateUIImages.DESC_UPDATED_CO };
175         return null;
176     }
177
178     public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
179         return getText(obj);
180     }
181     public Image getColumnImage(Object JavaDoc obj, int index) {
182         return getImage(obj);
183     }
184
185     public Image getImageFromURL(
186         URL installURL,
187         String JavaDoc subdirectoryAndFilename) {
188         Image image = null;
189         try {
190             URL newURL = new URL(installURL, subdirectoryAndFilename);
191             String JavaDoc key = newURL.toString();
192             image = (Image)images.get(key);
193             if (image == null) {
194                 ImageDescriptor desc = ImageDescriptor.createFromURL(newURL);
195                 image = desc.createImage();
196                 images.put(key, image);
197             }
198         } catch (MalformedURLException e) {
199         } catch (SWTException e) {
200         }
201         return image;
202     }
203 }
204
Popular Tags