KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > CPVariableElementLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.wizards.buildpaths;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.IPath;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.widgets.Display;
21
22 import org.eclipse.jface.resource.ImageRegistry;
23 import org.eclipse.jface.viewers.DecorationOverlayIcon;
24 import org.eclipse.jface.viewers.IColorProvider;
25 import org.eclipse.jface.viewers.IDecoration;
26 import org.eclipse.jface.viewers.LabelProvider;
27
28 import org.eclipse.ui.ISharedImages;
29 import org.eclipse.ui.PlatformUI;
30
31 import org.eclipse.jdt.internal.corext.util.Messages;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 import org.eclipse.jdt.internal.ui.JavaPluginImages;
35 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
36
37
38 public class CPVariableElementLabelProvider extends LabelProvider implements IColorProvider {
39     
40     // shared, do not dispose:
41
private Image fJARImage;
42     private Image fFolderImage;
43     private Color fResolvedBackground;
44     
45     private Image fDeprecatedJARImage;
46     private Image fDeprecatedFolderImage;
47     
48     private boolean fHighlightReadOnly;
49     
50     public CPVariableElementLabelProvider(boolean highlightReadOnly) {
51         ImageRegistry reg= JavaPlugin.getDefault().getImageRegistry();
52         fJARImage= reg.get(JavaPluginImages.IMG_OBJS_EXTJAR);
53         fFolderImage= PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
54         
55         fDeprecatedJARImage= new DecorationOverlayIcon(fJARImage, JavaPluginImages.DESC_OVR_DEPRECATED, IDecoration.TOP_LEFT).createImage();
56         fDeprecatedFolderImage= new DecorationOverlayIcon(fFolderImage, JavaPluginImages.DESC_OVR_DEPRECATED, IDecoration.TOP_LEFT).createImage();
57         
58         fHighlightReadOnly= highlightReadOnly;
59         fResolvedBackground= null;
60     }
61     
62     /*
63      * @see LabelProvider#getImage(java.lang.Object)
64      */

65     public Image getImage(Object JavaDoc element) {
66         if (element instanceof CPVariableElement) {
67             CPVariableElement curr= (CPVariableElement) element;
68             IPath path= curr.getPath();
69             if (path.toFile().isFile()) {
70                 return curr.isDeprecated() ? fDeprecatedJARImage : fJARImage;
71             }
72             return curr.isDeprecated() ? fDeprecatedFolderImage : fFolderImage;
73         }
74         return super.getImage(element);
75     }
76
77     /*
78      * @see LabelProvider#getText(java.lang.Object)
79      */

80     public String JavaDoc getText(Object JavaDoc element) {
81         if (element instanceof CPVariableElement) {
82             CPVariableElement curr= (CPVariableElement)element;
83             String JavaDoc name= curr.getName();
84             IPath path= curr.getPath();
85             
86             String JavaDoc result= name;
87             ArrayList JavaDoc restrictions= new ArrayList JavaDoc(2);
88             
89             if (curr.isReadOnly() && fHighlightReadOnly) {
90                 restrictions.add(NewWizardMessages.CPVariableElementLabelProvider_read_only);
91             }
92             if (curr.isDeprecated()) {
93                 restrictions.add(NewWizardMessages.CPVariableElementLabelProvider_deprecated);
94             }
95             if (restrictions.size() == 1) {
96                 result= Messages.format(NewWizardMessages.CPVariableElementLabelProvider_one_restriction, new Object JavaDoc[] {result, restrictions.get(0)});
97             } else if (restrictions.size() == 2) {
98                 result= Messages.format(NewWizardMessages.CPVariableElementLabelProvider_two_restrictions, new Object JavaDoc[] {result, restrictions.get(0), restrictions.get(1)});
99             }
100             
101             if (path != null) {
102                 String JavaDoc appendix;
103                 if (!path.isEmpty()) {
104                     appendix= path.toOSString();
105                 } else {
106                     appendix= NewWizardMessages.CPVariableElementLabelProvider_empty;
107                 }
108                 result= Messages.format(NewWizardMessages.CPVariableElementLabelProvider_appendix, new Object JavaDoc[] {result, appendix});
109             }
110             
111             return result;
112         }
113         
114         
115         return super.getText(element);
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
120      */

121     public Color getForeground(Object JavaDoc element) {
122         return null;
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
127      */

128     public Color getBackground(Object JavaDoc element) {
129         if (element instanceof CPVariableElement) {
130             CPVariableElement curr= (CPVariableElement) element;
131             if (fHighlightReadOnly && curr.isReadOnly()) {
132                 if (fResolvedBackground == null) {
133                     Display display= Display.getCurrent();
134                     fResolvedBackground= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND);
135                 }
136                 return fResolvedBackground;
137             }
138         }
139         return null;
140     }
141
142     /* (non-Javadoc)
143      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
144      */

145     public void dispose() {
146         super.dispose();
147         fDeprecatedFolderImage.dispose();
148         fDeprecatedJARImage.dispose();
149     }
150
151 }
152
Popular Tags