Retrieving and Configuring Split Window Settings for IntelliJ IDEA Plugin Development

I am writing an IntelliJ IDEA plugin to save open tab sessions called session tab . This question is a continuation of IntelliJ IDEA Plugin Development: saving groups of tabs, keeping them persistent, and reloading the set of tabs at the user's request .

Split windows are not currently supported. Therefore, I want to do two things:

  • Get information about all split or unconnected windows that are containers for editor tabs. I need their position and the direction of separation (horizontal or vertical).
  • When this information is saved and I need to load the tab session, I need to restore the broken panels and their tabs exactly as they were before.

Due to a lack of documentation, I am currently browsing the source code and found this promising piece of code:

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

This allows me to go through a set of split windows using EditorWindow[] windows = getSplittersFromFocus.getOrderedWindows() . They contain editor tabs and information about their width and height. But I did not find any information about the direction of the separation and how to restore the broken windows, as they were before.

Can anyone help?

+47
java intellij-idea intellij-plugin
Nov 01. '13 at 14:12
source share
1 answer

This is unverified code, but since it closely revises the procedures inside the EditorsSplitters writeExternal and writePanel functions, I'm sure this will work.

Two methods are presented:

  • writeExternal → access writeExternal should be more stable API and provide easier access to file information.
  • splitter access components -> so writeExternal creates its information; unfortunately, there is at least one protected field without getter involvement ( window.myPanel inside 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; } } 
+4
Mar 04 '14 at 9:30
source share



All Articles