Rev 2098 | Rev 2106 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
using System;using System.Collections;using Word;namespace EA_DocGen{/// <summary>/// Summary description for TextualContent./// </summary>public class TextualContent{private Word.Application WordApp = null;private Word.Document WordDocument = null;private DocSectionTracking DocSectionTracker = null;public TextualContent(Word.Application wordApp, Word.Document wordDocument, DocSectionTracking docSectionTracker){WordApp = wordApp;WordDocument = wordDocument;DocSectionTracker = docSectionTracker;}public void AcceptWordAppAndDoc(Word.Application wordApp, Word.Document wordDocument){WordApp = wordApp;WordDocument = wordDocument;}public Word.Range appendDescription(string wordText, EA_Utilities EA_Utils){return appendDescription(wordText, EA_Utils, false);}public Word.Range appendDescription(string wordText, EA_Utilities EA_Utils, bool continuation){return appendDescription(wordText, EA_Constants.styleName_Body1, EA_Utils, continuation);}public Word.Range appendDescription(string wordText, string styleName, EA_Utilities EA_Utils, bool continuation){Word.Range wr = null;if (wordText != null && wordText.Length > 0)wr = appendAndSelectText(wordText, styleName, continuation);else if (!EA_Utils.options.opt_SuppressElementDescriptionMissingWarnings){wr = appendAndSelectText("Description missing!", styleName, continuation);// For some wierd reason, if continuing on same paragraph, we have to decrement the range start by 1// otherwise the D of Description, does not get the red color or italic styleif (continuation)wr.Start--;wr.Font.Italic = 1;wr.Font.Color = Word.WdColor.wdColorRed;}return wr;}/// <summary>/// Appends a specified text string to the word document, selects the new text, and applies/// the specified style formatting to it./// </summary>/// <param name="wordText"></param>/// <param name="styleText"></param>/// <param name="continuation"></param>public Word.Range appendAndSelectText(string wordText, string styleText){return appendAndSelectText(wordText, styleText, false );}public Word.Range appendAndSelectText(string wordText, string styleText, bool continuation ){if (wordText.Length > 0){Word.Range WordRange = null;object startLocation;object endLocation;object style = styleText;int i;startLocation = 0;endLocation = i = WordDocument.Content.End;WordRange = WordDocument.Range(ref startLocation, ref endLocation);if (!continuation)WordRange.InsertAfter( "\n" );WordRange.InsertAfter( wordText );// Make a range out of the pasted textstartLocation = i;endLocation = WordDocument.Content.End;WordRange = WordDocument.Range(ref startLocation, ref endLocation);// and set the pasted text styleWordRange.set_Style(ref style);return WordRange;}return null;}/// <summary>/// Appends a specified text string to the word document, selects the new text, and applies/// a heading level style to it./// </summary>/// <param name="wordText"></param>/// <param name="level"></param>public void appendAndSelectHeadingText(string wordText, int level){// A caller is requesting a new heading level so pass the level to the tracking// object so that we can predict exactly what the level numbering should be for// diagnostics and fake heading generation (see below in the switch statement).DocSectionTracker.trackDocSection(level);// Convert level to heading stylestring styleText;switch(level){case 1: styleText = EA_Constants.styleName_Heading1; break;case 2: styleText = EA_Constants.styleName_Heading2; break;case 3: styleText = EA_Constants.styleName_Heading3; break;case 4: styleText = EA_Constants.styleName_Heading4; break;case 5: styleText = EA_Constants.styleName_Heading5; break;case 6: styleText = EA_Constants.styleName_Heading6; break;case 7: styleText = EA_Constants.styleName_Heading7; break;case 8: styleText = EA_Constants.styleName_Heading8; break;case 9: styleText = EA_Constants.styleName_Heading9; break;default:// MS-Word cannot produce headings above level 9 and so we have to// fake a heading by constructing it from our document section tracking data.styleText = EA_Constants.styleName_Heading10OrAbove;wordText = DocSectionTracker.formHeadingString(wordText);break;}// append the text as a headingappendAndSelectText(wordText, styleText);}public string testSuiteName(EA.Element theElement){return "Test Suite - " + theElement.Name;}public void appendUnitTestSuite(EA.Element theElement, int recurse_level, ref ArrayList classList, ref EA_Utilities EA_Utils){if (theElement.StereotypeEx.IndexOf("API") < 0){// element does not have an API stereotype, so is not a class for which unit tests are expected.return;}// only feed non-private classes through to the unit test section of a document// NOTE: May need an override option for this filter.if (!theElement.Visibility.StartsWith("Private")){appendAndSelectHeadingText(testSuiteName(theElement), recurse_level+1);if (theElement.Tests.Count > 0){classList.Add( theElement.ElementID );foreach(EA.Test theTest in theElement.Tests){appendAndSelectHeadingText("Test Case - " + theTest.Name, recurse_level+2);appendAndSelectHeadingText("Description", recurse_level+3);appendAndSelectText(theTest.Notes, EA_Constants.styleName_Body1);appendAndSelectHeadingText("Inputs", recurse_level+3);appendAndSelectText(theTest.Input, EA_Constants.styleName_Body1);appendAndSelectHeadingText("Expected Results", recurse_level+3);appendAndSelectText(theTest.AcceptanceCriteria, EA_Constants.styleName_Body1);}}else{Word.Range wr = appendAndSelectText("Test Cases missing!", EA_Constants.styleName_Body1);wr.Font.Italic = 1;wr.Font.Color = Word.WdColor.wdColorRed;}}}public void appendUnitTestSuite(EA.Package thePackage, int recurse_level, ref ArrayList classList, ref EA_Utilities EA_Utils){EA_ElementSorter elementSorter = new EA_ElementSorter(thePackage);EA.Element theElement = null;int theElementsRelativeLevel = 0;if (true == elementSorter.getFirst(ref theElement, ref theElementsRelativeLevel)){do{if (theElement.Type.StartsWith("Class")){appendUnitTestSuite(theElement, recurse_level, ref classList, ref EA_Utils);}} while (true == elementSorter.getNext(ref theElement, ref theElementsRelativeLevel));}// Scan through the packages within this package.foreach(EA.Package lowerLevelPackage in thePackage.Packages){// recurseappendUnitTestSuite(lowerLevelPackage, recurse_level, ref classList, ref EA_Utils);}}public void SelectInsertionPointAtEndOfDocument(){object unit;object extend;unit = Word.WdUnits.wdStory;extend = Word.WdMovementType.wdMove;WordApp.Selection.EndKey(ref unit, ref extend);}/// <summary>/// Generates the text for requirements. This uses custom styles to format the text/// rather than formatting it in code./// </summary>public bool generateRequirementText( EA.Element theElement, EA_Utilities EA_Utils ){if ( theElement.Type.StartsWith("Requirement")){string reqID = "";string reqShortDesc = "";string reqNotes = "";string reqStatusText = "";string reqHeadingStyle ="";string reqParaStyle = "";object reqNameStyle = EA_Constants.styleName_ReqName;Word.Range shortDescRange;// Set the style depending on the statusswitch ( theElement.Status ){case "Proposed":reqStatusText = "(Proposed)";reqHeadingStyle = EA_Constants.styleName_ReqPropHdr;reqParaStyle = EA_Constants.styleName_ReqPropBody;break;case "Rejected":reqHeadingStyle = EA_Constants.styleName_ReqRejHdr;reqParaStyle = EA_Constants.styleName_ReqRejBody;break;case "Approved"://reqStatusText = "(Approved)";//reqStatusText = "(Phase " + theElement.Phase + ")";reqHeadingStyle = EA_Constants.styleName_ReqAppHdr;reqParaStyle = EA_Constants.styleName_ReqAppBody;break;default:reqStatusText = "(" + theElement.Status + ")";//reqStatusText = "(" + theElement.Status + ")(Phase " + theElement.Phase + ")";reqHeadingStyle = EA_Constants.styleName_ReqAppHdr;reqParaStyle = EA_Constants.styleName_ReqAppBody;break;}// Pull out the ID from the nameint pos = theElement.Name.IndexOf( " ", 0, theElement.Name.Length );reqID = theElement.Name.Substring( 0, pos );// Pull out the short description from the rest of the namereqShortDesc = theElement.Name.Substring( pos, theElement.Name.Length-pos );reqShortDesc = reqShortDesc.Trim();// Pull out the notesreqNotes = theElement.Notes.ToString();// Add the textappendAndSelectText( reqID + '\t', reqHeadingStyle );shortDescRange = appendAndSelectText( reqShortDesc, reqHeadingStyle, true );Word.Range WordRange = appendAndSelectText( '\t' + reqStatusText, reqHeadingStyle, true );//reapply the name char style to the short desc.shortDescRange.Start = shortDescRange.Start-1;shortDescRange.End = WordRange.Start-1;shortDescRange.set_Style( ref reqNameStyle );appendAndSelectText( reqNotes, reqParaStyle );return true;}return false;}}}