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

2

8

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 :) ).

Prickly answered 23/9, 2014 at 11:44 Comment(3)
Perhaps @YairAltman will hear this call and answer.Edrisedrock
@Sam - Hehe... Nice try :)Prickly
Ahhhh! Thanks for the hotkey !! That is what I was looking for!Seminary
P
3

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.

Prickly answered 7/10, 2014 at 22:42 Comment(0)
T
1

Not a perfect solution but simulating the default hotkey press with java.awt.robot is possible.

...finding a way to actually fire the Action directly would be better...

import java.awt.Robot;
import java.awt.event.*;
RoboKey = Robot;

jTextComp = com.mathworks.mlservices.MLEditorServices. ... 
        getEditorApplication.getActiveEditor.getTextComponent;


jTextComp.grabFocus()
drawnow;            %// give time for focus


if jTextComp.hasFocus()
    RoboKey.keyPress(KeyEvent.VK_CONTROL);
    RoboKey.keyPress(KeyEvent.VK_EQUALS);

    RoboKey.keyRelease(KeyEvent.VK_CONTROL);
    RoboKey.keyRelease(KeyEvent.VK_EQUALS);

    com.mathworks.mde.cmdwin.CmdWin.getInstance.grabFocus;  %// focus back to cmdwin

else
    warning('Failed to collapse folds: Editor could not take focus')
end
Tersanctus answered 23/9, 2014 at 16:52 Comment(1)
I saw this sort of solution somewhere (I believe here), but was hoping for something much more compact.Prickly

© 2022 - 2024 — McMap. All rights reserved.