Question
· Jan 17, 2022

How to create a toolbar button in Cache Studio that trigger a custom command after click ?

I would like to create a toolbar button in Cache Studio. After clicking on it, it would run a custom command (eg: to execute a routine that will clean a global).

I took a look at dialog that is shown after right clicking on a toolbar in Studio, then choosing "Customize" but AFAIK there is nothing there that allow such a thing.

I know it's possible to customize menu items by extending %Studio.SourceControl.Base class, is there something similar for toolbars ?


Here is some base code example (based on Danny Wijnschenk answer) :

(as he explained, the source control class has to be selected in the Portal (System Administration > Configuration > Additional settings > Source Control)

Class Test Extends %Studio.SourceControl.Base
{

XData Menu
{
<MenuBase>
<Menu Name="SourceControl" Type="0">
<MenuItem Name="NewItem"/>
</Menu>
</MenuBase>
}

Method UserAction(Type As %Integer, Name As %String, InternalName As %String, SelectedText As %String, ByRef Action As %String, ByRef Target As %String, ByRef Msg As %String, ByRef Reload As %Boolean) As %Status
{     
    if Name="SourceControl,NewItem"
    {
        //do something here
    }

    Quit $$$OK
}

}

Once menu item is added, it can be dragged to toolbar as a button.

Product version: Caché 2018.1
Discussion (6)2
Log in or sign up to continue

Ok, this is a test i wrote to add some basic source control (auto save of class and manually exporting the current project). It shows how you can add 2 menu items.
Don't  forget to activate the source control class for a namespace in the portal (admin-config-additional settings-source control)
 

Class Studio.Extension Extends %Studio.Extension.Base
{ XData Menu
{
<MenuBase>
<Menu Name="GIT" Type="0">
<MenuItem Name="Directory Setting"/>
<MenuItem Name="Export Current Project"/>
</Menu>
</MenuBase>
} /// Perform any login step here.
Method Login(Name As %String, Password As %String) As %Status
{
Write "==============================================================================================",!
Write "= Welcome ",$Get(Name),", this namespace is under source control by class Studio.Extension",!
Write "= Source files are saved in ",$Get(^SourceControl(0,$UserName,"GIT_Dir")),!
Write "==============================================================================================",!
Set ^SourceControl($J,"currentProject")=""
Quit $$$OK
} Method UserAction(Type As %Integer, Name As %String, InternalName As %String, SelectedText As %String, ByRef Action As %String, ByRef Target As %String, ByRef Msg As %String, ByRef Reload As %Boolean) As %Status
{
If $Piece($Get(InternalName),".",*)="PRJ" {
Set ^SourceControl($J,"currentProject")=$Piece(InternalName,".",1,*-1)
}
;Write "StudioExtension: Loading ",$Get(InternalName)," (",$Get(Type),"-",$Get(Name),"), current project is ",$Get(^sourcecontrol($J,"currentProject")),!
If Type=0 Do ..ExecuteMenu(Name, .Action, .Target, .Msg)
Quit $$$OK
} Method ExecuteMenu(Name As %String, ByRef Action As %String, ByRef Target As %String, ByRef Msg As %String)
{
If Name="GIT,Directory Setting" {
Set Target="Directory for GIT source control"
Set Msg=$Get(^SourceControl(0,$UserName,"GIT_Dir"))
Set Action=7
ElseIf Name="GIT,Export Current Project" {
Set Target="Export Current Project"
Set Msg=$Get(^SourceControl($J,"currentProject"))
Set Action=7
}
} Method AfterUserAction(Type As %Integer, Name As %String, InternalName As %String, Answer As %Integer, Msg As %String = "", ByRef Reload As %Boolean) As %Status
{
#Dim project, dir as %String ;Write Type,Name,InternalName," : "
;Write "You answered ",$Get(Answer) ;1=yes, 0=no, 2=cancel
;Write "with message ",$Get(Msg),!
If Answer=1,Type=0 {
If Name="GIT,Export Current Project" {
Set project = Msg
Set objProject = ##class(%Studio.Project).%OpenId(project)
If objProject'="" {
Set dir = $Get(^SourceControl(0,$UserName,"GIT_Dir"))
If dir="" {
Write !,"StudioExtension: Please set GIT directory first"
else {
If $E(dir,*)'="\" Set dir=dir_"\"
Do objProject.Export(dir_project_$ZDate($H,8)_".xml")
}
else {
Write !,"StudioExtension: Project does not exist !"
}
Set objProject = ""
ElseIf Name="GIT,Directory Setting" {
Set ^SourceControl(0,$UserName,"GIT_Dir") = Msg
Write !,"StudioExtension: GIT Directory Saved"
}
} Quit $$$OK
} /// Called after the compile of the item is done.
Method OnAfterCompile(InternalName As %String) As %Status
{
#Dim dir, separator, items, extension, fullFileName as %String Set dir = $Get(^SourceControl(0,$UserName,"GIT_Dir"))
If dir="" {
Write !,"StudioExtension: ","No directory setup to save source code"
else {
Set separator = $Select($ZV["Windows":"\",1:"/")
If dir'="", $E(dir,*)'=separator Set dir=dir_separator
Set extension=$Piece(InternalName,".",*)
Set dir=dir_extension_separator
Set fullFileName=dir_$Replace($Piece(InternalName,".",1,*-1),".",separator)_".xml"
Do ##class(%File).CreateDirectoryChain($Piece(fullFileName,separator,1,*-1)) If 0 {  ;do not export as .xml file for Git :
Set items(InternalName)=""
Do $system.OBJ.Export(.items,fullFileName)
Write !,"StudioExtension: ",InternalName_" saved as "_fullFileName
} If $Piece(InternalName,".",*)="CLS" {
Set $Piece(fullFileName,".",*) = "cls"
If $system.OBJ.ExportUDL(InternalName,$Replace(fullFileName,".xml",".cls")) {
Write !,"StudioExtension: ",$Piece(InternalName,".",1,*-1)," saved as ",fullFileName
else {
Write !,"StudioExtension: ","Error while saving "_$Piece(InternalName,".",1,*-1)," as ",fullFileName
}
}
}
Quit $$$OK
} }