Sunteți pe pagina 1din 30

This demo script shows all the steps necessary to do round trip engineering with Rational Rose 2000

and Visual C++. We start by reverse engineering the Scribble application that is one of the sample applications in Visual C++. We will then add information to the model and generate the code for the added information. Software required: Rational Rose Enterprise or Rational Rose Professional C++ Edition Visual C++ 6.0 Setup VISUAL C++ ADD-IN

1. Start Rational Rose 2000. Do not use a framework. 2. If Visual C++ is not an available language, select the Add-Ins: Add-In Manager menu choice. 3. Select the VC++ add-in. 4. Click the OK button to close the Add-In Manager window.

SET THE DEFAULT LANGUAGE

1. Select the Tools: Options menu command. 2. Select the Notation tab. 3. Click the arrow in the Default Language field to make the drop down menu visible. 4. Select Visual Basic. 5. Click the Apply button to apply the change (but do not close the Options window.

Visual C++ Demo Script

Page 1

SET DIAGRAM OPTIONS

1. Select the Diagram tab on the Options window. 2. Select the Label radio button in the Stereotype Display field. 3. De-select the Focus of Control checkbox. 4. Keep all other defaults. 5. Click the OK button to close the Options window.

SCRIBBLE APPLICATION 1. Unzip the file scribble.zip and store it on your hard drive.

Visual C++ Demo Script

Page 2

Scribble is an application that comes with Visual C++. Lets see what it does.

1. Start Visual C++. 2. Select the File: Open Workspace menu command. 3. Navigate to the Scribble directory. 4. Select scribble.dsw 5. Select the Build: Execute scribble.exe menu command.

The executable opens a window that allows you to draw whatever you want (in other words, you can scribble).

1. Draw something. 2. Select the File: Exit menu command to close the Scribble window. 3. You do not have to save changes. 4. Select the File: Exit menu command to close Visual C++.

Visual C++ Demo Script

Page 3

We need to update this application to count the number of strokes and the average stroke length for your scribble. We will therefore start by reverse engineering the Scribble project.

1. Create a new Rose model. Do not use a Framework. 2. Select the Tools: Visual C++: Update Model from Code menu command. 3. Click the Add a VC++ Component link. This will bring up the Select a VC++ Project File window.

4. 5. 6. 7.

Select the Existing tab. Navigate to the Scribble folder. Select Scribble.dsw Click the Open button.

Visual C++ Demo Script

Page 4

8. Click the OK button.

9. Click the Finish button.

Visual C++ Demo Script

Page 5

The results are shown in the Log window.

1. Click the Close button to close the Model Update Tool.

Rational Rose adds the classes to a package called Reverse Engineered. One of the first things we will do is move the Scribble package to the root Logical view.

1. Click the + next to the Logical View package to expand the tree. 2. Click the + next to the Reverse Engineered package to expand the tree. 3. Click to select the Scribble package in the Browser. 4. Drag the package onto the Logical View Package. 5. Point out that Rose automatically created a class diagram containing the reverse engineered classes. This is a feature that is controlled via the Tools: Visual C++: Properties menu. NOTE: Rose will line the classes up. I re-arranged the diagram so all classes were visible on the diagram. Visual C++ Demo Script Page 6

Since Scribble is a MCF application we need to import the MFC classes.

1. Select the Tools: Visual C++: Quick Import MFC 6.0 menu command.

In our example the CScribbleView class can handle windows messages. We will now update this class to handle the OnChar message. When the code for this class is regenerated this new message handler along with all the necessary message map macro entries will be generated.

1. Click right on the CScribble View class on a class diagram or in the Browser to make the pop up menu visible. 2. Select the Model Assistant menu command. 3. Select the MFC tab. 4. Click the + next to the Windows Messages folder to expand the tree. 5. Click the + next to the Input folder to expand the tree. 6. Click to select the OnChar message. 7. Click the OK button to close the Model Assistant.

Visual C++ Demo Script

Page 7

Classes can be added to the project either through Rose intially or through the Visual C++ IDE, and then reversed engineered into the model. As a general rule Dialog based classes and even CWnd derived classes are easier to add with the IDE and reverse engineer into Rose, while other general classes are easier to add with Rose and forward engineer into the code. The big advantage of letting the Visual C++ IDE create CDialog based classes is that the IDE contains a sophisticated dialog editor, and creates all the necessary resource identifiers for using dialogs in an MFC based application. We will use Visual C++ to add a new dialog to our project.

1. Go to Visual C++. 2. Click the Resource View tab in the Workspace tool box.

The first step is to add a new dialog to our project.

1. Click the + next to the Scribble resources folder to expand the tree. 2. Click right on the Dialog folder to make the pop up menu visible. 3. Select the Insert Dialog menu command. This will insert a new dialog called IDD_DIALOG1. 4. Click right on the new dialog to make the pop up menu visible. 5. Select the Properties menu command. 6. Enter the Dialog ID in the ID field (ID field = IDD_DOC_SUMMARY). 7. Enter the caption in the Caption field (Document Summary). 8. Click the X in the Properties Window to close the window. Visual C++ Demo Script Page 8

When Visual C++ creates a new dialog two buttons are automatically added to the dialog an OK button and a Cancel button. Since this dialog is just an informational dialog we can delete the Cancel button.

1. Click to select the Cancel button. 2. Click the Delete key on your keyboard.

We next add 4 static text controls to the dialog, two labels and two place holders, that will be set with the number of strokes in the Scribble document and the average stroke length.

1. Click the Static Text button (Aa) on the Controls tool box and click on the Dialog to place the button. 2. While the button is still selected, enter its caption (Number of strokes:) 3. Click the X in the Text Properties window to close the window. 4. Click the Static Text button (Aa) on the Controls tool box and click on the Dialog to place the button. 5. While the button is still selected, enter its caption (XXXXXXXX) 6. Enter the ID (IDC_NUM_STROKES) in the ID field of the Text Properties window.

Visual C++ Demo Script

Page 9

7. Click the X in the Text Properties window to close the window. 8. Click to select the two static text controls just created and select the Layout: Align: Bottom menu command. 9. Click the Static Text button (Aa) on the Controls tool box and click on the Dialog to place the button. 10. While the button is still selected, enter its caption (Average stroke length:) 11. Click the X`in the Text Properties window to close the window. 12. Click the Static Text button (Aa) on the Controls tool box and click on the Dialog to place the button. 13. While the button is still selected, enter its caption (XXXXXXXX) 14. Enter the ID in the ID field (ID = IDC_AVE_STROKE_LENGTH). 15. Click the X in the Text Properties window to close the window. 16. Click to select the two static text controls just created and select the Layout: Align: Bottom menu command. 17. Click to select the Number of strokes control, depress the Ctrl key, and click to select the Average stroke length control and select the Layout: Align: Left menu command.

We will now use the MFC Class Wizard to create a new dialog class for our dialog resource.

1. Double click on the Document Summary dialog to start the MFC Class Wizard. 2. Click the OK button to create a new class (default).

Visual C++ Demo Script

Page 10

You will be asked to enter a name for the new class.

1. Enter the class name in the Name field (CDocSummaryDlg). 2. Click the OK button.

The class wizard creates and binds member variables to the two static text controls that will contain the summary values of the scribble document. We need to give these members names and data types.

1. Select the Member Variables tab. 2. Select the IDC_AVE_STROKE_LENGTH Control ID. 3. Click the Add Variable button.

Visual C++ Demo Script

Page 11

The variable name is added via the Add Variable window.

1. Enter the variable name (m_nAveStrokeLength) in the Member variable name field. 2. Click the OK button.

Now we will add a variable for the IDC_NUM_STROKES control ID.

1. Select the IDC_NUM_STROKES Control ID. 2. Click the Add Variable button.

Visual C++ Demo Script

Page 12

Visual C++ Demo Script

Page 13

Add the variable name.

1. Enter the variable name (m_nNumStrokes) in the Member variable name field. 2. Click the OK button to close the Add Member Variable button. 3. Click the OK button to close the MFC Class Wizard.

The next step is to add a menu item for our Document Summary.

1. Click the + next to the Menu folder in the Browser to expand the tree. 2. Double click on IDR_SCRIBBTYPE. 3. Click the File menu to see the menu items.

Visual C++ Demo Script

Page 14

We need to add a menu item for our Document Summary.

1. Click the file menu to make the pop up menu visible. 2. Double click on the empty menu item to make the Menu Item Properties window visible. 3. Enter the ID for the new menu item (ID_FILE_DOC_SUMMA RY). 4. Enter the Caption for the new menu item (&Document Summary...). 5. Click the X in the Menu Item Properties window to close the window.

Next we will move our new menu item to a different place on the menu.

1. Click to select the Document Summary menu item. 2. Drag it to its new location (after the separator between Send and Recent file).

Visual C++ Demo Script

Page 15

Finally we will add a separator between the Document Summary menu item and the Recent File menu item.

1. Double click on the empty menu item to make the Menu Item Properties window visible. 2. Select the Separator checkbox. 3. Click the X in the Menu Item Properties window to close the window.

Now we will drag the new separator to its proper location.

1. Click to select the separator. 2. Drag it to its new location (below the Document Summary menu item).

Select the File: Save Workspace menu command to save the workspace. Exit from Visual C++. Enter Yes if prompted to save Scribble.rc.

Visual C++ Demo Script

Page 16

At this point our model and code are out of synch. We will use the reverse engineering capabilities of Rational Rose to update our model. We only need to reverse the new class that we created.

1. Go to the Rational Rose model.

We will use the Model Update Tool to update our model.

1. Select the Tools: Visual C++: Update Model from Code menu command. 2. Click the + next to the Scribble component to expand the tree. 3. Click the Scribble checkbox to de-select all the classes. 4. Click the CDocSummaryDlg checkbox to select it. 5. Click the Finish button.

Visual C++ Demo Script

Page 17

The Summary window contains reverse engineering information.

1. Click the Close button to close the window.

The Model Update tool adds new classes to the Reverse Engineered package in the Browser.

1. Click the + next to the Reverse Engineered package to expand the tree. 2. Click the + next to the Scribble package to expand the tree. 3. Point out the new class.

Visual C++ Demo Script

Page 18

We will now move the CDocSummaryDlg class to the Scribble package.

1. Click to select the CDocSummaryDlg class in the Browser. 2. Drag it to the Scribble package.

Next we will add relationships to our new class. In order to do this we will create a new class diagram called Document Summary.

1. Click right on the Scribble package to make the pop up menu visible. 2. Select the New: Class Diagram menu command. This will add a class diagram called NewDiagram to the Browser. 3. While the new diagram is still selected enter its name ... Documentation Summary. 4. Double click on the new class diagram in the Browser to open the diagram.

Visual C++ Demo Script

Page 19

We will add two classes to the diagram. They are CScribbleDoc and CDocSummaryDlg.

1. Click to select the CScribbleDoc class in the Browser. 2. Drag it onto the diagram. 3. Click to select the CDocSummaryDlg class in the Browser. 4. Drag it onto the diagram.

Next we will create a new class called CDocSummary with two attributes: m_nNumStrokes of type int and m_nAveStrokeLenght of type float. The CDocSummary class has one method called Analyze that accepts a single parameter of type CScribbleDocument.

1. Click to select the Class icon on the toolbar. 2. Click on the diagram to place the class. 3. While the new class is still selected, enter its name ... CDocSummary. 4. Click right on the CDocSummary class to make the pop up menu visible. 5. Select the New Attribute menu command. 6. While the new attribute is still selected enter its name and data type ... m_nNumStrokes : int. 7. Click right on the CDocSummary class to make the pop up menu visible. 8. Select the New Attribute menu command.

Visual C++ Demo Script

Page 20

9. While the new attribute is still selected enter its name and data type ... m_nAveStrokeLength : float. 10. Click right on the CDocSummary class to make the pop up menu visible. 11. Select the New Operation menu command. 12. While the new operation is still selected enter its name... Analyze(aDoc:CScribbleDoc*)

Next we need some relationships. When the CScribbleDocument class handles the request to display its document summary it will create an instance of the CDocSummaryDlg class. This implies that there is a dependency relationship between the CScribbleDocument class and the CDocSummaryDlg class. We also need a dependency relationship between the CDocSummaryDlg class and CScribbleDoc class since an instance of CScribbleDoc is passed as a parameter to the constructor of CDocSummaryDlg class.

1. Click to select the Dependency Relationship icon on the toolbar. 2. Click on the CScribbleDoc class and drag the line to the CDocSummaryDlg class. 3. Click to select the Dependency Relationship icon on the toolbar. 4. Click on the class CDocSummaryDlg and drag the line to the CScribbleDoc class. 5. This will place the two dependency lines on top of one another. Click to select one of the dependency lines and drag it to separate the lines.

Visual C++ Demo Script

Page 21

The CDocSummaryDlg class creates an instance of the CDocSummary class, which tells us that there is a dependency relationship between these two classes.

1. Click to select the Dependency Relationship icon on the toolbar. 2. Click on the CDocSummaryDlg class and drag the line to the CDocSummary class.

Finally, the CDocSummary class maintains a relationship to the CScribbleDoc Class.

1. Click to select the Unidirectional Association icon on the toolbar. 2. Click on the CDocSummary class and drag the line to the CScribbleDoc class. 3. Click right on the association line near the arrow to make the pop up menu visible. 4. Select the Role Name menu command. 5. Click right on the association line near the arrow to make the pop up menu visible. 6. Select the Multiplicity: 1 menu command.

Visual C++ Demo Script

Page 22

We will use the model assistant to specify the getters for the two attributes of the CDocSummary class.

1. Click right on the CDocSummary class in the Browser or on the diagram to make the pop up menu visible. 2. Select the Model Assistant menu command. 3. Click the + next to the m_nAveStrokeLenght attribute to expand the tree. 4. Select the get_AveStrokeLength() checkbox. 5. Click the + next to the m_nNumStrokes attribute to expand the tree. 6. Select the get_NumStrokes() checkbox. 7. Click the OK button to close the Model Assistant. We now need to create a command handler in the CScribbleDoc class to handle our new menu item. We can use the Model Assistant to do this.

1. Click right on the CScribbleDoc class in the Browser or on the diagram to make the pop up menu visible. 2. Select the Model Assistant menu command. 3. Select the MFC tab. 4. Click right on the Command Handlers package to make the pop up menu visible. 5. Select the New Handler menu command. 6. Click the arrow in the Command ID field to make the drop down menu visible. 7. Select the ID_FILE_DOC_SUMMARY command id. 8. Click the OK button. 9. Click the OK button to close the Model Assistant.

Visual C++ Demo Script

Page 23

Finally, we will use the Model Assistant to update the CDocSummaryDlg constructor to accept a pointer to the Scribble document.

1. Click right on the CDocSummaryDlg class in the Browser or on the diagram to make the pop up menu visible. 2. Select the Model Assistant menu command. 3. Click right the constructor CDocSummaryDlg(CWnd*) to make the pop up menu visible. 4. Select the New Parameter menu command. This will add a new parameter called argname. 5. While the new parameter is still selected, enter its name and data type (pDoc : CScribbleDoc* = NULL). 6. Click the OK button.

The last step is to assign our new class to the Scribble component.

1. Double-click on the CDocSummary class in the Browser or on the diagram to open the Class Specification. 2. Select the Components tab. 3. Click right on the Scribble component to make the pop up menu visible. 4. Select the Assign menu command. 5. Click the OK button to close the Class Specification.

Visual C++ Demo Script

Page 24

We are now ready to update the code with our changes. We will use the Code Update Tool to do this.

1. Select the Tools: Visual C++: Update Code menu command. 2. Click the + next to Scribble to expand the tree. 3. Select the CDocSummary, CDocSummaryDlg, CScribbleDoc and CScribbleView classes. 4. Click the Finish button.

The Summary window shows no error or warnings (this is good).

1. Click the Close button.

Visual C++ Demo Script

Page 25

Now we need to implement our methods. First we will implement the menu handler of the CScribble Doc class.

1. In Visual C++, select the Class View tab. 2. Click the + next to the CScribbleDoc class to expand the tree. 3. Double click on OnFileDocSummary(). 4. Enter the code below. 5. Click the X to close the window. Save the code when prompted.

Code: CDocSummaryDlg dlg(NULL, this); dlg.DoModal();


Next we need to implement the constructor for the CDocSummaryDlg class.

1. Click the + next to the CDocSummaryDlg class to expand the tree. 2. Double click on the constructor CDocSummaryDlg. 3. Enter the code below. 4. Click the X to close the window. Save the changes when prompted.

Visual C++ Demo Script

Page 26

Code: CDocSummary summary; summary.Analyze(pDoc); m_nNumStrokes.Format(%d, summary.get_NumStrokes()); m_nAveStrokeLength.Format(%.1f, summary.get_AveStrokeLength());


We need to implement the Analyze operation of the CDocSummary class.

1. Click the + next to the CDocSummary class to expand the tree. 2. Double click on Analyze. 3. Enter the code below. 4. Click the X to close the window. Save the changes when prompted.

Code: POSITION pos; CStroke *pStroke; float totalLength=0.0; m_nNumStrokes = aDoc -> m_strokeList.GetCount(); pos = aDoc -> m_strokeList.GetHeadPosition(); while (pos != NULL) { pStroke = aDoc->m_strokeList.GetNext(pos); totalLength = totalLength+(float)(pStroke -> m_pointArray.GetSize()); } m_nAveStrokeLength = totalLength/ (float) m_nNumStrokes;

Visual C++ Demo Script

Page 27

The last thing we need to do is implement the OnChar Message Handler.

1. Click the + next to the CScribbleView class to expand the tree. 2. Double click on OnChar. 3. Enter the code below. 4. Click the X to close the window. Save the changes when prompted.

Code: this -> MessageBox(OnChar message handler has been called);

Visual C++ Demo Script

Page 28

We can now run our application.

1. Select the Build: Execute Scribble.exe menu command. 2. Select Yes to re-build out of date files.

We will next invoke the OnChar message handler by touching a character on our keyboard.

1. Press any key on the keyboard. 2. Point out the message box. 3. Click the OK button to close the message box.

Visual C++ Demo Script

Page 29

Now we will scribble.

1. Draw something.

Lets now see our Document Summary.

1. Select the File: Document Summary menu command. 2. Click the OK button to close the Document Summary. 3. Select the File: Exit menu command to close Scribble.

In this demo you have seen how Rational Rose 2000 and Visual C++ 6.0 may be used to extend a Visual C++ MFC application. Rational Rose 2000 and Visual C++ 6.0 are a well-matched pair of developer tools. Rose is the tool of choice for managing models, and Visual C++ is the tool of choice for developing Windows C++ applications. The two tools are tightly integrated to optimize designer and implementer productivity.

Visual C++ Demo Script

Page 30

S-ar putea să vă placă și