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
Stringthat corresponds to theActionmay be found in theenumcom.mathworks.mde.editor.ActionIDand is:'collapse-all-folds'. - When the
Actionactivates, 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 an
ActionMapto 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 :) ).