小编典典

检索和设置IntelliJ IDEA插件开发的拆分窗口设置

java

我正在编写一个IntelliJ IDEA插件,用于保存称为Tab Session的打开选项卡的会话。该问题是IntelliJIDEA插件开发的后续工作:保存选项卡组,永久保存它们,并根据用户的要求重新加载一组选项卡。

当前,不支持分割窗口。因此,我想做两件事:

  1. 检索有关所有拆分窗口或未拆分窗口的信息,这些窗口是编辑器选项卡的容器。我需要它们的位置和分割方向(水平或垂直)。
  2. 保存此信息并需要加载选项卡会话时,我需要像以前一样完全重建拆分的窗格及其选项卡。

由于缺乏文档,我目前正在浏览源代码,并发现了这段有希望的代码:

private EditorsSplitters getSplittersFromFocus() {
  return FileEditorManagerEx.getInstanceEx(myProject).getSplitters();
}

它允许我使用来遍历一组分割的窗口EditorWindow[] windows = getSplittersFromFocus.getOrderedWindows()。它们包含编辑器选项卡以及有关其宽度和高度的信息。但是我没有找到有关拆分方向以及如何像以前一样重建拆分后的窗口的任何信息。

有人可以帮忙吗?


阅读 489

收藏
2020-12-03

共1个答案

小编典典

这是未经测试的代码,但是由于它紧密地重新组合了EditorsSplitters
writeExternalwritePanel函数内部的过程,因此我相信这是可行的。

提出了两种方法:

  • writeExternal->的访问输出应该是更稳定的API,并且可以更轻松地访问文件信息
  • 访问splitter的组件->以这种方式writeExternal创建它的信息;可悲的是,至少有一个保护场没有涉及吸气剂(window.myPanel内部findWindowWith

    import com.intellij.openapi.actionSystem.AnAction;
    import com.intellij.openapi.actionSystem.AnActionEvent;
    import com.intellij.openapi.fileEditor.FileEditorManager;
    import com.intellij.openapi.fileEditor.impl.EditorsSplitters;
    import com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl;
    import com.intellij.openapi.project.Project;
    import com.intellij.openapi.ui.Splitter;
    import org.jdom.Element;

    import javax.swing.;
    import java.awt.
    ;
    import java.util.ArrayList;

    public class SplitterAction extends AnAction {

    public SplitterAction() {
        super("Splitter _Action");
    }
    
    private static class Info {
    
    }
    
    private static class SplitInfo extends Info {
        public Info    first;
        public Info    second;
        public boolean vertical;
        public float   proportions;
    }
    
    private static class FileInfo extends Info {
        public String[] fileNames;
    }
    
    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        final Project project = anActionEvent.getProject();
        final FileEditorManagerImpl fileEditorManager = (FileEditorManagerImpl) FileEditorManager.getInstance(project);
        EditorsSplitters splitters = fileEditorManager.getSplitters();
        // com.intellij.openapi.fileEditor.impl.EditorsSplitters.writeExternal() and
        // com.intellij.openapi.fileEditor.impl.EditorsSplitters#writePanel inspired this
        final Component component = splitters.getComponent(0);
        final SplitInfo infos = splitterVisitor(component);
    
        // or you could use this
        Element root = new Element("root");
        splitters.writeExternal(root);
    
        elementVisitor(root);
    
        // to restore from writeExternal the following should suffice
        splitters.readExternal(root);
        splitters.openFiles();
    }
    
    /**
     * Reads writeExternal output
     */
    private Info elementVisitor(Element root) {
        final Element splitter = root.getChild("splitter");
        if (splitter != null) {
            // see com.intellij.openapi.fileEditor.impl.EditorsSplitters#writePanel
            final SplitInfo splitInfo = new SplitInfo();
            // "vertical" or "horizontal"
            splitInfo.vertical = "vertical".equals(splitter.getAttributeValue("split-orientation"));
            splitInfo.proportions = Float.parseFloat(splitter.getAttributeValue("split-proportion"));
            Element first = splitter.getChild("split-first");
            if (first != null) {
                splitInfo.first = elementVisitor(first);
            }
            Element second = splitter.getChild("split-second");
            if (second != null) {
                splitInfo.second = elementVisitor(second);
            }
            return splitInfo;
        }
        final Element leaf = root.getChild("leaf");
        if (leaf != null) {
            final ArrayList<String> fileNames = new ArrayList<String>();
            for (Element file : leaf.getChildren("file")) {
                final String fileName = file.getAttributeValue("leaf-file-name");
                fileNames.add(fileName);
                // further attributes see com.intellij.openapi.fileEditor.impl.EditorsSplitters#writeComposite
            }
            final FileInfo fileInfo = new FileInfo();
            fileInfo.fileNames = fileNames.toArray(new String[fileNames.size()]);
            return fileInfo;
        }
        return null;
    }
    
    /**
     * Acts directly upon Component
     */
    private SplitInfo splitterVisitor(Component component) {
        if (component instanceof JPanel && ((JPanel) component).getComponentCount() > 0) {
            final Component child = ((JPanel) component).getComponent(0);
            if (child instanceof Splitter) {
                final Splitter splitter = (Splitter) child;
                final SplitInfo splitInfos = new SplitInfo();
                splitInfos.vertical = splitter.isVertical();
                splitInfos.proportions = splitter.getProportion();
                splitInfos.first = splitterVisitor(splitter.getFirstComponent());
                splitInfos.second = splitterVisitor(splitter.getSecondComponent());
                return splitInfos;
            }
            // TODO: retrieve file information
        }
        return null;
    }
    

    }

2020-12-03