How to add the java element icons (e.g. class, methods, package, etc as that in the package explorer) in SWT's Tree control?
e.g, I have the following tree structure:
somePackage
--> somePackage
|
|--> someClass
|
|--> someMethod
Here --> represents the folding icon. I want to keep this intact but additionally I want to have the appropriate icon for respective java element (as it is visible in the package explorer). I am using SWT Tree to built the tree view. following is the code to construct it:
final Tree tree = new Tree (composite, SWT.BORDER);
for (int i=0; i<4; i++) {
// Package
TreeItem iItem = new TreeItem (tree, 0);
iItem.setText ("TreeItem (0) -" + i);
for (int j=0; j<4; j++) {
// Class
TreeItem jItem = new TreeItem (iItem, 0);
jItem.setText ("TreeItem (1) -" + j);
for (int k=0; k<4; k++) {
// Method or Fields
TreeItem kItem = new TreeItem (jItem, 0);
kItem.setText ("TreeItem (2) -" + k);
}
}
}
tree.setBounds(25, 50, 580, 200);
解决方案
You can add the dependency org.eclipse.jdt.ui to your project to get access to the shared images of the JDT project. The following code works for Eclipse 3.x. For plain SWT applications (no Eclipse plugin) you need to add a bunch of eclipse plugins (including org.eclipse.jdt.ui) to get this functionality in your project.
import org.eclipse.jdt.ui.ISharedImages;
import org.eclipse.jdt.ui.JavaUI;
...
ISharedImages images = (ISharedImages) JavaUI.getSharedImages();
Image image = images.getImage(ISharedImages.IMG_OBJS_CLASS); // class file icon
EDIT:
If you don't want to include all the libraries only because you want to use a a few eclipse images: I found a list of all eclipse shared images in the web. (based on eclipse 3.4)