KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > ws > ActivityCategoryLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.ui.internal.activities.ws;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jface.resource.DeviceResourceException;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.resource.JFaceResources;
19 import org.eclipse.jface.resource.LocalResourceManager;
20 import org.eclipse.jface.viewers.LabelProvider;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.activities.IActivity;
24 import org.eclipse.ui.activities.ICategory;
25 import org.eclipse.ui.activities.NotDefinedException;
26
27 /**
28  * Provides labels for elements drawn from <code>IActivityManagers</code>.
29  * Ie: <code>IActivity</code> and <code>ICategory</code> objects.
30  *
31  * @since 3.0
32  */

33 public class ActivityCategoryLabelProvider extends LabelProvider {
34
35     private LocalResourceManager manager;
36     private Map JavaDoc descriptorMap = new HashMap JavaDoc();
37
38     /**
39      * Create a new instance of this class.
40      */

41     public ActivityCategoryLabelProvider() {
42         manager = new LocalResourceManager(JFaceResources.getResources());
43     }
44     
45     /* (non-Javadoc)
46      * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
47      */

48     public Image getImage(Object JavaDoc element) {
49         try {
50             ImageDescriptor descriptor = getDescriptor(element);
51             if (descriptor != null) {
52                 return manager.createImage(descriptor);
53             }
54         }
55         catch (DeviceResourceException e) {
56             //ignore
57
}
58         return null;
59     }
60     
61     private ImageDescriptor getDescriptor(Object JavaDoc element) {
62         ImageDescriptor descriptor = (ImageDescriptor) descriptorMap.get(element);
63         if (descriptor != null) {
64             return descriptor;
65         }
66         
67         if (element instanceof ICategory) {
68             ICategory category = (ICategory) element;
69             descriptor = PlatformUI.getWorkbench().getActivitySupport()
70                     .getImageDescriptor(category);
71             if (descriptor != null) {
72                 descriptorMap.put(element, descriptor);
73             }
74         } else if (element instanceof IActivity) {
75             IActivity activity = (IActivity) element;
76             descriptor = PlatformUI.getWorkbench().getActivitySupport()
77                     .getImageDescriptor(activity);
78             if (descriptor != null) {
79                 descriptorMap.put(element, descriptor);
80             }
81         }
82         return descriptor;
83     }
84
85     /* (non-Javadoc)
86      * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
87      */

88     public String JavaDoc getText(Object JavaDoc element) {
89         if (element instanceof IActivity) {
90             IActivity activity = (IActivity) element;
91             try {
92                 return activity.getName();
93             } catch (NotDefinedException e) {
94                 return activity.getId();
95             }
96         } else if (element instanceof ICategory) {
97             ICategory category = ((ICategory) element);
98             try {
99                 return category.getName();
100             } catch (NotDefinedException e) {
101                 return category.getId();
102             }
103         }
104         return super.getText(element);
105     }
106     
107     /* (non-Javadoc)
108      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
109      */

110     public void dispose() {
111         manager.dispose();
112         descriptorMap.clear();
113     }
114 }
115
Popular Tags