简体   繁体   English

如何在eclipse中创建自定义向导,该向导从EMF ecore模型读取类数据,创建其对象(通过向导)并在导航器中显示它?

[英]How to create a custom wizard in eclipse, which reads class data from EMF ecore model, creates its object (via Wizard) and shows it in navigator?

I am learning eclipse plugin development using EMF model and want to create a wizard in eclipse (like the one to create a class), which reads an EMF ecore model and gives functionality to create objects of classes in ecore model via the Wizard. 我正在学习使用EMF模型进行的eclipse插件开发,并想在eclipse中创建一个向导(就像创建类的向导一样),该向导读取EMF ecore模型并提供通过向导在ecore模型中创建类对象的功能。

This object should then appear in the navigator view (say package explorer), under appropriate groups. 然后,该对象应出现在导航器视图中(例如,程序包浏览器),在适当的组下。 (grouped by class and its references) (按类及其引用分组)

I have the following ecore model. 我有以下ecore模型。 Here I have two classes Course and Student . 在这里我有两个课程CourseStudent Each course can have many students. 每个课程可以有很多学生。 Both classes have two attributes Id and Name . 这两个类都有两个属性IdName Course has another attribute, a list of students taking that course ( Students ). 课程还有另一个属性,即参加该课程的学生列表( Students )。

Ecore模型

I have another plugin project, in which I want to use the meta data from above ecore model and create a Wizard to create objects for these classes. 我有另一个插件项目,其中我想使用来自ecore模型的元数据,并创建一个向导来为这些类创建对象。 See the example Wizard for Student class below. 请参阅下面的学生类向导示例。

学生向导

After clicking Finish, this object should appear in the navigator as shown below. 单击完成后,该对象应出现在导航器中,如下所示。 Clicking on this object will open and editor to edit it. 单击该对象将打开并进行编辑以对其进行编辑。 Note that the folders Course1 and Course2 are not actually folders, but objects of class Course. 需要注意的是文件夹Course1Course2实际上不是文件夹,但类课程的对象。 Thus the grouping of nodes under Courses and Students is also required. 因此,还需要对“ 课程学生”下的节点进行分组。 学生浏览器/编辑器

What I have done so far. 到目前为止我所做的。

  • Created the ecore model and generated code. 创建了ecore模型并生成了代码。
  • Created a Wizard in another plugin which looks like the second image. 在另一个类似于第二张图片的插件中创建了一个向导。
  • Following is the code for Student Wizard's First Page. 以下是学生向导首页的代码。

public class StudentPageOne extends WizardPage { 公共类StudentPageOne扩展了WizardPage {

private Text texts[]; //texts for Id and Name
private Composite container;

public StudentPageOne() {

}

@Override
public void createControl(Composite parent) {
    container = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    container.setLayout(layout);
    layout.numColumns = 2;

    SimpleExamplePackage eINSTANCE = SimpleExample.impl.SimpleExamplePackageImpl.init();
    int student_features = SimpleExamplePackage.STUDENT_FEATURE_COUNT;

    texts = new Text[student_features];
    for(int i =0;i<student_features;i++) {

        System.out.println(eINSTANCE.getStudent().getEStructuralFeature(i).getName());
        Label label = new Label(container, SWT.NONE);
        label.setText(eINSTANCE.getStudent().getEStructuralFeature(i).getName());

        texts[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
        texts[i].setText("");
        texts[i].addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }

        });
        texts[i].setLayoutData(gd);

    }

    // required to avoid an error in the system
    setControl(container);
    setPageComplete(false);

}

public Text[] getTexts() {
    return texts;
}

} }

  • I get the Id and Name from the first page (via getTexts method) and create an object student , shown in the following code. 我从第一页获得ID名称 (通过getTexts方法),并创建一个对象Student ,如以下代码所示。

     public boolean performFinish() { Text[] texts = one.getTexts(); SimpleExamplePackage.eINSTANCE.eClass(); SimpleExampleFactory factory = SimpleExampleFactory.eINSTANCE; SimpleExample.Student student = factory.createStudent(); student.setId(Integer.parseInt(texts[0].getText())); student.setName(texts[1].getText()); return true; } 

What I want to do next 我下一步要做什么

I now want to show the student object from above code in the navigator (3rd Image) and create an editor to edit its attributes. 现在,我想在导航器(第3个图像)中显示上述代码中的Student对象,并创建一个编辑器来编辑其属性。

Further I would want to store the whole project as an xml. 此外,我想将整个项目存储为xml。

Please let me know if there's a better way to do what I have done till now and if there is some reference or tutorial available online to achieve it. 请让我知道,到目前为止,是否有更好的方法可以做我所做的事情,并且在线上有一些参考或指南可以实现。

You might want to take a look at EMF Parsley 您可能想看看EMF Parsley

With Parsley, you can create custom editors and views based on EMF models with relative easy. 使用Parsley,您可以相对容易地基于EMF模型创建自定义编辑器和视图。 Or may use its components in wizard pages. 或者可以在向导页面中使用其组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM