1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="images/../../../css/book.css" type="text/css"/>
<link rel="stylesheet" href="images/../../../css/emf-book.css" type="text/css"/>
<title>Generating an Extended EMF Model</title>
</head>
<body lang="EN-US" xml:lang="EN-US">
<h1>Generating an Extended EMF Model</h1>
<p>Last updated: May 31, 2006</p>
<p>This tutorial is a follow-on to
<a href="../../tutorials/clibmod/clibmod.html">Generating an EMF Model</a>,
in which a simple library model is generated. In that tutorial, we showed how an
EMF model can be generated very easily from a Rose model or a set of Java
interface files. In this tutorial, we will show you how to generate an EMF model
that extends an existing model.</p>
<p>First of all, let us look back at the library model:</p>
<img src="images/../../clibmod/images/model.gif" alt="Library UML model"/>
<p>We are now going to extend this library model by creating a new package,
schoollibrary. This package contains three classes, two of which extend classes
in the library model:</p>
<img src="images/model.gif" alt="Schoollibrary UML model"/>
<p>This tutorial will show you step-by-step how to generate an EMF model of this
schoollibrary package, using the library model that you've already created.
As in the previous tutorial, we will demonstrate creating this new model from a
Rose model and from a set of Java interfaces.</p>
<p>The screenshots are based on version 3.2.0 RC6 of the Eclipse SDK and version 2.2.0 RC6a
of EMF.</p>
<hr/>
<h2>Contents</h2>
<table border="0" cellspacing="2" cellpadding="2" width="100%">
<tr>
<td valign="top">Step 1:</td>
<td valign="top"><a href="#step1a">Import the Model from Rose</a> or
<a href="#step1b">Define the Model Using Annotated Java</a>
</td>
</tr>
<tr>
<td valign="top">Step 2:</td>
<td valign="top"><a href="#step2">Generate the EMF Model and Editor Code</a>
</td>
</tr>
<tr>
<td valign="top">Step 3:</td>
<td valign="top"><a href="#step3">Run the Generated Editor</a> </td>
</tr>
<tr>
<td valign="top">Step 4:</td>
<td valign="top"><a href="#step4">Modify the Editor</a>
</td>
</tr>
<tr>
<td valign="top">Appendix:</td>
<td valign="top"><a href="#appendix">An Alternative Way to Generate the Model</a>
</td>
</tr>
</table>
<br/>
<hr/>
<p style="text-align: right"><a id="step0" name="step0"> </a><a href="#top">contents</a></p>
<h2>Step 0: Prerequisites</h2>
<p>The library model and editor were generated in the previous tutorial,
<a href="../../tutorials/clibmod/clibmod.html">Generating an EMF Model</a>.</p>
<ul>
<li>Launch Eclipse and verify that these three projects are shown in the
Navigator view in the Resource perspective: "library", library.edit", and
"library.editor".<br/>
<img src="images/img001.gif" alt="Existing library projects"/><br/>
</li>
</ul>
<p>If these packages are not listed, you will need to work through the previous
tutorial, or see the <a href="#appendix">Appendix</a> for how to create both
models at once.</p>
<hr/>
<p style="text-align: right"><a id="step1a" name="step1a"> </a><a href="#top">contents</a></p>
<h2>Step 1a: Import the Model from Rose</h2>
<p>Save the Rose model file
<a target="_code" href="images/../schoollibrary.mdl">schoollibrary.mdl</a>
somewhere on your workstation. It contains both the library and schoollibrary
packages.</p>
<p>When sharing packages among various models, we really should place each
package in its own .cat file, and just reference them in an .mdl file. However,
for the purposes of this tutorial, we have simply duplicated and extended the
library package in a single model file. The generator behaves exactly the same,
whether packages are contained inside a single .mdl file or referenced in
external .cat files.<br/>
</p>
<p>Create a new EMF project in the workspace:</p>
<ul>
<li>Bring up the "File/New/Project..." dialog.<br/>
<img src="images/../../clibmod/images/img101.gif" alt="File/New/Project..."/><br/>
</li>
<li>Expand "Eclipse Modeling Framework" and select "EMF Project". Click the
"Next" button.<br/>
<img src="images/../../clibmod/images/img102.gif" alt="EMF Project"/><br/>
</li>
<li>Give the project a name, say, "schoollibrary", and then click the
"Next" button.<br/>
<img src="images/img101.gif" alt="Name the project"/><br/>
</li>
<li>Select "Rose class model" and click the "Next" button.<br/>
<img src="images/../../clibmod/images/img104.gif" alt="Rose class model"/><br/>
</li>
<li>Click on the "Browse" button and use the file dialog to locate the Rose
model file. The file will be examined, and a default generator model name will
be suggested. You can change the name in the entry box if you wish. Then, click
the "Next" button.<br/>
<img src="images/img102.gif" alt="Browse for Rose file and name generator model"/><br/>
</li>
<li>The Rose model contains two packages, "org.eclipse.example.library" and
"org.eclipse.example.schoollibrary". We select only the package that we wish to
generate, "org.eclipse.example.schoollibrary". However, because the latter package
contains references to classes in the former, we need to specify where to find
that model, so that it can be reused. Click the "Browse" button.<br/>
<img src="images/img103.gif" alt="Select package"/><br/>
</li>
<li>In the file selection dialog, expand the folder that contains the
library model, and select the generator model, "library.genmodel". Click the
"OK" button.<br/>
<img src="images/img104.gif" alt="Select referenced model"/><br/>
</li>
<li>Expand the "Library" model, and select the "org.eclipse.example.library" package.
Note that the error message disappears. Click the "Finish" button.<br/>
<img src="images/img105.gif" alt="Select referenced package"/><br/>
</li>
<li>An Ecore model (schoollibrary.ecore) and a generator model
(schoollibrary.genmodel) will be created. The latter is opened in the main
view.<br/>
<img src="images/img106.gif" alt="Opened generator"/><br/>
</li>
</ul>
<hr/>
<p style="text-align: right"><a id="step1b" name="step1b"> </a><a href="#top">contents</a></p>
<h2>Step 1b: Define the Model Using Annotated Java</h2>
<p>Here are the annotated Java interfaces for the schoollibrary package. We
can generate the EMF model from these interface files, instead of a Rose
model.</p>
<blockquote>
<strong><a target="_code" href="images/../SchoolLibrary.java">SchoolLibrary.java</a></strong><a name="schoollibrarysrc"> </a><br/>
<table border="1" frame="box"><tr><td>
<pre>package org.eclipse.example.schoollibrary;
import org.eclipse.example.library.Library;
/**
* @model
*/
public interface SchoolLibrary extends Library
{
/**
* @model
*/
String getLocation();
}</pre>
</td></tr></table>
</blockquote>
<blockquote>
<strong><a target="_code" href="images/../Asset.java">Asset.java</a></strong><a name="assetsrc"> </a><br/>
<table border="1" frame="box"><tr><td>
<pre>package org.eclipse.example.schoollibrary;
/**
* @model
*/
public interface Asset
{
/**
* @model
*/
float getValue();
}</pre>
</td></tr></table>
</blockquote>
<blockquote>
<strong><a target="_code" href="images/../SchoolBook.java">SchoolBook.java</a></strong><a name="schoolbooksrc.java"> </a><br/>
<table border="1" frame="box"><tr><td>
<pre>package org.eclipse.example.schoollibrary;
import org.eclipse.example.library.Book;
/**
* @model
*/
public interface SchoolBook extends Book, Asset
{
}</pre>
</td></tr></table>
</blockquote>
<p>Create a new empty EMF project in the workspace:</p>
<ul>
<li>Bring up the "File/New/Project..." dialog.<br/>
<img src="images/../../clibmod/images/img101.gif" alt="File/New/Project..."/><br/>
</li>
<li>Expand "Eclipse Modeling Framework" and select "Empty EMF Project". Click
the "Next" button.<br/>
<img src="images/../../clibmod/images/img121.gif" alt="Empty EMF Project"/><br/>
</li>
<li>Give the project a name, say, "schoollibrary", and click the "Finish" button.<br/>
<img src="images/img121.gif" alt="Name the project"/><br/>
</li>
</ul>
<p>Add a plug-in dependency on the existing library project:</p>
<ul>
<li>In the Package Explorer view, expand "schoollibrary/META-INF" and double-click
"MANIFEST.MF" to open the Plug-in Manifest Editor.<br/>
<img src="images/img122.gif" alt="Opened Plug-in Manifest Editor"/><br/>
</li>
<li>Switch to the "Dependencies" tab, and click the "Add..." button under "Required Plug-ins."<br/>
<img src="images/img123.gif" alt="Switch to the Depedencies tab and click Add"/><br/>
</li>
<li>Select the "library" plug-in and click the "OK" button.<br/>
<img src="images/img123b.gif" alt="Select dependent plug-in: library"/><br/>
</li>
<li>Save the change to the manifest file and close the editor.<br/>
<img src="images/img124.gif" alt="New plug-in dependency added to manifest"/><br/>
</li>
</ul>
<p>You could create and type in the interfaces as in the previous tutorial, but
instead, we will show here how to import them from the zip file
<a href="images/../schoollibrary.zip">schoollibrary.zip</a>. Save this file
somewhere on your workstation, or in an otherwise empty project in your workspace.</p>
<ul>
<li>Right-click the "src" folder and select "Import..." from the pop-up
menu.<br/>
<img src="images/img125.gif" alt="Import..."/><br/>
</li>
<li>Expand "General" and select "Archive file" as the import source. Click the "Next" button.<br/>
<img src="images/img126.gif" alt="Select an import source"/><br/>
</li>
<li>Click the "Browse" button and locate the zip file. Ensure that all of the
zip file's contents are selected. You can expand the folder tree and click
"schoollibrary" to see the files it contains. Ensure that they will be imported
into "schoollibrary/src". Click the "Finish" button.<br/>
<img src="images/img127.gif" alt="Select a zip file"/><br/>
</li>
<li>Expand the "src" folder and observe that the interfaces were imported.<br/>
<img src="images/img128.gif" alt="Imported interfaces"/><br/>
</li>
</ul>
<p>Create the EMF model:</p>
<ul>
<li>Right-click the "model" folder and select "New/Other..." from the pop-up
menu.<br/>
<img src="images/img129.gif" alt="New/Other..."/><br/>
</li>
<li>Expand "Eclipse Modeling Framework" and select "EMF Model". Click the
"Next" button.<br/>
<img src="images/../../clibmod/images/img131.gif" alt="EMF Model"/><br/>
</li>
<li>Change the file name to "schoollibrary.genmodel" and click the "Next" button.<br/>
<img src="images/img130.gif" alt="Enter the file name"/><br/>
</li>
<li>Select "Annotated Java" and click the "Next" button.<br/>
<img src="images/../../clibmod/images/img134.gif" alt="Annotated Java"/><br/>
</li>
<li>Select the "org.eclipse.example.schoollibrary" package and click the "Finish" button.<br/>
<img src="images/img131.gif" alt="Select package"/><br/>
</li>
<li>An Ecore model (schoollibrary.ecore) and a generator model
(schoollibrary.genmodel) will be created. The latter is opened in the main
view.<br/>
<img src="images/img132.gif" alt="Opened generator"/><br/>
</li>
</ul>
<hr/>
<p style="text-align: right"><a id="step2" name="step2"> </a><a href="#top">contents</a></p>
<h2>Step 2: Generate the EMF Model and Editor Code</h2>
<p>The generator model shows a root object, representing the whole model. This
model object's children represent the packages in the model.</p>
<ul>
<li>Expand the model to see its various elements. Notice that the icon for the
"Library" package has a overlayed arrow, indicating that it is a reference to
the package defined in the existing library project.<br/>
<img src="images/img201.gif" alt="Expanded generator model"/><br/>
</li>
<li>You can generate the model code and the editors for all the packages in the
model in a single step by right-clicking on the root element and selecting
"Generate All" from the pop-up menu. This will also create a tests plug-in, containing generated JUnit test code.<br/>
<img src="images/img202.gif" alt="Generate All"/><br/>
</li>
<li>Code is generated into the schoollibrary, schoollibrary.edit, schoollibrary.editor,
and schoollibrary.tests projects. No code for the referenced model, library, will
be generated.<br/>
<img src="images/img203.gif" alt="Generated files"/><br/>
</li>
</ul>
<p>The code should be compiled automatically as it is generated, and should
recompile whenever it is changed. If you have disabled automatic building in the
workbench preferences, do not forget to rebuild the code whenever it
changes.</p>
<hr/>
<p style="text-align: right"><a id="step3" name="step3"> </a><a href="#top">contents</a></p>
<h2>Step 3: Run the Generated Editor</h2>
<p>In order to test the new plug-ins, a second instance of Eclipse must be launched. The plug-ins will
run in this workbench.</p>
<ul>
<li>Select one of the projects and choose "Run As/Eclipse Application" from the "Run" menu
or toolbar drop-down.<br/>
<img src="images/../../clibmod/images/img401.gif" alt="Run As/Eclipse Application"/><br/>
</li>
<li>Wait for a second instance of the Eclipse IDE to come up. Bring up the
"Help/About Eclipse Platform" dialog, click on the "Plug-in Details" button, and
verify that the generated plug-ins are there.<br/>
<img src="images/img301.gif" alt="Generated plug-ins: schoollibrary.edit, schoollibrary.editor, schoollibrary"/><br/>
</li>
</ul>
<p>The Schoollibrary Model wizard can now be used to create a new instance of
the model.</p>
<ul>
<li>Bring up the "File/New/Project..." dialog. Expand "General", select
"Project", and click the "Next" button. Give the project a name and click the
"Finish" button.<br/>
<img src="images/img302.gif" alt="Name the project"/><br/>
</li>
<li>Right-click the project and select "New/Other..." from the pop-up menu.<br/>
<img src="images/img303.gif" alt="New/Other..."/><br/>
</li>
<li>Expand "Example EMF Model Creation Wizards" and select "Schoollibrary
Model". Click the "Next" button.<br/>
<img src="images/img304.gif" alt="Schoollibrary Model"/><br/>
</li>
<li>Enter a file name for the schoollibrary model. Make sure it ends with
a ".schoollibrary" extension. Then, click the "Next" button.<br/>
<img src="images/img305.gif" alt="Name the schoollibrary model file"/><br/>
</li>
<li>Select "SchoolLibrary" as the model object and click the "Finish"
button.<br/>
<img src="images/img306.gif" alt="Select the model object"/><br/>
</li>
<li>The newly created model is opened in the main view.<br/>
<img src="images/img307.gif" alt="New schoollibrary model"/><br/>
</li>
</ul>
<p>The root object in this editor corresponds to the My.schoollibrary resource.
Note that the object beneath it is indeed a school library.</p>
<ul>
<li>Expand the "platform:/resource/librarytest/My.schoollibrary" resource to see
the "School Library" object. Select it.<br/>
<img src="images/img308.gif" alt="Expanded resource"/><br/>
</li>
<li>If the Properties view isn't already showing, right-click the "School
Library" object and select "Show Properties View" from the pop-up menu. Enter
some values for the "Location" and "Name" attributes.<br/>
<img src="images/img309.gif" alt="School library properties"/><br/>
</li>
<li>Right-click the school library and select "New Child" from the pop-up menu.
Notice that three kinds of objects can be created under a school library:
"Writer", "Book", and "School Book". Writer and Book are defined in the library
package, while SchoolBook is defined in the schoollibrary package.<br/>
<img src="images/img310.gif" alt="New Child"/><br/>
</li>
<li>Create a couple writers, a book, and a school book. Notice that SchoolBook
inherits all the attributes of Book and adds an extra attribute (value), as we
intended.<br/>
<img src="images/img311.gif" alt="School book properties"/><br/>
</li>
<li>Save the model.<br/>
<img src="images/img312.gif" alt="File/Save"/><br/>
</li>
</ul>
<p>Quit the second instance of Eclipse, returning to original, development workbench.</p>
<hr/>
<p style="text-align: right"><a id="step4" name="step4"> </a><a href="#top">contents</a></p>
<h2>Step 4: Modify the Editor</h2>
<p>This part of the tutorial will show how to modify the code that gets
generated. We'll just change a label in the generated editor, but we'll do it in
a few different, illustrative ways.</p>
<p>First, we'll make a change in the generator model, which will affect the
code that gets generated.</p>
<ul>
<li>In the school library generator model, select the "SchoolLibrary" class.
In the Properties view, change the "Label Feature" to the "location : EString"
attribute. This determines which feature will be used in the label for
SchoolLibrary objects.<br/>
<img src="images/img401.gif" alt="Change the Label Feature"/><br/>
</li>
<li>To have this change take effect, we only need to regenerate the item provider
class for SchoolLibrary. Save your changes, then right-click "SchoolLibrary" and
select "Generate Edit Code" from the pop-up menu. This generates just this one
item provider, along with the edit artifacts for the containing package and model.
There is also no harm in regenerating all the code, however, it just takes longer.<br/>
<img src="images/img402.gif" alt="Generate Edit Code"/><br/>
</li>
</ul>
<p>The following table summarizes the files that are generated by the "Generate
Model Code", "Generate Edit Code", "Generate Editor Code", and "Generate Tests
Code" menu items in the context-sensitive menus of different objects. The
"Generate All" menu item is equivalent to selecting all three menu items.</p>
<table cellpadding="2" cellspacing="2" border="1" width="100%">
<tr>
<td valign="top"> </td>
<td valign="top">Generate Model Code</td>
<td valign="top">Generate Edit Code</td>
<td valign="top">Generate Editor Code</td>
<td valign="top">Generate Tests Code</td>
</tr>
<tr>
<td valign="top">Model <M></td>
<td valign="top">
MANIFEST.MF<br/>
build.properties<br/>
plugin.properties<br/>
plugin.xml<br/>
<M>Plugin.java *<br/>
...plus the files for each package
</td>
<td valign="top">
MANIFEST.MF<br/>
build.properties<br/>
plugin.properties<br/>
plugin.xml<br/>
<M>EditPlugin.java<br/>
...plus the files for each package
</td>
<td valign="top">
MANIFEST.MF<br/>
build.properties<br/>
plugin.properties<br/>
plugin.xml<br/>
<M>EditorPlugin.java<br/>
<M>EditorAdvisor.java *<br/>
...plus the files for each package
</td>
<td valign="top">
MANIFEST.MF<br/>
build.properties<br/>
plugin.properties<br/>
plugin.xml<br/>
<M>AllTests.java<br/>
...plus the files for each package
</td>
</tr>
<tr>
<td valign="top">Package <P></td>
<td valign="top">
<P>Package.java<br/>
<P>PackageImpl.java<br/>
<P>Factory.java<br/>
<P>FactoryImpl.java<br/>
<P>Switch.java<br/>
<P>AdaptorFactory.java<br/>
<P>ResourceImpl.java *<br/>
<P>ResourceFactoryImpl.java *<br/>
<P>Validator.java *<br/>
<P>XMLProcessor.java *<br/>
...plus the files for each class and enum<br/>
and for the model
</td>
<td valign="top">
<P>ItemProviderAdaptorFactory.java<br/>
...plus the files for each class<br/>
and for the model
</td>
<td valign="top">
<P>Editor.java<br/>
<P>ModelWizard.java<br/>
<P>ActionBarContributor.java<br/>
...plus the files for the model
</td>
<td valign="top">
<P>Tests.java<br/>
<P>Example.java<br/>
...plus the files for each class<br/>
and for the model
</td>
</tr>
<tr>
<td valign="top">Class <C> </td>
<td valign="top">
<C>.java<br/>
<C>Impl.java<br/>
...plus the files for the package
</td>
<td valign="top">
<C>ItemProvider.java<br/>
...plus the files for the package
</td>
<td valign="top"> </td>
<td valign="top">
<C>Test.java<br/>
...plus the files for the package
</td>
</tr>
<tr>
<td valign="top">Enum <E></td>
<td valign="top">
<E>.java<br/>
...plus the files for the package
</td>
<td valign="top"> </td>
<td valign="top"> </td>
<td valign="top"> </td>
</tr>
</table>
<p>* These files are not generated by default.</p>
<p>Now, we can test our change.</p>
<ul>
<li>Launch a second instance of Eclipse again ("Run/Run As/Eclipse Application")
open the "My.schoollibrary" resource. Expand the resource object and select the
school library. Notice that the location of the school library is shown in the
label, instead of its name.<br/>
<img src="images/img403.gif" alt="School Library with location label"/><br/>
</li>
</ul>
<p>Suppose now that you don't like the prefix "School Library" in the label and
want to get rid of it. The only way to do this is to edit the code, but it is an
easy change to make.</p>
<p>As described in the <a href="../../references/overview/EMF.Edit.html">EMF.Edit
Framework Overview</a>, EMF.Edit uses item providers to, among other things,
determine what label to display for a given type of object. In particular, it
is the getText() method that does this, and that we'll need to change.</p>
<ul>
<li>In the Package Explorer, expand the "schoollibrary.edit" project. Locate and
open "SchoolLibraryItemProvider.java".<br/>
<img src="images/img404.gif" alt="Locate SchoolLibraryItemProvider.java"/><br/>
</li>
<li>Locate the "getText()" method in the Outline view and select it.<br/>
<img src="images/img405.gif" alt="Select getText()"/><br/>
</li>
<li>The cursor in the Java editor will move to that method.<br/>
<img src="images/img406.gif" alt="Generated implementation of getText()"/><br/>
</li>
<li>Remove the second getString() invocation, which preprends the externalized
class name string, from the return statement. To ensure the change is not lost
when the code is regenerated, the "@generated" Javadoc tag needs to be removed
as well.<br/>
<img src="images/img407.gif" alt="Modified getText() implementation: @generated removed and final operand of ternary operator changed to label"/><br/>
</li>
<li>Save the change, launch the second instace of Eclipse again, and open
"My.schoollibrary". Notice that the label for the school library now contains
just the value of its location attribute.<br/>
<img src="images/img408.gif" alt="School library with unqualified location label"/><br/>
</li>
</ul>
<p>We have changed the implementation of getText() from what was originally
generated. The label feature property on the SchoolLibrary class in the
generator model no longer has any effect on the generated code. This is because
we have removed the @generated Javadoc tag, preventing this method from being
overwritten during code generation.</p>
<p>Now suppose that you have not yet decided whether the editor should display
the value of the location attribute or of the name attribute. Instead, you want
to be able to change it via the generator model later. However, you know that
you don't want the "School Library" prefix to be displayed. Essentially, you
would like to keep the generated implementation available to be used by a
hand-coded method that removes the prefix from whatever it returns. The new
method must be called "getText()", so the generated method must be renamed.</p>
<p>Fortunately, the EMF code generator supports this: when a method it is going
to generate would conflict with a method that does not have an @generated tag,
it looks for a method with the same name plus the suffix "Gen". If the method
exists and is tagged with "@generated", the implementation will be generated
into this method, instead.</p>
<ul>
<li>Rename the getText() method to "getTextGen(), keeping its "@generated" tag
intact. To show that it will really be regenerated, remove the method body and
replace it by a single return statement. Create a getText() method that removes
the prefix from the string returned by getTextGen().<br/>
<img src="images/img409.gif" alt="Empty getTextGen() implementation and hand-coded getText()"/><br/>
</li>
<li>Save SchoolLibraryItemProvider.java, switch back to the generator model,
select "SchoolLibrary", and regenerate the edit code.<br/>
<img src="images/img410.gif" alt="Generate Edit Code"/><br/>
</li>
<li>Switch back to the getTextGen() method in SchoolLibraryItemProvider.java.
Notice that the original implementation has been generated back into it.<br/>
<img src="images/img411.gif" alt="Regenerated getTextGen()"/><br/>
</li>
</ul>
<p>You can go back to the generator model, change the label feature property,
regenerate the code, and verify that that does indeed affect the generated
code.</p>
<hr/>
<p style="text-align: right"><a id="appendix" name="appendix"> </a><a href="#top">contents</a></p>
<h2>Appendix: An Alternative Way to Generate the Model</h2>
<p>If you do not already have the base library model its editor generated in
separate projects, you can generate both the library and school library models
into the same set of projects in a single step. This can be done either from
the Rose model or the set of annotated Java interfaces.</p>
<p>Starting with the Rose model, the process is the same as described above,
except that both packages are selected for code generation.</p>
<ul>
<li>On the last page of the New Project wizard, select both the "org.eclipse.example.library" and
"org.eclipse.example.schoollibrary" packages, instead of referencing one.<br/>
<img src="images/img501.gif" alt="Select both packages for code generation"/><br/>
</li>
</ul>
<p>Starting with annotated Java interfaces, both packages are imported into a
single empty EMF project before any code generation is done.</p>
<ul>
<li>Import the packages from <a href="images/../library.zip">library.zip</a> and
<a href="images/../schoollibrary.zip">schoollibrary.zip</a>.<br/>
</li>
<li>On the last page of the New Models wizard, select both the "org.eclipse.example.library" and
"org.eclipse.example.schoollibrary" packages.<br/>
<img src="images/img502.gif" alt="Select both packages for code generation"/><br/>
</li>
</ul>
<p>When you launch the run-time workspace to test the new editor, you may
notice one small difference in the editor for the library model, as compared to
when we generated it separately from the schoollibrary model.</p>
<ul>
<li>Create or open a library model, expand the resource object, and right-click
the "Library" object. Select the "New Child" menu item.<br/>
<img src="images/img503.gif" alt="New Child"/><br/>
</li>
</ul>
<p>Notice that there are three types of children available, whereas in the
previous tutorial, there were only two. Specifically, "School Book", which comes
from the schoollibrary package, is included. Previously, the code generator was
not aware of it when generating the item provider for Library. Now, since the
two packages were generated together, the base package knows all about the
package that extends it.</p>
<hr/>
<p style="text-align: right"><a href="#top">contents</a></p>
</body>
</html>
|