epart使用parthandler在Mpart中加载表

时间:2014-09-04 13:23:20

标签: java osgi e4

我是E4的初学者,在JAVA中一直都是。 我创建了一个partHandler,我想显示在这部分的其他类中创建的表。 table在TreeTableCreation类中被cteated。如果你可以提供帮助,那就太好了。 现在它创建一个Tab,但内部没有表,并抛出Nullpointer异常。 tahnk你。<​​/ p>

public class DynamicPartHandlerCode {

@Execute
public void execute(EModelService modelService, MApplication application, final IEclipseContext context,
        @Named(IServiceConstants.ACTIVE_SHELL) final Shell shell) {
    EPartService partService = context.get(EPartService.class);
    // create new part
    MPart mPart = modelService.createModelElement(MPart.class);
    String id = "org.testeditor.ui.uidashboard.partdescriptor.0";
    mPart = partService.findPart(id);
    if (mPart == null) {
        mPart = partService.createPart(id);
    }

    List<MPartStack> stacks = modelService.findElements(application, null, MPartStack.class, null);
    stacks.get(2).getChildren().add(mPart);

    ((TreeTableCreation) mPart.getObject()).createTable();

    partService.showPart(mPart, PartState.ACTIVATE);
}
}

这里创建表的类

public class TreeTableCreation {
// Injected services

@Inject
@Named(IServiceConstants.ACTIVE_SHELL)
Shell shell;

@Inject
MPart mPart;

public void createTable() {
    // public static void main(String[] args) {
    // Shell shell;
    Display display = new Display();
    // final Shell shell = new Shell(display);
    // shell = new Shell(Display.getCurrent());
    // shell.setSize(500, 500);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    final TreeViewer v = new TreeViewer(tree);

    // Header der
    // Tabelle*********************************************************************
    String[] titles = { "Datum ", "Testname", "Erfolgreich", "Durchgefallen", "Dauer", "Läufe" };
    for (int i = 0; i < titles.length; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.CENTER);
        column.setText(titles[i]);
        column.setWidth(150);
    }

    v.setLabelProvider(new MyLabelProvider());
    v.setContentProvider(new MyContentProvider());
    v.setInput(TestResultTest.getData());

    // // selecting cells getting
    // // items******************************************************
    tree.addListener(SWT.MouseDoubleClick, new Listener() {
        final int columnCount = 6;

        public void handleEvent(Event event) {
            Point pt = new Point(event.x, event.y);
            TreeItem item = tree.getItem(pt);
            int index = tree.indexOf(item);
            System.out.println("Item Index-" + index);
            if (item == null)
                return;
            for (int i = 0; i < columnCount; i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {


                    TreeTableCreation2 anothershell = new TreeTableCreation2();
                    DrawMultipleLine grafshell = new DrawMultipleLine();

                    anothershell.open();
                    System.out.println("Läufe gewählt");

                    grafshell.open();
                    System.out.println("Dauer gewählt");
                }
            }
        }
    });

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

    display.dispose();
}

}

1 个答案:

答案 0 :(得分:0)

您无法像这样转换独立的SWT程序。您需要阅读有关编写e4程序的教程,例如this one

您不能创建新的Display,e4已经有了。{/ p>

你不能乱用当前的Shell,e4负责这个并且Shell中已有许多其他对象。

请勿拨打shell.packshell.open或使用显示调度循环。

请勿丢弃显示屏。

不要((TreeTableCreation) mPart.getObject()).createTable();。使用零件的@PostConstruct方法。

类似于:

public class TreeTableCreation {

@PostConstruct   
public void createTable(Composite parent) {

    final Tree tree = new Tree(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    final TreeViewer v = new TreeViewer(tree);

    // Header der
    // Tabelle*********************************************************************
    String[] titles = { "Datum ", "Testname", "Erfolgreich", "Durchgefallen", "Dauer", "Läufe" };
    for (int i = 0; i < titles.length; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.CENTER);
        column.setText(titles[i]);
        column.setWidth(150);
    }

    v.setLabelProvider(new MyLabelProvider());
    v.setContentProvider(new MyContentProvider());
    v.setInput(TestResultTest.getData());

    // // selecting cells getting
    // // items******************************************************
    tree.addListener(SWT.MouseDoubleClick, new Listener() {
        final int columnCount = 6;

        public void handleEvent(Event event) {
            Point pt = new Point(event.x, event.y);
            TreeItem item = tree.getItem(pt);
            int index = tree.indexOf(item);
            System.out.println("Item Index-" + index);
            if (item == null)
                return;
            for (int i = 0; i < columnCount; i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {


                    TreeTableCreation2 anothershell = new TreeTableCreation2();
                    DrawMultipleLine grafshell = new DrawMultipleLine();

                    anothershell.open();
                    System.out.println("Läufe gewählt");

                    grafshell.open();
                    System.out.println("Dauer gewählt");
                }
            }
        }
    });
}