Debian Codegraph @

doc/org.eclipse.emf.doc/tutorials/slibmod/slibmod.html

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