EMF series (7. Preliminary introduction to. Edit)

Source: Internet
Author: User
Tags list of attributes

In addition to the interfaces and implementation classes used to generate models (also known as "core models"), EMF also generates a name named. the project ended with Edit, which has a relationship with the core model and editor.
Closely classifiedCode. This part of the code has been carefully designed and can be reused to a considerable extent. They are not only used in a large number of editor projects generated by EMF, but should also be filled when we expand the editor.
Sub-utilization.

In the example of an online store, the com. My. Shop. Edit project contains an itemprovideradapterfactory class and a group
Subclass of itemprovideradapter, which corresponds to the interfaces of the core model. For example, the shop, category, and product of the core model correspond
Shopitemprovider, categoryitemprovider, and productitemprovider. This post mainly introduces these
Itemprovider, And the content about itemprovideradapterfactory will be introduced in future posts. In fact, as the name suggests,
Itemprovideradapterfactory is mainly used to generate itemprovider. In fact, the EMF application is being constructed.ProgramWe often need to modify
The code in itemprovider, while itemprovideradapterfactory rarely changes.


Figure 1. Edit project generated by EMF

Note: The word "adapter" is omitted in the subclass name of itemprovideradapter in the. Edit project. For example:
Categoryitemprovider rather than categoryitemprovideradapter, you should be aware that it is an adapter because it
The adapter interface is indeed implemented. In EMF, another itemprovider class is specially prepared for the non-adapter type.
Itemprovider refers to xxxitemprovider, which is a subclass of itemprovideradapter.

Note: although the adapter interface in EMF is similar to the iadaptable interface in eclipse runtime, it is not the same concept (for iadaptable, see the previous translation post ),
The adapter in EMF is equivalent to the listener (listener, observer), which listens to the object is the notifier of EMF, In a notifier
You can register multiple adapters. On the other hand, itemprovideradapterfactory is similar to iadaptable, which can be used for dynamic conversion.
Type, but the former is generally only used for notifier to adapter conversion, the latter has no restrictions, and the conversion method name is also different, the former is adapt (), the latter is
Getadapter ().

As shown in figure 1, itemprovider forms the main part of the. Edit project. These itemproviders have the following functions.

1. Implemented the contentprovider and labelprovider functions in jface.

Jface viewer is a kind of packaging of controls in SWT. For example, tableviewer is a table packaging, and treeviewer is a tree package.
In this way, you can separate the control from the data displayed in the control to a certain extent, so as to facilitate the update of the data display. Many eclipse applications use jface
When the viewer displays data, the contentprovider and labelprovider associated with the viewer respectively control the data displayed in the viewer and the display mode of each data entry.

Take the contentprovider of treeviewer as an example. The itreecontentprovider interface should be implemented in jface.
The getparent (), haschildren (), and getchildren () methods are defined. In EMF
The itreeitemcontentprovider interface corresponds to this interface, which also has these three methods. Each itemprovider in the. Edit Section implements
EMF has fully known our model structure, so these three methods have been implemented in the itemprovideradapter class. However
After all, itreeitemcontentprovider cannot be directly used by jface's treeviewer. Therefore, EMF provides
Adapterfactorycontentprovider for adaptation, you can see how to use it in the editor code.

Labelprovider is similar in that it mainly controls the displayed text and icons. The itemprovider generated by EMF is not implemented by default.
Itableitemlabelprovider, so if you want to use tableviewer, You need to modify the code to implement
For the itableitemlabelprovider interface and additional methods, see productitemprovider in the online store example. Slave
From the jface perspective, itemprovider is equivalent to the Code integrating contentprovider and labelprovider of various viewers.
"Contentlabelprovider ". Therefore, developers only need to modify a small amount of code when changing the viewer, instead of changing
Write a new contentprovider and labelprovider.

2. Provides an Attribute Table for correlated objects

The getpropertydescriptors () method of each itemprovider returns the list of attributes displayed in the attribute view. Each element in the list is
Itempropertydescriptor object, which determines the label, description, icon, and whether to edit each attribute. The code generated by EMF will help us define every
Properties are displayed in the property list. If you want to hide some properties, you can delete them by modifying this method.

Taking product as an example, the getpropertydescriptors () method of productitemprovider contains the following six statements, which respectively represent the product name, price, description, availability, evaluation, and color attributes, if you want to remove the color attribute from the attribute list, you just need to delete the last sentence.

Addnamepropertydescriptor (object );
Addpricepropertydescriptor (object );
Adddescriptionpropertydescriptor (object );
Addavaiablepropertydescriptor (object );
Addscorepropertydescriptor (object );
Addbackgroundpropertydescriptor (object );

3. Various commands for generating and editing Models

There are many createxxxcommand () methods in the itemprovideradapter base class. If you have used GEF, you should be familiar with these names, because
There are similar methods in editpolicy. We know that to implement the Undo/Redo function, we should use command to implement every change to the model.
Command is saved in the Command Stack. Each command object stores Undo/Redo information. Itemprovideradapter is equivalent to
When the factory that produces these commands, the user's request for model editing will be converted to the corresponding command through it. For example, the user modifies an attribute value in the attribute view. When press enter, yes
Call the createsetcommand () method of the itemprovider class associated with this object to generate a setcommand object.

Note: The getchildrenfeatures () method is called in the createcommand () method, and this method is also required when getchildren () of contentprovider is implemented, therefore, the returned results of this method affect both itemprovider functions.

4. Notify the model change to the view responsible for displaying the Model

There are often many viewer display models in an eclipse application. No matter how you modify the model, make the content displayed in the viewer always the current model, the best way is to allow the viewer to respond to model changes. Itemprovider can be used as the listener to complete this task.

When the model changes, the categorychanged () method of the itemprovider associated with the modified object is called, and the event is immediately notified
Itemprovideradapterfactory, which is the event processing mechanism of the entire model, and all itemproviders use
Itemprovideradapterfactory is created and registered as a listener, so itemprovideradapterfactory can
Firenotifychanged () notifies all these listeners to digest the notifychanged () method. Figure 2 shows the notification process, which is from
Figure 3.10 in section 3.2.4 of eclipse modeling framework: a developer's guide.


Figure 2 notification process of itemprovider

Finally, itemprovider also has a collectnewchilddescriptors () method, which determines the corresponding
Which child elements can be created for that object. For example, in the online store model, the sub-elements of the category object are category and product. Then, right-click
Select "new" for the category object
Child, the "category" and "product" options appear. In some cases, you can modify the code to hide some of the options.

References: Eclipse modeling framework a developers Guide, section 3.2 and section 10.1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.