KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > FileEditorMappingLabelProvider


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.ui.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.viewers.ITableLabelProvider;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.osgi.util.TextProcessor;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.ui.IFileEditorMapping;
22
23 /**
24  * A label provider for displaying of <code>IFileEditorMapping</code>
25  * objects in viewers.
26  * <p>
27  * This class has a singleton instance,
28  * <code>FileEditorMappingLabelProvider.INSTANCE</code>,
29  * which can be used any place this kind of label provider is needed.
30  * </p>
31  * <p>
32  * The singleton instance hangs on to images, which get freed up when
33  * <code>dispose</code> is called.
34  * </p>
35  *
36  * @see org.eclipse.jface.viewers.ILabelProvider
37  */

38 public class FileEditorMappingLabelProvider extends LabelProvider implements
39         ITableLabelProvider {
40
41     /**
42      * Singleton instance accessor.
43      */

44     public final static FileEditorMappingLabelProvider INSTANCE = new FileEditorMappingLabelProvider();
45
46     /**
47      * Images that will require disposal.
48      */

49     private List JavaDoc imagesToDispose = new ArrayList JavaDoc();
50
51     /**
52      * Creates an instance of this class. The private visibility of this
53      * constructor ensures that this class is only usable as a singleton.
54      */

55     private FileEditorMappingLabelProvider() {
56         super();
57     }
58
59     /* (non-Javadoc)
60      * Method declared on ILabelProvider.
61      */

62     public void dispose() {
63         super.dispose();
64         for (Iterator JavaDoc e = imagesToDispose.iterator(); e.hasNext();) {
65             ((Image) e.next()).dispose();
66         }
67         imagesToDispose.clear();
68     }
69
70     /**
71      * The <code>ResourceTypeEditorMappingLabelProvider</code> implementation of this
72      * <code>ITableLabelProvider</code> method creates and returns an new image. The
73      * image is remembered internally and will be deallocated by
74      * <code>dispose</code>.
75      */

76     public Image getColumnImage(Object JavaDoc element, int row) {
77         return getImage(element);
78     }
79
80     /* (non-Javadoc)
81      * Method declared on ITableLabelProvider.
82      */

83     public String JavaDoc getColumnText(Object JavaDoc element, int row) {
84         return getText(element);
85     }
86
87     /**
88      * The <code>ResourceTypeEditorMappingLabelProvider</code> implementation of this
89      * <code>ILabelProvider</code> method creates and returns an new image. The image
90      * is remembered internally and will be deallocated by <code>dispose</code>.
91      */

92     public Image getImage(Object JavaDoc element) {
93         if (element instanceof IFileEditorMapping) {
94             Image image = ((IFileEditorMapping) element).getImageDescriptor()
95                     .createImage();
96             imagesToDispose.add(image);
97             return image;
98         }
99         return null;
100     }
101
102     /* (non-Javadoc)
103      * Method declared on ILabelProvider.
104      */

105     public String JavaDoc getText(Object JavaDoc element) {
106         if (element instanceof IFileEditorMapping) {
107             return TextProcessor.process(((IFileEditorMapping) element)
108                     .getLabel(), "*."); //$NON-NLS-1$
109
}
110
111         return null;
112     }
113 }
114
Popular Tags