« Back to AAU Project

Object class-dependent context menu

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Object class-dependent context menu
Answer
9/23/15 12:45 PM
I am trying to implement the context menu that is going to be shown only for the objects of the particular class, but, according to the documentation, this seems to be not possible:
"You cannot constrain the new context menu to a specific class or relationship type. So it is wise to give the menu entry a general name and then adjust the functionality,
according to the selected object, within the body of the function. "

I have tried to investigate if the workaround like the one used for model-type-specific menus could be used in this case, but, again, this seems to be not
possible as well because I was not able to find the event fired on activation of the particular object on a diagram.

This is quite an unfortunate restriction for me as I have currently implemented a deployment configuration for my system as a diagram object and wanted to make a context menu for
the particular configuration run a script performing the actual deployment. Unfortunately, there are other objects on this diagram which must not support this context menu.

Am I missing anything? Are there any workarounds for this "context menu" approach?

RE: Object class-dependent context menu
Answer
9/23/15 1:03 PM as a reply to Vladimir Shekhovtsov.
When defining context menus, it is not possible to define a context entry specifically for one class or relation but only for abstract tool "types. The AdoScript code to add a context entry and support context definitions are listed below:

1CC "Application" INSERT_CONTEXT_MENU_ITEM context:Context item:strValue [ pos:strValue ]

where context can be one of the following:
"drawingarea.general" -- General drawing area context menu.
"drawingarea.swimlane" -- Drawing area context menu for swimlanes.
"drawingarea.mobject" -- Drawing area context menu for modeling objects.
"drawingarea.connector" -- Drawing area context menu for connectors.
"modelingtable" -- "Tabular modeling" browser context menu.
"explorer.db" -- Explorer context menu in "models in database" mode.
"explorer.windows" -- Explorer context menu in "opened model windows" mode.
"startpage.thumb" -- Start page context menu for a model thumbnail.

The menu name is specified with item. If the item shall be placed in a submenu, the parts of the item path are separated by TAB ("\t") characters, e.g. "New\tModelgroup...". A separator can be inserted by using "-" as item name.

To accomplish the behaviour to distinguish between types, you can implement a mechanim retrieve all selected elements, iterate through them and distinguish between specific classes.

An example for such an implementation is shown in the code fragement below, applicable for the "drawingarea.mobject" context.

A. Add the context in "External coupling" library attribute
1ON_EVENT "AppInitialized" {
2  # ...
3  CC "Application" INSERT_CONTEXT_MENU_ITEM context:"drawingarea.mobject" item:"Context on object"
4  CC "Application" raw SET_CMI_SELECT_HDL context:"drawingarea.mobject" item:"Context on object" {
5    EXECUTE file: ("db:\\objectcontext.asc")
6   }
7  # ...
8}

B. Code in "objectcontext.asc"

 1# 1. Get all objects selected when triggering the context action
 2CC "Modeling" GET_SELECTED
 3SET lSelectedObjects: (objids)
 4# 2. Retrieve class information, and perform action for each class type
 5FOR sObjectID in: (lSelectedObjects) {
 6  CC "Core" GET_CLASS_ID objid: ( VAL sObjectID)
 7  SET bIsRelation: (isrel)
 8  SET nClassID: (classid)
 9  IF (bIsRelation) {
10    # no action performed, as the context is only for classes
11  }
12  ELSE {
13    # get class name
14    CC "Core" GET_CLASS_NAME classid: (nClassID)
15    SET sClassName: (classname)
16    IF (sClassName = "Car") {
17      CC "AdoScript" INFOBOX ("Found a car with ID: " + sObjectID + "\n\nPerform a specific car action now!")
18    }
19    ELSIF (sClassName = "Truck") {
20      CC "AdoScript" INFOBOX ("Found a truck with ID: " + sObjectID + "\n\nPerform a specific truck action now!")   
21    }
22    ELSE {
23      #catch all case
24      CC "AdoScript" INFOBOX ("No action found for class: " + sClassName + "!\n\nSeems the developer has not though about that case")
25    }
26  }
27}

A similar approach can be used for relationclasses. An example implementation can be found in the attached library and AdoScript files.
Attachments: ContextMenu Library.abl (18.5k), objectcontext.asc (1.0k), relationcontext.asc (0.9k)