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

牧云@^-^@ 提交于 2019-12-05 23:25:10

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/25994261/how-to-execute-collapse-all-folds-in-the-matlab-editor-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!