Book a Demo

Author Topic: Select a package in model browser  (Read 7538 times)

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Select a package in model browser
« on: October 18, 2015, 10:00:55 pm »
Hello,

Is there an API to select a package in model browser automatically, by sending the appropriate path?
Selecting is limited to packages only, or elements can be selected this way as well?

Thanks.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Select a package in model browser
« Reply #1 on: October 18, 2015, 10:29:51 pm »
Try Repository.ShowInProjectView (element). You need to supply the EAelement so for a package it is its Package.Element. Haven't tried but it should work.

q.
« Last Edit: October 18, 2015, 10:30:16 pm by qwerty »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Select a package in model browser
« Reply #2 on: October 20, 2015, 03:24:04 am »
It is indeed ShowInProjectView(), but you should can pass the package as well.

This is what I have in my Framework to select an item in the project browser.

Code: [Select]
               if (value is Package)
                {
                      this.wrappedModel.ShowInProjectView(((Package)value).wrappedPackage);
                }
                else if (value is ElementWrapper)
                {
                      this.wrappedModel.ShowInProjectView(((ElementWrapper)value).wrappedElement);
                }
              else if (value is Operation)
              {
                  this.wrappedModel.ShowInProjectView(((Operation)value).wrappedOperation);
              }
              else if (value is Attribute)
              {
                  this.wrappedModel.ShowInProjectView(((Attribute)value).wrappedAttribute);
              }
              else if (value is Parameter)
              {
                    Operation operation = (Operation)((Parameter)value).operation;
                    this.wrappedModel.ShowInProjectView(operation.wrappedOperation);
              }

Geert

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Select a package in model browser
« Reply #3 on: October 20, 2015, 06:34:13 pm »
Hi Tehila,


Selecting something in the project browser is very straightforward. It's Repository.ShowInProjectView() as qwerty says, it takes a single argument, and you can pass an Element, a Package, a Diagram, an Attribute or a Method.

You should not pass Package.Element to select a package. It will probably work, but there's no need. Just pass the package object itself, or the diagram or whatever, and EA will sort it out.

Geert's code sample is not what you do in the EA API, you need his own wrapper API for that to work. With the EA API, just call Repository.ShowInProjectView(myElement).

However, if you're asking if you can select an item in the project browser based on the path you get from right-clicking in the project browser and selecting Copy / Paste -- Copy Node Path to Clipboard, the answer is no.

The node path separates package levels with a single period ('.'), but there's nothing stopping people from using that character in their package names, and the node path function has no escape-character scheme.

So you can't be sure if the periods in the node path are part of package names or just separators, and thus there is no API call to which you can pass such a node path.


Cheers,


/Uffe
My theories are always correct, just apply them to the right reality.

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: Select a package in model browser
« Reply #4 on: October 20, 2015, 07:01:45 pm »
Quote
Hi Tehila,


Selecting something in the project browser is very straightforward. It's Repository.ShowInProjectView() as qwerty says, it takes a single argument, and you can pass an Element, a Package, a Diagram, an Attribute or a Method.


/Uffe

Thanks for detailed responses.
I have only a package path separated with slashes.

I have to find the appropriate package elements in order to send it to the ShowInProjectView() method.

Is there any API for finding package element by path?

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Select a package in model browser
« Reply #5 on: October 20, 2015, 07:32:16 pm »
I'm afraid not. The EA API has no path-resolver method.

Just like with the periods I mentioned before, slashes are perfectly valid in package names. You can traverse the package hierarchy step by step, but as long as the separator character is valid in an EA element name there will always be the possibility of ambiguity.

Consider the following package hierarchy:
+- A
|   |- B
|
+- A/B

From the path "A/B", you can't determine which of the packages "B" and "A/B" you should select in the project browser.

That's a constructed example, but the point is that essentially all printable characters are valid in EA element names so neither period, space, slash or backslash work as separator characters. If you want to be sure, you need to pick a separator character that's invalid (such as CR/LF), or implement an escape character.

If you don't have control over the format and are stuck with slashes, your solution won't be 100 per cent.

That said, if those limitations are acceptable, you can easily parse the path string, breaking it into its constituent parts, and traverse the tree from the top.

The top-level packages ("root nodes") are stored in Repository.Models, which is a collection of Packages. Each package holds a collection of its immediate children (Package.Packages), so traversing top-down is easy. There's even a Collection.GetByName() method you can use.

Cheers,


/Uffe
« Last Edit: October 20, 2015, 07:33:27 pm by Uffe »
My theories are always correct, just apply them to the right reality.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Select a package in model browser
« Reply #6 on: October 20, 2015, 07:32:52 pm »
Quote
Is there any API for finding package element by path?

I Implemented that function in the EA Navigator
You can find the relevant code  here
Look for the operation getItemFromFQN() and follow it down.

Code: [Select]
public UML.UMLItem getItemFromFQN(string FQN)
{
      //split the FQN in the different parts
      UML.UMLItem foundItem = null;
      foreach(UML.Classes.Kernel.Package package in  this.rootPackages)
      {
            
            foundItem = package.getItemFromRelativePath(FQN.Split('.').ToList<string>());
            if (foundItem != null)
            {
                  break;
            }
      }
      return foundItem;
}

Geert
« Last Edit: October 20, 2015, 07:34:46 pm by Geert.Bellekens »

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: Select a package in model browser
« Reply #7 on: October 20, 2015, 10:50:18 pm »
Thanks querty, Geert and Uffe for the efficient help.

I implemented your advice, and here is the code- for others' sake:

Code: [Select]
           private EA.Package FindPackageElementByPath (string pathOfPackage)
            {
                  //Get the fisrt package name in path
                  string fisrtPackage = pathOfPackage.Substring(0,  pathOfPackage.IndexOf('/'));
                  
                  //Get the rest of path (the whole path, without the fisrt package)
                  string restOfPath = pathOfPackage.Substring(pathOfPackage.IndexOf('/') + 1);
                  
                  //Stores the appropriate package for current package name
                  EA.Package currentPackage = null;
                  
                  //Get the appropriate package of the first package name. The repository.Models collection stores the root packages of model.
                  Object firstPackageObject = connectToDB.GetRepository.Models.GetByName(fisrtPackage);
                  //Holds the first package currently in path
                  EA.Package parentPackage = firstPackageObject as EA.Package;
                  
                  //Find the next package in path
                  if (parentPackage != null)
                  {
                        //Get the next packages in path, until the end of it
                        while(restOfPath.IndexOf('/') >0)
                        {
                              string currentPackageName;
                              currentPackageName = restOfPath.Substring(0,  restOfPath.IndexOf('/') );
                              
                              //Cut the current package name from path
                              restOfPath = restOfPath.Substring(restOfPath.IndexOf('/')  + 1);
                              
                              //Get the appropriate package of current name
                              object currentPackageObject = parentPackage.Packages.GetByName(currentPackageName);
                              currentPackage = currentPackageObject as EA.Package;
                              //Set the parentPackage to store the current package, in order to find the next package in path in parentPackage directly owned packages.
                              parentPackage = currentPackage;
                        }//while
                        
                        //When there is no more slashes in path, path contains the last package name of original path, fins its appropriate package.
                        object lastPackageObject = currentPackage.Packages.GetByName(restOfPath);
                        EA.Package lastPackage = lastPackageObject as EA.Package;
                        return lastPackage;
                  }//if
                  
                  //If last package was not found, return at least its parent package
                  return currentPackage;

            }//FindPackageElementByPath()

of course one can use the returned package as the parameter of ShowInProjectView() method.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Select a package in model browser
« Reply #8 on: October 20, 2015, 11:12:58 pm »
Good stuff. :)

Just be aware that Collection.GetByName() throws an exception if it doesn't find a package with the specified name, so you need to catch that somewhere.

/U
My theories are always correct, just apply them to the right reality.