LAB-8110 NetBeans Enterprise Pack: Advanced UML Modeling

Contributed and maintained by Alexandr Scherbatiy, Andrew Korostelev, Anatoli Fomenko

The NetBeans IDE, in addition to providing support for developing IDE plug-in modules and rich client applications based on the NetBeans platform, contains the NetBeans Enterprise Pack 5.5 Preview software which in turn provides UML modeling, Orchestration Designer, and XML tools to the NetBeans community.

This Hands-on Lab takes you through the process of developing a Java application with the UML Modeling component of the NetBeans Enterprise Pack 5.5 Preview. During this lab you will learn how to design and develop a real life Java application, how to create a reverse engineered UML project from a Java library, how to analyze a project structure and find and correct mistakes using the UML graphical presentation.

Expected duration: 90 minutes

Prerequisites

This exercise assumes you have a basic familiarity with the Java programming language and UML modeling. If you are new to UML, a good resource for UML modeling techniques and theory is the official UML resource page at http://www.uml.org/.

System requirements

This tutorial assumes your system meets the following requirements.

Software Needed for the Tutorial

Before you begin, you need to install the following software on your computer.

Notations Used in the Tutorial

Tutorial Exercises

Where to send questions or feedback's on this lab and public discussion forums

Exercise 0: Starting Netbeans 5.5 (5 minutes)

In this exercise, you will learn how to start the Netbeans IDE. 

Start NetBeans 5.5 Preview (if you have not done it so yet)

  1. Under Windows, do one of the following:


  2. Under Solaris/Linux, type the following commands in a terminal window:


Exercise 1: Developing Application using UML Modeling


In this exercise we will develop a small application that implements editing a simple contact list.

The Contact information will be based on the Contact specifications described in RFC 2425 and RFC 2426. These specifications describe the widely used vcard format used for interchanging contact information between computer based address books. For brevity we will work directly with a subset of those specifications.

Firstly, to identify the primary elements and processes of the application, we will develop a Use Case Model using the UML Use Case Modelling.

Then we will develop the information model using a UML Class Diagram. UML code generation feature will allow us to generate the java source code for designed application. We will learn how model updates affect source and source updates affect model. At the end we will compile and run generated application.


Steps to follow:

  1. Creating a UML Java-Platform Model Project
  2. Developing a Use Case Model
  3. Developing a Class Hierarchy
  4. Defining Application Classes
  5. Generating Java Source Code
  6. Filling up Bodies of Generated Operations
  7. Run The Developed Application

Creating a UML Java-Platform Model Project


  1. Choose File from the menu bar and then select New Project. The New Project dialog box appears.
  2. Under Choose Project pane, select UML under Categories and select Java-Platform Model under Projects.
  3. Select UML Java-Platform Model project

  4. Click Next.
  5. Under Name and Location pane,
    • For the Project Name field, enter ContactLibrary_UML.
    • For the Project Location field, click Browse to navigate, select the <lab_root>\nbeuml2 directory and click Open.

    Select Name And Location for UML Java-Platform Model project

  6. Click Finish.

    The New Wizard dialog box appears.

  7. For the Diagram Type field, select Use Case Diagram.
  8. For the Diagram Name field, enter ContactUseCaseDiagram.
  9. Select And Name the Use Case Diagram

  10. Click OK.

    The ContactLibrary_UML project appears in the Projects window.


  11. Click to Enlarge.
    Click to Enlarge

Developing a Use Case Model


  1. Activate ContactUseCaseDiagram diagram by click on diagram's tab or by double click in project tree.
  2. Open UML Palette (if closed) with main menu Window > Palette.
  3. Add Actor to the diagram.
    • Select Actor in palette.
    • Click left mouse button in free space of diagram area.
      You'll get new Actor element in your model.
  4. Name it ContactManager.
    • Select the element on the diagram (if not selected).
    • Type name of element and press Enter.

    Click to Enlarge - Actor.
    Click to Enlarge

  5. Add Package to the diagram.
  6. Name it contactAssistant.

  7. Enlarge the Package to a hold a couple of Use Cases.
  8. Use Case Diagram - Package

  9. Add UseCase to the Package.
  10. Name it Manage Contacts.

  11. Draw Association link from ContactManager actor to Manage Contacts use case.
    • Select Association link in palette.
    • Click on Actor element.
    • Click on Use Case element.

    Click to Enlarge - Association Link.
    Click to Enlarge

  12. Add UseCase to the Package.
  13. Name it Add Contact.

  14. Draw Include link from Manage Contacts use case to Add Contact use case.

    Click to Enlarge - Include Link.
    Click to Enlarge

  15. Add UseCase to the Package.
  16. Name it Add Personal Information.

  17. Draw Include link from Add Contact use case to Personal Information use case.

  18. Add UseCase to the Package.
  19. Name it Add First Name.

  20. Add UseCase to the Package.
  21. Name it Add Last Name.

  22. Draw Include link from Add Personal Information use case to Add First Name use case.

  23. Draw Include link from Add Personal Information use case to Add Last Name use case.

    Click to Enlarge - Personal Information Use Case.
    Click to Enlarge

  24. Insert Add Birth Date Extension Point to Add Personal Information use case.
    • Invoke Context menu on the use case.
    • Use Case Diagram - Extension Point Context menu

    • Click on Insert Extension Point menu item.
      Text field was added to use case.
    • Type Extension Point name.
    • Press Enter.
    • Use Case Diagram - Extension Point

  25. Add UseCase to the Package.
  26. Name it Add EMail Address.

  27. Draw Include link from Add Contact use case to Add EMail Addres use case.

  28. Insert Add Address Extension Point to Add Contact use case.
  29. Insert Add Organization Extension Point to Add Contact use case.
  30. Insert Update Contact Extension Point to Manage Contacts use case.
  31. Insert Remove Contact Extension Point to Manage Contacts use case.
  32. Click to Enlarge - Use Case Diagram.
    Click to Enlarge

Developing a Class Hierarchy


  1. Create ContactClassDiagram under UML Project Model tree.
    • In the Projects window, expand the ContactLibrary_UML node.
    • Invoke Context menu on the Model node.
    • Click on Add Diagram menu item.
      The New Wizard dialog box appears.
    • For the Diagram Type field, select Class Diagram.
    • For the Diagram Name field, enter ContactClassDiagram.
    • Class Hierarchy- New Wizard

    • Click OK.
  2. Activate ContactClassDiagram diagram by click on diagram's tab or by double click in project tree.
  3. Open UML Palette (if closed) with main menu Window > Palette.
  4. Click to Enlarge - Class Hierarchy.
    Click to Enlarge

  5. Add Package to the diagram.
    • Select Package in palette.
    • Click left mouse button in free space of diagram area.
      You'll get new package element in your model.
  6. Name it contact.
    • Select the element on the diagram (if not selected).
    • Type name of element and press Enter.
    • Click to Enlarge - Class Hierarchy.
      Click to Enlarge

  7. Add Class to the diagram.
  8. Name it Contact.

    Click to Enlarge - Class Hierarchy.
    Click to Enlarge

  9. Add Class to the diagram.
  10. Name it ContactList.

  11. Add Class to the diagram.
  12. Name it PersonalIdentity.

  13. Add Class to the diagram.
  14. Name it EMailAddress.

    Class Hierarchy - Contact, ContactList, PersonalIdentity, EMailAddress classes

  15. Draw Nested link from Contact class to contact package.
    • Select Nested link in palette.
    • Click on Class element.
    • Click on Package element.
      Now class is located in package.
    • Click to Enlarge - Nested Link.
      Click to Enlarge

  16. Draw Nested link from ContactList class to contact package.
  17. Draw Nested link from PersonalIdentity class to contact package.
  18. Draw Nested link from EMailAddress class to contact package.
  19. Class Hierarchy - Whole Class Diagram

Defining Application Classes


  1. Insert String firstName attribute to PersonalIdentity class.
    • Invoke Context menu on Attributes title within the class.
    • Insert Attribute Menu

    • Click on Insert Attribute menu item.
      Attribute with default name was added to class.
    • Delete default name (with backspace) and delete default attribute type (int)
    • Type attribute type.
    • Type attribute name.
    • Insert Attribute Type and Name

    • Press Enter.
    • Insert Attribute

  2. Insert String lastName attribute to PersonalIdentity class.
  3. PersonalIdentity Class Attributes

  4. Insert String address attribute to EMailAddress class.
  5. PersonalIdentity Class Attributes

  6. Insert boolean hasPersonalIdentity attribute to Contact class.
  7. Set default value of hasPersonalIdentity attribute to false in Contact class.
    • Double-click the attribute.
    • Go to the end of text field.
    • Press = and type default value.
    • Press Enter.
  8. Contact Class Boolean Attribute

  9. Insert PersonalIdentity personalIdentity attribute to Contact class.
  10. Insert ArrayList<EMailAddress> eMailAddressList attribute to Contact class.
  11. Set default value of eMailAddressList attribute to new ArrayList<EMailAddress>() in Contact class.
  12. Contact Class Attributes

  13. Insert boolean addEMailAddress(EMailAddress eMailAddress) operation to Contact class.
    • Invoke Context menu on Operation title within the class.
    • Insert Operation Menu

    • Click on Insert Operation menu item.
      Operation with default name was added to class.
    • Delete default name (with backspace) and delete default attribute type (int)
    • Type operation type.
    • Type operation name.
    • Type operation arguments.
    • Insert Operation

    • Press Enter.
    • Insert Operation

  14. Insert boolean removeEMailAddress(EMailAddress eMailAddress) operation to Contact class.
  15. Insert EMailAddress removeEMailAddress(int index) operation to Contact class.
  16. Insert EMailAddress setEMailAddress(int index, EMailAddress eMailAddress) operation to Contact class.
  17. Contact Class Operations

  18. Insert ArrayList<Contact> contactList attribute to ContactList class.
  19. Set default value of ArrayList<Contact> contactList attribute to new ArrayList<Contact>() in ContactList class.
  20. Insert boolean addContact(Contact contact) operation to ContactList class.
  21. Insert boolean removeContact(Contact contact) operation to ContactList class.
  22. Insert Contact removeContact(int index) operation to ContactList class.
  23. Insert Contact setContact(int index, Contact contact) operation to ContactList class.
  24. ContactList Class Operations

    The Class diagram should be like this:

    Click to Enlarge - Defining Application Classes.
    Click to Enlarge

Generating Java Source Code


  1. Create empty ContactLibrary_JAVA Java Application.

    • Choose File from the menu bar and then select New Project.
      The New Project dialog box appears.
    • Under Choose Project pane, select General under Categories and select Java Application under Projects. Click Next.
    • Source Code: New Project

    • For the Project Name field, enter ContactLibrary_JAVA.
    • For the Project Location field, click Browse to navigate, select the <lab_root>\nbeuml2 directory and click Open.
    • Uncheck Create Main Class checkbox
    • Source Code: New Project

    • Click Finish.
    • Source Code: New Java Project

  2. Associate ContactLibrary_UML project with ContactLibrary_JAVA project.

    • In the Projects window, select the ContactLibrary_UML node and invoke Context menu.
    • Click on Properties menu item.
      The Project Properties - ContactLibrary_UML dialog box appears.
    • Select Modeling under Categories
    • Check Generate Code checkbox
    • For the Java Project list, select ContactLibrary_JAVA.
    • Source Code: New Java Project

    • Click OK.
  3. Generate Java Source Code

    • In the Projects window, select the ContactLibrary_UML node and invoke Context menu.
    • Source Code: Generate Code

    • Click on Generate Code menu item.
      The Code Generation dialog box appears.
    • Source Code: Generate Code

    • Click Done.
      contact package appears in ContactLibrary_JAVA Java Application
      Contact, ContactList, PersonalIdentity, EMailAddress classes appear under contact package in ContactLibrary_JAVA Java Application
    • Source Code: Generate Code

Filling up Bodies of Generated Operations


  1. Activate ContactClassDiagram diagram by click on diagram's tab or by double click in project tree.
  2. Go to source of the EMailAddress class element.

    • Invoke Context menu within the class element.
    • Filling up Methods: Navigate To Source menu

    • Click on Navigate To Source menu item.
      You'll see the source code for class element.
    • Filling up Methods: Navigate To Source Java File


    The following methods were generated for the EMailAddress class

    • Constructor.
    • get/set methods for address attribute.
    • Filling up Methods: Navigate To Source Methods

  3. Go to source of the PersonalIdentity class element.
    The following methods were generated for the PersonalIdentity class

    • Constructor.
    • get/set methods for firstName attribute.
    • get/set methods for lastName attribute.
  4. Go to source of the Contact class element.
    The following methods were generated with default bodies

    • boolean addEMailAddress(EMailAddress eMailAddress).
    • boolean removeEMailAddress(EMailAddress eMailAddress).
    • EMailAddress removeEMailAddress(int index).
    • EMailAddress setEMailAddress(int index, EMailAddress eMailAddress).
  5. Change body of boolean addEMailAddress(EMailAddress eMailAddress) operation for Contact class to
            return this.eMailAddressList.add(eMailAddress);
           

    Filling up Methods: Change body for Contact Class

  6. Change body of boolean removeEMailAddress(EMailAddress eMailAddress) operation for Contact class to
            return this.eMailAddressList.remove(eMailAddress);
           
  7. Change body of EMailAddress removeEMailAddress(int index) operation for Contact class to
            return this.eMailAddressList.remove(index);
           
  8. Change body of EMailAddress setEMailAddress(int index, EMailAddress eMailAddress) operation for Contact class to
            return this.eMailAddressList.set(index, eMailAddress);
           

    Filling up Methods: Change body for Contact Class

  9. Press <Alt+Shift+F> to fix all class's imports
  10. Filling up Methods: Fix Imports

  11. Go to source of the ContactList class element.
  12. Change body of boolean addContact(Contact contact) operation for ContactList class to
            return this.contactList.add(contact);
           
  13. Change body of boolean removeContact(Contact contact) operation for ContactList class to
            return this.contactList.remove(contact);
           
  14. Change body of Contact removeContact(int index) operation for ContactList class to
            return this.contactList.remove(index);
           
  15. Change body of Contact setContact(int index, Contact contact) operation for ContactList class to
            return this.contactList.set(index, contact);
           

    Filling up Methods: Change body for ContactList Class

  16. Press <Alt+Shift+F> to fix all class's imports.
  17. Filling up Methods: Change body for ContactList Class

  18. Build ContactLibrary_JAVA application

    • In the Projects window, select the ContactLibrary_JAVA node and invoke Context menu.
    • Click on Build Project menu item.
    • Filling up Methods: Change body for ContactList Class

    • Correct errors if build was failed
    • Filling up Methods: Change body for ContactList Class

Run The Developed Application


  1. Add contactView package into ContactLibrary_JAVA application.
  2. Get ContactEditor JFrame (java and form files) from <lab_root>\nbeuml2\AppCreate\ContactLibrary\src\contactView directory and copy them to contactView package of ContactLibrary_JAVA application (<lab_root>\nbeuml2\ContactLibrary_JAVA\src\contactView).
  3. Run ContactLibrary_JAVA application

    • In the Projects window, select the ContactLibrary_JAVA node and invoke Context menu.
    • Run Application: Run Project menu

    • Click on Run Project menu item.
      The Run Project dialog box appears.
    • Select contactView.ContactEditor under Select the main class.
    • Run Application: Run Project menu

    • Click OK.
      The Contact Editor window appears.
    • Run Application: Contact Editor Window

  4. There are two contacts in the Contact List on the Contact Editor window

    • Gail Gardner, Gail.Gardner@sun.com.
    • Tom Davis, Tom.Davis@sun.com.
  5. Add Mike Wazowski, Mike.Wazowski@monster.com contact to Contact list

    • For the First Name field, enter Mike.
    • For the Last Name field, enter Wazowski.
    • For the EMail Address field, enter Mike.Wazowski@monster.com .
    • Run Application: Add Contact

    • Click Add.
      Mike Wazowski, Mike.Wazowski@monster.com contact is added to Contact list.
    • Run Application: Add Contact

  6. Close Contact Editor window.

Summary

In this exercise, you learned how to create new Java application using UML Modeling.
You learned how to perform the following tasks:

  • Desing and analyze application using Use Case Diagram
  • Develop Class hierarchy of Java application on Class Diagram
  • Filling Java classes with attributes and operations on UML diagram
  • Generating Java source code for Application developed on diagram

  • Exercise 2: Application Maintainance using UML Modeling


    In this chapter we will present a process for Maintaining a Java Contact Editor application using UML modeling techniques.

    We will use java application created by another developer in Exercise 1.
    To understand project structure we will begin with generating a UML diagram from an existing Java sources.

    The Next we will generate a UML Sequence diagram illustrating the interaction that takes place between the contact list view and the contact class library model.
    We will use the Sequence diagram to find a bug in the update process.
    We will then proceed with fixing localized bug. And at the end we will compile and run the editor to check the fix.


    Steps to follow:

    1. Step 1: Open Existing Java project with existing sources
    2. Step 2: Run project to find a mistake
    3. Step 3: Create a UML project by reverse engineering
    4. Step 4: Generate Class Diagram for Project
    5. Step 5: Change diagram view
    6. Step 6: Review project structure on Class Diagram
    7. Step 7: Generate a Sequence Diagram for an Operation
    8. Step 8: Review update operation on Sequence diagram
    9. Step 9: Fix problem
    10. Step 10: Run project to check

    Step 1: Open Existing Java project with existing sources


    Note: Close all projects from from previous exercises.

    1. Choose File from the menu bar and then select Open Project.
    2. Browse to the <lab_root>/nbeuml2/AppMaintain directory and select ContactLibrary2 project. Click Open Project Folder.

      Click to Enlarge. Opening Java project
      Click to Enlarge

    3. New ContactLibrary2 project node will appear in Projects tab.

    Step 2: Run project to find a mistake


    1. Right-click on the java Project node and select 'Run Project' from Context menu.
    2. In opened 'Run Project' dialog window select 'contactView.ContactEditor' main class

      Select main class to run project.

      Press OK.
    3. Output window shows compilation and running status.

      Output window.

    4. Contact Editor form is started.
    5. Double-Click on 'First Name' of existing 'Tom Davis' contact item to edit it.
      Table cell becomes editable.
      Type new Name 'Bob'.
      Change Email to 'Bob.Davis@sun.com'.

      New First Name and Email. Update button

      Click 'Update' button.
    6. Close Contact Editor window.
      Look at output window.
      Editor application will print out updated Contact information.
      Actually you can see that 'Tom Davis' record wasn't updated.

      Application output.

    7. This means there is an error in updating mechanism.
      We should review application, find a mistale and fix it.
    8. Now you can close 'Output - ContactLibrary2 (run)' window.

    Step 3: Create a UML project by reverse engineering


    1. Invoke 'File | New Project' command from the menu bar. The New Project dialog box appears.
    2. Under Choose Project pane, select UML under Categories and select Java-Platform Model by Reverse-Engineering a Java Project under Projects. Click Next.


      Click to Enlarge. Select Java-Platform Model by Reverse-Engineering a Java Project
      Click to Enlarge

      Note: You can create uml project from your sources another way by selecting Java-Platform Model project with turning on Generate Source on next wizard step
    3. Under Name and Location pane,

      • For the Project Name field, enter ContactLibrary2UML .
      • For the Project Location field, click Browse to navigate to and select the AppMaintain folder and click Open.
      • Choose ContactLibrary2 project in Java Project combobox.
      • Click to Enlarge. Choose Java Project
        Click to Enlarge

      Note: You have to select java project to proceed with Java-Platform Model by Reverse-Engineering a Java Project project creation. You can choose only from already opened java projects.

    4. When the reverse engineering is done, click Done. The ContactLibrary2UML project appears in the Projects window.

      Reverse Engineering performed by Netbeans Enterpise Pack


    Step 4: Generate Class Diagram for Project


    1. In the Projects tab window, under the ContactLibrary2UML node, expand the Model node.
      Expand the contact and contactView package nodes.
    2. Select the contact and contactView packages and all class nodes under them.
      You may click on contact folder and then, holding Shift key, click on ContactEditor to select all element between them.
    3. Right-click the selected elements and choose Create Diagram From Selected Elements from the contextual menu.
    4. In the Diagram Type list, select Class Diagram.
      Type ContactClassDiagram in the Diagram Name field,
      leave ContactLibrary2UML in the Namespace field
      and click OK.

      Choose Class Diagram

    5. This action does the following:

      • Creates a ContactClassDiagram node under the Model node
      • Displays the new diagram in the Diagram editor
      • Opens the Modeling Palette

      Click to Enlarge. Class Diagram created From Selected Elements
      Click to Enlarge

    Step 5: Change diagram view


    1. Double-click the BankClassDiagram tab at the top of the Diagram editor.
      Note: To reverse this action double-click the BankClassDiagram tab again.
    2. Select 'Zoom with marquee' The Zoom with marquee diagram toolbar button. button on diagram toolbar.
    3. Click on empty diagram area near the top left corner of ContactEditor symbol on diagram and do not release the mouse button.
      Holding mouse button move pointer to select the whole ContactEditor symbol. Release button.

      Select area to enlarge.

    4. You will see selected area enlarged to the whole visible diagram area.

      Click to enlarge. Enlarged Element
      Click to Enlarge

    Step 6: Review project structure on Class Diagram


    1. Look at ContactEditor class symbol on diagram.
    2. There is 'updateButtonActionPerformed(ActionEvent)' operation which name
      (according to widespread naming approach) shows that it is invoked from 'update' button event listener.
    3. And method 'setContact(int, Vector)' which performs actual data updating

      Operation Review

    4. setContact() is probably invoked from updateButtonActionPerformed().

    Step 7: Generate a Sequence Diagram for an Operation


    1. Let's analyze updateButtonActionPerformed() method.
      Right-click on 'private void updateButtonActionPerformed(ActionEvent)' operation area
      and invoke 'Reverse Engineer Operation' command.

      Reverse Engineer operation

    2. The New Wizard dialog for creating a new diagram appears.

      Reverse engineer operation dialog

    3. In the Diagram Type list, select Sequence Diagram.
    4. In the Diagram Name field, type ContactEditorSD.
    5. Accept the default value in the Namespace field and click OK. A sequence diagram appears in the Diagram Editor.
    6. Expand the Diagram editor and manipulate the zoom level so that you can easily examine the new sequence diagram.

      Click to Enlarge. Sequence Diagram of operation
      Click to Enlarge

    Step 8: Review update operation on Sequence diagram


    1. We can see Objects created and used in updateButtonActionPerformed method.

      Click to Enlarge. Created and used objects.
      Click to Enlarge

    2. We can see combined fragments like 'if', loops etc.

      Sequence Diagram. if clause.

    3. And We can analyze messages invocation sequence.
    4. For updateButtonActionPerformed we see method getSelectedRow() that selects updated row .

      Sequence Diagram. Row selection method.

    5. Then requests new values using getValueAt(int, Integer) method.

      Sequence Diagram. Geting new values methods.

    6. Adds them into Vector.

      Sequence Diagram. Putting values to vector.

    7. And then should invoke setContact(int, Vector) method to perform update.
      But it doesn't!
    8. We have localized a mistake.

    Step 9: Fix problem


    1. Switch to class diagram
    2. Open context menu for updateButtonActionPerformed method of ContactEditor class element
      and invoke 'Navigate to source' command.

      Navigate to source.

    3. Add 'this.setContact(rowIndex, rowData);' invocation as the last line of 'if' clause.

      Click to Enlarge. Correct mistake in Source Code.
      Click to Enlarge

    Step 10: Run project to check


    1. Run ContactLibrary2 project again to check if updating works now.
    2. Change First Name and Email Address to 'Bob' and 'Bob.Davis@sun.com' again.
      Press Update button.

      New First Name and Email. Update button

    3. Close application window and check in output that contact data was updated.

      Updated contact data

    Summary

    In this exercise, you learned how to reverse engineer and analyze a Java application.
    You learned how to perform the following tasks:

  • Create Reverse-Engineered UML Project
  • Generate a class diagram from the imported application
  • Generate a sequence diagram for an operation
  • Analyze generated sequence diagram


  • For more information on NetBeans Enterprise Pack UML Modeling, consult the online help from the IDE and visit the NetBeans Enterprise Pack Developers Resource portal for tutorials, videos, and other resources on UML modeling.


    Next Steps

    To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE J2EE development features, join the nbusers@netbeans.org mailing list.