如何以编程方式在MATLAB编辑器中执行"collapse-all-folds"?

Dev*_*-iL 8 java matlab netbeans matlab-java

我一直在努力解决这个问题的时间比我想承认的要长一些.

我正在尝试以编程方式执行Action当用户单击View> Collapse All按钮或在编辑器窗口中右键单击然后Code Folding>时发生的相同操作Fold All.

到目前为止我发现了什么?

  • String对应于Action可在发现enum com.mathworks.mde.editor.ActionID并为:'collapse-all-folds'.
  • Action激活时,似乎执行以下方法:( org.netbeans.api.editor.fold.FoldUtilities.collapseAll(...)因此netbeans标记).
  • 此代码可以让我的情况下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');
Run Code Online (Sandbox Code Playgroud)

我的问题是,我不能找到一种方法,实际上是激活Action.

任何想法/替代品?


EDIT1:在"书"中挖了一下之后,我觉得我比以前更接近(但仍然不是那里).从书中引用:

Java GUI组件通常使用a ActionMap来存储Actions由侦听器在鼠标,键盘,属性或容器事件上调用的runnable .与对象方法不同Actions,MATLAB不能直接调用.

然后解释了一个解决方法,其中大致涉及:获取某种Action对象; 使用as作为参数创建ActionEvent和调用Action' ,如下所示:actionPerformedActionEvent

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);
Run Code Online (Sandbox Code Playgroud)

这段代码运行没有错误 - 但是(貌似?)没有.我怀疑我打电话ActionEvent,并actionPerformed在错误的对象(ActionManager有可能无关,与这个问题在所有).


PS

我知道有一个热键可以执行此操作(Ctrl+ =),但这不是我正在寻找的(除非有一个模拟热键按下的命令:)).

Dev*_*-iL 3

经过无数次的挖掘、尝试和太多的错误——我已经做到了!

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
Run Code Online (Sandbox Code Playgroud)

或者如果压缩成一个单一的、混乱的命令:

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

请注意,这适用于当前在编辑器中打开的脚本。