Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute "collapse-all-folds" in the MATLAB editor programatically?

I've been struggling with the problem in the subject for a bit longer than I'd like to admit.

I'm attempting to programatically execute the same Action that occurs when the user either clicks on the View > Collapse All button or right-clicks within the editor window and then Code Folding > Fold All.

What I tried\found so far:

  • The String that corresponds to the Action may be found in the enum com.mathworks.mde.editor.ActionID and is: 'collapse-all-folds'.
  • When the Action activates, the following method seems to be executed: org.netbeans.api.editor.fold.FoldUtilities.collapseAll(...) (hence the netbeans tag).
  • This code allows me to get instances of EditorAction, ActionManager, MatlabEditor:

jEd = com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor;
jAm = com.mathworks.mde.editor.ActionManager(jEd);
jAc = com.mathworks.mde.editor.EditorAction('collapse-all-folds');

My problem is that I can't find a way to actually activate the Action.

Any ideas / alternatives?


EDIT1: After digging a bit in "the book", I think I came even closer than before (but still not quite there). Quoting from the book:

Java GUI components often use anActionMapto store runnableActionsthat are invoked by listeners on mouse, keyboard, property, or container events. Unlike object methods,Actionscannot be directly invoked by MATLAB.

And then a workaround is explained which involves roughly: getting some sort of an Action object; creating an ActionEvent and invoking Action's actionPerformed with the ActionEvent as an argument, as implemented below:

import java.awt.event.*;
jEd = com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor;
jAm = com.mathworks.mde.editor.ActionManager(jEd);
jAc = jAm.getAction(com.mathworks.mde.editor.EditorAction('collapse-all-folds'));
jAe = ActionEvent(jAm, ActionEvent.ACTION_PERFORMED, '');
jAc.actionPerformed(jAe);

This code runs without errors - but does (seemingly?) nothing. I suspect that I'm calling ActionEvent and actionPerformed on the wrong objects (ActionManager has possibly nothing to do with this problem at all).


P.S.

I know that there's a hotkey that does this (Ctrl + =), but this is not what I'm looking for (unless there's a command to simulate a hotkey press :) ).

like image 872
Dev-iL Avatar asked Sep 23 '14 11:09

Dev-iL


1 Answers

After immeasurable digging, trial and way too much error - I've done it!

function FullyCollapseCurrentScript()

%// Get the relevant javax.swing.text.JTextComponent:
jTc = com.mathworks.mlservices.MLEditorServices ...
        .getEditorApplication.getActiveEditor.getTextComponent();
%// Get the FoldHierarchy for the JTextComponent:
jFh = org.netbeans.api.editor.fold.FoldHierarchy.get(jTc);
%// Finally, collapse every possible fold:
org.netbeans.api.editor.fold.FoldUtilities.collapseAll(jFh);

end

or if compressed into a single, messy, command:

org.netbeans.api.editor.fold.FoldUtilities.collapseAll(...
org.netbeans.api.editor.fold.FoldHierarchy.get(com.mathworks. ...
mlservices.MLEditorServices.getEditorApplication.getActiveEditor. ...
getTextComponent()));

Note that this works on the script currently open in the editor.

like image 141
Dev-iL Avatar answered Nov 14 '22 22:11

Dev-iL