Eclipse RCP获取零件实例

时间:2018-07-08 15:47:58

标签: java eclipse reference rcp

我试图获取对连接到Java类的部分的引用。我可以使用

`@PostConstruct
public void createComposite(Composite parent) {

}`

然后是我需要的“父”变量。但是我想有另一种方法。我正在尝试添加静态变量来像这样保存它:

public class BibliotekaZmianyPart {
private static Label label;
private static Button button;
private static Composite part;

@PostConstruct
public void createComposite(Composite parent) {
    part = parent;
}

public static void editBook() {
    GridLayout layout = new GridLayout(2, false);
    part.setLayout(layout);
    label = new Label(part, SWT.NONE);
    label.setText("A label");
    button = new Button(part, SWT.PUSH);
    button.setText("Press Me");
}}

,然后“ part”应该是我需要的变量-但它不起作用。

1 个答案:

答案 0 :(得分:0)

您不能拥有引用实例变量的静态方法。

如果要引用另一个零件中的现有零件,请使用EPartService查找零件:

@Inject
EPartService partService;


MPart mpart = partService.findPart("part id");

BibliotekaZmianyPart part = (BibliotekaZmianyPart)mpart.getObject();

part.editBook();   // Using non-static 'editBook'

如果尚未打开零件,则使用零件服务showPart方法:

MPart mpart = partService.showPart("part id", PartState.ACTIVATE);

BibliotekaZmianyPart part = (BibliotekaZmianyPart)mpart.getObject();

part.editBook();

所以您的课程将是:

public class BibliotekaZmianyPart {
private Label label;
private Button button;
private Composite part;

@PostConstruct
public void createComposite(Composite parent) {
    part = parent;
}

public void editBook() {
    GridLayout layout = new GridLayout(2, false);
    part.setLayout(layout);
    label = new Label(part, SWT.NONE);
    label.setText("A label");
    button = new Button(part, SWT.PUSH);
    button.setText("Press Me");

    // You probably need to call layout on the part
    part.layout();
}}