Customizing Workspace context-menu

My first article of 2010 described customizing Matlab’s Workspace table, in particular the values presented in the Bytes column.

Last week, a reader of that article posted a comment asking how to customize the context (right-click) menu with some user-defined actions. The user has found that modifying the menu via the regular table handles gets reset automatically.

Today’s article will describe an easy and effective way to add user-defined actions to the Workspace table, for specific object types.

The trick is not to modify the context-menu directly. As the reader above has noticed, this menu is automatically recreated on the fly, so no change will be persistent. Instead, we modify Matlab’s internal registry of class-specific context-menus. This is done in several prefspanel.m files in the Matlab codebase (For example: \toolbox\matlab\audiovideo\prefspanel.m and \toolbox\signal\signal\prefspanel.m).

The mechanism relies on the following Java method, which is unsupported and undocumented, yet has existed in the present form for the past several releases:

classes = {'double', 'java.lang.Object'};
menuName = 'My context-menu';
menuItems = {'Inspect', 'Properties', '-', 'class name'};
menuActions = {'inspect($1)', 'get($1)', '', 'class($1)'};
com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry.registerClassCallbacks(classes,menuName,menuItems,menuActions);

Customizable Workspace table context-menu

Customizable Workspace table context-menu

Once you have found your particular context-menu configuration useful, place the short customization code in your startup.m file so that the change becomes permanent in all future Matlab sessions.

A few other supporting static methods are available in the com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry class: getClassCallbacksInformation (className), registerSimilarClassCallbacks(newClassNames,definedClassName) and unregisterClassCallbacks(definedClassName). For example:

com.mathworks.mlwidgets.workspace.MatlabCustomClassRegistry.getClassCallbacksInformation('double')
 
ans =
java.lang.Object[]:
    'My context-menu'
    [4 element array]        % = menuItems
    [4 element array]        % = menuActions
Categories: Desktop, High risk of breaking in future versions, Java, Undocumented feature

Tags: , , ,

Bookmark and SharePrint Print

19 Responses to Customizing Workspace context-menu

  1. Donn Shull says:

    It may be worth noting for people developing toolboxes that at startup MATLAB searches the path for prefspanel.m and executes them. An alternative location for context-menu configurations is in your own prefspanel.m file.

    Thanks Yair

  2. Mike L says:

    Works like a charm! I’m amazed so much of the underlying machinery is exposed and how straightforwardly you were able to go ahead and play with it. Thanks, Yair!

    • Thanks Mike – it may look straightforward when I present it (which is exactly my purpose), but believe me when I say that each of these seemingly simple posts is the result of many hours of research, trial-and-error, tests, blind-alleys etc….

  3. Bluesmaster says:

    is this also possible for the “current folder” component?
    I would like to add an entry for tortoise svn commit for single files

    best regards

    Bluesmaster

    • @Bluesmaster – I don’t think so. There’s probably a similar mechanism somewhere else, but I don’t think it’s under com.mathworks.mlwidgets.workspace.*. Then again, I never checked so maybe it is… Let us all know if you discover anything interesting.

    • Bluesmaster says:

      hm too bad. And what about a workaround? Maybe a button in the “current folder” toolbar?
      They also have to know which file is selected e.g. for creating a new folder into the selected one.

      Do you have an idea?

      Thanks

    • @Bluemaster – I never tried this but I’m sure this is also possible. I showed how to customize the Desktop toolbar in section 8.1.4 of my Matlab-Java book, and you could start from there.

  4. Bluesmaster says:

    ok got it going, thanks to your advice

    http://www.mathworks.com/matlabcentral/fileexchange/40685-tortoise-svn-toolbar-integration

    The deciding lines are:

    com.mathworks.mde.explorer.Explorer.getInstance.getComponents
    com.mathworks.mde.explorer.Explorer.getInstance.getTable

    Got some questions/ advices:

    – what about a diff-tool in findjobj (like regshot), return of investment could be reached in days

    – what about a small search line in findjobj ( popup( ‘class’ , ‘property’ …) | searchstring)

    – copying classnames to clipboard could also be usefull (lost an hour by a typing error :( )

    – how to get informations about com.mathworks.* without having an entity? (just had luck, that I “discovered” .getInstance)

    sorry for the big amount of text, and best regards

    Bluesmaster

    • I think you may be trying to turn findjobj into a lot more than it was intended for…

      For a diff tool, try my objdiff utility.

      For the object classname, simply copy the object to the workspace (using one of the right-click context menu options) and then run the class function on it (class(h))

      For basic information about a class you could use either my checkClass utility or my uiinspect utility. For documentation on the internal classes, read my Matlab-Java book.

    • Bluesmaster says:

      “I think you may be trying to turn findjobj into a lot more than it was intended for…”

      …that is possible, but I thought gathering ideas is never a bad idea.

      objdiff is great, but it doesn’t support recursion ( ? ) so it does not suite for the purpose of “Checking a checkbox and find out the corresponding java-obj that changed”.
      That’s what I meant about “Return of investment”, cause I thought your work is a lot about investigation ( of course today you seem to know the most things, but some newcomers like me maybe not :) )

      Thanks for your help

      Bluesmaster

  5. Bluesmaster says:

    Hello Yair,

    I need a small advice. I got access to the currentfileBrowser. It is a JideSoft TreeTable (i think)

    cfb = com.mathworks.mde.explorer.Explorer.getInstance.getTable

    I searched for hours, but i cant find a way to customize a cells style (icon/ background…)
    You have a lot of experience with the jide-Tools can you tell me how to to do that if I got lets say this item:

    cfb.getItem(0)

    That would help me so much.

    best regards

    Bluesmaster

    • @Bluesmaster – Answering your query is non-trivial and goes beyond what I can provide in a simple comment here.

      You should read either my book or my uitable report as a starting point. I can also help you customize your GUI as a consultant if you wish.

  6. CrisPi says:

    Hi,
    great and super useful article, but I ran into a bit of a problem – the classes ‘single’ and ‘double’ only work when they are not complex (Matlab 2013a). I haven’t had any luck finding the appropriate class name for the complex case (e.g. ‘complex single’) – could you please help me out?
    Thanks, Chris

    • Malcolm Lidierth says:

      @CrisPi

      Complex numbers do not have a special type: a complex number is stored as two numbers representing its real and imaginary types, so e.g

      >> i
      ans =
              0 + 1.0000i
       
      >> isreal(imag(i))
      ans = 
              true
    • Yair Altman says:

      @CrisPi – all you need to do is to add the class which is specified in the Workspace’s Class column (in this case, 'double (complex)' or 'single (complex)'), and then restart Matlab. As easy as that…

      …and similarly for any user defined or third-party class…

      classes = {'double', 'single', 'double (complex)', 'single (complex)', 'java.lang.Object', 'com.mathworks.mde.desk.MLDesktop'};
      ...
    • CrisPi says:

      Thanks so much Yair, that worked – I was looking at the Value column, not the Class one.

      But on a related note: Is it also possible to define a command for multiple choices? ie selecting two, and then the command is executed on both of them (or they are combined, like it’s done for the plot(var1,var2) possibility)…

  7. Lex says:

    Thank you, Yair! I use Matlab 2017a and make menu for Java class ‘java.lang.Object’. But this menu item appears only for this class and don’t take inheritance into account. Can I make menu item for all Java variables?

  8. MatthieuC says:

    Hello !
    Is there an equivalent for the “Current Folder” context-menu?
    I know the package is com.mathworks.mlwidgets.explorer but I cannot handle the way of adding context-menu with it.

    Thanks

    • MatthieuC says:

      To give more detail about my question… I would add some Subversion specific actions on Right-Click (for instance, setting working copy depth commands).
      Thank you for your help.

Leave a Reply

Your email address will not be published. Required fields are marked *