How to programmatically perform collapse-all-folds in the MATLAB editor?

I have been struggling with a problem in the subject for a bit longer than I would admit.

I am trying to programmatically perform the same Action that occurs when the user either clicks View > Collapse All , or right-click in the editor window, and then Code Folding > Fold All .

What I tried \ found so far:

  • String that corresponds to Action can be found in enum com.mathworks.mde.editor.ActionID and is: 'collapse-all-folds' .
  • When you activate Action , the following method is org.netbeans.api.editor.fold.FoldUtilities.collapseAll(...) : 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 cannot find a way to actually activate the Action .

Any ideas / alternatives?


EDIT1 : after you worked a little in the β€œbook” , I think I came even closer than before (but still not quite there). Quote from the book:

Java GUI components often use ActionMap to store runnable Actions that are called by listeners on the mouse, keyboard, properties, or container events. Unlike object methods, Actions cannot be directly called MATLAB.

And then a workaround is explained that includes something like: getting some Action object; creating an ActionEvent and calling the Action actionPerformed with the ActionEvent as an argument, as shown 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 works without errors - but (it seems?) Nothing. I suspect that I am ActionEvent and actionPerformed on the wrong objects (the ActionManager has nothing to do with this problem at all).


PS

I know that there is a hotkey that does this ( Ctrl + = ), but that’s not what I am looking for (if there is no command to simulate a hotkey, press :)).

+8
java matlab netbeans matlab-java
source share
2 answers

After immeasurable digging, trial and error too much - I did 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, erratic 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 in the script that is currently open in the editor.

+3
source share

Not an ideal solution , but it is possible to simulate a default hotkey by using java.awt.robot .

... finding a way to actually trigger 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 
+1
source share

All Articles