KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > RuntimeClasspathEntryLabelProvider


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  * BEA - Daniel R Somerfield - Bug 88939
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.debug.ui.launcher;
13
14
15 import java.io.File JavaDoc;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.ui.DebugUITools;
24 import org.eclipse.debug.ui.IDebugUIConstants;
25 import org.eclipse.jdt.core.IClasspathContainer;
26 import org.eclipse.jdt.core.IJavaElement;
27 import org.eclipse.jdt.core.IJavaProject;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.internal.debug.ui.classpath.ClasspathEntry;
30 import org.eclipse.jdt.internal.launching.JREContainer;
31 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
32 import org.eclipse.jdt.launching.IRuntimeClasspathEntry2;
33 import org.eclipse.jdt.launching.IVMInstall;
34 import org.eclipse.jdt.launching.JavaRuntime;
35 import org.eclipse.jdt.ui.ISharedImages;
36 import org.eclipse.jdt.ui.JavaUI;
37 import org.eclipse.jface.viewers.LabelProvider;
38 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.ui.model.WorkbenchLabelProvider;
41
42 import com.ibm.icu.text.MessageFormat;
43
44 /**
45  * Label provider for runtime classpath entries.
46  */

47 public class RuntimeClasspathEntryLabelProvider extends LabelProvider {
48         
49     private WorkbenchLabelProvider lp = new WorkbenchLabelProvider();
50     
51     /**
52      * Context in which to render containers, or <code>null</code>
53      */

54     private ILaunchConfiguration fLaunchConfiguration;
55     
56     /* (non-Javadoc)
57      * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
58      */

59     public Image getImage(Object JavaDoc element) {
60         IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry)element;
61         IResource resource = entry.getResource();
62         switch (entry.getType()) {
63             case IRuntimeClasspathEntry.PROJECT:
64                 //TODO what if project not loaded?
65
IJavaElement proj = JavaCore.create(resource);
66                 return lp.getImage(proj);
67             case IRuntimeClasspathEntry.ARCHIVE:
68                 if (resource instanceof IContainer) {
69                     return lp.getImage(resource);
70                 }
71                 boolean external = resource == null;
72                 boolean source = (entry.getSourceAttachmentPath() != null && !Path.EMPTY.equals(entry.getSourceAttachmentPath()));
73                 String JavaDoc key = null;
74                 if (external) {
75                     IPath path = entry.getPath();
76                     if (path != null)
77                     {
78                         //TODO: check for invalid paths and change image
79
File JavaDoc file = path.toFile();
80                         if (file.exists() && file.isDirectory()) {
81                             key = ISharedImages.IMG_OBJS_PACKFRAG_ROOT;
82                         } else if (source) {
83                             key = ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE_WITH_SOURCE;
84                         } else {
85                             key = ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE;
86                         }
87                     }
88
89                 } else {
90                     if (source) {
91                         key = ISharedImages.IMG_OBJS_JAR_WITH_SOURCE;
92                     } else {
93                         key = ISharedImages.IMG_OBJS_JAR;
94                     }
95                 }
96                 return JavaUI.getSharedImages().getImage(key);
97             case IRuntimeClasspathEntry.VARIABLE:
98                 return DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_ENV_VAR);
99             case IRuntimeClasspathEntry.CONTAINER:
100                 return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_LIBRARY);
101             case IRuntimeClasspathEntry.OTHER:
102                 IRuntimeClasspathEntry delegate = entry;
103                 if (entry instanceof ClasspathEntry) {
104                     delegate = ((ClasspathEntry)entry).getDelegate();
105                 }
106                 Image image = lp.getImage(delegate);
107                 if (image != null) {
108                     return image;
109                 }
110                 if (resource == null) {
111                     return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_LIBRARY);
112                 }
113                 return lp.getImage(resource);
114         }
115         return null;
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
120      */

121     public String JavaDoc getText(Object JavaDoc element) {
122         IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry)element;
123         switch (entry.getType()) {
124             case IRuntimeClasspathEntry.PROJECT:
125                 IResource res = entry.getResource();
126                 IJavaElement proj = JavaCore.create(res);
127                 return lp.getText(proj);
128             case IRuntimeClasspathEntry.ARCHIVE:
129                 IPath path = entry.getPath();
130                 if (path == null) {
131                     return MessageFormat.format(LauncherMessages.RuntimeClasspathEntryLabelProvider_Invalid_path, new String JavaDoc[]{"null"}); //$NON-NLS-1$
132
}
133                 if (!path.isAbsolute() || !path.isValidPath(path.toString())) {
134                     return MessageFormat.format(LauncherMessages.RuntimeClasspathEntryLabelProvider_Invalid_path, new String JavaDoc[]{path.toOSString()});
135                 }
136                 String JavaDoc[] segments = path.segments();
137                 StringBuffer JavaDoc displayPath = new StringBuffer JavaDoc();
138                 if (segments.length > 0) {
139                     displayPath.append(segments[segments.length - 1]);
140                     displayPath.append(" - "); //$NON-NLS-1$
141
String JavaDoc device = path.getDevice();
142                     if (device != null) {
143                         displayPath.append(device);
144                     }
145                     displayPath.append(File.separator);
146                     for (int i = 0; i < segments.length - 1; i++) {
147                         displayPath.append(segments[i]).append(File.separator);
148                     }
149                 } else {
150                     displayPath.append(path.toOSString());
151                 }
152                 return displayPath.toString();
153             case IRuntimeClasspathEntry.VARIABLE:
154                 path = entry.getPath();
155                 IPath srcPath = entry.getSourceAttachmentPath();
156                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc(path.toString());
157                 if (srcPath != null) {
158                     buf.append(" ["); //$NON-NLS-1$
159
buf.append(srcPath.toString());
160                     IPath rootPath = entry.getSourceAttachmentRootPath();
161                     if (rootPath != null) {
162                         buf.append(IPath.SEPARATOR);
163                         buf.append(rootPath.toString());
164                     }
165                     buf.append(']');
166                 }
167                 // append JRE name if we can compute it
168
if (path.equals(new Path(JavaRuntime.JRELIB_VARIABLE)) && fLaunchConfiguration != null) {
169                     try {
170                         IVMInstall vm = JavaRuntime.computeVMInstall(fLaunchConfiguration);
171                         buf.append(" - "); //$NON-NLS-1$
172
buf.append(vm.getName());
173                     } catch (CoreException e) {
174                     }
175                 }
176                 return buf.toString();
177             case IRuntimeClasspathEntry.CONTAINER:
178                 path = entry.getPath();
179                 if (fLaunchConfiguration != null) {
180                     try {
181                         IJavaProject project = null;
182                         try {
183                             project = JavaRuntime.getJavaProject(fLaunchConfiguration);
184                         } catch (CoreException e) {
185                         }
186                         if (project == null) {
187                             if (path.segmentCount() > 0 && path.segment(0).equals(JavaRuntime.JRE_CONTAINER)) {
188                                 IVMInstall vm = JavaRuntime.getVMInstall(path);
189                                 if (vm != null) {
190                                     JREContainer container = new JREContainer(vm, path, project);
191                                     return container.getDescription();
192                                 }
193                             }
194                         } else {
195                             IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
196                             if (container != null) {
197                                 return container.getDescription();
198                             }
199                         }
200                     } catch (CoreException e) {
201                     }
202                 }
203                 return entry.getPath().toString();
204             case IRuntimeClasspathEntry.OTHER:
205                 IRuntimeClasspathEntry delegate = entry;
206                 if (entry instanceof ClasspathEntry) {
207                     delegate = ((ClasspathEntry)entry).getDelegate();
208                 }
209                 String JavaDoc name = lp.getText(delegate);
210                 if (name == null || name.length() == 0) {
211                     return ((IRuntimeClasspathEntry2)delegate).getName();
212                 }
213                 return name;
214         }
215         return ""; //$NON-NLS-1$
216
}
217     
218     /* (non-Javadoc)
219      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
220      */

221     public void dispose() {
222         super.dispose();
223         lp.dispose();
224     }
225     
226     /**
227      * Sets the launch configuration context for this label provider
228      */

229     public void setLaunchConfiguration(ILaunchConfiguration configuration) {
230         fLaunchConfiguration = configuration;
231         fireLabelProviderChanged(new LabelProviderChangedEvent(this));
232     }
233 }
234
Popular Tags