KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > ViewLabelProvider


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  * Benjamin Muskalla - bug 77710
11  *******************************************************************************/

12 package org.eclipse.ui.internal.dialogs;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.viewers.IColorProvider;
19 import org.eclipse.jface.viewers.LabelProvider;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.ui.ISharedImages;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.internal.WorkbenchImages;
26 import org.eclipse.ui.internal.WorkbenchMessages;
27 import org.eclipse.ui.views.IViewCategory;
28 import org.eclipse.ui.views.IViewDescriptor;
29
30 /**
31  * Provides labels for view children.
32  */

33 public class ViewLabelProvider extends LabelProvider implements IColorProvider {
34     private HashMap JavaDoc images;
35     private final IWorkbenchWindow window;
36     private final Color dimmedForeground;
37
38     /**
39      * @param window the workbench window
40      * @param dimmedForeground the dimmed foreground color to use for views that are already open
41      */

42     public ViewLabelProvider(IWorkbenchWindow window, Color dimmedForeground) {
43         this.window = window;
44         this.dimmedForeground = dimmedForeground;
45     }
46
47     Image cacheImage(ImageDescriptor desc) {
48         if (images == null) {
49             images = new HashMap JavaDoc(21);
50         }
51         Image image = (Image) images.get(desc);
52         if (image == null) {
53             image = desc.createImage();
54             images.put(desc, image);
55         }
56         return image;
57     }
58
59     /* (non-Javadoc)
60      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
61      */

62     public void dispose() {
63         if (images != null) {
64             for (Iterator JavaDoc i = images.values().iterator(); i.hasNext();) {
65                 ((Image) i.next()).dispose();
66             }
67             images = null;
68         }
69         super.dispose();
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
74      */

75     public Image getImage(Object JavaDoc element) {
76         if (element instanceof IViewDescriptor) {
77             ImageDescriptor desc = ((IViewDescriptor) element)
78                     .getImageDescriptor();
79             if (desc != null) {
80                 return cacheImage(desc);
81             }
82         } else if (element instanceof IViewCategory) {
83             ImageDescriptor desc = WorkbenchImages
84                     .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
85             return cacheImage(desc);
86         }
87         return null;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
92      */

93     public String JavaDoc getText(Object JavaDoc element) {
94         String JavaDoc label = WorkbenchMessages.ViewLabel_unknown;
95         if (element instanceof IViewCategory) {
96             label = ((IViewCategory) element).getLabel();
97         } else if (element instanceof IViewDescriptor) {
98             label = ((IViewDescriptor) element).getLabel();
99         }
100         return DialogUtil.removeAccel(label);
101     }
102
103     public Color getBackground(Object JavaDoc element) {
104         return null;
105     }
106
107     public Color getForeground(Object JavaDoc element) {
108         if (element instanceof IViewDescriptor) {
109             IWorkbenchPage activePage = window.getActivePage();
110             if (activePage != null) {
111                 if (activePage
112                         .findView(((IViewDescriptor) element).getId()) != null) {
113                     return dimmedForeground;
114                 }
115             }
116         }
117         return null;
118     }
119 }
120
Popular Tags