Usage example (UModel Find sample)

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  UModel Programmer's Reference > Scripting Editor >

Usage example (UModel Find sample)

This scripting example is available in the ...\UModelExamples folder as ScriptSampleFind.asprj.

 


Global Declarations:

 

// import System.Windows.Forms namespace for all macros, forms and events:

CLR.Import( "System.Windows.Forms" );

 


Form FindSample

 

//Occurs whenever the user loads the form.

function FindKinds_Load( objSender, e_EventArgs )

{

 // initialize all controls:

 objSender.btnFind.Enabled = false;

 

 objSender.gbOptions.cbMatchCase.Checked = bMatchCase;

 objSender.gbOptions.cbWholeName.Checked = bMatchWholeName;

 

 objSender.gbSearchKinds.cbClasses.Checked                         = bSearchClasses;

 objSender.gbSearchKinds.cbInterfaces.Checked                         = bSearchInterfaces;

 objSender.gbSearchKinds.cbEnumerations.Checked                         = bSearchEnumerations;

 objSender.gbSearchKinds.cbPackages.Checked                         = bSearchPackages;

 objSender.gbSearchKinds.cbProperties.Checked                         = bSearchProperties;

 objSender.gbSearchKinds.cbOperations.Checked                         = bSearchOperations;

 objSender.gbSearchKinds.cbEnumerationLiterals.Checked                = bSearchEnumLiterals;

 objSender.gbSearchKinds.cbOthers.Checked                                 = bSearchOthers;

}

 

//Event raised when the value of the Text property is changed on Control.

function FindKinds_tbFindText_TextChanged( objSender, e_EventArgs )

{

 // objSender is the 'Find' TextBox; also get the form:

 var objForm = objSender.FindForm();

 

 // to enable the find button, searchstring should at least have one character

 objForm.btnFind.Enabled = ( objSender.Text.length > 0 );

}

 

//Occurs when the component is clicked.

function FindType_btnFind_Click( objSender, e_EventArgs )

{

 // objSender is the 'Find' Button; also get the form:

 var objForm = objSender.FindForm();

 

 // save chosen settings in global macro variables:

 bMatchCase                 = objForm.gbOptions.cbMatchCase.Checked;

 bMatchWholeName         = objForm.gbOptions.cbWholeName.Checked;

 

 bSearchClasses         = objForm.gbSearchKinds.cbClasses.Checked;

 bSearchInterfaces        = objForm.gbSearchKinds.cbInterfaces.Checked;

 bSearchEnumerations        = objForm.gbSearchKinds.cbEnumerations.Checked;

 bSearchPackages        = objForm.gbSearchKinds.cbPackages.Checked;

 bSearchProperties        = objForm.gbSearchKinds.cbProperties.Checked;

 bSearchOperations        = objForm.gbSearchKinds.cbOperations.Checked;

 bSearchEnumLiterals        = objForm.gbSearchKinds.cbEnumerationLiterals.Checked;

 bSearchOthers                = objForm.gbSearchKinds.cbOthers.Checked;

 

 var sFindText = objForm.tbFindText.Text;

 

 if ( !bMatchCase )

         sFindText = sFindText.toUpperCase();

         

 // show wait cursor during search; load static "Cursors" class:

 var objCursors = CLR.Static( "Cursors" );

 objForm.Cursor = objCursors.WaitCursor;

 // start update of ListView:

 objForm.lvResults.BeginUpdate();

 // reset Items:

 objForm.lvResults.Items.Clear();

 // start find with root-package:

 Find( Application.ActiveDocument.RootPackage, sFindText );

 // ListView has been updated:

 objForm.lvResults.EndUpdate();

 // reset cursor to default:

 objForm.Cursor = objCursors.Default;        

 

 // inform user when nothing has been found:

 if ( objForm.lvResults.Items.Count == 0 )

         alert( "Nothing found!" );

}

 

//Occurs whenever the 'SelectedIndex' property for this ListView changes.

function FindKinds_lvResults_SelectedIndexChanged( objSender, e_EventArgs )

{

 // focus selected UML element in Model Tree:

 if ( objSender.SelectedItems.Count == 1 )

 {

         // get qualified name of selected item:

         var selItem = objSender.SelectedItems.Item( 0 );

         var sQualifiedName = selItem.SubItems.Item( 2 ).Text;

 

         var objDocument = Application.ActiveDocument;

         // find UML element by help of the qualified name:

         var objUMLElement = objDocument.RootPackage.FindOwnedMemberWithQualifiedName( sQualifiedName );

         // focus UML element in Model Tree:

         objDocument.FocusUMLDataInModelTree( objUMLElement, false, true );

 }

}

 


Macro Find Sample:

 

// ############################################

// 'Find Sample' macro

// Find UML model elements by name and kind

// ############################################

 

// global variables of macro

var bMatchCase                = false;

var bMatchWholeName = false;

 

var bSearchClasses                = true;

var bSearchInterfaces        = true;

var bSearchEnumerations        = true;

var bSearchPackages                  = true;

var bSearchProperties          = true;

var bSearchOperations          = true;

var bSearchEnumLiterals        = true;

var bSearchOthers                = false;

 

// verify if UML element kind should be considered

function MatchKind( objUMLElement )

{

 if ( objUMLElement.KindName == "Class"                         ) return bSearchClasses;

 if ( objUMLElement.KindName == "Interface"                 ) return bSearchInterfaces;

 if ( objUMLElement.KindName == "Enumeration"                ) return bSearchEnumerations;

 if ( objUMLElement.KindName == "Package"                         ) return bSearchPackages;

 if ( objUMLElement.KindName == "Property"                 ) return bSearchProperties;

 if ( objUMLElement.KindName == "Operation"                 ) return bSearchOperations;

 if ( objUMLElement.KindName == "EnumerationLiteral"        ) return bSearchEnumLiterals;

 return bSearchOthers;

}

 

// verify if UML element name matches search options

function MatchName( objUMLNamedElement, sFindText )

{

 var sName = objUMLNamedElement.Name;

 if ( !bMatchCase )

         sName = sName.toUpperCase();

 

 if ( bMatchWholeName )

         return ( sName == sFindText )

 else

         return ( sName.indexOf( sFindText ) != -1 )

}

 

// recursively search all elements

function Find( objUMLElement, sFindText )

{

 // we are only interested in named elements:

 if ( objUMLElement.IsKindOf( "NamedElement" ) )

 {

         var objUMLNamedElement = objUMLElement;

         

         // only list elements where kind and name match:

         if ( MatchKind( objUMLNamedElement ) && 

              MatchName( objUMLNamedElement, sFindText ) )

         {

                 // create a new ListViewItem class

                 var newItem = CLR.Create( "ListViewItem", objUMLNamedElement.KindName );

                 // and add it to the listview - items:

                 lastForm.lvResults.Items.Add( newItem );

                 // append name and qualified name in sub-items:

                 newItem.SubItems.Add( objUMLNamedElement.Name );

                 newItem.SubItems.Add( objUMLNamedElement.QualifiedName );

         }

 }

 

 // also iterate over all children (ownedElements):

 var ownedElements = objUMLElement.OwnedElements;

 var itr = new Enumerator( ownedElements );

 for ( ; !itr.atEnd(); itr.moveNext() )

         Find( itr.item(), sFindText );

}

 

// create and show the form:

ShowForm( "FindSample" );

 

 


© 2019 Altova GmbH