Java element icons SWT ECLIPSE

天大地大妈咪最大 提交于 2019-12-12 01:09:20

问题


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

回答1:


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)

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




回答2:


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.



来源:https://stackoverflow.com/questions/15921149/java-element-icons-swt-eclipse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!