Skip to end of metadata
Go to start of metadata

Viewpoints may be offered to the user via the context menu of objects in the model. This way you can select one or more objects, and then choose to execute an appropriate viewpoint on this selection. The viewpoint's script is able to retrieve the selected object(s) via the predefined variable selection. To which object(s) the viewpoint should be applicable can be determined by an additional script with the special name contextMenu.

There are two variants of the contextMenu script: one that outputs to "true" and one that outputs to "false". Which one you use depends on your prefererence. 

The if statement in the script specifies on what elements the viewpoint should be available in the context menu. For example, if you want a viewpoint to be available in the context menu of a view, specify the object "AbstractView". If a viewpoint should only be available on Business actors, specify "BusinessActor" as the object.

The header statement in the script defines the viewpoint title that is actually shown to the user in the Execute Viewpoint on Selection window. If no header statement is present, the name of the viewpoint is displayed.

Below are two examples of a contextMenu script.

//contextMenu

header "Count object per type";
// All objects in the selection must be either a layer or a model, check all of them:
forall obj in selection {
	if ( !(obj is "AbstractScheme") && !(obj is "MM_Model") ) {
		output false;	// not a layer or model: viewpoint not in context menu
		stop;			// no use in checking the other objects in the selection, stop now
	}
}
// If we get here, all objects in the selection were layers or models
output true;			// viewpoint in context menu


//contextMenu

header "My color view";
// All objects in the selection must be a view, check all of them:
forall obj in selection {
 if ( obj is "AbstractView" ) {
    output true;	// a view: viewpoint in context menu
    stop;			// no use in checking the other objects in the selection, stop now
 }
}
// If we get here, none of the objects in the selection were views
output false;		// viewpoint not in context menu

Steps:

Please take note of the following: Currently, presenting a viewpoint in an object's context menu only works when creating a new viewpoint. Including the contextMenu script in existing scripts does not work. A workaround would be recreating the existing viewpoint and saving it including the contextMenu script.

  1. Open the (newly created and unsaved) viewpoint in the Query tool.

  2. Right-click the current script tab and select Insert script.

  3. Right-click the new script tab and select Properties.

  4. Enter the name contextMenu and click OK. The new tab now has the name contextMenu.

  5. Paste the contextMenu script on the new script tab.



  6. In Output type, specify the output of the viewpoint.

  7. Click Save as, and save the viewpoint in the model.

  8. Close the Query tool.


Depending on where the viewpoint should be available, right-click it and select Execute Viewpoint. In the window that appears the viewpoint should be available to execute.

Example

The following example defines a table type viewpoint that counts all objects in the selection. The script looks under all objects in selection for child objects and keeps a total per object type. These totals are output at the end.

totals = Index();
forall s in selection {
     forall obj in s {
          if ( !(obj is "Relation") ) {
                  cnt = totals.valueFor( obj.type() );
                  if ( cnt.isUndefined() ) {
                         totals.add( obj.type(), 1 );
                  }
                  else {
                        totals.add( obj.type(), cnt+1 );
                  }
            }
      }
}
header "Type", "Number";
forall obj, cnt in totals output obj, cnt;
sort;


By adding the contextMenu script "Count object per type" (as shown above), the viewpoint will be available when you select the object and/or one or more layers in it. Right-clicking this selection and then selecting Execute viewpoint should show the viewpoint.


Example of a viewpoint available in the context menu of a layer