KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > model > PerspectiveLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.model;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.viewers.ITableLabelProvider;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.ui.IPerspectiveDescriptor;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
24 import org.eclipse.ui.internal.WorkbenchImages;
25 import org.eclipse.ui.internal.WorkbenchMessages;
26
27 /**
28  * A table label provider implementation for showing workbench perspectives
29  * (objects of type <code>IPerspectiveDescriptor</code>) in table- and
30  * tree-structured viewers.
31  * <p>
32  * Clients may instantiate this class. It is not intended to be subclassed.
33  * </p>
34  *
35  * @since 3.0
36  */

37 public final class PerspectiveLabelProvider extends LabelProvider implements
38         ITableLabelProvider {
39
40     /**
41      * List of all Image objects this label provider is responsible for.
42      */

43     private HashMap JavaDoc imageCache = new HashMap JavaDoc(5);
44
45     /**
46      * Indicates whether the default perspective is visually marked.
47      */

48     private boolean markDefault;
49
50     /**
51      * Creates a new label provider for perspectives.
52      * The default perspective is visually marked.
53      */

54     public PerspectiveLabelProvider() {
55         this(true);
56     }
57
58     /**
59      * Creates a new label provider for perspectives.
60      *
61      * @param markDefault <code>true</code> if the default perspective is to be
62      * visually marked, and <code>false</code> if the default perspective is
63      * not treated as anything special
64      */

65     public PerspectiveLabelProvider(boolean markDefault) {
66         super();
67         this.markDefault = markDefault;
68     }
69
70     /* (non-Javadoc)
71      * @see org.eclipse.jface.viewers.ILabelProvider
72      */

73     public final Image getImage(Object JavaDoc element) {
74         if (element instanceof IPerspectiveDescriptor) {
75             IPerspectiveDescriptor desc = (IPerspectiveDescriptor) element;
76             ImageDescriptor imageDescriptor = desc.getImageDescriptor();
77             if (imageDescriptor == null) {
78                 imageDescriptor = WorkbenchImages
79                         .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE);
80             }
81             Image image = (Image) imageCache.get(imageDescriptor);
82             if (image == null) {
83                 image = imageDescriptor.createImage();
84                 imageCache.put(imageDescriptor, image);
85             }
86             return image;
87         }
88         return null;
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.jface.viewers.ILabelProvider
93      */

94     public final void dispose() {
95         for (Iterator JavaDoc i = imageCache.values().iterator(); i.hasNext();) {
96             ((Image) i.next()).dispose();
97         }
98         imageCache.clear();
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.jface.viewers.ILabelProvider
103      */

104     public final String JavaDoc getText(Object JavaDoc element) {
105         if (element instanceof IPerspectiveDescriptor) {
106             IPerspectiveDescriptor desc = (IPerspectiveDescriptor) element;
107             String JavaDoc label = desc.getLabel();
108             if (markDefault) {
109                 String JavaDoc def = PlatformUI.getWorkbench().getPerspectiveRegistry()
110                         .getDefaultPerspective();
111                 if (desc.getId().equals(def)) {
112                     label = NLS.bind(WorkbenchMessages.PerspectivesPreference_defaultLabel, label );
113                 }
114             }
115             return label;
116         }
117         return WorkbenchMessages.PerspectiveLabelProvider_unknown;
118     }
119
120     /**
121      * @see ITableLabelProvider#getColumnImage
122      */

123     public final Image getColumnImage(Object JavaDoc element, int columnIndex) {
124         return getImage(element);
125     }
126
127     /**
128      * @see ITableLabelProvider#getColumnText
129      */

130     public final String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
131         return getText(element);
132     }
133 }
134
Popular Tags