The third Post introduces how to customize an EMF application.ProgramThis article describes how to customize the application interface. No interface is omnipotent, so the customization work
Inevitably, most of the customization is through modificationCode. In practical applications, the same requirement may be implemented in multiple ways. In my opinion, the fewer modifications (classes and methods), the better.
Give full play to the advantages of EMF, so we should have a certain understanding of the Code generated by EMF, which is the basis for exerting our creativity.
The following are several common requirements. Through the implementation of these requirements, I believe you will have a more specific understanding of the development process of the EMF application.
1. Simplified Model Creation wizard
EMF helps us create a Model Wizard (menu File> New> Other-> shop
Model) is divided into two steps, the first step requires the user to enter the file name, for the store example file name is *. shop format; Step 2: select the object to use as the root node, and specify the XML file
The store object is obviously the root node in the store example, so the second step can be omitted to avoid user problems.
The generated wizard class is shopmodelwizard, which is much easier to remove than adding a step. First, find the addpages () method and comment out the last four statements about initialobjectcreationpage;
/**
* The framework callthis to create the contents of the wizard.
* <! -- Begin-user-doc -->
* <! -- End-user-doc -->
* @ Generated not
*/
Public Void Addpages (){
// Initialobjectcreationpage = new shopmodelwizardinitialobjectcreationpage ("whatever2 ");
// Initialobjectcreationpage. settitle (shopeditorplugin. instance. getstring ("_ ui_shopmodelwizard_label "));
// Initialobjectcreationpage. setdescription (shopeditorplugin. instance. getstring ("_ ui_wizard_initial_object_description "));
// Addpage (initialobjectcreationpage );
}
Now, because the Wizard Page is unavailable, we need to provide the information provided by it in the program, so we need to modify the other two methods: first, createinitialmodel () the method is to create a model of the user-selected object as the root node. We will change it to directly create a shop object;
Protected Eobject createinitialmodel (){
// Eclass = (eclass) shoppackage. geteclassifier (initialobjectcreationpage. getinitialobjectname ());
// Eobject rootobject = shopfactory. Create (eclass );
Eobject rootobject = Shopfactory. createshop ();
Return Rootobject;
}
Second, set the file encoding in the javasmfinish () method, instead of using UTF-8 encoding, of course you can also specify other encoding, but the user can not choose:
Public Boolean Restore mfinish (){
Options. Put (xmlresource. option_encoding, " UTF-8 " /* Initialobjectcreationpage. getencoding () */ );
}
Therefore, we have modified a total of three methods in this class. Remember to delete or modify the @ generated mark before each method. Now, you can simply specify the file name and finish it, as shown in 1.
Figure 1 last page of the wizard
Ii. display the Outline View
For EMF, there are two layers on the Root Node of the application model: Resource and resourceset. In the store example, the parent node of category
Is shop, the parent node of shop is resource (xmlresource), and the parent node of resource is resourceset.
Is a many-to-one relationship. By default, the Outline View displays the complete resourceset tree (root nodes are not displayed), and the top node displayed is "Platform:
/Resource/project3/My. Shop ", which represents a resource. This resource (through this URI) points to
XML file my. Shop. This node is meaningless for the user. The root node you see should be the shop object saved in the. Shop file, as shown in Figure 2
.
Figure 2 Hide the top node in the Outline View
Which code should be modified to meet this requirement? The content in the Outline View is obtained from the getadapter () method of shopeditor.
Shopeditor's getadapter () method finds that the method named getcontentoutlinepage () is responsible for generating the outline model. In this method, change
Contentoutlineviewer is the packaging object of the tree control in the Outline View. Its input (input) is
Editingdomain. getresourceset (), we need to change its input to the first resource of resourceset. The modified code is as follows:
Below:
Public Icontentoutlinepage getcontentoutlinepage (){
// Contentoutlineviewer. setinput (editingdomain. getresourceset ());
Contentoutlineviewer. setinput (editingdomain. getresourceset (). getresources (). Get ( 0 ));
}
You may ask, how do you get the shop object from resource? Very simple, (Shop) yourresource. getcontents (). Get
(0). If you are interested, you can try setting the input in the outline view as a shop object to see something. Do not forget to modify @ generated for the last time. I will not remind you later.
3. Remove unnecessary tab pages in the editor
The editor generated by EMF provides six tabs for us to demonstrate how to display data in various ways (for example, select a category pair in the Outline View ).
You can easily see the previous category-> shop-> resource-> resourceset mark on the parent page.
System). Generally, these pages are not used in actual applications. Below we will only keep the table page and remove the other five pages. Using the combination of outline and table pages, we can achieve resources similar to Windows.
Manager interface.
The page in the editor is added in the createpages () method. Although it is very long, EMF generates a lot of comments in this method. The role of each code segment is obvious, as long as we do not
Comment out the five required sections. Now, run the program, open a model file, slightly adjust the layout, and place the Outline View next to the editor, as shown in 3. It looks like a resource manager.
Figure 3 similar layout as Resource Manager
However, unfortunately, there are only two columns in the table, and the content in the two columns is the same. According to the Resource Manager design, When you select an object in the Outline View, the table should display the list of sub-object details of this object, now that the sub-Object List is available (each row in the table is a sub-object), let's make some modifications to display details.
First, add a table column, or modify it in the createpages () method of shopeditor. It is easy to search tablecolumn to find the bit to be modified.
. The names of the new three columns are "name", "children", and "Description". The children column displays the number of sub-objects.
Public Void Createpages (){
Tablecolumn objectcolumn = New Tablecolumn (table, SWT. None );
Layout. addcolumndata ( New Columnweightdata ( 3 , 100 , True ));
Objectcolumn. settext ( " Name " );
Objectcolumn. setresizable ( True );
Tablecolumn childrencolumn = New Tablecolumn (table, SWT. None );
Layout. addcolumndata ( New Columnweightdata ( 2 , 100 , True ));
Childrencolumn. settext ( " Children " );
Childrencolumn. setresizable ( True );
Tablecolumn desccolumn = New Tablecolumn (table, SWT. None );
Layout. addcolumndata ( New Columnweightdata ( 2 , 100 , True ));
Desccolumn. settext ( " Description " );
Desccolumn. setresizable ( True );
}
If you run the program now, you will see three columns, but the content is still the same. The content displayed in the table is determined by the generated xxxitemprovider class. For example
How to display a category object in a table or tree control is the responsibility of categoryitemprovider. You can regard it
After adding labelprovider to contentprovider, xxxitemprovider is put in the. Edit project. EMF generated
Categoryitemprovider does not implement the itableitemlabelprovider interface, so table display is not supported by default (can be displayed,
But the content of each column is the same), so we need to modify the code and add it to the list of interfaces implemented by categoryitemprovider.
Itableitemlabelprovider and implement its two methods. The modified code is as follows:
Public Class Categoryitemprovider
Extends Itemprovideradapter
Implements
Ieditingdomainitemprovider,
Istructureditemcontentprovider,
Itreeitemcontentprovider,
Iitemlabelprovider,
Iitempropertysource,
Itableitemlabelprovider {
Public Object getcolumnimage (Object object, Int Columnindex ){
Return Null ;
}
Public String getcolumntext (Object object, Int Columnindex ){
CATEGORY category = (Category) object;
Switch (Columnindex ){
Case 0 :
Return Category. getname ();
Case 1 :
Return Category. getchildren (). Size () + "" ;
Case 2 :
Return "" ; // Categories don't own descriptions
Default :
Return "" ;
}
}
}
Now, only one step is complete. If you have read the tableviewer code defined in the createpages () method of shopeditor, you will find this
Tableviewer's contentprovider and labelprovider are both
Adapterfactorycontentprovider object, which sets tableviewer to gettext () and getelements
() The request is forwarded to xxxitemprovider. before forwarding, it needs to obtain the xxxitemprovider, which is through
The Adapt () method of shopitemprovideradapterfactory is implemented, while
Shopitemprovideradapterfactory maintains a supporttypes list. Only types registered in this list can be added to adapt.
There are a lot of new content, which may not be easy to understand. It doesn't matter, because they will be introduced in future posts, now remember to register the newly implemented interface type
Shopitemprovideradapterfactory in supporttypes. The specific method is to modify its construction method, as shown below:
Public Shopitemprovideradapterfactory (){
Supportedtypes. Add (ieditingdomainitemprovider. Class );
Supportedtypes. Add (istructureditemcontentprovider. Class );
Supportedtypes. Add (itreeitemcontentprovider. Class );
Supportedtypes. Add (iitemlabelprovider. Class );
Supportedtypes. Add (iitempropertysource. Class );
Supportedtypes. Add (itableitemlabelprovider. Class ); // Added to support table
}
Now, the category object displayed in the table lists other information as required. 4 shows that the description column is blank because the category does not have this
Attributes. We should also modify the productitemprovider to display the product details. The method is similar to modifying the category, and
The steps of supporttypes do not need to be repeated, so it is simpler. You may leave them for exercises.
Figure 4 customized table
After the above customization, we have implemented the conversion of the application from the EMF default interface to the Resource Manager style interface. Although there are many texts, this process will be quite fast in the future, other customization methods are the same. Click here to download the code.