如何执行“折叠全部折叠”在MATLAB编辑器中以编程方式? [英] How to execute "collapse-all-folds" in the MATLAB editor programatically?

查看:376
本文介绍了如何执行“折叠全部折叠”在MATLAB编辑器中以编程方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决主题中的问题比我想承认的要长一点。

I've been struggling with the problem in the subject for a bit longer than I'd like to admit.

我试图以编程方式执行相同的操作当用户点击查看> 全部折叠操作 $ c>按钮或右键单击编辑器窗口,然后代码折叠> 全部折叠

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.

到目前为止我尝试了什么:


  • String 对应 Action ,可在 enum com.mathworks.mde.editor.ActionID 并且是:'collapse-all-folds'

  • Action 激活时,似乎执行以下方法: org.netbeans.api.editor.fold。 FoldUtilities.collapseAll(...)(因此netbeans标签)。

  • 此代码允许我获取 EditorAction的实例 ActionManager MatlabEditor

  • 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.

任何想法/替代品?

EDIT1 :在这本书,我想我比以前更接近(但仍然不是那里)。引用本书:

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组件通常使用 ActionMap 存储可运行的 Actions ,它们是由监听器在鼠标,键盘,属性或容器事件上调用的
。与对象方法不同,MATLAB不能直接调用 Actions

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.



<然后解释了一个大致涉及的解决方法:获取某种 Action 对象;创建 ActionEvent 并使用以下方法调用 Action actionPerformed ActionEvent 作为参数,如下所示:

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);

此代码运行没有错误 - 但是(似乎?)没有。我怀疑我在错误的对象上调用 ActionEvent actionPerformed ActionManager 可能与此问题无关。)

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

PS

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

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

推荐答案

经过不可估量的挖掘,试验和方式太多错误 - 我已经完成了!

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.

这篇关于如何执行“折叠全部折叠”在MATLAB编辑器中以编程方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆