Obaid Maroof
Obaid Maroof

Reputation: 1579

Java element icons SWT ECLIPSE

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);

Upvotes: 0

Views: 1533

Answers (2)

jens-na
jens-na

Reputation: 2274

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

enter image description here

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)

Source:
interface org.eclipse.jdt.ui.ISharedImages

Upvotes: 3

Baz
Baz

Reputation: 36894

Doesn't TreeItem.setImage(Image) do what you want?

public static void main(String[] a)
{
    Display display = new Display();
    Shell shell = new Shell(display);

    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout());
    Tree tree = new Tree(shell, SWT.SINGLE | SWT.BORDER);
    TreeItem child1 = new TreeItem(tree, SWT.NONE);
    child1.setText("1");
    child1.setImage(display.getSystemImage(SWT.ICON_INFORMATION));

    TreeItem child11 = new TreeItem(child1, SWT.NONE);
    child11.setText("1_1");

    TreeItem child2 = new TreeItem(tree, SWT.NONE);
    child2.setText("2");
    child2.setImage(display.getSystemImage(SWT.ICON_ERROR));

    TreeItem child22 = new TreeItem(child2, SWT.NONE);
    child22.setText("2_2");

    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

If you are just looking for the Eclipse icons, you can find them here.

Upvotes: 2

Related Questions