Subversion Repositories DevTools

Rev

Rev 2102 | Rev 2106 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Text;
using Word;

namespace EA_DocGen
{
   /// <summary>
   /// A Base Class designed to work with the findAndProcessPackageElements method.
   /// Users will normally derive their own classes from this and add real functionality
   /// to the over-ridable methods the base class provides.
   /// </summary>
   public class EA_UtilitiesRecursionWorker
   {
      public EA_UtilitiesRecursionWorker()
      {
      }

      public virtual void processElement( EA.Element theElement )
      {
      }
      public virtual void processPackage( EA.Package thePackage )
      {
      }
   }

   

        /// <summary>
        /// Class containing functions that implement the EA_DocGen menu items.
        /// </summary>
        public class EA_Utilities
        {
      // Data
      public EA_DocGenOptions options = null;

      public GUID_Clipboard guid_clipboard = null;

      private EA.Repository EA_Repository = null;

      public EA_Finders EA_Finder = null;

      // Operations

                public EA_Utilities()
                {
         guid_clipboard = new GUID_Clipboard(this);

         options = new EA_DocGenOptions(this);

         EA_Finder = new EA_Finders();
                }


      public void accept_EA_RepositoryRef(EA.Repository EA_RepositoryRef)
      {
         EA_Repository = EA_RepositoryRef;
         EA_Finder.accept_EA_RepositoryRef(EA_RepositoryRef);
      }


      public void modifyOptions()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otElement)
         {
            EA.Element ele = (EA.Element)obj;

            if (0 == ele.Name.CompareTo(EA_Constants.EA_DocGenBaseName))
            {
               if (true == options.lookForAndProcess_EA_DocGen_Element( ele ))
               {
                  EA_DocGenOptionsForm dlg = new EA_DocGenOptionsForm();
                  dlg.populate(EA_Repository, options);
                  DialogResult dlgRes = dlg.ShowDialog();
                  if (dlgRes == DialogResult.OK)
                  {
                     dlg.read(EA_Repository, options);

                     options.updateEA_DocGen(ele);
                  }
               }
            }
         }
      }


      /// <summary>
      /// Begins the document generation process, opening the dialog to capture input/output
      /// files. the createWordDoc dialog class does most of the work ofcoarse.
      /// </summary>
      public void createWordDoc()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            // Make sure this package has an EA_DocGen element
            if (true == options.lookForAndProcess_EA_DocGen_Element( ((EA.Package)obj) ))
            {
               // bring up the dialog for doc generation
               createWordDoc dialog = new createWordDoc(EA_Repository, ((EA.Package)obj), this);
               dialog.Text = "Generate Document From Model Layout";
               dialog.ShowDialog();
            }
            else
            {
               MessageBox.Show("EA_DocGen document model packages must contain an EA_DocGen element");
            }
         }
         else
         {
            MessageBox.Show("You must select a package whose content is to be formed into a document");
         }
      }


      /// <summary>
      /// Creates a package under the specified parent package. the package is given a tree position
      /// as specified in order for it to be ordered in the model as the caller requires.
      /// </summary>
      /// <param name="parentPackage"></param>
      /// <param name="name"></param>
      /// <param name="treePos"></param>
      /// <returns></returns>
      public EA.Package createPackage(EA.Package parentPackage, string name, int treePos)
      {
         EA.Package newobj = (EA.Package)parentPackage.Packages.AddNew(name, "Package");
         newobj.TreePos = treePos;
         newobj.Update();
         return newobj;
      }


      /// <summary>
      /// Generate a package hierarchy that reflects the document layout of BMS-00289, the
      /// ERG Product Software Design Document.
      /// </summary>
      public void create_BMS00289_Layout()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (  ((EA.Package)obj).Packages.Count == 0
               && ((EA.Package)obj).Elements.Count == 0
               && ((EA.Package)obj).Diagrams.Count == 0 )
            {
               EA.Package parentPackage = ((EA.Package)obj);

               EA.Package newobj = null;
               EA.Package newobj2 = null;
               EA.Package newobj3 = null;
               EA.Package newobj4 = null;
               EA.Package newobj5 = null;

               newobj  = createPackage(parentPackage, "Introduction", 1);
               newobj2 = createPackage(newobj, "Purpose", 1);
               newobj2 = createPackage(newobj, "Scope", 2);
               newobj2 = createPackage(newobj, "Terminology", 3);
               newobj2 = createPackage(newobj, "References", 4);

               newobj  = createPackage(parentPackage, "Design Assumptions And Constraints", 2);

               newobj  = createPackage(parentPackage, "System Overview", 3);

               newobj  = createPackage(parentPackage, "High Level Design", 4);
               newobj2 = createPackage(newobj, "Software Architecture", 1);
               newobj2 = createPackage(newobj, "External Interfaces", 2);
               newobj2 = createPackage(newobj, "Internal Interfaces", 3);
               newobj2 = createPackage(newobj, "Memory And Processing Time Allocation", 4);
               newobj2 = createPackage(newobj, "Operational Modes", 5);
               newobj2 = createPackage(newobj, "Component Descriptions", 6);

               newobj = createPackage(parentPackage, "Detailed Design", 5);

               newobj = createPackage(parentPackage, "Unit Test Design",6);

               newobj2 = createPackage(newobj, "Test Data", 1);
               newobj2 = createPackage(newobj, "Test Stubs", 2);
               newobj2 = createPackage(newobj, "Other Elements", 3);
               newobj2 = createPackage(newobj, "Unit Test Traceability To Design", 4);
               newobj2 = createPackage(newobj, "Test Suites and Test Cases", 5);
               newobj3 = createPackage(newobj2, "Test Suite Name", 1);
               newobj4 = createPackage(newobj3, "Test Name", 1);
               newobj5 = createPackage(newobj4, "Description", 1);
               newobj5 = createPackage(newobj4, "Inputs", 2);
               newobj5 = createPackage(newobj4, "Expected Outputs", 3);

               newobj = createPackage(parentPackage, "Requirements Traceability", 7);

               // create an EA_DocGen element
               EA.Element newElement = (EA.Element)parentPackage.Elements.AddNew( "EA_DocGen", "InformationItem" );
               newElement.Notes = "[ElementTypes]\r\nRequirement\r\nClass\r\nComponent\r\n";
               newElement.Update();

               parentPackage.Packages.Refresh();

               // refresh project browser view
               EA_Repository.RefreshModelView(parentPackage.PackageID);
            }
            else
            {
               MessageBox.Show("Can only insert layout into an empty package");
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the layout will be inserted");
         }
      }


      /// <summary>
      /// Generate a package hierarchy that reflects a basic document layout.
      /// </summary>
      public void create_basic_Layout()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (  ((EA.Package)obj).Packages.Count == 0
               && ((EA.Package)obj).Elements.Count == 0
               && ((EA.Package)obj).Diagrams.Count == 0 )
            {
               EA.Package parentPackage = ((EA.Package)obj);

               EA.Package newobj = null;
               EA.Package newobj2 = null;
               //EA.Package newobj3 = null;
               //EA.Package newobj4 = null;
               //EA.Package newobj5 = null;

               newobj  = createPackage(parentPackage, "Introduction", 1);
               newobj2 = createPackage(newobj, "Purpose", 1);
               newobj2 = createPackage(newobj, "Scope", 2);
               newobj2 = createPackage(newobj, "Terminology", 3);
               newobj2 = createPackage(newobj, "References", 4);

               newobj  = createPackage(parentPackage, "Section 2 (rename me as required)", 2);

               newobj  = createPackage(parentPackage, "Section 3 (rename me as required)", 3);

               // create an EA_DocGen element
               EA.Element newElement = (EA.Element)parentPackage.Elements.AddNew( EA_Constants.EA_DocGenBaseName,
                                                                                  EA_Constants.EA_DocGenElementType );
               newElement.Notes = "[ElementTypes]\r\nRequirement\r\nClass\r\nComponent\r\n";
               newElement.Update();

               parentPackage.Packages.Refresh();

               // refresh project browser view
               EA_Repository.RefreshModelView(parentPackage.PackageID);
            }
            else
            {
               MessageBox.Show("Can only insert layout into an empty package");
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the layout will be inserted");
         }
      }


      /// <summary>
      /// This function works its way up the parental hierarchy to the root model, collecting the
      /// strings names of each package and prepending them to a string accumulator with a view to
      /// obtaing the full path of an element,diagram, or package.
      /// </summary>
      /// <param name="parentId"></param>
      /// <param name="instr"></param>
      /// <returns></returns>
      public string GetPackagePath( int parentId, string instr )
      {
         if (parentId != 0)
         {
            EA.Package pkg;
            pkg = EA_Repository.GetPackageByID( parentId );
            if (pkg != null)
            {
               instr = pkg.Name + "/" + instr;
               instr = GetPackagePath(pkg.ParentID, instr);
            }
         }
         return instr;
      }


      private void copy_GUID_to_clipboard(object obj, EA.ObjectType objType, bool confirm)
      {
         guid_clipboard.copy(obj, objType, confirm);
      }


      /// <summary>
      /// For the selected item, copy its GUID to the internal add-in GUID clipboard and the
      /// global external clipboard. Also copy to the internal add-in GUID clipboard, the name of
      /// the element and its type. This will support the AddLinkElement() method.
      /// </summary>
      public void copy_GUID_to_clipboard()
      {
         EA.ObjectType objType;
         object obj;
         objType = EA_Repository.GetTreeSelectedItem( out obj );
         guid_clipboard.copy(obj, objType, true);
      }


      public void AddTestTraceabilityElement()
      {
         EA.ObjectType objType;
         object obj;
         objType = EA_Repository.GetTreeSelectedItem( out obj );
         AddTestTraceabilityElement(obj, objType);
      }
      public void AddTestTraceabilityElement(object obj, EA.ObjectType objType)
      {
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenTestTraceability, EA_Constants.EA_DocGenElementType);
            if (newobj != null)
            {
               ((EA.Element)newobj).Update();
            }         
         }
         else
         {
            MessageBox.Show("You must select a package into which the element will be inserted");
         }
      }


      /// <summary>
      /// Using the internal add-in GUID clipboard, paste a test link element into the selected
      /// package.
      /// </summary>
      public void AddTestLinkElement()
      {
         EA.ObjectType objType;
         object obj;
         objType = EA_Repository.GetTreeSelectedItem( out obj );
         AddTestLinkElement(obj, objType);
      }

      private void AddTestLinkElement(object obj, EA.ObjectType objType)
      {
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            if (   guid_clipboard.objType == EA.ObjectType.otElement
                || guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenTestLink + " - " + guid_clipboard.name, EA_Constants.EA_DocGenElementType);

               if (newobj != null)
               {
                  ((EA.Element)newobj).Notes = guid_clipboard.name + "\r\n" + guid_clipboard.guid;
                  ((EA.Element)newobj).Update();
               }
            }
            else
            {
               MessageBox.Show("The GUID clipboard must first contain the GUID of an element (normally, a class), or a package");
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the test link will be inserted");
         }
      }


      /// <summary>
      /// Using the internal add-in GUID clipboard, paste a link element into the selected
      /// package.
      /// </summary>
      public void AddLinkElement()
      {
         EA.ObjectType objType;
         object obj;
         objType = EA_Repository.GetTreeSelectedItem( out obj );
         AddLinkElement(obj, objType);
      }

      private void AddLinkElement(object obj, EA.ObjectType objType)
      {
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenPackageLink + " - " + guid_clipboard.name, EA_Constants.EA_DocGenElementType);
            }
            else if (guid_clipboard.objType == EA.ObjectType.otDiagram)
            {
               newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenDiagramLink + " - " + guid_clipboard.name, EA_Constants.EA_DocGenElementType);
            }
            else if (guid_clipboard.objType == EA.ObjectType.otElement)
            {
               newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenElementLink + " - " + guid_clipboard.name, EA_Constants.EA_DocGenElementType);
            }
            else if (guid_clipboard.objType == EA.ObjectType.otAttribute)
            {
               MessageBox.Show("Attribute links are not currently supported");
            }
            else if (guid_clipboard.objType == EA.ObjectType.otMethod)
            {
               MessageBox.Show("Method links are not currently supported");
            }

            if (newobj != null)
            {
               ((EA.Element)newobj).Notes = guid_clipboard.name + "\r\n" + guid_clipboard.guid;
               ((EA.Element)newobj).Update();
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the link will be inserted");
         }
      }


      public void AddNameLinkElement()
      {
         EA.ObjectType objType;
         object obj;
         objType = EA_Repository.GetTreeSelectedItem( out obj );
         AddNameLinkElement(obj, objType);
      }

      private void AddNameLinkElement(object obj, EA.ObjectType objType)
      {
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenNameLink + " - " + guid_clipboard.name, EA_Constants.EA_DocGenElementType);
               if (newobj != null)
               {
                  StringBuilder sb = new StringBuilder();
                  sb.Append(guid_clipboard.name);
                  sb.Append("\r\n");
                  sb.Append(guid_clipboard.guid);
                  sb.Append("\r\n");
                  sb.Append("### Add as many package names as you like below here.\r\n");
                  sb.Append("### Each should be of the form: package=name\r\n");
                  sb.Append("### Delete any unused package= lines from these notes.\r\n");
                  sb.Append("### The names are case sensitive and must match exactly\r\n");
                  sb.Append("### the names of the packages you wish to pull in to the\r\n");
                  sb.Append("### document model.\r\n");
                  sb.Append("package=\r\n");
                  sb.Append("package=\r\n");
                  sb.Append("package=\r\n");

                  ((EA.Element)newobj).Notes = sb.ToString();
                  ((EA.Element)newobj).Update();
               }            
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the name link will be inserted");
         }
      }


      public void AddTableElement()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenTable, EA_Constants.EA_DocGenElementType);
            if (newobj != null)
            {
               ((EA.Element)newobj).Notes = "title=insertYourTitleHere\r\n"
                                          + "columns=2\r\n"
                                          + "seperator=,\r\n"
                                          + "indent=1\r\n"
                                          + "widths=column1Width,column2Width\r\n"
                                          + "column1Title,column2Title\r\n"
                                          + "cellcontent,cellcontent\r\n"
                                          + "cellcontent,cellcontent\r\n"
                                          + "cellcontent,cellcontent\r\n"
                                          + "etc,etc";
               ((EA.Element)newobj).Update();
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the Table Element will be inserted");
         }
      }


      public void AddTextElement()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenText, EA_Constants.EA_DocGenElementType);
            if (newobj != null)
            {
               ((EA.Element)newobj).Notes = "Add your text here.";
               ((EA.Element)newobj).Update();
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the Text Element will be inserted");
         }
      }


      public void AddRelationshipMatrixElement()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            RelationshipMatrixType dlg = new RelationshipMatrixType();
            DialogResult dlgRes = dlg.ShowDialog();
            if (dlgRes == DialogResult.OK)
            {
               object newobj = null;
               newobj = ((EA.Package)obj).Elements.AddNew(EA_Constants.EA_DocGenRelationshipMatrix, EA_Constants.EA_DocGenElementType);
               if (newobj != null)
               {
                  ((EA.Element)newobj).Notes  = EA_RelationshipMatrix.optionTemplateForRelationshipMatrix(dlg.style());
                  ((EA.Element)newobj).Update();
               }
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the RelationshipMatrix Element will be inserted");
         }
      }


      public void GeneratePackagesFromElementList()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               EA.Package theFoundPackage = EA_Finder.findPackageInRepositoryByGUID(guid_clipboard.guid);
               if (theFoundPackage != null)
               {
                  EA.Package srcPackage = theFoundPackage;
                  EA.Package destPackage = ((EA.Package)obj);

                  foreach( EA.Element srcElement in srcPackage.Elements)
                  {
                     if (false == EA_Finder.elementNameExistsInPackage(destPackage, srcElement.Name.ToString()))
                     {
                        createPackage(destPackage, srcElement.Name.ToString(), 1);
                     }
                  }
               }
            }
            else
            {
               MessageBox.Show("You must first copy a Source Package GUID to the clipboard");
            }
         }
         else
         {
            MessageBox.Show("You must select a Destination Package into which the Generated Packages will be inserted");
         }
      }


      public void GenerateSubPackageLinks()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               EA.Package theFoundPackage = EA_Finder.findPackageInRepositoryByGUID(guid_clipboard.guid);
               if (theFoundPackage != null)
               {
                  EA.Package srcPackage = theFoundPackage;
                  EA.Package destPackage = ((EA.Package)obj);

                  foreach( EA.Package subPackage in srcPackage.Packages)
                  {
                     copy_GUID_to_clipboard( (object)subPackage, EA.ObjectType.otPackage, false);
                     AddLinkElement( (object)destPackage, EA.ObjectType.otPackage);
                  }
               }
            }
            else
            {
               MessageBox.Show("You must first copy a Source Package GUID to the clipboard");
            }
         }
         else
         {
            MessageBox.Show("You must select a Destination Package into which the Sub-Package Links will be inserted");
         }
      }


      public void GenerateElementLinks()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               EA.Package theFoundPackage = EA_Finder.findPackageInRepositoryByGUID(guid_clipboard.guid);
               if (theFoundPackage != null)
               {
                  EA.Package srcPackage = theFoundPackage;
                  EA.Package destPackage = ((EA.Package)obj);

                  foreach( EA.Element subElement in srcPackage.Elements)
                  {
                     copy_GUID_to_clipboard( (object)subElement, EA.ObjectType.otElement, false);
                     AddLinkElement( (object)destPackage, EA.ObjectType.otPackage);
                  }
               }
            }
            else
            {
               MessageBox.Show("You must first copy a Source Package GUID to the clipboard");
            }
         }
         else
         {
            MessageBox.Show("You must select a Destination Package into which the Element Links will be inserted");
         }
      }

      /// <summary>
      /// This function is designed to parse EA models/packages in a predefined way, whilst allowing
      /// a user to specify what processing is to be done upon or with each element found.
      /// </summary>
      /// <param name="thePackage"></param>
      /// <param name="worker"></param>
      /// <param name="recurse"></param>
      public void findAndProcessPackageElements( EA.Package thePackage, EA_UtilitiesRecursionWorker worker, bool recurse)
      {
         worker.processPackage( thePackage );

         foreach (EA.Element theElement in thePackage.Elements)
         {
            worker.processElement( theElement );
         }

         if (recurse == true)
         {
            foreach (EA.Package subPackage in thePackage.Packages)
            {
               // RECURSION
               findAndProcessPackageElements( subPackage, worker, true);
            }
         }

      }


      public void addDocumentReference()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            DocReferenceForm dlg = new DocReferenceForm();
            DialogResult dlgRes = dlg.ShowDialog();
            if (dlgRes == DialogResult.OK)
            {
               object newobj = null;

               newobj = ((EA.Package)obj).Elements.AddNew(dlg.textBox_DocNumber.Text, EA_Constants.EA_DocGenElementType);
               if (newobj != null)
               {
                  ((EA.Element)newobj).Notes = dlg.textBox_DocName.Text;
                  ((EA.Element)newobj).Update();
               }
            }
         }
      }


      public void addTerminology()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            TerminologyForm dlg = new TerminologyForm();
            DialogResult dlgRes = dlg.ShowDialog();
            if (dlgRes == DialogResult.OK)
            {
               object newobj = null;

               newobj = ((EA.Package)obj).Elements.AddNew(dlg.textBox_Term.Text, EA_Constants.EA_DocGenElementType);
               if (newobj != null)
               {
                  ((EA.Element)newobj).Notes = dlg.textBox_TermExpansion.Text;
                  ((EA.Element)newobj).Update();
               }
            }
         }
      }


      public string ReadTag(EA.Element theElement, string tagName)
      {
         string result;

         EA.TaggedValue tag = (EA.TaggedValue)theElement.TaggedValues.GetByName(tagName);
         if (null != tag)
         {
            result = tag.Value;
         }
         else
         {
            result = "";
         }
         return result;
      }


      public bool WriteTag(EA.Element theElement, string tagName, string value)
      {
         bool result;
         EA.TaggedValue tag;
            
         tag = (EA.TaggedValue)theElement.TaggedValues.GetByName(tagName);
         if (null != tag)
         {
            tag.Value = value;
         }
         else
         {
            tag = (EA.TaggedValue)theElement.TaggedValues.AddNew(tagName, value);
         }

         if (tag != null)
         {
            result = tag.Update();
         }
         else
         {
            result = false;
         }
         return result;
      }


      public void RemoveAPIStereotypeFromContent()
      {
         EA.ObjectType objType;
         object obj;
         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            RemoveAPIStereotypeFromContent((EA.Package)obj);
            EA_Repository.RefreshModelView(((EA.Package)obj).PackageID);
         }
         else if (objType == EA.ObjectType.otDiagram)
         {
            RemoveAPIStereotypeFromDiagram((EA.Diagram)obj);
            EA_Repository.RefreshModelView(((EA.Diagram)obj).PackageID);
         }
         else if (objType == EA.ObjectType.otElement)
         {
            RemoveAPIStereotypeFromContent((EA.Element)obj);
            EA_Repository.RefreshModelView(((EA.Element)obj).PackageID);
         }
      }

      private void RemoveAPIStereotypeFromPackage(EA.Package thePackage)
      {
         thePackage.StereotypeEx = thePackage.StereotypeEx.Replace(",API","");
         thePackage.StereotypeEx = thePackage.StereotypeEx.Replace("API,","");
         thePackage.StereotypeEx = thePackage.StereotypeEx.Replace("API","");
         thePackage.Update();
      }

      private void RemoveAPIStereotypeFromDiagram(EA.Diagram theDiagram)
      {
         // diagrams only use one stereotype, not a list of them
         if (theDiagram.Stereotype.Equals("API"))
         {
            theDiagram.Stereotype = "";
            theDiagram.Update();
         }
      }

      private void RemoveAPIStereotypeFromElement(EA.Element theElement)
      {
         theElement.StereotypeEx = theElement.StereotypeEx.Replace(",API","");
         theElement.StereotypeEx = theElement.StereotypeEx.Replace("API,","");
         theElement.StereotypeEx = theElement.StereotypeEx.Replace("API","");
         theElement.Update();
      }

      private void RemoveAPIStereotypeFromContent(EA.Package thePackage)
      {
         RemoveAPIStereotypeFromPackage(thePackage);

         foreach(EA.Diagram diagram in thePackage.Diagrams)
         {
            RemoveAPIStereotypeFromDiagram(diagram);
         }

         foreach(EA.Element element in thePackage.Elements)
         {
            RemoveAPIStereotypeFromElement(element);
         }

         foreach(EA.Package package in thePackage.Packages)
         {
            RemoveAPIStereotypeFromContent(package); // recursion
         }
      }

      private void RemoveAPIStereotypeFromContent(EA.Element theElement)
      {
         RemoveAPIStereotypeFromElement(theElement);

         foreach(EA.Diagram diagram in theElement.Diagrams)
         {
            RemoveAPIStereotypeFromDiagram(diagram);
         }

         foreach(EA.Element element in theElement.Elements)
         {
            RemoveAPIStereotypeFromContent(element); // recursion
         }
      }


      public void MarkContentWithAPIStereotype()
      {
         EA.ObjectType objType;
         object obj;
         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            MarkContentWithAPIStereotype((EA.Package)obj);
            EA_Repository.RefreshModelView(((EA.Package)obj).PackageID);
         }
         else if (objType == EA.ObjectType.otDiagram)
         {
            MarkDiagramWithAPIStereotype((EA.Diagram)obj);
            EA_Repository.RefreshModelView(((EA.Diagram)obj).PackageID);
         }
         else if (objType == EA.ObjectType.otElement)
         {
            MarkContentWithAPIStereotype((EA.Element)obj);
            EA_Repository.RefreshModelView(((EA.Element)obj).PackageID);
         }
      }

      private void MarkPackageWithAPIStereotype(EA.Package thePackage)
      {
         if (thePackage.StereotypeEx.IndexOf("API") < 0)
         {
            if (thePackage.StereotypeEx.Length > 0)
               thePackage.StereotypeEx += ",API";
            else
               thePackage.StereotypeEx = "API";
            thePackage.Update();
         }
      }

      private void MarkDiagramWithAPIStereotype(EA.Diagram theDiagram)
      {
         // diagrams only use one stereotype, not a list of them
         theDiagram.Stereotype = "API";
         theDiagram.Update();
      }

      private void MarkElementWithAPIStereotype(EA.Element theElement)
      {
         if (theElement.StereotypeEx.IndexOf("API") < 0)
         {
            if (theElement.StereotypeEx.Length > 0)
               theElement.StereotypeEx += ",API";
            else
               theElement.Stereotype = "API";
            theElement.Update();
         }
      }

      private void MarkContentWithAPIStereotype(EA.Package thePackage)
      {
         MarkPackageWithAPIStereotype(thePackage);

         foreach(EA.Diagram diagram in thePackage.Diagrams)
         {
            MarkDiagramWithAPIStereotype(diagram);
         }

         foreach(EA.Element element in thePackage.Elements)
         {
            MarkElementWithAPIStereotype(element);
         }

         foreach(EA.Package package in thePackage.Packages)
         {
            MarkContentWithAPIStereotype(package); // recursion
         }
      }

      private void MarkContentWithAPIStereotype(EA.Element theElement)
      {
         MarkElementWithAPIStereotype(theElement);

         foreach(EA.Diagram diagram in theElement.Diagrams)
         {
            MarkDiagramWithAPIStereotype(diagram);
         }

         foreach(EA.Element element in theElement.Elements)
         {
            MarkContentWithAPIStereotype(element); // recursion
         }
      }



      #region Temporary or experimental code


      private void parse_element(int parentId, EA.Element theElement, int recurse_level)
      {
         //if (parentId == 0 || parentId == theElement.ParentID)
         {
            EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, 
               "ParentId:"
               + parentId.ToString()
               + ", Level:"
               + recurse_level.ToString()
               + ", TreePos:"
               + theElement.TreePos.ToString() 
               + ", ELE-ParentID:"
               + theElement.ParentID.ToString()
               + ", ELE-PackageID:"
               + theElement.PackageID.ToString()
               + ", ELE-ElementID:"
               + theElement.ElementID.ToString()
               + ",  ELE-Name: " 
               + theElement.Name, -1);
         
            foreach(EA.Diagram theDiagram in theElement.Diagrams)
            {
               EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, 
                  "Level:"
                  + recurse_level.ToString() 
                  + ", ParentID:"
                  + theDiagram.ParentID.ToString()
                  + ", PackageID:"
                  + theDiagram.PackageID.ToString()
                  + ", DiagramID:"
                  + theDiagram.DiagramID.ToString()
                  + "  DIAGRAM: " 
                  + theDiagram.Name, -1);
            }

            foreach(EA.Element subElement in theElement.Elements)
            {
               parse_element(theElement.ElementID, subElement, recurse_level+1);
            }
         }
      }


      /// <summary>
      /// Parse a package structure in a similar way to that taken when users are generating a word
      /// document, only here we simply write diagnostic text to the output tab.
      /// </summary>
      /// <param name="thePackage"></param>
      /// <param name="recurse_level"></param>
      private void parse_package(EA.Package thePackage, int recurse_level)
      {
         EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, 
                                      "Level:"
                                    + recurse_level.ToString() 
                                    + ", ParentID:"
                                    + thePackage.ParentID.ToString()
                                    + ", PackageID:"
                                    + thePackage.PackageID.ToString()
                                    + ",  PACKAGE: " 
                                    + thePackage.Name, -1);

         // default handling of diagrams
         foreach(EA.Diagram theDiagram in thePackage.Diagrams)
         {
            if (theDiagram.ParentID == 0)
            {
               EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, 
                  "Level:"
                  + recurse_level.ToString() 
                  + ", ParentID:"
                  + theDiagram.ParentID.ToString()
                  + ", PackageID:"
                  + theDiagram.PackageID.ToString()
                  + ", DiagramID:"
                  + theDiagram.DiagramID.ToString()
                  + "  DIAGRAM: " 
                  + theDiagram.Name, -1);
            }
         }

         foreach(EA.Element subElement in thePackage.Elements)
         {
            if (subElement.ParentID == 0 || thePackage.PackageID == subElement.ParentID)
               parse_element(thePackage.PackageID, subElement, recurse_level+1);
         }

//         EA_ElementSorter elementSorter = new EA_ElementSorter(thePackage);
//         EA.Element theElement = null;
//         int theElementsRelativeLevel = 0;
//         if (true == elementSorter.getFirst(ref theElement, ref theElementsRelativeLevel))
//         {
//            do
//            {
//               int theElementsRecurseLevel = recurse_level + theElementsRelativeLevel;
//
//               EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, 
//                    "Level:"
//                  + theElementsRecurseLevel.ToString()
//                  + ", "
//                  + theElement.TreePos.ToString() 
//                  + ", ParentID:"
//                  + theElement.ParentID.ToString()
//                  + ", PackageID:"
//                  + theElement.PackageID.ToString()
//                  + ", ElementID:"
//                  + theElement.ElementID.ToString()
//                  + ",  ELEMENT: " 
//                  + theElement.Name, -1);
//
//            } while (true == elementSorter.getNext(ref theElement, ref theElementsRelativeLevel));
//         }

         foreach(EA.Package lowerLevelPackage in thePackage.Packages)
         {
            parse_package(lowerLevelPackage, recurse_level+1);
         }
      }


      /// <summary>
      /// Parses a specified package in a similar way to what would be done if a user were generating
      /// a word document, but here do nothing except write diagnostic text to the output tab.
      /// </summary>
      public void showDiscoveryOrder()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            EA.Package EA_ParentPackage = ((EA.Package)obj);

            EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Showing Object Discovery order for " + EA_ParentPackage.Name, -1);

            foreach(EA.Package thePackage in EA_ParentPackage.Packages)
            {
               parse_package(thePackage, 1);
            }
         }
         else
         {
            MessageBox.Show("You must select a package to parse");
         }
      }


      public void SaveVersionControlledPackage()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            EA.Package EA_Package = ((EA.Package)obj);

            if (EA_Package.IsControlled == true)
            {
               EA.Project EA_Project = EA_Repository.GetProjectInterface();

               EA_Project.SaveControlledPackage( EA_Package.PackageGUID );

               //EA_Project.SaveControlledPackage( EA_Project.GUIDtoXML( EA_Package.PackageGUID ) );
            }
            else
            {
               MessageBox.Show("The package selected is not version controlled");
            }
          }
         else
         {
            MessageBox.Show("You must select a package");
         }
      }


      #endregion
        }

  
}